Files
ruflo/plugins/ruflo-core/scripts/escape-cmd-arg.test.cjs
T
rUv 8008bc3562 fix(federation): verified event identity must win over publisher-controlled content (#3322)
* fix(federation): verified event identity must win over publisher-controlled content

fetchRecent/fetchManyOn/fetchChannel spread the parsed message content
after the signature-verified id/pubkey/created_at, so a publisher could
put those keys in their own content and overwrite their verified
identity downstream (reduceClaims trusts .pubkey for claim
release/handoff authorization). Reorder so verified fields always win.

Also in x-federation-channels.ts: reqEvents() never called verifyEvent()
at all, so channel-read/grant-accept events were trusted unsigned; add
the check. Same content-override reorder in x_federation_channel_read.

fix(hooks): escape argv for the Windows shell:true npm-shim path

ruflo-hook.cjs's invokeHook() passes hook-derived values (command text,
file paths) through cmd.exe via shell:true to resolve npm's .cmd/.ps1
shims. Node does no escaping in that mode, so a value containing a
cmd.exe metacharacter could be reinterpreted as a separate command or
redirection instead of reaching the CLI as data. Add escapeCmdArg()
(qntm.org/cmd algorithm, same one cross-spawn uses) and apply it to
every argv element on that path.

No native Windows/cmd.exe available to execute this end-to-end in this
environment — verified at the string-transform level only (8 unit
tests). Real Windows execution remains a required follow-up gate.

Co-Authored-By: RuFlo <ruv@ruv.net>
Claude-Session: https://claude.ai/code/session_01RYP825phHMjPRmmjkcAWpq

* fix(ci): register new hook test env var and test file with CI guards

Follow-up to the previous commit's escape-cmd-arg.test.cjs:
- Register RUFLO_HOOK_UNIT_TEST in audit-env-var-precedence.mjs's
  KNOWN_ESCAPE_HATCHES, matching the existing RUFLO_HOOK_CLI_OVERRIDE/
  RUFLO_HOOK_DEBUG_STDOUT entries for the same file.
- Register plugins/ruflo-core/scripts/escape-cmd-arg.test.cjs in
  ci-test-baseline.txt, matching the sibling mcp-launch.test.cjs entry —
  both are node:test-based .cjs files that vitest's own JSON reporter
  marks "failed" (it doesn't recognize the node:test API) even though
  every test inside genuinely passes under `node --test`.

Verified locally: node scripts/audit-env-var-precedence.mjs now passes
(exit 0). A scoped `vitest run` + `ci-test-ratchet.mjs --report` against
just these two files confirms escape-cmd-arg.test.cjs is no longer an
unexpected failure (the one remaining flagged file is a stale local
worktree artifact under .claude/worktrees/, not part of a clean CI
checkout).

Co-Authored-By: RuFlo <ruv@ruv.net>
Claude-Session: https://claude.ai/code/session_01RYP825phHMjPRmmjkcAWpq
2026-09-14 17:12:05 +00:00

75 lines
3.5 KiB
JavaScript

'use strict';
/**
* Unit tests for ruflo-hook.cjs's escapeCmdArg() — the Windows cmd.exe
* argument escaper added for the shell:true argv-injection risk in
* invokeHook() (hook-derived values reach cmd.exe unescaped on win32).
*
* These assert the STRING TRANSFORM only — there is no Windows/cmd.exe
* available in this environment to execute the escaped output end-to-end.
* The properties checked below are the ones the algorithm
* (https://qntm.org/cmd, matching `cross-spawn`'s Windows escaping) exists
* to guarantee: every cmd.exe metacharacter is neutralized, and the
* CommandLineToArgvW quote/backslash rules are respected so the value
* round-trips as ONE literal argument. Real native-Windows execution
* remains a required follow-up gate before calling this closed.
*/
const test = require('node:test');
const assert = require('node:assert/strict');
// See ruflo-hook.cjs's own comment on this flag: requiring the file always
// runs main() otherwise (hooks.json has no require.main === module boundary
// to gate on), which would process.exit(0) before this test file's
// assertions ever ran.
process.env.RUFLO_HOOK_UNIT_TEST = '1';
const { escapeCmdArg } = require('./ruflo-hook.cjs');
const CMD_METACHARS = ['(', ')', '%', '!', '^', '"', '<', '>', '&', '|', ';', ','];
test('escapeCmdArg: every cmd.exe metacharacter is caret-escaped', () => {
for (const ch of CMD_METACHARS) {
const out = escapeCmdArg(`a${ch}b`);
assert.ok(out.includes(`^${ch}`), `expected ^${ch} in ${JSON.stringify(out)} for input a${ch}b`);
}
});
test('escapeCmdArg: a plain value round-trips as one quoted argument', () => {
assert.equal(escapeCmdArg('hello'), '^"hello^"');
assert.equal(escapeCmdArg('echo hi'), '^"echo hi^"');
});
test('escapeCmdArg: command chaining metacharacters are neutralized, not left live', () => {
// The exact #667 threat model: a hook-derived value like `x & calc.exe` or
// `a | b` must not contain a bare, unescaped &/| after escaping.
for (const payload of ['a & calc.exe', 'a | b', 'a && b', 'a; rm -rf /', 'a > out.txt', 'a < in.txt']) {
const out = escapeCmdArg(payload);
assert.ok(!/(?<!\^)[&|;<>]/.test(out), `unescaped metachar survived in ${JSON.stringify(out)}`);
}
});
test('escapeCmdArg: embedded double quotes do not break out of the argument', () => {
const out = escapeCmdArg('a"b');
// Every quote in the output — including the wrapping ones, which the
// metachar pass caret-escapes too so they survive a .cmd shim's second
// cmd.exe parse — must be caret-escaped. No bare, unescaped `"` anywhere.
assert.ok(!/(?<!\^)"/.test(out), `bare quote survived in ${JSON.stringify(out)}`);
});
test('escapeCmdArg: percent (env-var expansion) and delayed-expansion bang are neutralized', () => {
assert.ok(escapeCmdArg('%PATH%').includes('^%'));
assert.ok(escapeCmdArg('!VAR!').includes('^!'));
});
test('escapeCmdArg: a trailing backslash does not consume the closing quote', () => {
const out = escapeCmdArg('C:\\some\\path\\');
// Must end with an escaped-quote sequence, not a bare trailing backslash
// eating the terminating quote (the classic CommandLineToArgvW footgun).
assert.ok(out.endsWith('\\\\^"') || out.endsWith('^"'), `unexpected ending in ${JSON.stringify(out)}`);
});
test('escapeCmdArg: empty string still produces a valid quoted-empty argument', () => {
assert.equal(escapeCmdArg(''), '^"^"');
});
test('escapeCmdArg: non-string input is coerced safely', () => {
assert.equal(escapeCmdArg(42), '^"42^"');
});