-
Notifications
You must be signed in to change notification settings - Fork 17
fix: restrict sync-chapters input path #178
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| const test = require("node:test"); | ||
| const assert = require("node:assert/strict"); | ||
| const fs = require("fs"); | ||
| const os = require("os"); | ||
| const path = require("path"); | ||
|
|
||
| const { resolveInputPath } = require("../scripts/sync-chapters"); | ||
|
|
||
| // Each case builds a throwaway input directory so we exercise the containment | ||
| // boundary without touching the real /tmp. resolveInputPath takes the boundary | ||
| // as an argument for exactly this reason. | ||
| function withScratchDir(fn) { | ||
| const dir = fs.mkdtempSync(path.join(os.tmpdir(), "sync-chapters-")); | ||
| // Canonicalize so assertions compare against the same path realpath returns | ||
| // (macOS resolves the tmp symlink to /private/...). | ||
| const realDir = fs.realpathSync(dir); | ||
| try { | ||
| fn(realDir); | ||
| } finally { | ||
| fs.rmSync(dir, { recursive: true, force: true }); | ||
| } | ||
| } | ||
|
|
||
| test("resolveInputPath accepts a .json file inside the input directory", () => { | ||
| withScratchDir((dir) => { | ||
| fs.writeFileSync(path.join(dir, "chapters.json"), "{}"); | ||
| const resolved = resolveInputPath("chapters.json", dir); | ||
| assert.equal(resolved, path.join(dir, "chapters.json")); | ||
| }); | ||
| }); | ||
|
|
||
| test("resolveInputPath accepts an absolute path inside the input directory", () => { | ||
| withScratchDir((dir) => { | ||
| const abs = path.join(dir, "chapters.json"); | ||
| fs.writeFileSync(abs, "{}"); | ||
| assert.equal(resolveInputPath(abs, dir), abs); | ||
| }); | ||
| }); | ||
|
|
||
| test("resolveInputPath rejects ../ traversal out of the input directory", () => { | ||
| withScratchDir((dir) => { | ||
| assert.throws(() => resolveInputPath("../escape.json", dir), /inside/); | ||
| }); | ||
| }); | ||
|
|
||
| test("resolveInputPath rejects an absolute path outside the input directory", () => { | ||
| withScratchDir((dir) => { | ||
| assert.throws(() => resolveInputPath("/etc/passwd.json", dir), /inside/); | ||
| }); | ||
| }); | ||
|
|
||
| test("resolveInputPath rejects a lexical escape that climbs above the directory", () => { | ||
| withScratchDir((dir) => { | ||
| assert.throws( | ||
| () => resolveInputPath("subdir/../../escape.json", dir), | ||
| /inside/, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| test("resolveInputPath rejects a symlink inside the dir pointing outside", () => { | ||
| withScratchDir((dir) => { | ||
| const outside = fs.mkdtempSync(path.join(os.tmpdir(), "sync-outside-")); | ||
| const target = path.join(outside, "secret.json"); | ||
| fs.writeFileSync(target, "{}"); | ||
| const link = path.join(dir, "link.json"); | ||
| try { | ||
| fs.symlinkSync(target, link); | ||
| } catch { | ||
| // Some environments disallow symlink creation; nothing to assert there. | ||
| fs.rmSync(outside, { recursive: true, force: true }); | ||
| return; | ||
| } | ||
| try { | ||
| assert.throws(() => resolveInputPath("link.json", dir), /inside/); | ||
| } finally { | ||
| fs.rmSync(outside, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
| }); | ||
|
Comment on lines
+61
to
+80
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. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Do not hide unexpected symlink setup failures. Lines 69-73 treat every Skip only documented capability errors such as 🧰 Tools🪛 ast-grep (0.45.1)[warning] 64-64: Filesystem path is not a string literal; a request-/variable-derived path can enable path traversal. Validate and normalize the path before use. (detect-non-literal-fs-filename) 🤖 Prompt for AI Agents |
||
|
|
||
| test("resolveInputPath rejects a non-.json file inside the directory", () => { | ||
| withScratchDir((dir) => { | ||
| fs.writeFileSync(path.join(dir, "chapters.txt"), "{}"); | ||
| assert.throws(() => resolveInputPath("chapters.txt", dir), /\.json/); | ||
| }); | ||
| }); | ||
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.
🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: GRCEngClub/directory
Length of output: 10319
🏁 Script executed:
Repository: GRCEngClub/directory
Length of output: 13946
🏁 Script executed:
Repository: GRCEngClub/directory
Length of output: 13946
Open and validate the input through one file descriptor.
fs.realpathSync(candidate)validates the path at line 52, butfs.readFileSync(resolvedSrc, "utf8")opens it later. A process that can replace the canonical file with a symlink can redirect the read outside/tmp. Use a private staging directory or validate and read the same file descriptor. Add a regression test for replacement between validation and reading.🤖 Prompt for AI Agents