mirror of
https://github.com/pbakaus/impeccable.git
synced 2026-09-12 14:16:28 +03:00
* Fix: use argv exec and single-quote escaping for the four #476 shell-injection sites JSON.stringify and raw double-quote interpolation were used as shell quoting, but /bin/sh still expands $(...), backticks, and ${} inside double quotes. - is-generated.mjs / live.mjs runScript: switch execSync string commands to execFileSync argv form, which never invokes a shell. Closes the remote path where a source file named `$(...)` executes during the live-mode walk. - skills.mjs hook command + hook-lib.mjs ignore-value suggestion: values that must stay shell strings now use POSIX single-quote escaping instead of JSON/double quotes. The doctor's hook-token parser learns the single-quoted absolute form so it keeps verifying user-level installs. Adds regression tests for the single-quoted absolute hook form and the single-quoted ignore-value suggestion. Verified end to end in a browser through a real live-mode wrap walk against a hostile-named source file. Prepared with AI assistance (Cursor) under maintainer instruction. Co-authored-by: Cursor <cursoragent@cursor.com> * Test: lock in POSIX single-quoting for a $(...) absolute install path (#476) Follow-up from security review: prove an install path embedding $(...) is single-quoted in the written hook manifest, not double-quoted. Prepared with AI assistance (Cursor) under maintainer instruction. Co-authored-by: Cursor <cursoragent@cursor.com> * Fix: quote ignore-command args per platform so Windows cmd.exe keeps spaces (#533) Greptile flagged that switching quoteCommandArg to POSIX single quotes fixed $(...) injection on /bin/sh but regressed Windows cmd.exe, where single quotes are literal, so a --file path containing spaces was split and the ignore scope was stored malformed. The suggested command runs on the same machine the hook fired on, so branch on process.platform (the pattern skills.mjs already uses): single-quote on POSIX for the #476 fix, and keep the original double-quote escaping on Windows so that path's behavior is unchanged. Adds a regression test asserting both forms. Prepared with AI assistance (Cursor) under maintainer instruction. Co-authored-by: Cursor <cursoragent@cursor.com> * Test: prove the POSIX hook guard is inert under /bin/sh and Windows keeps double quotes (#533) Greptile's probe could not reach the generated manifest, leaving the hook command contract unverified. Convert that into committed proof: - POSIX: install with a $(touch pwned) absolute path, then actually execute the generated guard under /bin/sh from a clean cwd and assert no marker file appears and the guard exits 0 (single-quoted substitution stays inert). - Windows: drive copyProviderHooks as win32 in-process and assert the command keeps the double-quoted absolute path (usable when the install path has spaces; $(...) is inert on cmd.exe anyway). Test-only; source quoting is unchanged. Prepared with AI assistance (Cursor) under maintainer instruction. Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
73 lines
2.4 KiB
JavaScript
73 lines
2.4 KiB
JavaScript
/**
|
|
* Decide whether a given file is "generated" (regenerated by a build step,
|
|
* unsafe to write variants into) or "source" (safe to edit, changes persist).
|
|
*
|
|
* Why this matters: when the user picks an element on a page whose underlying
|
|
* file is regenerated by a build step (e.g. `scripts/build-sub-pages.js`
|
|
* rewriting `public/docs/*.html`), writing variants or accepted changes into
|
|
* that file is silent data loss — the next build wipes them.
|
|
*
|
|
* Signals, in order of reliability:
|
|
* 1. Git check-ignore: gitignored files are assumed generated.
|
|
* 2. File-header markers ("GENERATED", "DO NOT EDIT", "AUTO-GENERATED")
|
|
* within the first ~300 characters — catches non-git projects.
|
|
*/
|
|
|
|
import { execFileSync } from 'node:child_process';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
const HEADER_SCAN_BYTES = 300;
|
|
const HEADER_MARKERS = [
|
|
/@generated\b/i,
|
|
/\bGENERATED\s+FILE\b/,
|
|
/\bAUTO-?GENERATED\b/i,
|
|
/\bDO\s+NOT\s+EDIT\b/i,
|
|
];
|
|
|
|
/**
|
|
* @param {string} filePath - absolute or cwd-relative path
|
|
* @param {object} [options]
|
|
* @param {string} [options.cwd] - project root (defaults to process.cwd())
|
|
*/
|
|
export function isGeneratedFile(filePath, options = {}) {
|
|
const cwd = options.cwd || process.cwd();
|
|
const absPath = path.isAbsolute(filePath) ? filePath : path.resolve(cwd, filePath);
|
|
|
|
if (isGitIgnored(absPath, cwd)) return true;
|
|
if (hasGeneratedHeader(absPath)) return true;
|
|
return false;
|
|
}
|
|
|
|
function isGitIgnored(absPath, cwd) {
|
|
try {
|
|
// argv form, never a shell: this runs on every file the live-mode source
|
|
// walk reaches, so a hostile filename embedding $(...) or backticks must
|
|
// not be interpretable (issue #476). JSON.stringify is not shell quoting.
|
|
execFileSync('git', ['check-ignore', '--quiet', absPath], {
|
|
cwd,
|
|
stdio: 'ignore',
|
|
});
|
|
return true; // exit 0 = ignored
|
|
} catch (err) {
|
|
// Exit code 1 = not ignored. Exit code 128 = not a git repo or other error.
|
|
// In both cases, treat as "not known to be ignored."
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function hasGeneratedHeader(absPath) {
|
|
let fd;
|
|
try {
|
|
fd = fs.openSync(absPath, 'r');
|
|
const buf = Buffer.alloc(HEADER_SCAN_BYTES);
|
|
const bytesRead = fs.readSync(fd, buf, 0, HEADER_SCAN_BYTES, 0);
|
|
const head = buf.slice(0, bytesRead).toString('utf-8');
|
|
return HEADER_MARKERS.some((re) => re.test(head));
|
|
} catch {
|
|
return false;
|
|
} finally {
|
|
if (fd !== undefined) { try { fs.closeSync(fd); } catch {} }
|
|
}
|
|
}
|