Skip to content

fix(core): resolve bundled node_modules preloads and bins in install hooks - #91

Merged
tonibergholm-codento merged 7 commits into
mainfrom
fix/87-bundled-modules
Nov 20, 2026
Merged

tonibergholm-codento merged 7 commits into
mainfrom
fix/87-bundled-modules

Conversation

@tonibergholm-codento

Copy link
Copy Markdown
Contributor

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's bin. The install-scripts rule now resolves those names against the tarball itself, never against the host or the network.

  • Preload flags (-r, --require, --import, --loader) with a bare specifier resolve to the bundled module's file, main, index.js, .js/.mjs/.cjs candidates and every exports target. Relative preloads get the .js and /index.js retries, dotted names included.
  • NODE_OPTIONS and other VAR=value prefixes 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.
  • Command words also resolve through node_modules/.bin/<name> and every bundled package's bin map (npm's key and path normalization, all targets per name). POSIX shell builtins in direct position (echo ready) skip the bin lookup.
  • Fail closed: a bundled package whose exports walk exceeds 1,000 nodes, or whose directory name exceeds npm's 214-character limit, has every retained file treated as executed.
  • Work stays linear: one lazy bin map per audit, candidates resolved once per package, a per-hook merge guard, and a bounded quote walk. Timing tests cover 60,000 unclosed assignments and 20,000 bin aliases.

AUDIT_REVISION goes 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 test passes (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.

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
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 73f55320-a555-4b30-8b2b-f90c7d4ce6af
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T13:02:18.830186Z b332253 New commits
🔒 Security Review ✅ Completed 2026-11-20T13:04:31.806755Z b332253 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Sentinel dependency audit — WARN · ✓ ok

145 allow · 5 warn · 0 block · 0 error
provenance: 46 verified · 0 invalid · 104 absent · 0 unknown

package verdict score finding how to fix
esbuild@​0.28.1 warn 62/100 `install.js` combines launch + write + read primitives — partial materialization pattern. Review the finding details; approve with a recorded rationale only if you understand and accept the risk.
fast-uri@​3.1.9 warn 75/100 Network egress: connects to a hardcoded IP address. Confirm the egress is expected for this package's purpose; if not, remove it or pin to a version without it.
isexe@​2.0.0 warn 77/100 `test/basic.js` combines read + launch + write primitives — partial materialization pattern. Review the finding details; approve with a recorded rationale only if you understand and accept the risk.
tsx@​4.22.4 warn 76/100 `dist/index-D9F1FXzN.cjs` combines read + write primitives — partial materialization pattern. Review the finding details; approve with a recorded rationale only if you understand and accept the risk.
undici-types@​7.18.2 warn 64/100 Network egress: imports a raw networking module. Confirm the egress is expected for this package's purpose; if not, remove it or pin to a version without it.

▶ Run sentinel explain <package> <version> for a suggested safe version and a ready waiver.

Sentinel · 150 packages audited · 2026-11-20T13:00:03.952Z · SBOM uploaded as a build artifact

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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.
@tonibergholm-codento
tonibergholm-codento merged commit 5a7dd7a into main Nov 20, 2026
7 checks passed
@tonibergholm-codento
tonibergholm-codento deleted the fix/87-bundled-modules branch November 20, 2026 13:46
tonibergholm-codento added a commit that referenced this pull request Nov 20, 2026
Bump all seven workspaces, the exact internal pins, ENGINE_VERSION and the
CLI/MCP versions to 0.1.0-alpha.3, and add the release notes. The release
carries the security audit hardening (#84) and the install-script
detection-gap fixes (#89, #90, #91, #92).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detection gap: preloads and bins from bundled node_modules are not resolved in install scripts

1 participant