solidity loader: resolve @-scoped imports via Node-style walk-up#38
Merged
Conversation
The hardcoded `node_modules/<spec>` fallback couldn't see monorepos, didn't verify the package existed, and accepted `..` segments verbatim (letting a malicious solidity source read sibling files). Replace it with `resolveNodeModulesSpec(baseDir, specifier)`: - splits `@scope/pkg/sub/path` correctly - walks up from baseDir looking for `<dir>/node_modules/<pkg>/package.json` - rejects `..` in the subpath - rejects resolutions outside baseDir (the Bundle layer can't store them) `resolveSolImport` is pure again. The Node fallback is applied at both call sites (`collectSolidityFilesFromDisk` already has baseDir; `buildSolidityTree` now accepts it as an option, threaded through from `buildSolidityBundle`).
c80a562 to
f7d38ca
Compare
Drop the manual `while (true)` node_modules walk-up. Node already implements the algorithm via createRequire(anchor).resolve(specifier); anchoring with a sentinel path inside baseDir makes it walk up from there. The path-traversal guard (no `..` segments) and the "must stay inside baseDir" check stay — Node would happily resolve to a sibling file or a parent monorepo's node_modules, neither of which fit in a Bundle's workspace bucket.
resolveNodeModulesSpec(baseDir, fromFile, specifier) now anchors createRequire at <baseDir>/<fromFile> instead of <baseDir>/noop.js. That mirrors Node's actual algorithm: a file under node_modules/foo/ looks in foo/node_modules/ first, only then walking up to the top- level node_modules. Adds a nm-nested fixture that exercises this: src/A.sol imports @dep/x/Y.sol; Y.sol in turn imports @inner/z/Z.sol, which lives in @dep/x's own node_modules. The bundle ends up with three buckets — workspace, @dep/x, and the nested @inner/z under @dep/x.
Drop the separate resolveNodeModulesSpec + resolveNodeModulesRel pair
and inline Node-style resolution as a third strategy inside
resolveSolImport itself. The function now takes an options bag
({ remappings, baseDir }) and tries:
1. remappings (longest prefix wins)
2. relative paths (..-escape returns null)
3. createRequire(<baseDir>/<fromFile>).resolve(<spec>) for
@scope/pkg/... specifiers, when baseDir is provided
Both callers shrink to a single resolveSolImport call. Path-traversal
guards (..) and the bundle-relative conversion stay; they're just
collocated with the strategy that needs them now.
…olve() path.resolve takes multiple segments and normalizes them — there's no need to resolve baseDir first and then join.
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.
Summary
@scope/pkg/...imports when no remapping covers them.resolveNodeModulesSpec(baseDir, specifier)walks up frombaseDirlooking for<dir>/node_modules/<pkg>/package.json(mirroring Node's CJS resolution). Returns the subpath relative tobaseDir, or null if the package can't be found, the subpath contains.., or the resolution would escapebaseDir.resolveSolImportstays pure (no I/O); the Node fallback is layered on at both call sites (collectSolidityFilesFromDiskandbuildSolidityTree).tests/fixtures/solidity-bundle/nm-fallback/covers a project that resolves@oz/contracts/...purely via node_modules — noremappings.txtneeded.Test plan
node --run test— 301 tests passnode --run lint— cleancd nm-fallback && stasis bundle src/A.sol -o out.brresolves@oz/contracts/utils/Math.soltonode_modules/@oz/contracts/utils/Math.sol@oz/contracts/../../etc/passwdreturns nullnode_modules) returns nullhttps://claude.ai/code/session_01YFi2BPkgGhtb2vXKZKL2it
Generated by Claude Code