Skip to content

Commit

Permalink
fix(util): tune npm to escape injection on shell commands
Browse files Browse the repository at this point in the history
  • Loading branch information
narekhovhannisyan committed Dec 5, 2024
1 parent ace8e45 commit ced3b21
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions src/if-check/util/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,46 @@ export const executeCommands = async (manifest: string, cwd: boolean) => {
const sanitizedManifest = escapeShellArg(manifest);
const sanitizedExecutedManifest = escapeShellArg(executedManifest);

const ifEnvCommand = `${
isGlobal ? 'if-env' : 'npm run if-env'
} ${prefixFlag} -- -m ${sanitizedManifest}`;
const ifRunCommand = `${
isGlobal ? 'if-run' : 'npm run if-run'
} ${prefixFlag} -- -m ${sanitizedManifest} -o ${sanitizedExecutedManifest}`;
const ttyCommand = "node -p 'Boolean(process.stdout.isTTY)'";
const ifDiffCommand = `${
isGlobal ? 'if-diff' : 'npm run if-diff'
} ${prefixFlag} -- -s ${sanitizedExecutedManifest}.yaml -t ${sanitizedManifest}`;

const fullCommand =
[ifEnvCommand, ifRunCommand, ttyCommand].join(' && ') +
` | ${ifDiffCommand}`;
const ifEnvCommand = [
isGlobal ? 'if-env' : 'npm run if-env',
'--',
...(prefixFlag === '' ? [] : prefixFlag),
'-m',
sanitizedManifest,
];

const ifRunCommand = [
isGlobal ? 'if-run' : 'npm run if-run',
'--',
...(prefixFlag === '' ? [] : prefixFlag),
'-m',
sanitizedManifest,
'-o',
sanitizedExecutedManifest,
];

const ttyCommand = ['node', '-p', "'Boolean(process.stdout.isTTY)'"];
const ifDiffCommand = [
isGlobal ? 'if-diff' : 'npm run if-diff',
'--',
...(prefixFlag === '' ? [] : prefixFlag),
'-s',
`${sanitizedExecutedManifest}.yaml`,
'-t',
sanitizedManifest,
];

const fullCommand = [
...ifEnvCommand,
'&&',
...ifRunCommand,
'&&',
...ttyCommand,
'|',
...ifDiffCommand,
].join(' ');

// Execute the full command
await execPromise(fullCommand, {
cwd: process.env.CURRENT_DIR || process.cwd(),
});
Expand Down

0 comments on commit ced3b21

Please sign in to comment.