From 76b73311624e1ce8c3cdc79273c60be8ac4c98f8 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Thu, 17 Sep 2020 12:30:59 +0200 Subject: [PATCH] sandbox - make platform.ts fit for sandbox usages --- src/vs/base/common/platform.ts | 47 +++++++++++++------ src/vs/base/common/process.ts | 25 +++++----- .../parts/sandbox/electron-browser/preload.js | 17 +++++-- .../parts/sandbox/electron-sandbox/globals.ts | 27 +++++++---- .../sandbox.simpleservices.ts | 2 +- 5 files changed, 76 insertions(+), 42 deletions(-) diff --git a/src/vs/base/common/platform.ts b/src/vs/base/common/platform.ts index 0bbc5d6ef91..3361d83be5b 100644 --- a/src/vs/base/common/platform.ts +++ b/src/vs/base/common/platform.ts @@ -26,15 +26,16 @@ export interface IProcessEnvironment { [key: string]: string; } -interface INodeProcess { - platform: string; +export interface INodeProcess { + platform: 'win32' | 'linux' | 'darwin'; env: IProcessEnvironment; - getuid(): number; nextTick: Function; versions?: { electron?: string; }; type?: string; + getuid(): number; + cwd(): string; } declare const process: INodeProcess; declare const global: any; @@ -47,9 +48,20 @@ interface INavigator { declare const navigator: INavigator; declare const self: any; -const isElectronRenderer = (typeof process !== 'undefined' && typeof process.versions !== 'undefined' && typeof process.versions.electron !== 'undefined' && process.type === 'renderer'); +const _globals = (typeof self === 'object' ? self : typeof global === 'object' ? global : {} as any); + +let nodeProcess: INodeProcess | undefined = undefined; +if (typeof process !== 'undefined') { + // Native environment (non-sandboxed) + nodeProcess = process; +} else if (typeof _globals.vscode !== 'undefined') { + // Native envionment (sandboxed) + nodeProcess = _globals.vscode.process; +} + +const isElectronRenderer = typeof nodeProcess?.versions?.electron === 'string' && nodeProcess.type === 'renderer'; -// OS detection +// Web environment if (typeof navigator === 'object' && !isElectronRenderer) { _userAgent = navigator.userAgent; _isWindows = _userAgent.indexOf('Windows') >= 0; @@ -59,13 +71,16 @@ if (typeof navigator === 'object' && !isElectronRenderer) { _isWeb = true; _locale = navigator.language; _language = _locale; -} else if (typeof process === 'object') { - _isWindows = (process.platform === 'win32'); - _isMacintosh = (process.platform === 'darwin'); - _isLinux = (process.platform === 'linux'); +} + +// Native environment +else if (typeof nodeProcess === 'object') { + _isWindows = (nodeProcess.platform === 'win32'); + _isMacintosh = (nodeProcess.platform === 'darwin'); + _isLinux = (nodeProcess.platform === 'linux'); _locale = LANGUAGE_DEFAULT; _language = LANGUAGE_DEFAULT; - const rawNlsConfig = process.env['VSCODE_NLS_CONFIG']; + const rawNlsConfig = nodeProcess.env['VSCODE_NLS_CONFIG']; if (rawNlsConfig) { try { const nlsConfig: NLSConfig = JSON.parse(rawNlsConfig); @@ -80,6 +95,11 @@ if (typeof navigator === 'object' && !isElectronRenderer) { _isNative = true; } +// Unknown environment +else { + console.error('Unable to resolve platform.'); +} + export const enum Platform { Web, Mac, @@ -114,7 +134,7 @@ export const platform = _platform; export const userAgent = _userAgent; export function isRootUser(): boolean { - return _isNative && !_isWindows && (process.getuid() === 0); + return !!nodeProcess && !_isWindows && (nodeProcess.getuid() === 0); } /** @@ -157,7 +177,6 @@ export const locale = _locale; */ export const translationsConfigFile = _translationsConfigFile; -const _globals = (typeof self === 'object' ? self : typeof global === 'object' ? global : {} as any); export const globals: any = _globals; interface ISetImmediate { @@ -196,8 +215,8 @@ export const setImmediate: ISetImmediate = (function defineSetImmediate() { globals.postMessage({ vscodeSetImmediateId: myId }, '*'); }; } - if (typeof process !== 'undefined' && typeof process.nextTick === 'function') { - return process.nextTick.bind(process); + if (nodeProcess) { + return nodeProcess.nextTick.bind(nodeProcess); } const _promise = Promise.resolve(); return (callback: (...args: any[]) => void) => _promise.then(callback); diff --git a/src/vs/base/common/process.ts b/src/vs/base/common/process.ts index 1513edad802..5e85d672116 100644 --- a/src/vs/base/common/process.ts +++ b/src/vs/base/common/process.ts @@ -3,19 +3,11 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { isWindows, isMacintosh, setImmediate, IProcessEnvironment, globals } from 'vs/base/common/platform'; +import { isWindows, isMacintosh, setImmediate, globals, INodeProcess } from 'vs/base/common/platform'; -export interface IProcess { - platform: 'win32' | 'linux' | 'darwin'; - env: IProcessEnvironment; +declare const process: INodeProcess; - cwd(): string; - nextTick(callback: (...args: any[]) => void): void; -} - -declare const process: IProcess; - -let safeProcess: IProcess; +let safeProcess: INodeProcess; // Native node.js environment if (typeof process !== 'undefined') { @@ -30,10 +22,15 @@ else if (typeof globals.vscode !== 'undefined') { // Web environment else { safeProcess = { - cwd(): string { return '/'; }, - env: Object.create(null), + + // Supported get platform(): 'win32' | 'linux' | 'darwin' { return isWindows ? 'win32' : isMacintosh ? 'darwin' : 'linux'; }, - nextTick(callback: (...args: any[]) => void): void { return setImmediate(callback); } + nextTick(callback: (...args: any[]) => void): void { return setImmediate(callback); }, + + // Unsupported + get env() { return Object.create(null); }, + cwd(): string { return '/'; }, + getuid(): number { return -1; } }; } diff --git a/src/vs/base/parts/sandbox/electron-browser/preload.js b/src/vs/base/parts/sandbox/electron-browser/preload.js index fd56497a42a..9be3747b0a8 100644 --- a/src/vs/base/parts/sandbox/electron-browser/preload.js +++ b/src/vs/base/parts/sandbox/electron-browser/preload.js @@ -98,9 +98,10 @@ * Support for a subset of access to node.js global `process`. */ process: { - platform: process.platform, - env: process.env, - versions: process.versions, + get platform() { return process.platform; }, + get env() { return process.env; }, + get versions() { return process.versions; }, + get type() { return 'renderer'; }, _whenEnvResolved: undefined, whenEnvResolved: @@ -136,6 +137,14 @@ return process.cwd(); }, + getuid: + /** + * @returns the numeric user identity of the process + */ + function () { + return process.getuid(); + }, + getProcessMemoryInfo: /** * @returns {Promise} @@ -160,7 +169,7 @@ * Some information about the context we are running in. */ context: { - sandbox: process.argv.includes('--enable-sandbox') + get sandbox() { return process.argv.includes('--enable-sandbox'); } } }; diff --git a/src/vs/base/parts/sandbox/electron-sandbox/globals.ts b/src/vs/base/parts/sandbox/electron-sandbox/globals.ts index 194b5746da6..0fc95f894ff 100644 --- a/src/vs/base/parts/sandbox/electron-sandbox/globals.ts +++ b/src/vs/base/parts/sandbox/electron-sandbox/globals.ts @@ -4,8 +4,7 @@ *--------------------------------------------------------------------------------------------*/ import type { ProcessMemoryInfo } from 'vs/base/parts/sandbox/common/electronTypes'; -import type { IProcess } from 'vs/base/common/process'; -import { globals } from 'vs/base/common/platform'; +import { globals, INodeProcess } from 'vs/base/common/platform'; export const ipcRenderer = globals.vscode.ipcRenderer as { @@ -75,7 +74,7 @@ export const crashReporter = globals.vscode.crashReporter as { addExtraParameter(key: string, value: string): void; }; -export const process = globals.vscode.process as IProcess & { +export const process = globals.vscode.process as INodeProcess & { /** * The process.platform property returns a string identifying the operating system platform @@ -84,7 +83,17 @@ export const process = globals.vscode.process as IProcess & { platform: 'win32' | 'linux' | 'darwin'; /** - * The process.env property returns an object containing the user environment. See environ(7). + * The type will always be Electron renderer. + */ + type: 'renderer'; + + /** + * A list of versions for the current node.js/electron configuration. + */ + versions: { [key: string]: string | undefined }; + + /** + * The process.env property returns an object containing the user environment. */ env: { [key: string]: string | undefined }; @@ -93,6 +102,11 @@ export const process = globals.vscode.process as IProcess & { */ cwd(): string; + /** + * Returns the numeric user identity of the process. + */ + getuid(): number; + /** * Allows to await resolving the full process environment by checking for the shell environment * of the OS in certain cases (e.g. when the app is started from the Dock on macOS). @@ -125,11 +139,6 @@ export const process = globals.vscode.process as IProcess & { * process on macOS. */ getProcessMemoryInfo: () => Promise; - - /** - * A list of versions for the current node.js/electron configuration. - */ - versions: { [key: string]: string | undefined }; }; export const context = globals.vscode.context as { diff --git a/src/vs/workbench/electron-sandbox/sandbox.simpleservices.ts b/src/vs/workbench/electron-sandbox/sandbox.simpleservices.ts index d6f8e16fbf7..0872bd69433 100644 --- a/src/vs/workbench/electron-sandbox/sandbox.simpleservices.ts +++ b/src/vs/workbench/electron-sandbox/sandbox.simpleservices.ts @@ -91,6 +91,7 @@ export class SimpleWorkbenchEnvironmentService implements INativeWorkbenchEnviro get userDataSyncHome(): URI { return joinPath(this.userRoamingDataHome, 'syncHome'); } get tmpDir(): URI { return joinPath(this.userRoamingDataHome, 'tmp'); } get backupWorkspaceHome(): URI { return joinPath(this.userRoamingDataHome, 'Backups', 'workspace'); } + get logsPath(): string { return joinPath(this.userRoamingDataHome, 'logs').path; } options?: IWorkbenchConstructionOptions | undefined; logExtensionHostCommunication?: boolean | undefined; @@ -107,7 +108,6 @@ export class SimpleWorkbenchEnvironmentService implements INativeWorkbenchEnviro disableExtensions: boolean | string[] = []; extensionDevelopmentLocationURI?: URI[] | undefined; extensionTestsLocationURI?: URI | undefined; - logsPath: string = undefined!; logLevel?: string | undefined; args: NativeParsedArgs = Object.create(null); -- GitLab