@theia/core 1.72.0-next.59 → 1.72.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/README.md +6 -6
  2. package/i18n/nls.cs.json +93 -44
  3. package/i18n/nls.de.json +93 -44
  4. package/i18n/nls.es.json +93 -44
  5. package/i18n/nls.fr.json +93 -44
  6. package/i18n/nls.hu.json +93 -44
  7. package/i18n/nls.it.json +93 -44
  8. package/i18n/nls.ja.json +93 -44
  9. package/i18n/nls.json +93 -44
  10. package/i18n/nls.ko.json +93 -44
  11. package/i18n/nls.pl.json +93 -44
  12. package/i18n/nls.pt-br.json +93 -44
  13. package/i18n/nls.ru.json +93 -44
  14. package/i18n/nls.tr.json +93 -44
  15. package/i18n/nls.zh-cn.json +93 -44
  16. package/i18n/nls.zh-tw.json +93 -44
  17. package/lib/browser/catalog.json +13 -0
  18. package/lib/common/shell-quoting.d.ts +92 -0
  19. package/lib/common/shell-quoting.d.ts.map +1 -0
  20. package/lib/common/shell-quoting.js +146 -0
  21. package/lib/common/shell-quoting.js.map +1 -0
  22. package/lib/common/shell-quoting.spec.d.ts +2 -0
  23. package/lib/common/shell-quoting.spec.d.ts.map +1 -0
  24. package/lib/common/shell-quoting.spec.js +171 -0
  25. package/lib/common/shell-quoting.spec.js.map +1 -0
  26. package/lib/node/process-utils.d.ts +1 -0
  27. package/lib/node/process-utils.d.ts.map +1 -1
  28. package/lib/node/process-utils.js +15 -9
  29. package/lib/node/process-utils.js.map +1 -1
  30. package/lib/node/process-utils.spec.js +41 -0
  31. package/lib/node/process-utils.spec.js.map +1 -1
  32. package/package.json +6 -6
  33. package/src/common/i18n/nls.metadata.json +3417 -1177
  34. package/src/common/shell-quoting.spec.ts +176 -0
  35. package/src/common/shell-quoting.ts +236 -0
  36. package/src/node/process-utils.spec.ts +48 -0
  37. package/src/node/process-utils.ts +15 -8
@@ -0,0 +1,176 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2020 Ericsson and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { expect } from 'chai';
18
+
19
+ import { escapeForShell, BashQuotingFunctions, ShellQuoting, CmdQuotingFunctions, PowershellQuotingFunctions } from './shell-quoting';
20
+
21
+ describe('Shell arguments escaping:', () => {
22
+
23
+ // Procedurally execute tests from this list of data.
24
+ const testData = {
25
+ bash: {
26
+ // https://www.gnu.org/software/bash/manual/html_node/Quoting.html
27
+ quotingFunctions: BashQuotingFunctions,
28
+ data: {
29
+ [ShellQuoting.Escape]: [
30
+ { input: 'abc', expected: 'abc' },
31
+ { input: 'ab c', expected: 'ab\\ c' },
32
+ { input: 'ab"c', expected: 'ab\\"c' },
33
+ { input: 'ab\'c', expected: 'ab\\\'c' },
34
+ { input: 'ab\\ c\\', expected: 'ab\\\\\\ c\\\\' },
35
+ {
36
+ input: 'setTimeout(() => { console.log(1, "2\'3"); }, 100)',
37
+ expected: 'setTimeout\\(\\(\\)\\ =\\>\\ \\{\\ console.log\\(1,\\ \\"2\\\'3\\"\\)\\;\\ \\},\\ 100\\)',
38
+ },
39
+ ],
40
+ [ShellQuoting.Strong]: [
41
+ { input: 'abc', expected: '\'abc\'' },
42
+ { input: 'ab c', expected: '\'ab c\'' },
43
+ { input: 'ab"c', expected: '\'ab"c\'' },
44
+ { input: 'ab\'c', expected: '\'ab\'"\'"\'c\'' },
45
+ { input: 'ab\\ c\\', expected: '\'ab\\ c\\\'' },
46
+ {
47
+ input: 'setTimeout(() => { console.log(1, "2\'3"); }, 100)',
48
+ expected: '\'setTimeout(() => { console.log(1, "2\'"\'"\'3"); }, 100)\'',
49
+ },
50
+ ],
51
+ [ShellQuoting.Weak]: [
52
+ { input: 'abc', expected: '"abc"' },
53
+ { input: 'ab c', expected: '"ab c"' },
54
+ { input: 'ab"c', expected: '"ab\\"c"' },
55
+ { input: 'ab\'c', expected: '"ab\'c"' },
56
+ { input: 'ab\\ c\\', expected: '"ab\\ c\\\\"' },
57
+ {
58
+ input: 'setTimeout(() => { console.log(1, "2\'3"); }, 100)',
59
+ expected: '"setTimeout(() => { console.log(1, \\"2\'3\\"); }, 100)"',
60
+ },
61
+ ]
62
+ },
63
+ },
64
+ cmd: {
65
+ // https://ss64.com/nt/syntax-esc.html
66
+ quotingFunctions: CmdQuotingFunctions,
67
+ data: {
68
+ [ShellQuoting.Escape]: [
69
+ { input: 'abc', expected: 'abc' },
70
+ { input: 'ab c', expected: 'ab" "c' },
71
+ { input: 'ab"c', expected: 'ab\\"c' },
72
+ { input: 'ab\'c', expected: 'ab\'c' },
73
+ { input: 'ab^ c^', expected: 'ab^^" "c^^' },
74
+ {
75
+ input: 'setTimeout(() => { console.log(1, "2\'3"); }, 100)',
76
+ expected: 'setTimeout^(^(^)" "=^>" "{" "console.log^(1," "\\"2\'3\\"^);" "}," "100^)',
77
+ },
78
+ {
79
+ input: 'console.log("%PATH%")',
80
+ expected: 'console.log^(\\"^%PATH^%\\"^)',
81
+ },
82
+ ],
83
+ [ShellQuoting.Strong]: [
84
+ { input: 'abc', expected: '"abc"' },
85
+ { input: 'ab c', expected: '"ab c"' },
86
+ { input: 'ab"c', expected: '"ab\\"c"' },
87
+ { input: 'ab\'c', expected: '"ab\'c"' },
88
+ { input: 'ab^ c^', expected: '"ab^^ c^^"' },
89
+ {
90
+ input: 'setTimeout(() => { console.log(1, "2\'3"); }, 100)',
91
+ expected: '"setTimeout^(^(^) =^> { console.log^(1, \\"2\'3\\"^); }, 100^)"',
92
+ },
93
+ {
94
+ input: 'console.log("%PATH%")',
95
+ expected: '"console.log^(\\""%"PATH"%"\\"^)"',
96
+ },
97
+ ],
98
+ [ShellQuoting.Weak]: [
99
+ { input: 'abc', expected: '"abc"' },
100
+ { input: 'ab c', expected: '"ab c"' },
101
+ { input: 'ab"c', expected: '"ab\\"c"' },
102
+ { input: 'ab\'c', expected: '"ab\'c"' },
103
+ { input: 'ab^ c^', expected: '"ab^^ c^^"' },
104
+ {
105
+ input: 'setTimeout(() => { console.log(1, "2\'3"); }, 100)',
106
+ expected: '"setTimeout^(^(^) =^> { console.log^(1, \\"2\'3\\"^); }, 100^)"',
107
+ },
108
+ {
109
+ input: 'console.log("%PATH%")',
110
+ expected: '"console.log^(\\"%PATH%\\"^)"',
111
+ },
112
+ ]
113
+ },
114
+ },
115
+ powershell: {
116
+ // https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6
117
+ quotingFunctions: PowershellQuotingFunctions,
118
+ data: {
119
+ [ShellQuoting.Escape]: [
120
+ { input: 'abc', expected: 'abc' },
121
+ { input: 'ab c', expected: 'ab` c' },
122
+ { input: 'ab"c', expected: 'ab`"c' },
123
+ { input: 'ab\'c', expected: 'ab`\'c' },
124
+ { input: 'ab` c`', expected: 'ab``` c``' },
125
+ {
126
+ input: 'setTimeout(() => { console.log(1, "2\'3"); }, 100)',
127
+ expected: 'setTimeout`(`(`)` =`>` `{` console.log`(1,` `"2`\'3`"`)`;` `},` 100`)',
128
+ },
129
+ ],
130
+ [ShellQuoting.Strong]: [
131
+ { input: 'abc', expected: '\'abc\'' },
132
+ { input: 'ab c', expected: '\'ab c\'' },
133
+ { input: 'ab"c', expected: '\'ab"c\'' },
134
+ { input: 'ab\'c', expected: '\'ab\'\'c\'' },
135
+ { input: 'ab` c`', expected: '\'ab` c`\'' },
136
+ {
137
+ input: 'setTimeout(() => { console.log(1, "2\'3"); }, 100)',
138
+ expected: '\'setTimeout(() => { console.log(1, "2\'\'3"); }, 100)\'',
139
+ },
140
+ ],
141
+ [ShellQuoting.Weak]: [
142
+ { input: 'abc', expected: '"abc"' },
143
+ { input: 'ab c', expected: '"ab c"' },
144
+ { input: 'ab"c', expected: '"ab`"c"' },
145
+ { input: 'ab\'c', expected: '"ab\'c"' },
146
+ { input: 'ab` c`', expected: '"ab` c``"' },
147
+ {
148
+ input: 'setTimeout(() => { console.log(1, "2\'3"); }, 100)',
149
+ expected: '"setTimeout(() => { console.log(1, `"2\'3`"); }, 100)"',
150
+ },
151
+ ]
152
+ },
153
+ }
154
+ } as const;
155
+
156
+ // Iter through each runtime (bash/cmd/powershell):
157
+ for (const runtime of Object.keys(testData)) {
158
+ const testInfo = testData[runtime as keyof typeof testData];
159
+
160
+ // Get all quoting types (escape/strong/weak):
161
+ for (const quotingType of Object.keys(testInfo.data)) {
162
+ const testInput = testInfo.data[quotingType as keyof typeof testInfo['data']];
163
+
164
+ // Run the test for each input:
165
+ it(`${runtime}/${quotingType}`, () => {
166
+ for (const test of testInput) {
167
+ expect(escapeForShell({
168
+ quoting: quotingType as ShellQuoting,
169
+ value: test.input,
170
+ }, testInfo.quotingFunctions)).equal(test.expected);
171
+ }
172
+ });
173
+ }
174
+ }
175
+
176
+ });
@@ -0,0 +1,236 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2020 Ericsson and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ // #region vscode
18
+
19
+ /*---------------------------------------------------------------------------------------------
20
+ * Copyright (c) Microsoft Corporation. All rights reserved.
21
+ * Licensed under the MIT License. See License.txt in the project root for license information.
22
+ *--------------------------------------------------------------------------------------------*/
23
+
24
+ // See: https://github.com/microsoft/vscode/blob/9ebb7c43bc99fd6e1a295040674d1f8e5831b9be/src/vs/vscode.d.ts#L5326-L5370
25
+
26
+ /**
27
+ * Defines how an argument should be quoted if it contains
28
+ * spaces or unsupported characters.
29
+ */
30
+ export const enum ShellQuoting {
31
+
32
+ /**
33
+ * Character escaping should be used. This for example
34
+ * uses \ on bash and ` on PowerShell.
35
+ */
36
+ Escape = 'escape',
37
+
38
+ /**
39
+ * Strong string quoting should be used. This for example
40
+ * uses " for Windows cmd and ' for bash and PowerShell.
41
+ * Strong quoting treats arguments as literal strings.
42
+ * Under PowerShell echo 'The value is $(2 * 3)' will
43
+ * print `The value is $(2 * 3)`
44
+ */
45
+ Strong = 'strong',
46
+
47
+ /**
48
+ * Weak string quoting should be used. This for example
49
+ * uses " for Windows cmd, bash and PowerShell. Weak quoting
50
+ * still performs some kind of evaluation inside the quoted
51
+ * string. Under PowerShell echo "The value is $(2 * 3)"
52
+ * will print `The value is 6`
53
+ */
54
+ Weak = 'weak'
55
+ }
56
+
57
+ /**
58
+ * A string that will be quoted depending on the used shell.
59
+ */
60
+ export interface ShellQuotedString {
61
+ /**
62
+ * The actual string value.
63
+ */
64
+ value: string;
65
+
66
+ /**
67
+ * The quoting style to use.
68
+ */
69
+ quoting: ShellQuoting;
70
+ }
71
+
72
+ // #endregion vscode
73
+
74
+ /**
75
+ * Functions that provide shell quoting capabilities.
76
+ */
77
+ export interface ShellQuotingFunctions {
78
+
79
+ characters: {
80
+ /** Characters that require quotes, white space is always implied. */
81
+ needQuotes?: string
82
+ /** The character used to escape sequences. */
83
+ escape?: string
84
+ /** The character used to quote sequences, preventing variable expansion. */
85
+ strong?: string
86
+ /** The character used to quote sequences, allowing variable expansion. */
87
+ weak?: string
88
+ }
89
+
90
+ /**
91
+ * Should add escape-characters in front of forbidden characters.
92
+ */
93
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
94
+ escape?(this: any, arg: string): string
95
+
96
+ /**
97
+ * Should quote the argument in such a way that variables CANNOT be expanded.
98
+ */
99
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
100
+ strong?(this: any, arg: string): string;
101
+
102
+ /**
103
+ * Should quote the argument in such a way that variables CAN be expanded.
104
+ */
105
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
106
+ weak?(this: any, arg: string): string;
107
+ }
108
+
109
+ /**
110
+ * Converts a list of args into an escaped shell command.
111
+ *
112
+ * There are two main use cases when handling command/arguments for a shell:
113
+ * 1. User already wrote the escaped commandline, then just use that.
114
+ * 2. User wants a specific process to be invoked with some arguments.
115
+ *
116
+ * The `createShellCommandLine` function is useful for the latter.
117
+ *
118
+ * @param args Standard list of spawn/exec arguments, first item is the command.
119
+ * @param quotingFunctions Collection of functions to process arguments.
120
+ */
121
+ export function createShellCommandLine(args: Array<string | ShellQuotedString>, quotingFunctions?: ShellQuotingFunctions): string {
122
+ return args.map(arg => escapeForShell(arg, quotingFunctions)).join(' ');
123
+ }
124
+
125
+ /**
126
+ * Escape (or quote) a given input.
127
+ *
128
+ * @param arg Input to escape.
129
+ * @param quotingFunctions Collection of functions to process the given `arg`.
130
+ * @param quotingType Override the quoting type specified by the given `arg`.
131
+ */
132
+ export function escapeForShell(arg: string | ShellQuotedString, quotingFunctions?: ShellQuotingFunctions, quotingType?: ShellQuoting): string {
133
+ let value: string;
134
+ let quoting: ShellQuoting | undefined = quotingType;
135
+ if (typeof arg === 'string') {
136
+ if (!quoting) {
137
+ return arg;
138
+ }
139
+ value = arg;
140
+ } else {
141
+ if (!quoting) {
142
+ quoting = arg.quoting;
143
+ }
144
+ value = arg.value;
145
+ }
146
+ if (quotingFunctions && typeof quotingFunctions[quoting] === 'function') {
147
+ return quotingFunctions[quoting]!(value);
148
+ }
149
+ return value;
150
+ }
151
+
152
+ export const BashQuotingFunctions: Required<ShellQuotingFunctions> = {
153
+ characters: {
154
+ needQuotes: '()',
155
+ escape: '\\',
156
+ strong: '\'',
157
+ weak: '"',
158
+ },
159
+ escape(arg): string {
160
+ return arg
161
+ .replace(/[\s\\|(){}<>$&;"']/g, '\\$&');
162
+ },
163
+ strong(arg): string {
164
+ // ('+) becomes ('"'+"')
165
+ return `'${arg
166
+ .replace(/'+/g, '\'"$&"\'')}'`;
167
+ },
168
+ weak(arg): string {
169
+ return `"${arg
170
+ // Escape escape-characters.
171
+ .replace(/\\"/g, '\\\\"')
172
+ // Escape user-specified double-quotes.
173
+ .replace(/"/g, '\\"')
174
+ // Escape trailing (\), we don't want the user to escape our last quote.
175
+ .replace(/\\$/g, '\\\\')}"`;
176
+ },
177
+ };
178
+
179
+ export const CmdQuotingFunctions: Required<ShellQuotingFunctions> = {
180
+ characters: {
181
+ weak: '"',
182
+ },
183
+ escape(arg): string {
184
+ return arg
185
+ // Escape forbidden characters (see: cmd /?).
186
+ .replace(/[%&<>()@^|]/g, '^$&')
187
+ // Some characters must be escaped using `\`.
188
+ .replace(/[\\"]/g, '\\$&')
189
+ // Double-quote whitespaces, else we cannot escape it.
190
+ .replace(/\s+/g, '"$&"');
191
+ },
192
+ strong(arg): string {
193
+ return this.weak(arg)
194
+ // Try to prevent variable expansion.
195
+ .replace(/%/g, '"%"');
196
+ },
197
+ weak(arg): string {
198
+ return `"${arg
199
+ // Escape double quotes.
200
+ .replace(/\\"/g, '\\\\"')
201
+ .replace(/"/g, '\\"')
202
+ // Escape forbidden characters (see: cmd /?)
203
+ .replace(/[&<>()@^|]/g, '^$&')
204
+ // Escape trailing backslash, we don't want the user to escape our last quote.
205
+ .replace(/\\$/g, '\\\\')
206
+ // Escape line returns
207
+ .replace(/\r?\n/g, '^$&')}"`;
208
+ },
209
+ };
210
+
211
+ export const PowershellQuotingFunctions: Required<ShellQuotingFunctions> = {
212
+ characters: {
213
+ needQuotes: '()',
214
+ escape: '`',
215
+ strong: '\'',
216
+ weak: '"',
217
+ },
218
+ escape(arg): string {
219
+ return arg.replace(/[`|{}()<>;"' ]/g, '`$&');
220
+ },
221
+ strong(arg): string {
222
+ // In powershell, one must write ('') for a single quote to be displayed
223
+ // within a single quoted string.
224
+ return `'${arg
225
+ .replace(/'/g, '\'\'')}'`;
226
+ },
227
+ weak(arg): string {
228
+ return `"${arg
229
+ // Escape escape-characters.
230
+ .replace(/`"/g, '``"')
231
+ // Escape user-specified backticks.
232
+ .replace(/"/g, '`"')
233
+ // Escape trailing (`), we don't want the user to escape our last quote.
234
+ .replace(/`$/g, '``')}"`;
235
+ },
236
+ };
@@ -45,4 +45,52 @@ describe('ProcessUtils', () => {
45
45
  const pids = coreProcessManager['unixGetChildrenRecursive'](2);
46
46
  expect(Array.from(pids)).members([40, 5, 6, 7]);
47
47
  });
48
+
49
+ describe('#unixTerminateProcessTree', () => {
50
+ let originalKill: typeof process.kill;
51
+ let originalConsoleError: typeof console.error;
52
+ let loggedErrors: unknown[];
53
+
54
+ function throwingKill(code: string): typeof process.kill {
55
+ return (() => {
56
+ const error = new Error(`kill ${code}`) as NodeJS.ErrnoException;
57
+ error.code = code;
58
+ throw error;
59
+ }) as typeof process.kill;
60
+ }
61
+
62
+ beforeEach(() => {
63
+ originalKill = process.kill;
64
+ originalConsoleError = console.error;
65
+ loggedErrors = [];
66
+ console.error = (...args: unknown[]) => { loggedErrors.push(args); };
67
+ // One child plus the parent; report the parent as its own group leader so the `kill(-ppid)` branch runs too.
68
+ coreProcessManager['unixGetChildrenRecursive'] = () => new Set([424242]);
69
+ coreProcessManager['unixGetPGID'] = (pid: number) => pid;
70
+ });
71
+
72
+ afterEach(() => {
73
+ process.kill = originalKill;
74
+ console.error = originalConsoleError;
75
+ });
76
+
77
+ it('does not throw or log when processes in the tree are already gone (ESRCH)', () => {
78
+ process.kill = throwingKill('ESRCH');
79
+ expect(() => coreProcessManager['unixTerminateProcessTree'](424243)).to.not.throw();
80
+ expect(loggedErrors).to.be.empty;
81
+ });
82
+
83
+ it('logs unexpected kill errors (e.g. EPERM) without throwing', () => {
84
+ process.kill = throwingKill('EPERM');
85
+ expect(() => coreProcessManager['unixTerminateProcessTree'](424243)).to.not.throw();
86
+ expect(loggedErrors).to.not.be.empty;
87
+ });
88
+
89
+ it('does not throw when a kill rejects with a value that has no code (e.g. undefined)', () => {
90
+ const thrown: unknown = undefined;
91
+ process.kill = (() => { throw thrown; }) as typeof process.kill;
92
+ expect(() => coreProcessManager['unixTerminateProcessTree'](424243)).to.not.throw();
93
+ expect(loggedErrors).to.not.be.empty;
94
+ });
95
+ });
48
96
  });
@@ -47,21 +47,28 @@ export class ProcessUtils {
47
47
  for (const pid of this.unixGetChildrenRecursive(ppid)) {
48
48
  // Prevent killing the current process:
49
49
  if (pid !== process.pid) {
50
- // Don't stop if a process fails to be killed (keep on killing the others):
51
- try {
52
- process.kill(pid);
53
- } catch (error) {
54
- console.error(error);
55
- }
50
+ this.unixKill(pid);
56
51
  }
57
52
  }
58
53
  if (ppid === this.unixGetPGID(ppid)) {
59
54
  // When a process pgid === pid this means the the process is a group leader.
60
55
  // We can then kill every process part of its group by doing `kill(-pgid)`.
61
56
  // This can catch leaked processes under `init` that are still part of the group.
62
- process.kill(-ppid);
57
+ this.unixKill(-ppid);
58
+ }
59
+ this.unixKill(ppid);
60
+ }
61
+
62
+ protected unixKill(pid: number): void {
63
+ try {
64
+ process.kill(pid);
65
+ } catch (error) {
66
+ // ESRCH means the process is already gone, which is the goal here. Log
67
+ // anything else but keep going so the rest of the tree is still killed.
68
+ if ((error as NodeJS.ErrnoException | undefined)?.code !== 'ESRCH') {
69
+ console.error(`[${pid}] failed to kill`, error);
70
+ }
63
71
  }
64
- process.kill(ppid);
65
72
  }
66
73
 
67
74
  protected unixGetPGID(pid: number): number {