Skip to content

Commit

Permalink
fix: refactor shell escaping, only on windows and escape cmd/pwsh args
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed May 28, 2024
1 parent d5ec9ea commit 2b0700f
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 42 deletions.
16 changes: 10 additions & 6 deletions bin/pact-broker.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#!/usr/bin/env node

import childProcess = require('child_process');
import rubyStandalone from '../src/pact-standalone';
import {
standalone,
standaloneUseShell,
setStandaloneArgs,
} from '../src/pact-standalone';

const isWindows = process.platform === 'win32'
const opts = isWindows ? { shell: true } : {}
const args = process.argv.slice(2);
const opts = standaloneUseShell ? { shell: true } : {};
const { error, status } = childProcess.spawnSync(
rubyStandalone.brokerFullPath,
process.argv.slice(2),
standalone().brokerFullPath,
setStandaloneArgs(args, standaloneUseShell),
{
stdio: 'inherit',
...opts
...opts,
}
);
if (error) throw error;
Expand Down
17 changes: 11 additions & 6 deletions bin/pact-message.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#!/usr/bin/env node

import childProcess = require('child_process');
import rubyStandalone from '../src/pact-standalone';
import {
standalone,
standaloneUseShell,
setStandaloneArgs,
} from '../src/pact-standalone';

const args = process.argv.slice(2);
const opts = standaloneUseShell ? { shell: true } : {};

const isWindows = process.platform === 'win32'
const opts = isWindows ? { shell: true } : {}
const { error, status } = childProcess.spawnSync(
rubyStandalone.messageFullPath,
process.argv.slice(2),
standalone().messageFullPath,
setStandaloneArgs(args, standaloneUseShell),
{
stdio: 'inherit',
...opts
...opts,
}
);
if (error) throw error;
Expand Down
17 changes: 11 additions & 6 deletions bin/pact-mock-service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#!/usr/bin/env node

import childProcess = require('child_process');
import rubyStandalone from '../src/pact-standalone';
import {
standalone,
standaloneUseShell,
setStandaloneArgs,
} from '../src/pact-standalone';

const args = process.argv.slice(2);
const opts = standaloneUseShell ? { shell: true } : {};

const isWindows = process.platform === 'win32'
const opts = isWindows ? { shell: true } : {}
const { error, status } = childProcess.spawnSync(
rubyStandalone.mockServiceFullPath,
process.argv.slice(2),
standalone().mockServiceFullPath,
setStandaloneArgs(args, standaloneUseShell),
{
stdio: 'inherit',
...opts
...opts,
}
);
if (error) throw error;
Expand Down
17 changes: 11 additions & 6 deletions bin/pact-provider-verifier.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#!/usr/bin/env node

import childProcess = require('child_process');
import rubyStandalone from '../src/pact-standalone';
import {
standalone,
standaloneUseShell,
setStandaloneArgs,
} from '../src/pact-standalone';

const args = process.argv.slice(2);
const opts = standaloneUseShell ? { shell: true } : {};

const isWindows = process.platform === 'win32'
const opts = isWindows ? { shell: true } : {}
const { error, status } = childProcess.spawnSync(
rubyStandalone.verifierFullPath,
process.argv.slice(2),
standalone().verifierFullPath,
setStandaloneArgs(args, standaloneUseShell),
{
stdio: 'inherit',
...opts
...opts,
}
);
if (error) throw error;
Expand Down
17 changes: 11 additions & 6 deletions bin/pact-stub-service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#!/usr/bin/env node

import childProcess = require('child_process');
import rubyStandalone from '../src/pact-standalone';
import {
standalone,
standaloneUseShell,
setStandaloneArgs,
} from '../src/pact-standalone';

const args = process.argv.slice(2);
const opts = standaloneUseShell ? { shell: true } : {};

const isWindows = process.platform === 'win32'
const opts = isWindows ? { shell: true } : {}
const { error, status } = childProcess.spawnSync(
rubyStandalone.stubFullPath,
process.argv.slice(2),
standalone().stubFullPath,
setStandaloneArgs(args, standaloneUseShell),
{
stdio: 'inherit',
...opts
...opts,
}
);
if (error) throw error;
Expand Down
17 changes: 11 additions & 6 deletions bin/pact.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#!/usr/bin/env node

import childProcess = require('child_process');
import rubyStandalone from '../src/pact-standalone';
import {
standalone,
standaloneUseShell,
setStandaloneArgs,
} from '../src/pact-standalone';

const args = process.argv.slice(2);
const opts = standaloneUseShell ? { shell: true } : {};

const isWindows = process.platform === 'win32'
const opts = isWindows ? { shell: true } : {}
const { error, status } = childProcess.spawnSync(
rubyStandalone.pactFullPath,
process.argv.slice(2),
standalone().pactFullPath,
setStandaloneArgs(args, standaloneUseShell),
{
stdio: 'inherit',
...opts
...opts,
}
);
if (error) throw error;
Expand Down
17 changes: 11 additions & 6 deletions bin/pactflow.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#!/usr/bin/env node

import childProcess = require('child_process');
import rubyStandalone from '../src/pact-standalone';
import {
standalone,
standaloneUseShell,
setStandaloneArgs,
} from '../src/pact-standalone';

const args = process.argv.slice(2);
const opts = standaloneUseShell ? { shell: true } : {};

const isWindows = process.platform === 'win32'
const opts = isWindows ? { shell: true } : {}
const { error, status } = childProcess.spawnSync(
rubyStandalone.pactflowFullPath,
process.argv.slice(2),
standalone().pactflowPath,
setStandaloneArgs(args, standaloneUseShell),
{
stdio: 'inherit',
...opts
...opts,
}
);
if (error) throw error;
Expand Down
45 changes: 45 additions & 0 deletions src/pact-standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,49 @@ export const standalone = (
};
};

const isWindows = process.platform === 'win32';

function quoteCmdArg(arg: string) {
return `"${arg.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
}

function quotePwshArg(arg: string) {
return `'${arg.replace(/'/g, "''")}'`;
}

function quotePosixShArg(arg: string) {
return `'${arg.replace(/'/g, "'\\''")}'`;
}

function testWindowsExe(cmd: string, file: string) {
return new RegExp(`^(?:.*\\\\)?${cmd}(?:\\.exe)?$`, 'i').test(file);
}

function parseArgs(unparsed_args: string[]) {
if (isWindows === true) {
const file = process.env['comspec'] || 'cmd.exe';
if (testWindowsExe('cmd', file) === true) {
return unparsed_args.map((i) => quoteCmdArg(i));
}
if (testWindowsExe('(powershell|pwsh)', file) || file.endsWith('/pwsh')) {
return unparsed_args.map((i) => quotePwshArg(i));
}
return unparsed_args;
}
return unparsed_args.map((i) => quotePosixShArg(i));
}

export function setStandaloneArgs(
unparsed_args: string[],
shell: boolean
): string[] {
let parsedArgs = unparsed_args;
if (shell === true) {
parsedArgs = parseArgs(unparsed_args);
}
return parsedArgs;
}

export const standaloneUseShell = isWindows;

export default standalone();

0 comments on commit 2b0700f

Please sign in to comment.