-
Notifications
You must be signed in to change notification settings - Fork 39
Don't skip symlinks during local source upload #355
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
base: main
Are you sure you want to change the base?
Changes from all commits
39c0684
2937256
5a0d8a5
70578f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ package streamer | |||||||||||||
|
|
||||||||||||||
| import ( | ||||||||||||||
| "archive/tar" | ||||||||||||||
| "fmt" | ||||||||||||||
| "io" | ||||||||||||||
| "io/fs" | ||||||||||||||
| "os" | ||||||||||||||
|
|
@@ -21,24 +22,76 @@ func trimPrefix(prefix, fpath string) string { | |||||||||||||
| return strings.TrimPrefix(strings.ReplaceAll(fpath, prefix, ""), string(filepath.Separator)) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func writeFileToTar(tw *tar.Writer, src, fpath string, stat fs.FileInfo) error { | ||||||||||||||
| func writeFileToTar(tw *tar.Writer, t *Tar, fpath string, stat fs.FileInfo) error { | ||||||||||||||
| header, err := tar.FileInfoHeader(stat, stat.Name()) | ||||||||||||||
| if err != nil { | ||||||||||||||
| return err | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| header.Name = trimPrefix(src, fpath) | ||||||||||||||
| header.Name = trimPrefix(t.src, fpath) | ||||||||||||||
|
|
||||||||||||||
| // Symlink | ||||||||||||||
| if stat.Mode()&fs.ModeSymlink != 0 { | ||||||||||||||
| target, err := os.Readlink(fpath) | ||||||||||||||
| if err != nil { | ||||||||||||||
| return err | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // resolving target to absolute path, to determine if it is pointing | ||||||||||||||
| // outside the source directory. | ||||||||||||||
| var absSrc, absTarget string | ||||||||||||||
|
|
||||||||||||||
| if absSrc, err = filepath.Abs(t.src); err != nil { | ||||||||||||||
| return err | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if filepath.IsAbs(target) { | ||||||||||||||
| absTarget = target | ||||||||||||||
| } else { | ||||||||||||||
| fullTarget := filepath.Join(filepath.Dir(fpath), target) | ||||||||||||||
| if absTarget, err = filepath.Abs(fullTarget); err != nil { | ||||||||||||||
| return err | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if outside, _ := isSymlinkTargetOutsideOfDir(absSrc, absTarget); outside { | ||||||||||||||
| relPath, _ := filepath.Rel(t.src, fpath) | ||||||||||||||
| fmt.Fprintf(t.ioStreams.ErrOut, "WARNING: symlink %q points outside the source directory %q (target: %q)\n", relPath, absSrc, target) | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+57
to
+60
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should fail here, not warn. Reading the contents of a symlink that is outside of a given "directory root" is a very common vulnerability. See CWE-61. |
||||||||||||||
|
|
||||||||||||||
| header.Linkname = target | ||||||||||||||
|
dollierp marked this conversation as resolved.
|
||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if err := tw.WriteHeader(header); err != nil { | ||||||||||||||
| return err | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // #nosec G304 intentionally opening file from variable | ||||||||||||||
| f, err := os.Open(fpath) | ||||||||||||||
| // Copy regular file content | ||||||||||||||
| if stat.Mode().IsRegular() { | ||||||||||||||
| // #nosec G304 intentionally opening file from variable | ||||||||||||||
| f, err := os.Open(fpath) | ||||||||||||||
| if err != nil { | ||||||||||||||
| return err | ||||||||||||||
| } | ||||||||||||||
| if _, err := io.Copy(tw, f); err != nil { | ||||||||||||||
| return err | ||||||||||||||
| } | ||||||||||||||
| return f.Close() | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return nil | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func isSymlinkTargetOutsideOfDir(dir, target string) (bool, error) { | ||||||||||||||
|
dollierp marked this conversation as resolved.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth adding a godoc here around CWE-61
Suggested change
|
||||||||||||||
| realTarget, err := filepath.EvalSymlinks(target) | ||||||||||||||
| if err != nil { | ||||||||||||||
| return err | ||||||||||||||
| realTarget = filepath.Clean(target) | ||||||||||||||
| } | ||||||||||||||
| if _, err := io.Copy(tw, f); err != nil { | ||||||||||||||
| return err | ||||||||||||||
|
|
||||||||||||||
| rel, err := filepath.Rel(dir, realTarget) | ||||||||||||||
| if err != nil { | ||||||||||||||
| return false, err | ||||||||||||||
| } | ||||||||||||||
| return f.Close() | ||||||||||||||
|
|
||||||||||||||
| return !filepath.IsLocal(rel), nil | ||||||||||||||
| } | ||||||||||||||
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.
Golangci-lint failing here on an unhandled error from
os.Remove. I'm fine capturing the error and logging it.