Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable sandbox for proxy auth window #100907

Merged
merged 3 commits into from Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/vs/code/electron-main/auth.ts
Expand Up @@ -6,6 +6,7 @@
import { localize } from 'vs/nls';
import { Disposable } from 'vs/base/common/lifecycle';
import { Event } from 'vs/base/common/event';
import { URI } from 'vs/base/common/uri';
import { BrowserWindow, BrowserWindowConstructorOptions, app, AuthInfo, WebContents, Event as ElectronEvent } from 'electron';

type LoginEvent = {
Expand Down Expand Up @@ -58,10 +59,12 @@ export class ProxyAuthHandler extends Disposable {
show: true,
title: 'VS Code',
webPreferences: {
nodeIntegration: true,
preload: URI.parse(require.toUrl('vs/base/parts/sandbox/electron-browser/preload.js')).fsPath,
enableWebSQL: false,
sandbox: true,
devTools: false,
enableRemoteModule: false,
nativeWindowOpen: true
v8CacheOptions: 'bypassHeatCheck'
}
};

Expand All @@ -72,24 +75,27 @@ export class ProxyAuthHandler extends Disposable {
}

const win = new BrowserWindow(opts);
const config = {};
const baseUrl = require.toUrl('vs/code/electron-browser/proxy/auth.html');
const url = `${baseUrl}?config=${encodeURIComponent(JSON.stringify(config))}`;
const url = require.toUrl('vs/code/electron-sandbox/proxy/auth.html');
const proxyUrl = `${authInfo.host}:${authInfo.port}`;
const title = localize('authRequire', "Proxy Authentication Required");
const message = localize('proxyauth', "The proxy {0} requires authentication.", proxyUrl);
const data = { title, message };
const javascript = 'promptForCredentials(' + JSON.stringify(data) + ')';

const onWindowClose = () => cb('', '');
win.on('close', onWindowClose);

win.setMenu(null);
win.loadURL(url);
win.webContents.executeJavaScript(javascript, true).then(({ username, password }: Credentials) => {
cb(username, password);
win.webContents.on('did-finish-load', () => {
const data = { title, message };
win.webContents.send('vscode:openProxyAuthDialog', data);
});
win.webContents.on('ipc-message', (event, channel, credentials: Credentials) => {
if (channel === 'vscode:proxyAuthResponse') {
const { username, password } = credentials;
cb(username, password);
}
win.removeListener('close', onWindowClose);
win.close();
});
win.loadURL(url);
}
}
Expand Up @@ -5,7 +5,7 @@
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Security-Policy"
content="default-src 'none'; img-src 'self' https: data:; media-src 'none'; child-src 'self'; object-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; connect-src 'self' https:; font-src 'self' https:;">
content="default-src 'none'; img-src 'self' https: data:; media-src 'none'; child-src 'self'; object-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self' https:; font-src 'self' https:;">
<style>
html,
body {
Expand Down Expand Up @@ -78,43 +78,6 @@ <h1 id="title"></h1>
</section>
</body>

<script>

function promptForCredentials(data) {
return new Promise((c, e) => {
const $title = document.getElementById('title');
const $username = document.getElementById('username');
const $password = document.getElementById('password');
const $form = document.getElementById('form');
const $cancel = document.getElementById('cancel');
const $message = document.getElementById('message');

function submit() {
c({ username: $username.value, password: $password.value });
return false;
};

function cancel() {
c({ username: '', password: '' });
return false;
};

$form.addEventListener('submit', submit);
$cancel.addEventListener('click', cancel);

document.body.addEventListener('keydown', function (e) {
switch (e.keyCode) {
case 27: e.preventDefault(); e.stopPropagation(); return cancel();
case 13: e.preventDefault(); e.stopPropagation(); return submit();
}
});

$title.textContent = data.title;
$message.textContent = data.message;
$username.focus();
});
}

</script>
<script src="auth.js"></script>

</html>
48 changes: 48 additions & 0 deletions src/vs/code/electron-sandbox/proxy/auth.js
@@ -0,0 +1,48 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

'use strict';

const { ipcRenderer } = window.vscode;

function promptForCredentials(data) {
return new Promise((c, e) => {
const $title = document.getElementById('title');
const $username = document.getElementById('username');
const $password = document.getElementById('password');
const $form = document.getElementById('form');
const $cancel = document.getElementById('cancel');
const $message = document.getElementById('message');

function submit() {
c({ username: $username.value, password: $password.value });
return false;
}

function cancel() {
c({ username: '', password: '' });
return false;
}

$form.addEventListener('submit', submit);
$cancel.addEventListener('click', cancel);

document.body.addEventListener('keydown', function (e) {
switch (e.keyCode) {
case 27: e.preventDefault(); e.stopPropagation(); return cancel();
case 13: e.preventDefault(); e.stopPropagation(); return submit();
}
});

$title.textContent = data.title;
$message.textContent = data.message;
$username.focus();
});
}

ipcRenderer.on('vscode:openProxyAuthDialog', async (event, data) => {
const response = await promptForCredentials(data);
ipcRenderer.send('vscode:proxyAuthResponse', response);
});