Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Reject .. segments in archive entry names during extraction

Claudecbae2c7ebf5f
diff --git a/cli/beacon/internal/endpoint/selfupdate/fsutil.go b/cli/beacon/internal/endpoint/selfupdate/fsutil.go
index 3f13a753..d22f0b15 100644
--- a/cli/beacon/internal/endpoint/selfupdate/fsutil.go
+++ b/cli/beacon/internal/endpoint/selfupdate/fsutil.go
@@ -33,12 +33,19 @@ func acquireLock(path string) (func(), error) {
}
// safeJoin resolves an archive entry name against dest and rejects anything that
-// would escape dest (path traversal / "zip slip"). It rejects absolute paths and
-// any entry whose cleaned, joined path is not contained within dest.
+// would escape dest (path traversal / "zip slip"). It rejects absolute paths,
+// any ".." path segment (including backslash-separated ones), and any entry
+// whose cleaned, joined path is not contained within dest.
func safeJoin(dest, name string) (string, error) {
if filepath.IsAbs(name) || strings.HasPrefix(name, "/") || strings.HasPrefix(name, "\\") {
return "", fmt.Errorf("absolute path in archive entry: %q", name)
}
+ normalized := strings.ReplaceAll(name, "\\", "/")
+ for _, seg := range strings.Split(normalized, "/") {
+ if seg == ".." {
+ return "", fmt.Errorf("path traversal in archive entry: %q", name)
+ }
+ }
cleanDest := filepath.Clean(dest)
target := filepath.Clean(filepath.Join(cleanDest, name))
if target != cleanDest && !strings.HasPrefix(target, cleanDest+string(os.PathSeparator)) {
diff --git a/cli/beacon/internal/endpoint/selfupdate/fsutil_test.go b/cli/beacon/internal/endpoint/selfupdate/fsutil_test.go
index 2c078450..ea0296d0 100644
--- a/cli/beacon/internal/endpoint/selfupdate/fsutil_test.go
+++ b/cli/beacon/internal/endpoint/selfupdate/fsutil_test.go
@@ -12,9 +12,14 @@ import (
func TestSafeJoinRejectsTraversal(t *testing.T) {
dest := "/tmp/dest"
bad := []string{
+ "..",
"../escape",
"../../etc/passwd",
"a/../../escape",
+ "a/../b",
+ "a/..",
+ "a\\..\\..\\escape",
+ "..\\escape",
"/etc/passwd",
"\\windows\\system32",
}