fix: canonicalize root in safeJoin to handle symlinked storage roots#64
Open
kylezengo wants to merge 1 commit into
Open
fix: canonicalize root in safeJoin to handle symlinked storage roots#64kylezengo wants to merge 1 commit into
kylezengo wants to merge 1 commit into
Conversation
When the configured storage root (or a system temp dir on macOS where /var → /private/var) is reached through a symlink, filepath.EvalSymlinks on a child path resolves to the canonical form, but within() was comparing against the lexical root — causing valid paths to be rejected with "files: path escapes its area". Fix: resolve the root once with EvalSymlinks before the symlink guard loop, and compare resolved paths against the canonical root. The initial lexical containment check (line 108) still uses the original root so that the returned path keeps the caller's expected prefix. Adds two regression tests: - TestSafeJoinSymlinkedRoot: valid file under a symlinked root is accepted - TestSafeJoinChildSymlinkEscapeStillBlocked: escaping child symlink is still rejected Fixes profullstack#62
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.
Fixes #62
Problem
When the configured storage root is reached through a symlink — including the macOS system case where
/var→/private/var—safeJoinincorrectly rejects valid child paths.The root cause:
filepath.EvalSymlinkson a child path resolves to the canonical filesystem path, butwithin()compares against the original lexical root. When those two forms differ (symlinked root), the check produces a false-positive escape error.Fix
Canonicalize the root once with
filepath.EvalSymlinksbefore the symlink guard loop, then compare resolved paths against the canonical root. The initial lexical check (which guards against path traversal inrelbefore any I/O) still uses the original root so the returned path keeps the caller's expected prefix.Tests
Two regression tests added to
internal/files/files_test.go:TestSafeJoinSymlinkedRoot— creates a real directory, symlinks a second name at it, and verifies that a file under the symlinked root is accepted rather than rejected.TestSafeJoinChildSymlinkEscapeStillBlocked— same symlinked root setup, but plants an escaping child symlink (escape → /etc) and verifies it is still rejected.