fix(tfjs-node): prevent path traversal in NodeFileSystem.loadWeights#8709
Open
adilburaksen wants to merge 1 commit into
Open
fix(tfjs-node): prevent path traversal in NodeFileSystem.loadWeights#8709adilburaksen wants to merge 1 commit into
adilburaksen wants to merge 1 commit into
Conversation
Weight file paths in weightsManifest are joined with path.join(), which normalises ".." components without any boundary check. A crafted model.json can set paths like "../../../etc/passwd" to read arbitrary files that are accessible to the Node.js process. Fix: resolve the model directory to its canonical path via realpath(), then verify that each resolved weight file path starts with that canonical base. The realpath() call in the weight path catch handles the non-existent file case while still blocking traversal via fake symlink chains.
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
NodeFileSystem.loadWeights()constructs weight file paths by callingpath.join(dirName, path)wherepathcomes directly from theweightsManifest[].pathsarray in a user-suppliedmodel.json. Node.jspath.joinnormalises..components without any boundary restriction, so acrafted manifest entry like
"../../../etc/passwd"resolves to that file onthe host filesystem, which is then passed to
readFile.Minimal reproducer (no tfjs-node install needed — uses the exact vulnerable logic):
With
paths: ["../secret/credentials.txt"]and model at/app/model/model.json,joinresolves to/app/secret/credentials.txtandreadFilereads it.The raw bytes are then returned as the model's
weightDataand arerecoverable via
model.getWeights()[i].data().Fix
Resolve the model directory to its canonical path via
realpath(), then verifythat each resolved weight path is a strict descendant before calling
readFile.The
realpath()call also blocks symlink-based escapes where a symlink insidethe model directory points to a path outside it.
Test plan
loadLayersModel('file://...')with a normal model loads correctlyloadLayersModel('file://...')withweightsManifest.paths: ["../outside.bin"]throwsWeight file path … is outside the model directoryloadLayersModel('file://...')with absolute path inweightsManifest.pathsis rejected