fix: eliminate TOCTOU race and path traversal in realtime save handler#113
Open
hacktron-app-stg[bot] wants to merge 1 commit into
Open
fix: eliminate TOCTOU race and path traversal in realtime save handler#113hacktron-app-stg[bot] wants to merge 1 commit into
hacktron-app-stg[bot] wants to merge 1 commit into
Conversation
Replace the non-atomic fs.existsSync + fs.writeFileSync pattern with a single atomic write using the 'wx' flag (O_CREAT|O_EXCL), which fails if the file exists and does not follow a final symlink. Also validate data.name via path.basename to prevent directory traversal.
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.
Vulnerability
realtime.jshandledsavemessages by checkingfs.existsSync(path)and then callingfs.writeFileSync(path, ...)as two separate, non-atomic steps. Between the check and the write an attacker with local/shared-environment access could replace the target path with a symlink, causing the write to follow the link and overwrite arbitrary files writable by the Node process (TOCTOU). The destination was also built by string-concatenating unsanitizeddata.nameonto/srv/cache/, permitting directory traversal.Fix
fs.writeFileSync(path, data.body, { flag: 'wx' }). Thewxflag maps toO_CREAT | O_EXCL, which atomically fails if the path exists and refuses to follow a final symlink — removing the check/act window while preserving the original "only write if it doesn't already exist" semantics (anEEXISTerror is treated as a no-op).data.name: it must be a non-empty string equal to its ownpath.basename, rejecting.., path separators, and non-string inputs. The path is assembled withpath.join(CACHE_DIR, name).Verification
node --check realtime.jspasses.wxopen removes the separate existence check, and basename validation blocks traversal, so neither the symlink swap nor../escape is possible.No test infrastructure exists in the repo, so no automated regression test was added.
Automated fix by Hacktron for finding: https://staging.hacktron.ai/testestesttest/findings/47b7e2ec-f464-4ba4-852c-5594a99642bc