Skip to content

Commit cc44336

Browse files
authored
Merge pull request microsoft#182100 from microsoft/tyriar/181939
Make 633 parsing more strict
2 parents e29ee62 + 7518104 commit cc44336

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/vs/platform/terminal/common/xterm/shellIntegrationAddon.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,11 @@ export class ShellIntegrationAddon extends Disposable implements IShellIntegrati
316316
}
317317

318318
// Pass the sequence along to the capability
319-
const [command, ...args] = data.split(';');
320-
switch (command) {
319+
const argsIndex = data.indexOf(';');
320+
const sequenceCommand = argsIndex === -1 ? data : data.substring(0, argsIndex);
321+
// Cast to strict checked index access
322+
const args: (string | undefined)[] = argsIndex === -1 ? [] : data.substring(argsIndex + 1).split(';');
323+
switch (sequenceCommand) {
321324
case VSCodeOscPt.PromptStart:
322325
this._createOrGetCommandDetection(this._terminal).handlePromptStart();
323326
return true;
@@ -328,18 +331,21 @@ export class ShellIntegrationAddon extends Disposable implements IShellIntegrati
328331
this._createOrGetCommandDetection(this._terminal).handleCommandExecuted();
329332
return true;
330333
case VSCodeOscPt.CommandFinished: {
331-
const exitCode = args.length === 1 ? parseInt(args[0]) : undefined;
334+
const arg0 = args[0];
335+
const exitCode = arg0 !== undefined ? parseInt(arg0) : undefined;
332336
this._createOrGetCommandDetection(this._terminal).handleCommandFinished(exitCode);
333337
return true;
334338
}
335339
case VSCodeOscPt.CommandLine: {
340+
const arg0 = args[0];
341+
const arg1 = args[1];
336342
let commandLine: string;
337-
if (args.length >= 1 || args.length <= 2) {
338-
commandLine = deserializeMessage(args[0]);
343+
if (arg0 !== undefined) {
344+
commandLine = deserializeMessage(arg0);
339345
} else {
340346
commandLine = '';
341347
}
342-
this._createOrGetCommandDetection(this._terminal).setCommandLine(commandLine, args[1] === this._nonce);
348+
this._createOrGetCommandDetection(this._terminal).setCommandLine(commandLine, arg1 === this._nonce);
343349
return true;
344350
}
345351
case VSCodeOscPt.ContinuationStart: {
@@ -359,7 +365,8 @@ export class ShellIntegrationAddon extends Disposable implements IShellIntegrati
359365
return true;
360366
}
361367
case VSCodeOscPt.Property: {
362-
const deserialized = args.length ? deserializeMessage(args[0]) : '';
368+
const arg0 = args[0];
369+
const deserialized = arg0 !== undefined ? deserializeMessage(arg0) : '';
363370
const { key, value } = parseKeyValueAssignment(deserialized);
364371
if (value === undefined) {
365372
return true;
@@ -539,10 +546,14 @@ export function parseKeyValueAssignment(message: string): { key: string; value:
539546
}
540547

541548

542-
export function parseMarkSequence(sequence: string[]): { id?: string; hidden?: boolean } {
549+
export function parseMarkSequence(sequence: (string | undefined)[]): { id?: string; hidden?: boolean } {
543550
let id = undefined;
544551
let hidden = false;
545552
for (const property of sequence) {
553+
// Sanity check, this shouldn't happen in practice
554+
if (property === undefined) {
555+
continue;
556+
}
546557
if (property === 'Hidden') {
547558
hidden = true;
548559
}

src/vs/workbench/contrib/terminal/test/browser/xterm/shellIntegrationAddon.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ suite('ShellIntegrationAddon', () => {
186186
await writeP(xterm, '\x1b]633;D;7\x07');
187187
mock.verify();
188188
});
189+
test('should pass command line sequence to the capability', async () => {
190+
const mock = shellIntegrationAddon.getCommandDetectionMock(xterm);
191+
mock.expects('setCommandLine').once().withExactArgs('', false);
192+
await writeP(xterm, '\x1b]633;E\x07');
193+
mock.verify();
194+
195+
const mock2 = shellIntegrationAddon.getCommandDetectionMock(xterm);
196+
mock2.expects('setCommandLine').twice().withExactArgs('cmd', false);
197+
await writeP(xterm, '\x1b]633;E;cmd\x07');
198+
await writeP(xterm, '\x1b]633;E;cmd;invalid-nonce\x07');
199+
mock2.verify();
200+
});
189201
test('should not activate capability on the cwd sequence (OSC 633 ; P=Cwd=<cwd> ST)', async () => {
190202
strictEqual(capabilities.has(TerminalCapability.CommandDetection), false);
191203
await writeP(xterm, 'foo');

0 commit comments

Comments
 (0)