Skip to content

fix: harden npmGlobalRoot against a nameless home, rewritten npmrc keys and untrimmed env values - #403

Merged
breferrari merged 6 commits into
mainfrom
issue-399-npm-root-hardening
Oct 14, 2026
Merged

breferrari merged 6 commits into
mainfrom
issue-399-npm-root-hardening

Conversation

@breferrari

Copy link
Copy Markdown
Owner

Summary

Closes #399. Hardens npmGlobalRoot (#382) in lib/qmd.ts and its hand-kept twin in qmd-mcp.mjs. Every change lands in both copies, and the tests run each case against both.

  • A home the OS can't name no longer crashes the lookup. With HOME unset, os.homedir() throws for a UID with no passwd entry. The home is now looked up only at rule 3, the first rule that reads ~/.npmrc, and a throwing lookup asks npm. So an env prefix still answers without one.

  • npmrc keys that npm's ini would rewrite ask npm. In npm 10.9.2's bundled ini 5.0.0, read 2026-10-14, unsafe() trims a key and unquotes it, JSON-decoding a double-quoted one, and a ; or # cuts it. So these keys ask npm:

    • a quoted key;
    • a key built from ${…};
    • a key holding ;, #, brackets, a backslash or inner whitespace.

    Auth keys (//host/path/:_authToken, with %, ~ or +) still count as plain. This applies to the user npmrc, the Node-prefix npmrc and the builtin npmrc.

  • Env values are read as npm reads them. @npmcli/config's loadEnv skips only "" and doesn't trim. So a whitespace-only npm_config_* value is set, not unset, and a padded prefix asks npm, because exactPath rejects padded values. The old code trimmed them, which was wrong too.

  • The "npm agrees" test drops the npm_config_* that npm test exports, so rule 1 no longer just echoes npm back. It now compares roots whenever one is worked out, not only when qmd sits under it, so it catches a CI leg where the two differ.

  • Item 4 is declined (dated note on the issue). globalQmdEntry is now exported, with injectable parameters for the tests, and still asks npm when qmd isn't under the worked-out root. That root models the npm beside this Node, and PATH's npm can differ: a self-upgraded npm on Windows, or mixed installs (inferred). Skipping the spawn would then fall back to the shell qmd, which fails on stock Windows. The cost is the pre-Harden npmGlobalRoot: a throwing homedir(), quoted npmrc keys, whitespace env values, and a wasted npm spawn #399 one, paid only on a miss.

Left open:

Type

  • fix — bug fix

Checklist

  • PR title follows type: description format
  • Links an issue assigned to me, or is a trivial typo fix in documentation
  • A code change comes with a test that fails on main and passes with it: ae36045 adds the red tests ahead of the fix, in tests/qmd.test.ts, run against both copies.
  • Tests pass (cd .claude/scripts && npm test): left to CI. Locally: typecheck, and tests/qmd.test.ts passes 70/70 with nothing skipped.
  • Any NEW guard… : exactPath's padding rule and the npmrc key check each have a red test and a mutation (below).
  • Doc tables updated: n/a
  • No changes to CHANGELOG.md or vault-manifest.json version

Review. Each line names the commits the review ran on.

  • /simplify ran on ae36045 and 6d74ef9. Its findings are 34cb05a:

    • one shared key check;
    • the home looked up lazily;
    • the padding rule moved into exactPath;
    • rmTemp and a shared entry path in the tests.

    Skipped: one module for both copies, because the .mjs can't import the .ts.

  • /two-axis-review against main ran on ae36045 to 34cb05a. Its findings are e66c548: the askNpmForRoot name, and the dated npm source reading.

  • /code-review high ran on ae36045 to e66c548. Its findings are 19ba87b:

    • item 4 declined, after the repo owner's ruling;
    • the stronger npm-agrees test;
    • the narrowed key check;
    • the header comments.

    Skipped: upgrade-plan.ts's spawn.

  • /code-review high ran on 19ba87b, the commit after the first review. Its findings are 9341a54:

    • ; and # keys;
    • tests for every flagged form;
    • the docs;
    • askNpmForRoot reading stdout the same way in both copies;
    • the detector comment.

    Skipped, each per the ruling above:

    • the miss-path spawn cost;
    • the hit-path order;
    • resolving PATH's npm.

    9341a54 has not had a review of its own. Its mutations are below.

Mutation: 22 mutations, 11 in each copy, each caught by tests/qmd.test.ts:

  • the home lookup is not caught;
  • a rewritten key counts as plain;
  • quoted keys count as plain;
  • any punctuation asks npm (%, ~, +);
  • ; and # don't count (covers 9341a54);
  • brackets, a backslash and whitespace don't count (covers 9341a54);
  • the builtin's userconfig doesn't count;
  • whitespace env values are skipped;
  • a padded prefix is used;
  • a miss under the worked-out root doesn't ask npm;
  • a hit under the worked-out root still asks npm.

🤖 Generated with Claude Code

breferrari and others added 6 commits October 14, 2026 03:33
…keys, untrimmed env values, and the wasted npm spawn (#399)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…c keys and untrimmed env values, and skip the needless npm spawn (#399)

- A home the OS can't name (a UID with no passwd entry) asks npm instead
  of throwing out of resolveQmdEntry.
- A quoted npmrc key asks npm: npm's ini unquotes keys.
- Env values are read as npm's loadEnv reads them: only "" skipped,
  nothing trimmed, so a padded or whitespace prefix asks npm.
- A worked-out root without qmd is the answer: npm root -g would give
  the same root, so it is no longer spawned.
- The npm-agrees test drops the npm_config_* that npm test exports.

Both copies (lib/qmd.ts, qmd-mcp.mjs) carry each change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e 3 needs it, and the padding rule in exactPath (#399)

/simplify findings: both npmrc filters share one check, a key that isn't
a plain word asks npm (quotes, escapes and ${} alike); an env prefix
answers even when the OS home lookup throws; the untrimmed-value rule
lives in exactPath with its reason; globalQmdEntry reads computed ??
askNpm(); the new tests use rmTemp and a shared entry path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… reading (#399)

/two-axis-review findings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rrow the npmrc key check (#399)

/code-review findings: a worked-out root models the npm beside this Node,
and the npm on PATH can differ, so a miss there still asks npm (item 4
declined, 2026-10-14). The npm-agrees test now compares roots whenever
one is worked out. Auth keys with % ~ + are plain again; only a key npm's
ini would rewrite asks npm. Headers name every hand-kept function.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…orm (#399)

Second /code-review findings: a key holding ; or # sets the part before
it, so it asks npm; tests pin brackets, a backslash and inner whitespace
too; the docs list every form; askNpmForRoot reads stdout the same way in
both copies; the npm-agrees test says it is a detector.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@breferrari
breferrari merged commit e21ed91 into main Oct 14, 2026
12 checks passed
@breferrari
breferrari deleted the issue-399-npm-root-hardening branch October 14, 2026 01:38
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.

Harden npmGlobalRoot: a throwing homedir(), quoted npmrc keys, whitespace env values, and a wasted npm spawn

1 participant