[Fix] Eliminate TOCTOU race in context file path validation#185
Merged
Conversation
Merge validatePathSecurity into readContextFile as a single fd-centric
operation: open fd first, validate via /dev/fd/{fd} realpath, fstat and
read through the fd, never touching the path again after open.
Eliminates three TOCTOU windows:
- validate(path) → read(path) across two IPC steps
- statSync(path) → openSync(path) within text branch
- statSync(path) → readFileSync(path) within binary branch
Contributor
|
Hi @samzong, DetailsInstructions for interacting with me using comments are available here. |
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
Eliminate TOCTOU race condition in
validatePathSecurity/readContextFileby merging validation and reading into a single fd-centric operation. AfteropenSync, the file descriptor is pinned to an inode — all subsequent operations (realpath resolution, stat, read) go through the fd, never touching the original path again.Type of change
[Fix]bug fixWhy is this needed?
The previous implementation called
validatePathSecurity(path)thenreadContextFile(path)as two separate steps. Between validation and read, a symlink in a user-added context folder (e.g./tmp) could be swapped to point outside allowed folders. Additionally, withinreadContextFileitself,statSync(path)and the subsequentopenSync(path)/readFileSync(path)had their own TOCTOU windows.What changed?
readContextFilenow acceptscontextFolders, opens the fd first, validates viarealpathSync("/dev/fd/${fd}"), usesfstatSync(fd)for size, and reads viareadSync(fd)for both text and binary tiersvalidatePathSecurityremoved — its logic is inlined into the atomic fd-based flowcontext-handlers.tssimplified: singlereadContextFile(path, folders)call replaces the two-step validate-then-readArchitecture impact
context:read-fileparams) is unchangeddocs/architecture-invariants.md: noneLinked issues
Closes #115
Validation
pnpm lintpnpm testpnpm check:ui-contractpnpm check:architecturepnpm buildCommands, screenshots, or notes:
Screenshots or recordings
No UI changes.
Release note
NONE.Checklist
[Feat],[Fix],[UI],[Docs],[Refactor],[Build], or[Chore]