-
Notifications
You must be signed in to change notification settings - Fork 1
fix(security): extract archives through an os.Root handle #196
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package policyfile | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
| "bytes" | ||
| "compress/gzip" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| // buildTarball gzips a tarball from the given entries (name -> body). | ||
| func buildTarball(t *testing.T, files map[string]string) []byte { | ||
| t.Helper() | ||
| var buf bytes.Buffer | ||
| gz := gzip.NewWriter(&buf) | ||
| tw := tar.NewWriter(gz) | ||
| for name, body := range files { | ||
| hdr := &tar.Header{Name: name, Mode: 0o644, Typeflag: tar.TypeReg, Size: int64(len(body))} | ||
| if err := tw.WriteHeader(hdr); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := tw.Write([]byte(body)); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| if err := tw.Close(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := gz.Close(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| return buf.Bytes() | ||
| } | ||
|
|
||
| // TestExtractCookbookTarballRejectsTraversal pins the plain "../" case. | ||
| func TestExtractCookbookTarballRejectsTraversal(t *testing.T) { | ||
| dest := t.TempDir() | ||
| archive := buildTarball(t, map[string]string{"nginx/../../escape.txt": "pwned"}) | ||
| if err := extractCookbookTarball(bytes.NewReader(archive), dest); err == nil { | ||
| t.Error("expected a traversal entry to be refused") | ||
| } | ||
| if _, err := os.Stat(filepath.Join(filepath.Dir(dest), "escape.txt")); err == nil { | ||
| t.Fatal("traversal entry escaped dest") | ||
| } | ||
| } | ||
|
|
||
| // TestExtractCookbookTarballDoesNotFollowSymlinkOutOfDest covers what a | ||
| // lexical check cannot see. After the leading cookbook segment is stripped, | ||
| // "nginx/cache/evil.rb" resolves to dest/cache/evil.rb, which is inside dest | ||
| // by string comparison; if dest already holds a "cache" symlink the write | ||
| // still lands outside it. | ||
| func TestExtractCookbookTarballDoesNotFollowSymlinkOutOfDest(t *testing.T) { | ||
| dest := t.TempDir() | ||
| outside := t.TempDir() | ||
| if err := os.Symlink(outside, filepath.Join(dest, "cache")); err != nil { | ||
| t.Skipf("symlinks unavailable: %v", err) | ||
| } | ||
|
|
||
| archive := buildTarball(t, map[string]string{"nginx/cache/evil.rb": "pwned"}) | ||
| if err := extractCookbookTarball(bytes.NewReader(archive), dest); err == nil { | ||
| t.Error("extractCookbookTarball wrote through a symlink in dest without complaint") | ||
| } | ||
| if _, err := os.Stat(filepath.Join(outside, "evil.rb")); err == nil { | ||
| t.Fatal("archive entry escaped dest through a pre-existing symlink") | ||
| } | ||
| } |
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.