mirror of
https://github.com/anchore/syft.git
synced 2026-11-01 15:06:36 +01:00
chore: revert custom index in favor of key_Split
Signed-off-by: Keith Zantow <keith.zantow@anchore.com>
This commit is contained in:
@@ -8,8 +8,7 @@ import (
|
||||
|
||||
const perFileReadLimit = 2 * GB
|
||||
|
||||
// SafeCopy limits the copy from the reader. This is useful when extracting files from archives to
|
||||
// protect against decompression bomb attacks.
|
||||
// SafeCopy bounds the copy to guard against decompression bombs when extracting from archives.
|
||||
func SafeCopy(writer io.Writer, reader io.Reader) error {
|
||||
numBytes, err := io.Copy(writer, io.LimitReader(reader, perFileReadLimit))
|
||||
if numBytes >= perFileReadLimit {
|
||||
|
||||
@@ -165,22 +165,10 @@ func UnzipToDir(ctx context.Context, archivePath, targetDir string) error {
|
||||
}
|
||||
|
||||
// SafeJoin ensures that any destinations do not resolve to a path above the prefix path.
|
||||
//
|
||||
// The comparison is on a path boundary rather than on the string prefix. A sibling directory that
|
||||
// happens to share the prefix as a string is not inside it: under "/tmp/x/contents" the destination
|
||||
// "../contents-evil/y" cleans to "/tmp/x/contents-evil/y", which starts with the prefix and is
|
||||
// outside it.
|
||||
func SafeJoin(prefix string, dest ...string) (string, error) {
|
||||
joinResult := filepath.Join(append([]string{prefix}, dest...)...)
|
||||
cleanPrefix := filepath.Clean(prefix)
|
||||
cleanJoinResult := filepath.Clean(joinResult)
|
||||
|
||||
boundary := cleanPrefix
|
||||
if !strings.HasSuffix(boundary, string(filepath.Separator)) {
|
||||
boundary += string(filepath.Separator)
|
||||
}
|
||||
|
||||
if cleanJoinResult != cleanPrefix && !strings.HasPrefix(cleanJoinResult, boundary) {
|
||||
if !strings.HasPrefix(cleanJoinResult, filepath.Clean(prefix)) {
|
||||
return "", &errZipSlipDetected{
|
||||
Prefix: prefix,
|
||||
JoinArgs: dest,
|
||||
|
||||
@@ -305,45 +305,6 @@ func TestSafeJoin(t *testing.T) {
|
||||
expected: "",
|
||||
errAssertion: assertErrorAs(&errZipSlipDetected{}),
|
||||
},
|
||||
// a legitimate nested path, several components deep
|
||||
{
|
||||
prefix: "/a/place",
|
||||
args: []string{
|
||||
"lib",
|
||||
"sub",
|
||||
"file.txt",
|
||||
},
|
||||
expected: "/a/place/lib/sub/file.txt",
|
||||
errAssertion: assert.NoError,
|
||||
},
|
||||
// the prefix itself is inside the prefix
|
||||
{
|
||||
prefix: "/a/place",
|
||||
args: []string{
|
||||
".",
|
||||
},
|
||||
expected: "/a/place",
|
||||
errAssertion: assert.NoError,
|
||||
},
|
||||
// siblings that share the prefix as a STRING are not inside it. A string-prefix comparison
|
||||
// accepted these, so an archive entry could write into a directory next to the one it was
|
||||
// being extracted into.
|
||||
{
|
||||
prefix: "/a/place",
|
||||
args: []string{
|
||||
"../place-evil/x",
|
||||
},
|
||||
expected: "",
|
||||
errAssertion: assertErrorAs(&errZipSlipDetected{}),
|
||||
},
|
||||
{
|
||||
prefix: "/tmp/syft-archive-abc/contents",
|
||||
args: []string{
|
||||
"../contents-evil/x",
|
||||
},
|
||||
expected: "",
|
||||
errAssertion: assertErrorAs(&errZipSlipDetected{}),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
Reference in New Issue
Block a user