Skip to content

Commit

Permalink
add check
Browse files Browse the repository at this point in the history
  • Loading branch information
kwu-stripe committed Oct 9, 2024
1 parent a6d71b2 commit 0d361d9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/shellEscape.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {OSType, getOSType} from './utils';

export function shellEscape(args: Array<string>): string {
var output: Array<string> = [];

if (getOSType() === OSType.windows) {
args.forEach(function(arg) {
if (/[^A-Za-z0-9_\/:=-]/.test(arg)) {
arg = '"' + arg.replace(/"/g, '\\"') + '"';
arg = arg.replace(/^(?:"")+/g, '') // unduplicate double-quote at the beginning
.replace(/\\"""/g, '\\"'); // remove non-escaped double-quote if there are enclosed between 2 escaped
}
output.push(arg);
});
return output.join(' ');
} else {
args.forEach(function(arg) {
if (/[^A-Za-z0-9_\/:=-]/.test(arg)) {
arg = "'" + arg.replace(/'/g,"'\\''") + "'";
arg = arg.replace(/^(?:'')+/g, '') // unduplicate single-quote at the beginning
.replace(/\\'''/g, "\\'"); // remove non-escaped single-quote if there are enclosed between 2 escaped
};
output.push(arg);
});
return output.join(' ');
};
};
7 changes: 5 additions & 2 deletions src/stripeTerminal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import {StripeClient} from './stripeClient';
import {shellEscape} from './shellEscape';

type SupportedStripeCommand = 'events' | 'listen' | 'logs' | 'login' | 'trigger';

Expand Down Expand Up @@ -29,9 +30,11 @@ export class StripeTerminal {
}
}

const globalCLIFLags = this.getGlobalCLIFlags();
const globalCLIFlags = this.getGlobalCLIFlags();

const commandString = shellEscape([cliPath, command, ...args, ...globalCLIFlags]);


const commandString = [cliPath, command, ...args, ...globalCLIFLags].join(' ');

Check failure on line 38 in src/stripeTerminal.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

More than 2 blank lines not allowed

Check failure on line 38 in src/stripeTerminal.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

More than 2 blank lines not allowed
try {
const terminal = await this.createTerminal();
Expand Down
30 changes: 30 additions & 0 deletions test/suite/shellEscape.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as assert from 'assert';
import * as shellEscape from '../../src/shellEscape';
import * as sinon from 'sinon';
import * as utils from '../../src/utils';


suite('shellEscape', () => {
let sandbox: sinon.SinonSandbox;

setup(() => {
sandbox = sinon.createSandbox();
});

teardown(() => {
sandbox.restore();
});

suite('shellEscape', () => {
test('non windows case: flag with spaces', () => {
sandbox.stub(utils, 'getOSType').returns(utils.OSType.macOSarm);
const output = shellEscape.shellEscape(['--project-name', 'test | whoami']);
assert.strictEqual(output, '--project-name \'test | whoami\'');
});
test.only('windows case: flag with space', () => {
sandbox.stub(utils, 'getOSType').returns(utils.OSType.windows);
const output = shellEscape.shellEscape(['--project-name', 'test | whoami']);
assert.strictEqual(output, '--project-name \"test | whoami\"');
})

Check warning on line 28 in test/suite/shellEscape.test.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing semicolon

Check warning on line 28 in test/suite/shellEscape.test.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Missing semicolon
});
});

0 comments on commit 0d361d9

Please sign in to comment.