Repository navigation
Validate require.resolve options.paths entries and handle relative paths #35191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,3 +199,41 @@ | |
| Module._resolveFilename("path", __filename, false, { paths: { 0: "/some/path" } }); | ||
| }).toThrow(); | ||
| }); | ||
|
|
||
| test("require.resolve throws ERR_INVALID_ARG_TYPE if options.paths contains a non-string", () => { | ||
| let err; | ||
| try { | ||
| require.resolve("nonexistent", { paths: [new DataView(new ArrayBuffer(8))] }); | ||
| } catch (e) { | ||
| err = e; | ||
| } | ||
| expect(err.code).toBe("ERR_INVALID_ARG_TYPE"); | ||
|
|
||
| err = undefined; | ||
| try { | ||
| require.resolve("nonexistent", { paths: ["/tmp", {}] }); | ||
| } catch (e) { | ||
| err = e; | ||
| } | ||
| expect(err.code).toBe("ERR_INVALID_ARG_TYPE"); | ||
|
Check warning on line 218 in test/js/node/module/module-resolve-filename-paths.test.js
|
||
| }); | ||
|
|
||
| test("Module._resolveFilename throws ERR_INVALID_ARG_TYPE if options.paths contains a non-string", () => { | ||
| let err; | ||
| try { | ||
| Module._resolveFilename("nonexistent", null, false, { paths: [{}] }); | ||
| } catch (e) { | ||
| err = e; | ||
| } | ||
| expect(err.code).toBe("ERR_INVALID_ARG_TYPE"); | ||
| }); | ||
|
|
||
| test("require.resolve with a relative directory in options.paths does not crash", () => { | ||
| let err; | ||
| try { | ||
| require.resolve("nonexistent", { paths: ["not/an/absolute/path"] }); | ||
| } catch (e) { | ||
| err = e; | ||
| } | ||
| expect(err.code).toBe("MODULE_NOT_FOUND"); | ||
| }); | ||
|
Check warning on line 239 in test/js/node/module/module-resolve-filename-paths.test.js
|
||
|
Comment on lines
+231
to
+239
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 This test only proves the panic is gone — a resolver that skipped every non-absolute entry (replacing the Extended reasoning...What's missingThe let abs_custom = if bun_paths::is_absolute(custom_slice) {
custom_slice
} else {
match self.fs_ref().abs_buf_checked(&[custom_slice], &mut abs_buf) {
Some(p) => p, // (a) absolutize against top_level_dir and search there
None => continue, // (b) skip on failure
}
};The new test at line 231 only asserts Step-by-step: the test can't distinguish the fix from a weaker one
Both implementations produce identical observable behavior for this input. Per REVIEW.md: "confirm deleting each load-bearing clause of your fix breaks at least one test — a test that passes both ways is worse than no test." The Why existing coverage doesn't helpThe other tests in this file ( Suggested additionSomething along these lines (mirroring the existing test("require.resolve resolves a relative options.paths entry against cwd", () => {
const { path: dir, cleanup } = createTempDir("require-resolve-relative-paths", {
"sub/node_modules/rel-pkg/package.json": JSON.stringify({ name: "rel-pkg", main: "index.js" }),
"sub/node_modules/rel-pkg/index.js": "module.exports = 1;",
});
const prev = process.cwd();
try {
process.chdir(dir);
const resolved = require.resolve("rel-pkg", { paths: ["./sub"] });
expect(resolved).toBe(resolve(dir, "sub/node_modules/rel-pkg/index.js"));
} finally {
process.chdir(prev);
cleanup();
}
});This would fail ( Severitynit — the fuzzer-reported panic (the primary bug) is properly regression-tested: a panic would not yield |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 The new tests only assert
err.code, but both C++ sites go to the trouble of buildingmakeString("paths["_s, i, "]"_s)so the error names the offending index — and the["/tmp", {}]case is clearly designed to exercise index 1. Consider also assertingexpect(err.message).toContain("paths[1]")here (and"paths[0]"for the DataView /_resolveFilenamecases) so that an off-by-one inpathIndex++or the loop counter would be caught.Extended reasoning...
What the gap is
Both changed C++ sites deliberately construct an indexed argument name for the error:
ImportMetaObject.cpp:Bun::ERR::INVALID_ARG_TYPE(scope, globalObject, makeString("paths["_s, i, "]"_s), "string"_s, path)inside thefor (size_t i = 0; ...)loop.NodeModuleModule.cpp: a manually-maintainedsize_t pathIndex = 0;incremented inside theforEachInIterablelambda, withmakeString("paths["_s, pathIndex, "]"_s).The new tests, however, only assert
err.code === "ERR_INVALID_ARG_TYPE". They never look aterr.message, so the index that the C++ code computes is untested.Why it matters
The
["/tmp", {}]case at lines ~212-218 exists specifically to put the bad entry at a non-zero index. But if theNodeModuleModule.cppimplementation hadpathIndex++placed before theisString()check instead of after — a very easy mistake with a captured counter inside a lambda — the error would saypaths[1]for the first element andpaths[2]for the second, and every test in this file would still pass. Similarly, if theImportMetaObject.cpploop reported the wrongi, or if either site dropped the index entirely and just said"paths", nothing would fail.REVIEW.md's test guidance is explicit: "assert the strongest invariant… specific error class/code/message (never bare
toThrow())" and "Error messages are reviewed word-for-word as code". The tests already assert the specific error code (which is good — better than baretoThrow()), but since this PR's diff went out of its way to add index formatting, the tests should lock that in.Step-by-step proof
NodeModuleModule.cpp, the lambda body is:pathIndex++;above theisString()check (or switches to a post-increment inside themakeStringcall and gets it wrong).{ paths: ["/tmp", {}] }: iteration 0 sees"/tmp"(string) →pathIndexbecomes 1; iteration 1 sees{}→ error reportspaths[1]. Correct today. But with the increment moved before the check: iteration 0 →pathIndexbecomes 1, string, continue; iteration 1 →pathIndexbecomes 2, not string → error reportspaths[2].err.code === "ERR_INVALID_ARG_TYPE", which is unchanged → test still passes despite the wrong index in the user-visible message.Suggested fix
Add one assertion per case:
This is a test-strengthening suggestion, not a production defect — the runtime behavior is correct as written.