security: mkdtempSync for all scratch dirs (fixes 9 CodeQL insecure-temp-file alerts)#20
Merged
Merged
Conversation
…insecure-temporary-file) Every demo/test/MCP scratch path was built from a predictable name (os.tmpdir() + a fixed prefix + process.pid), which a local attacker can pre-create or symlink ahead of the write. Switch all of them to fs.mkdtempSync(path.join(os.tmpdir(), '<prefix>-')), which creates an unpredictable 0700 directory atomically. - demo.mjs, test/stack.test.mjs, demo/mcp-demo.mjs, test/mcp.test.mjs: HOME/KH now mkdtempSync; the redundant mkdirSync is dropped. - audit-trail.mjs runTrilogy(): default home is mkdtempSync; the explicit mkdirSync(home) stays so caller-supplied homes are still created. - test/audit.test.mjs: the four caller-supplied homes are mkdtempSync too, so the interprocedural write in runTrilogy has no predictable source. - mcp.mjs canon_scan: write the manifest into a mkdtempSync dir and rmSync the whole dir in finally; the predictable pid+hash filename (and the now unused hashStr helper) are removed. Also normalizes demo/mcp-demo.mjs and test/mcp.test.mjs, which used the same pattern but were not yet flagged, so the class can't resurface. Tests: 16/16 pass; demo.mjs and demo/mcp-demo.mjs run clean.
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.
What
All 9 open CodeQL
js/insecure-temporary-filealerts (#13–#21) share one root cause: scratch paths built from a predictable name —path.join(os.tmpdir(), '<prefix>-' + process.pid). A local attacker can pre-create or symlink that path before the write.This switches every such site to
fs.mkdtempSync(path.join(os.tmpdir(), '<prefix>-')), which atomically creates an unpredictable, 0700 directory.Changes
demo.mjsHOME→mkdtempSync; drop redundantmkdirSynctest/stack.test.mjsHOME→mkdtempSync; drop redundantmkdirSyncaudit-trail.mjsrunTrilogy()defaulthome→mkdtempSync(keepsmkdirSync(home)for caller-supplied homes)test/audit.test.mjsmkdtempSync, so the interprocedural write inrunTrilogyhas no predictable sourcemcp.mjsmkdtempSyncdir,rmSyncthe dir infinally; predictablepid+hashfilename and the now-deadhashStrhelper removedAlso normalizes
demo/mcp-demo.mjsandtest/mcp.test.mjs— same predictable pattern, not yet flagged — so the class can't resurface on the next scan.Verification
npm test→ 16/16 passnode demo.mjsandnode demo/mcp-demo.mjsrun cleanNo public API change; the composed gate's behavior is unchanged.