-
Notifications
You must be signed in to change notification settings - Fork 2
fix(extract): add path traversal guard for tar extraction #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
In Go's os.Link(oldname, newname), how are relative oldname paths resolved?💡 Result:
In Go, os.Link(oldname, newname) passes oldname and newname strings directly to the platform’s hard-link syscall (syscall.Link on Unix/POSIX), without any Go-level “base directory” handling for relative paths. Therefore: - If oldname is a relative path, it is resolved by the operating system relative to the process’s current working directory at the time of the call. - newname is likewise resolved relative to the process’s current working directory. Evidence: - The os.Link implementation on Unix calls syscall.Link(oldname, newname) with the original strings (no filepath.Abs/Join/Clean-to-CWD step is performed).
Citations:
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 41
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 82
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 1544
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 2500
🏁 Script executed:
# Get the full extract.go file to understand complete flow wc -l pkg/extract/extract.goRepository: devsy-org/devsy
Length of output: 86
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 3405
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 3882
🌐 Web query:
tar format hard link symlink Linkname relative path archive root semantics💡 Result:
In the tar archive format (ustar/POSIX), the linkname field (bytes 157-256 in the 512-byte header) stores the target path for both hard links (typeflag '1') and symbolic links (typeflag '2'). The linkname is a null-terminated string (unless full 100 bytes used). It records the target's pathname as it was on the original filesystem at archive creation time. This is typically a relative path from the directory containing the link, without using the prefix field (prefix is only for the name field of the entry itself). When extracting: - For symlinks ('2'): The symlink is created at the extracted path (name/prefix), pointing exactly to the linkname string as stored. If linkname has '..' components, the symlink will traverse up the extraction root. GNU tar delays such symlinks until end to avoid cycles but creates them relative to the extraction root. - For hard links ('1'): The link is created to the previously extracted file whose archived name matches the linkname. linkname must refer to an earlier entry in the archive. If it has '..' (relative), GNU tar resolves it against the archive's path structure; absolute or '..' may be delayed or fail if target not found. Hard links do not dereference; they reference the archive entry. Path semantics relative to archive root: tar paths (name and linkname) are treated relative to the extraction directory. GNU tar strips leading '/' from paths by default (use --absolute-names to preserve). linkname paths are not automatically relativized or absolutized; they are literal strings interpreted relative to the current extraction root. This allows links to span outside the root if '..' or absolute paths are used, but tools like GNU tar may restrict dangerous traversals for security. PAX extensions allow longer paths via extended headers, but ustar linkname remains 100 bytes limited.
Citations:
Hard link resolution does not match
os.Linksemantics, creating a security/correctness mismatch.validateLinkTargetresolves relativeLinknameagainst the parent directory of the link being created, butextractEntrypasses the rawheader.Linknametoos.Link, which resolves relative paths against the process CWD — not the extraction root. This inconsistency causes two problems:Security issue: The traversal check validates a different path than the one actually linked. An archive with a relative hard link like
../../../etc/passwdwould be validated against the archive extraction structure but at extraction time could resolve to a completely different inode if the CWD differs from the extraction directory.Correctness issue: Legitimate relative hard links in archives (which should reference previously extracted entries in the archive) will fail or point to the wrong file when the process CWD is not the extraction directory.
The fix is to resolve hard link targets consistently with the validation logic. Unlike symlinks — which store the target literally and resolve at access time relative to the symlink's directory — hard links must resolve to the correct inode at creation time.
🔧 Suggested direction
🤖 Prompt for AI Agents