feat: add followSymlinks option to restrict symlink traversal#298
Open
DebuggingMax wants to merge 1 commit intopillarjs:masterfrom
Open
feat: add followSymlinks option to restrict symlink traversal#298DebuggingMax wants to merge 1 commit intopillarjs:masterfrom
DebuggingMax wants to merge 1 commit intopillarjs:masterfrom
Conversation
Add a new `followSymlinks` option that allows restricting symlink traversal to stay within the configured root directory. When `followSymlinks` is set to `false`, the module will use `fs.realpath()` to resolve the canonical path of files and reject requests (with 403) if the resolved path falls outside the root directory. This provides an explicit opt-in safeguard for applications that serve user-writable directories. - Default: `true` (backward compatible - current behavior) - When `false`: reject symlinks pointing outside root - Requires `root` option to be set for the check to apply Closes pillarjs#297
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
This PR adds a new
followSymlinksoption that allows restricting symlink traversal to stay within the configured root directory.Motivation
Currently,
sendresolves file paths using string normalization but does not check whether the final canonical path (after symlink resolution) remains inside the configured root. This means symbolic links inside the root can point to — and serve — files outside of it.This is especially problematic for applications that serve user-writable directories where users could create symlinks pointing to sensitive system files.
Implementation
When
followSymlinksis set tofalse:fs.realpath()to resolve the canonical path of requested filesOptions
followSymlinks: true(default) - Current behavior, symlinks are followed without restrictionfollowSymlinks: false- Reject requests for symlinks that resolve outside rootRequirements
rootoption must be set for the symlink check to applyExample
Changes
followSymlinksoption toSendStreamconstructorcheckSymlinkmethod to verify paths remain within rootsendFileandsendIndexto check symlinks when option is disabledTests
All 146 tests pass, including 7 new tests for the
followSymlinksoption:Closes #297