From 6197c162858d3bc3137a5c312a93ba767c89ed3e Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 30 Sep 2026 03:15:21 +1000 Subject: [PATCH] fix(prsource): use a CodeQL-recognised containment guard when extracting archives The tar.gz extraction already refused ".." escapes, but it did so with filepath.Rel and checked the resulting relative path. CodeQL's go/zipslip query models a sanitizer only as strings.HasPrefix applied to the value that reaches the file-system sink, so the derived relative-path check was invisible to it and it flagged os.MkdirAll/os.WriteFile on the joined target (alert 17: internal/infrastructure/prsource/archive.go). Resolve destDir to an absolute path once, reject an absolute entry name outright, then test the joined target against the destination root with strings.HasPrefix -- the PrefixCheck guard the query recognises. Behaviour is unchanged: a ".." entry is still refused, and a wrapper directory is still stripped. Adds an absolute-entry rejection test and a test proving an escaping entry writes nothing outside destDir. --- internal/infrastructure/prsource/archive.go | 23 ++++++++++---- .../infrastructure/prsource/archive_test.go | 30 +++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/internal/infrastructure/prsource/archive.go b/internal/infrastructure/prsource/archive.go index f01917528..475fc8938 100644 --- a/internal/infrastructure/prsource/archive.go +++ b/internal/infrastructure/prsource/archive.go @@ -28,13 +28,18 @@ func extractTarGz(r io.Reader, destDir string) error { } defer gz.Close() + dest, err := filepath.Abs(destDir) + if err != nil { + return fmt.Errorf("prsource: resolve destination directory: %w", err) + } + tr := tar.NewReader(gz) prefix, entries, err := planExtraction(tr) if err != nil { return err } for _, e := range entries { - if err := writeEntry(destDir, prefix, e); err != nil { + if err := writeEntry(dest, prefix, e); err != nil { return err } } @@ -96,8 +101,9 @@ func planExtraction(tr *tar.Reader) (prefix string, entries []tarEntry, err erro } // writeEntry writes one archive entry under destDir, with prefix (if any) -// stripped from its name. Directories, regular files, and nothing else are -// written: a symlink or device entry from an untrusted PR archive is +// stripped from its name. destDir is the absolute destination directory +// resolved by extractTarGz. Directories, regular files, and nothing else +// are written: a symlink or device entry from an untrusted PR archive is // skipped rather than followed. func writeEntry(destDir, prefix string, e tarEntry) error { name := path.Clean(filepath.ToSlash(e.header.Name)) @@ -118,9 +124,16 @@ func writeEntry(destDir, prefix string, e tarEntry) error { return nil } + // Refuse a name that is itself absolute, and one that escapes destDir + // through "..". The escape is checked on the joined target rather than + // on a relative path derived from it: filepath.Join folds a leading + // ".." away, so only the joined result catches a name like "../x". + if path.IsAbs(name) { + return fmt.Errorf("prsource: archive entry %q escapes the destination directory", e.header.Name) + } target := filepath.Join(destDir, filepath.FromSlash(name)) - rel, err := filepath.Rel(destDir, target) - if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { + destRoot := strings.TrimSuffix(destDir, string(filepath.Separator)) + string(filepath.Separator) + if !strings.HasPrefix(target, destRoot) { return fmt.Errorf("prsource: archive entry %q escapes the destination directory", e.header.Name) } diff --git a/internal/infrastructure/prsource/archive_test.go b/internal/infrastructure/prsource/archive_test.go index 260f50613..23e0f851a 100644 --- a/internal/infrastructure/prsource/archive_test.go +++ b/internal/infrastructure/prsource/archive_test.go @@ -96,6 +96,36 @@ func TestExtractTarGzRefusesPathTraversal(t *testing.T) { } } +func TestExtractTarGzRefusesAnAbsoluteEntryPath(t *testing.T) { + data := buildTarGz(t, "", map[string]string{"/etc/passwd-clone": "evil"}) + dest := t.TempDir() + + if err := extractTarGz(bytes.NewReader(data), dest); err == nil { + t.Fatal("want an error for an absolute archive entry, not a silent write") + } +} + +func TestExtractTarGzWritesNothingOutsideDestWhenAnEntryEscapes(t *testing.T) { + base := t.TempDir() + dest := filepath.Join(base, "snapshot") + if err := os.MkdirAll(dest, 0o755); err != nil { + t.Fatal(err) + } + // Two entries with different top-level components keep the wrapper + // prefix empty, so the leading ".." is preserved rather than stripped. + data := buildTarGz(t, "", map[string]string{ + "../escaped.txt": "evil", + "keep.txt": "ok", + }) + + if err := extractTarGz(bytes.NewReader(data), dest); err == nil { + t.Fatal("want an error for an entry that escapes destDir") + } + if _, err := os.Stat(filepath.Join(base, "escaped.txt")); err == nil { + t.Fatal("an escaping entry must not be written outside destDir") + } +} + func TestExtractTarGzLeavesNoDotGit(t *testing.T) { data := buildTarGz(t, "wrapper", map[string]string{ "a.go": "package a\n",