Repository navigation
fix(core): resolve bundled node_modules preloads and bins in install hooks - #91
Conversation
A package can run its own code from a lifecycle hook through a bundled dependency or an extension-less preload without the hook escalating. Bare names now resolve against the tarball only (never the host or the network): - `-r`, `--require`, `--import` and `--loader` with a bare specifier try `node_modules/<spec>` as a file, then `.js`, then the bundled package's `main` and `index.js` (scoped specifiers too). A name that is not shipped resolves nothing, so a benign bundled `dotenv/config` stays clean. - `NODE_OPTIONS` and any `VAR=value` prefix are tokenized (a quoted value may span words) and get the same preload handling as the command line, including extension-less relative values. - A command word also resolves to `node_modules/.bin/<word>` and to the file a bundled package's `bin` entry points at (string or object form), mapped once per audit. Two tests that pinned the old behavior now assert it with an unshipped module and a benign bundled module. Bump AUDIT_REVISION to 5: rule semantics changed. Closes #87
- Quoted assignment values (`NODE_OPTIONS='-r ./hook'`) are spanned in linear time: the open quote is counted from the real quote state, the closing word comes from the precomputed index, and each word is spanned at most once. The spanned words are still read as normal words, so spanning only adds coverage and cannot hide a command. - Every file a bundled package's `bin` points at is scanned, per command name and across all declaring packages, together with `node_modules/.bin/<name>`, regardless of tar order. Targets are exact normalized paths (no `.js` appended, no `..`), and package names over 214 characters are skipped. - Shell builtins in direct command position skip the bin lookup; behind `env`, `sudo`, `exec`, `nohup`, `npx` or `xargs` they do not. - Preload flags are parsed only as flags (`-r`, `--require`, `--import`, `--loader`, `--experimental-loader`), shared by the command line and `NODE_OPTIONS`; quotes around the value are removed, and `--title=--require=x` is not a preload. - A dotted relative preload (`./hook.prod`) tries `.js`. - A bundled module is followed through the spec as a file (`.js`, `.mjs`, `.cjs`), `main`, `index.js` and every string target of `exports` (bounded walk). Refs #87
… packages
- After a quoted assignment value spans words, prefix parsing resumes
after the closing quote, so `NODE_OPTIONS='-r ./hook' node setup`
resolves `setup` and `A='x y' NODE_OPTIONS=--require=./h.js node -v`
finds `h.js`. The spanned words are also scanned as a command line of
their own, so spanning cannot hide a command.
- Option tokens split on unquoted whitespace only, so text inside a
quoted `--title` is not a preload flag.
- Bin targets and command names use npm's normalization (`\` to `/`,
then `path.posix.join("/", v).slice(1)`, so `..` clamps at the root).
- `xargs` is a wrapper, so its command operand gets the bundled-bin
lookup.
- A dotted relative preload also tries `.js` through the suffix lookup
(after a `cd`).
- Each bundled package is resolved once, cached by directory (misses
included), and merged into a hook's result once, with no duplicate
spill entries.
- A package that cannot be enumerated cheaply fails closed: when its
directory name is over 214 characters or its `exports` walk hits the
node bound, every retained code file under it counts as executed. One
pass groups the retained files by package directory.
- The shell-builtin skip list is limited to builtins of every shell npm
may use.
Refs #87
…osed set - A fail-closed package's file list is merged through the per-hook package guard for bin aliases as well as module fallbacks, so any number of aliases merges it at most once. - A fail-closed package counts every retained file under its directory, not only code files; its entry, declared bin targets and `exports` targets are among them. - Bin command names follow npm's order: `\` and `:` to `/`, basename, then normalize. - `NODE_OPTIONS` tokens are the union of a quote-aware and a plain whitespace split, so tokenizer differences with Node can only add coverage. This replaces the quote-aware-only expectation in the test. - A quoted assignment span ends at the first word holding an unescaped closing quote, so an unquoted suffix after it (`'x y'z`) no longer hides the assignments that follow. Refs #87
A quoted assignment value now ends at the first word after which the quote state is balanced, found by feeding each following word through the same quote and escape state machine. `A='x y'z'w q' node setup` no longer ends early and still reaches an extension-less `setup`. Spans never overlap, and a per-command word budget keeps the walks linear; an unbalanced span consumes only its own word. `NODE_OPTIONS` values are also scanned after shell quote removal (`--require=./a'b'.js` is `./ab.js`), as part of the existing union of tokenizations. Refs #87
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Sentinel dependency audit — WARN · ✓ ok145 allow · 5 warn · 0 block · 0 error
▶ Run Sentinel · 150 packages audited · 2026-11-20T13:00:03.952Z · SBOM uploaded as a build artifact |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 93d3d27bd9
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
`node -r buffer` loads the core module even when node_modules/buffer is bundled. A fixed built-in list (or a node: specifier) skips bundled resolution, keeping the verdict independent of the auditing Node version.
`buffer/evil` is not a core module; Node resolves it from node_modules. Match whole ids plus the real built-in subpaths instead of the first segment.
A package could run its own code from an install hook through a module it bundles in
node_modules, and the hook would only rate low (#87):node -r evil setup.js,NODE_OPTIONS=--require=./hook node -v, or a command name served by a bundled package'sbin. Theinstall-scriptsrule now resolves those names against the tarball itself, never against the host or the network.-r,--require,--import,--loader) with a bare specifier resolve to the bundled module's file,main,index.js,.js/.mjs/.cjscandidates and everyexportstarget. Relative preloads get the.jsand/index.jsretries, dotted names included.NODE_OPTIONSand otherVAR=valueprefixes go through the same preload parser. The value is scanned three ways (quote-aware, plain whitespace, after shell quote removal) and the results are combined, so tokenizer differences only add coverage. Quoted assignments that span words end where the shell's quotes balance.node_modules/.bin/<name>and every bundled package'sbinmap (npm's key and path normalization, all targets per name). POSIX shell builtins in direct position (echo ready) skip the bin lookup.exportswalk exceeds 1,000 nodes, or whose directory name exceeds npm's 214-character limit, has every retained file treated as executed.AUDIT_REVISIONgoes to 5.Proof: 44 new tests in
packages/core/test/security-bundled-modules.test.ts, each failing before its fix (two are timing guards).npm testpasses (1512 tests, 0 failures, 4 Linux-only skips). Reviewed with sol-loop: 4 rounds with gpt-6.2-sol. Sol verdict: approved.Closes #87.
Created with Claude Opus 5.5 in Claude Code (T3 Code). A Sonnet 5.5 subagent wrote most of the code.