fix: redact json output values one at a time so urls cannot corrupt it - #10041
Open
Sanjays2402 wants to merge 1 commit into
Open
Sanjays2402 wants to merge 1 commit into
Sanjays2402 wants to merge 1 commit into
Conversation
Previously the whole compact JSON document was passed through redactLog, so a url inside one string value (e.g. a deprecation message) could match across the surrounding JSON up to the next @ and corrupt the output, making commands like npm ls --json --long print nothing and exit 1.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #9873.
I ran into this while generating an SBOM: any project whose tree contains a package with a URL in its deprecation message (e.g.
@esbuild-kit/esm-loader, deprecated as "Merged into tsx: https://tsx.hirok.io") makesnpm ls --json --longprint nothing to stdout and exit 1. No error on the console, just the debug-log path on stderr, so it silently breaks downstream tooling that consumes the JSON.The culprit is
redactValueinlib/utils/display.js. It serialized the whole document to compact JSON and ran the result throughredactLogas one big string.@npmcli/redact's URL matcher then matched from thehttps://inside one value across the JSON punctuation around it (up to the next@, e.g. a scoped_idlike@esbuild-kit/esm-loader@2.6.5), treated the span as embedded credentials, and redacted it, producing JSON that no longer parses. The parse error was swallowed by the output handler, hence the empty stdout and exit 1.The fix redacts each string value on its own instead: the object is normalized with a
JSON.stringify/JSON.parseround trip (sotoJSONvalues like Dates keep working), then a small recursive walk appliesredactLogper string. A URL in one value can no longer swallow its neighbors, while passwords in URLs are still redacted as before.This also addresses #9112, which has the same root cause (
npm search --jsonoutput corrupted the same way).Repro (before the fix,
npm ls --json --longexits 1 with 0 bytes on stdout; after, exit 0 with valid JSON):Tests: added a regression test in
test/lib/utils/display.jscovering a URL-bearing deprecation message next to a scoped_id, arrays, Date round-tripping, and that URL passwords still get redacted. The new test fails on the old code (Unexpected token '*' ... is not valid JSON) and passes with the fix. Fulltest/lib/utils/display.jssuite (71 asserts), plustest/lib/commands/ls.jsandtest/lib/commands/search.js, all green; eslint clean on both touched files.