mirror of
https://github.com/affaan-m/ECC.git
synced 2026-11-23 08:15:29 +00:00
fix(dev-server-block): decide the script name from tokens, not raw text
The hook matched `npm\s+run\s+dev` against the raw command string, which cannot see through the shell's own quoting. Every one of these reached the shell as a plain `npm run dev` and was allowed: npm run "dev" npm --silent run dev npm run 'dev' yarn --cwd app dev npm run de"v" pnpm "dev" / yarn run "dev" / bun run "dev" Read the script name from the tokens instead, reusing the quote-aware readToken() the file already had: skip options (and the value of one known to take a value), skip a `run` / `run-script` keyword, stop at `--`, then compare the first remaining token against /^dev(?::|$)/. `dev-setup` / `dev-docs` / `dev-build` stay allowed by the same rule the old lookahead encoded, `npm test -- --grep dev` is allowed because `--` ends the manager's own arguments, and the tmux launcher exemption is unchanged. The blocking tests were skipped on Windows, where the hook no-ops, so this class could only be caught on CI. The new cases force the platform in-process and therefore run everywhere.
This commit is contained in:
1 parent
d8409a4b08
commit
ddfdfffffc
2 files changed
+156
-6
No files matched your search
@@ -161,11 +161,82 @@ process.stdin.on('data', chunk => {
|
||||
});
|
||||
|
||||
const TMUX_LAUNCHER = /^\s*tmux\s+(new|new-session|new-window|split-window)\b/;
|
||||
// Trailing (?![\w-]) rather than \b: \b treats a hyphen as a word boundary, so
|
||||
// `dev\b` matches the `dev` prefix of distinct scripts like `dev-setup` /
|
||||
// `dev-docs` / `dev-build` and wrongly blocks them. The lookahead still matches
|
||||
// the dev server (`dev`, `dev;`, `dev:ssr`, ...) but not a `dev-<suffix>` script.
|
||||
const DEV_PATTERN = /\b(npm\s+run\s+dev|pnpm(?:\s+run)?\s+dev|yarn(?:\s+run)?\s+dev|bun(?:\s+run)?\s+dev)(?![\w-])/;
|
||||
// The script name is decided from TOKENS, not from a regex over the raw text.
|
||||
// A raw-text `npm\s+run\s+dev` cannot see through the shell's own quoting, so
|
||||
// `npm run "dev"`, `npm run 'dev'` and `npm run de"v"` all reached the shell as
|
||||
// `npm run dev` and started the very server this hook exists to keep in tmux.
|
||||
// An option in between hid it just as well: `npm --silent run dev` and
|
||||
// `yarn --cwd app dev` were not matched either.
|
||||
//
|
||||
// Trailing `(?::|$)` rather than a word boundary: `dev` and `dev:ssr` are the
|
||||
// dev server, while `dev-setup` / `dev-docs` / `dev-build` are distinct scripts
|
||||
// that must keep running.
|
||||
const DEV_SCRIPT_NAME = /^dev(?::|$)/;
|
||||
const PACKAGE_MANAGERS = new Set(['npm', 'pnpm', 'yarn', 'bun']);
|
||||
const RUN_KEYWORDS = new Set(['run', 'run-script']);
|
||||
// Options that consume the following token, so their value is never mistaken
|
||||
// for the script name (`yarn --cwd app dev` must read `dev`, not `app`).
|
||||
const MANAGER_OPTIONS_WITH_VALUE = new Set([
|
||||
'--prefix',
|
||||
'--cwd',
|
||||
'-C',
|
||||
'--dir',
|
||||
'-d',
|
||||
'--workspace',
|
||||
'-w',
|
||||
'--filter',
|
||||
'-F',
|
||||
'--package',
|
||||
'-p',
|
||||
'--config',
|
||||
'-c'
|
||||
]);
|
||||
|
||||
/**
|
||||
* The script a package manager is being asked to run, or null.
|
||||
*
|
||||
* Reads the tokens after the manager word: skips options (and the value of an
|
||||
* option known to take one), skips a `run` / `run-script` keyword, stops at a
|
||||
* `--` separator, and returns the first remaining token.
|
||||
*/
|
||||
function getRunScriptName(segment, managerWord) {
|
||||
let index = 0;
|
||||
let seenManager = false;
|
||||
let skipNextValue = false;
|
||||
|
||||
while (index < segment.length) {
|
||||
const parsed = readToken(segment, index);
|
||||
if (!parsed) return null;
|
||||
index = parsed.end;
|
||||
|
||||
const token = parsed.token;
|
||||
if (!token) continue;
|
||||
|
||||
if (skipNextValue) {
|
||||
skipNextValue = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!seenManager) {
|
||||
if (normalizeCommandWord(token) === managerWord) seenManager = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Everything after `--` is an argument to the script, not the script name.
|
||||
if (token === '--') return null;
|
||||
|
||||
if (isOptionToken(token)) {
|
||||
if (MANAGER_OPTIONS_WITH_VALUE.has(token)) skipNextValue = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (RUN_KEYWORDS.has(token.toLowerCase())) continue;
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect every command-line segment we should evaluate. Returns the top-level
|
||||
@@ -202,7 +273,11 @@ function collectCheckSegments(cmd) {
|
||||
function isBlockedDevSegment(segment) {
|
||||
const commandWord = getLeadingCommandWord(segment);
|
||||
if (!commandWord || !DEV_COMMAND_WORDS.has(commandWord)) return false;
|
||||
return DEV_PATTERN.test(segment) && !TMUX_LAUNCHER.test(segment);
|
||||
if (TMUX_LAUNCHER.test(segment)) return false;
|
||||
if (!PACKAGE_MANAGERS.has(commandWord)) return false;
|
||||
|
||||
const script = getRunScriptName(segment, commandWord);
|
||||
return Boolean(script) && DEV_SCRIPT_NAME.test(script);
|
||||
}
|
||||
|
||||
process.stdin.on('end', () => {
|
||||
|
||||
@@ -32,6 +32,25 @@ function runScript(command) {
|
||||
return { code: result.status || 0, stdout: result.stdout || '', stderr: result.stderr || '' };
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the hook with `process.platform` forced to a POSIX value.
|
||||
*
|
||||
* The hook is a no-op on Windows, so its decision cannot be observed there at
|
||||
* all — yet the decision itself has nothing platform-specific in it.
|
||||
*/
|
||||
function runScriptAsPosix(command) {
|
||||
const input = { tool_input: { command } };
|
||||
const bootstrap =
|
||||
"Object.defineProperty(process, 'platform', { value: 'linux' });" +
|
||||
`require(${JSON.stringify(script)});`;
|
||||
const result = spawnSync('node', ['-e', bootstrap], {
|
||||
encoding: 'utf8',
|
||||
input: JSON.stringify(input),
|
||||
timeout: 10000,
|
||||
});
|
||||
return { code: result.status || 0, stdout: result.stdout || '', stderr: result.stderr || '' };
|
||||
}
|
||||
|
||||
function runTests() {
|
||||
console.log('\n=== Testing pre-bash-dev-server-block.js ===\n');
|
||||
|
||||
@@ -238,6 +257,62 @@ function runTests() {
|
||||
assert.strictEqual(result.stdout.trim(), inputStr, `Expected stdout to contain original input`);
|
||||
}) ? passed++ : failed++);
|
||||
|
||||
// --- Quoted and option-separated spellings (all platforms) ---
|
||||
//
|
||||
// The hook no-ops on Windows, so the blocking tests above are skipped there
|
||||
// and this class of bug could only be caught on CI. The DECISION is
|
||||
// platform-independent, so these force the platform in-process and run
|
||||
// everywhere.
|
||||
//
|
||||
// Measured before the token-based script lookup, every one of these reached
|
||||
// the shell as a plain `npm run dev` and was allowed:
|
||||
//
|
||||
// npm run "dev" allow npm --silent run dev allow
|
||||
// npm run 'dev' allow yarn --cwd app dev allow
|
||||
// pnpm "dev" allow
|
||||
|
||||
for (const command of [
|
||||
'npm run "dev"',
|
||||
"npm run 'dev'",
|
||||
'npm run de"v"',
|
||||
'pnpm "dev"',
|
||||
'yarn run "dev"',
|
||||
'bun run "dev"',
|
||||
'npm --silent run dev',
|
||||
'yarn --cwd app dev'
|
||||
]) {
|
||||
(test(`blocks a quoted or option-separated dev script: ${command}`, () => {
|
||||
const result = runScriptAsPosix(command);
|
||||
assert.strictEqual(result.code, 2, `Expected exit code 2, got ${result.code}`);
|
||||
assert.ok(result.stderr.includes('BLOCKED'), `Expected BLOCKED, got: ${result.stderr}`);
|
||||
}) ? passed++ : failed++);
|
||||
}
|
||||
|
||||
for (const command of [
|
||||
'npm run dev-setup',
|
||||
'pnpm run dev-docs',
|
||||
'yarn dev-build',
|
||||
'npm run build',
|
||||
'npm test',
|
||||
'npm install',
|
||||
// `--` ends the manager's own arguments: everything after it belongs to the
|
||||
// script, so a stray "dev" there is not a dev server.
|
||||
'npm test -- --grep dev',
|
||||
'tmux new-session -d -s dev "npm run dev"'
|
||||
]) {
|
||||
(test(`still allows: ${command}`, () => {
|
||||
const result = runScriptAsPosix(command);
|
||||
assert.strictEqual(result.code, 0, `Expected exit code 0, got ${result.code}: ${result.stderr}`);
|
||||
}) ? passed++ : failed++);
|
||||
}
|
||||
|
||||
(test('blocks the bare `pnpm dev` / `yarn dev` / `bun dev` forms', () => {
|
||||
for (const command of ['pnpm dev', 'yarn dev', 'bun dev', 'npm run dev:ssr']) {
|
||||
const result = runScriptAsPosix(command);
|
||||
assert.strictEqual(result.code, 2, `Expected exit code 2 for ${command}, got ${result.code}`);
|
||||
}
|
||||
}) ? passed++ : failed++);
|
||||
|
||||
// --- Summary ---
|
||||
|
||||
console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`);
|
||||
|
||||
Reference in new issue
Block a user