fix(ext/fs): truncate should not follow a terminal symlink#35239
Merged
Conversation
The permission check for Deno.truncate / truncateSync is performed no-follow (against the named path, without canonicalization), but the open followed symlinks. A writable symlink inside an allowed scope could therefore be used to zero out a file it points to outside that scope. Open the path with O_NOFOLLOW on Unix so truncating through a terminal symlink fails instead of silently truncating the target.
Assert the terminal-symlink case throws FilesystemLoop specifically (not a permission error or silent success), and add coverage that an intermediate directory symlink is still resolved so truncating a regular file reached through it keeps working.
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.
The permission check for
Deno.truncate/Deno.truncateSyncis performedno-follow: it is checked against the path you name, without canonicalizing
symlinks. The implementation, however, opened the path with a plain
OpenOptions::write(true).open(path), which follows symlinks on Unix. Awritable symlink sitting at a path inside an allowed scope could therefore be
used to truncate (zero out) the file it points to, even when the target lives
outside that scope.
This opens the path with
O_NOFOLLOWon Unix so that truncating through aterminal symlink fails with
ELOOPrather than silently truncating the link'starget. Truncating a regular file is unchanged, and only the final path
component is affected (intermediate symlinks are still resolved, matching the
existing path model). Windows behavior is unchanged.
Added unit tests covering both the sync and async paths: a symlink whose target
holds data is truncated through the link, and the operation now throws while
the target is left untouched.