The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
rules pull: reject archive entries containing '..' (go/zipslip)
Justin D'Souza13556e6d3ea4
diff --git a/cli/beacon/cmd/rules.go b/cli/beacon/cmd/rules.goindex e2de48ec..1ab35c36 100644--- a/cli/beacon/cmd/rules.go+++ b/cli/beacon/cmd/rules.go@@ -6,7 +6,9 @@ import ("fmt""io""net/http"+ neturl "net/url""os"+ "path""path/filepath""strings""time"@@ -117,6 +119,13 @@ func runRulesPull(cmd *cobra.Command, args []string) error {if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {return fmt.Errorf("url must start with http:// or https://")}+ // Detect the pack format from the URL path only; query/fragment must not+ // influence the suffix match or the downloaded filename.+ parsed, err := neturl.Parse(url)+ if err != nil {+ return fmt.Errorf("parse url: %w", err)+ }+ urlPath := parsed.Pathout := cmd.OutOrStdout()fmt.Fprintf(out, "Fetching %s (network)…\n", url)@@ -138,13 +147,13 @@ func runRulesPull(cmd *cobra.Command, args []string) error {var src stringswitch {- case strings.HasSuffix(url, ".tar.gz"), strings.HasSuffix(url, ".tgz"):+ case strings.HasSuffix(urlPath, ".tar.gz"), strings.HasSuffix(urlPath, ".tgz"):if err := extractRuleTarball(resp.Body, tmp); err != nil {return err}src = tmp- case strings.HasSuffix(url, ".rule.yaml"):- dest := filepath.Join(tmp, filepath.Base(url))+ case strings.HasSuffix(urlPath, ".rule.yaml"):+ dest := filepath.Join(tmp, path.Base(urlPath))if err := writeReaderToFile(resp.Body, dest); err != nil {return err}@@ -186,10 +195,15 @@ func extractRuleTarball(r io.Reader, dir string) error {if hdr.Typeflag != tar.TypeReg || !strings.HasSuffix(hdr.Name, ".rule.yaml") {continue}- // Flatten archive directory structure to a base name, then confirm the joined- // destination stays within cleanDir before any file operation. The same `dest`- // value is both validated and written, guarding against archive path traversal+ // Reject any entry whose name contains a path-traversal element before it is+ // used in a filesystem path — the primary guard against archive path traversal// ("Zip Slip").+ if strings.Contains(hdr.Name, "..") {+ return fmt.Errorf("archive entry %q contains a path traversal element", hdr.Name)+ }+ // Flatten archive directory structure to a base name, then confirm the joined+ // destination stays within cleanDir before any file operation (defense in depth;+ // the same `dest` value is both validated and written).dest := filepath.Join(cleanDir, filepath.Base(filepath.ToSlash(hdr.Name)))if dest != cleanDir && !strings.HasPrefix(dest, cleanDir+string(os.PathSeparator)) {return fmt.Errorf("archive entry %q resolves outside the destination directory", hdr.Name)diff --git a/cli/beacon/cmd/rules_test.go b/cli/beacon/cmd/rules_test.goindex 4a3d74d3..778cea38 100644--- a/cli/beacon/cmd/rules_test.go+++ b/cli/beacon/cmd/rules_test.go@@ -4,6 +4,8 @@ import ("archive/tar""bytes""compress/gzip"+ "net/http"+ "net/http/httptest""os""path/filepath""strings"@@ -103,35 +105,86 @@ func makeTarGz(t *testing.T, entries map[string]string) []byte {return buf.Bytes()}-func TestExtractRuleTarballNeutralizesTraversal(t *testing.T) {+func TestExtractRuleTarballRejectsTraversal(t *testing.T) {dir := t.TempDir()- // One legit entry, one path-traversal entry, and a nested-dir entry.data := makeTarGz(t, map[string]string{"ok.rule.yaml": "id: ok","../../../../tmp/evil.rule.yaml": "id: evil",- "pack/sub/nested.rule.yaml": "id: nested",- "notes.txt": "ignored",+ })+ err := extractRuleTarball(bytes.NewReader(data), dir)+ if err == nil || !strings.Contains(err.Error(), "path traversal") {+ t.Fatalf("expected path-traversal rejection, got: %v", err)+ }+ // The traversal target must NOT exist outside the dest dir.+ escaped := filepath.Join(filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(dir)))), "tmp", "evil.rule.yaml")+ if _, statErr := os.Stat(escaped); statErr == nil {+ t.Fatalf("traversal entry escaped the destination: %s", escaped)+ }+}++func TestExtractRuleTarballExtractsCleanEntries(t *testing.T) {+ dir := t.TempDir()+ // A top-level rule, a nested-dir rule, and a non-rule file.+ data := makeTarGz(t, map[string]string{+ "ok.rule.yaml": "id: ok",+ "pack/sub/nested.rule.yaml": "id: nested",+ "notes.txt": "ignored",})if err := extractRuleTarball(bytes.NewReader(data), dir); err != nil {t.Fatalf("extract: %v", err)}- // All .rule.yaml entries land flattened inside dir; nothing escapes.- for _, name := range []string{"ok.rule.yaml", "evil.rule.yaml", "nested.rule.yaml"} {+ // Rule entries are flattened into dir; archive directory structure is dropped.+ for _, name := range []string{"ok.rule.yaml", "nested.rule.yaml"} {if _, err := os.Stat(filepath.Join(dir, name)); err != nil {t.Errorf("expected %s inside dest dir: %v", name, err)}}- // The traversal target must NOT exist outside the dest dir.- escaped := filepath.Join(filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(dir)))), "tmp", "evil.rule.yaml")- if _, err := os.Stat(escaped); err == nil {- t.Fatalf("traversal entry escaped the destination: %s", escaped)- }// Non-rule files are ignored.if _, err := os.Stat(filepath.Join(dir, "notes.txt")); err == nil {t.Errorf("notes.txt should not be extracted")}}+func TestRulesPullAcceptsURLWithQuery(t *testing.T) {+ t.Setenv("HOME", t.TempDir())+ prev := rulesOpts+ rulesOpts.userMode, rulesOpts.systemMode, rulesOpts.force = true, false, false+ t.Cleanup(func() { rulesOpts = prev })++ pack := makeTarGz(t, map[string]string{"cli-test-rule.rule.yaml": validRuleYAML})+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Write(pack)+ }))+ t.Cleanup(srv.Close)++ // The path is a valid pack; the query string must not defeat suffix detection.+ cmd, buf := newCmd()+ if err := runRulesPull(cmd, []string{srv.URL + "/pack.tar.gz?version=1&token=abc"}); err != nil {+ t.Fatalf("pull with query string failed: %v\n%s", err, buf.String())+ }+ if !strings.Contains(buf.String(), "cli-test-rule") {+ t.Fatalf("expected the pack rule to be installed, got: %s", buf.String())+ }+}++func TestRulesPullRejectsUnsupportedPath(t *testing.T) {+ t.Setenv("HOME", t.TempDir())+ prev := rulesOpts+ rulesOpts.userMode, rulesOpts.systemMode = true, false+ t.Cleanup(func() { rulesOpts = prev })++ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Write([]byte("nope"))+ }))+ t.Cleanup(srv.Close)++ cmd, _ := newCmd()+ // A non-pack path must still be rejected even with a pack-like query string.+ if err := runRulesPull(cmd, []string{srv.URL + "/index.html?file=pack.tar.gz"}); err == nil {+ t.Fatal("expected unsupported-url rejection for a non-pack path")+ }+}+func TestLoadRuleFilesSingleFile(t *testing.T) {dir := t.TempDir()p := writeRuleFile(t, dir, "ok.rule.yaml", validRuleYAML)diff --git a/cli/beacon/internal/endpoint/detect/detect.go b/cli/beacon/internal/endpoint/detect/detect.goindex 95571d13..4c03be41 100644--- a/cli/beacon/internal/endpoint/detect/detect.go+++ b/cli/beacon/internal/endpoint/detect/detect.go@@ -210,9 +210,32 @@ func InstallFiles(userMode bool, src string, force bool) ([]Installed, error) {todo = append(todo, pending{id: rule.ID, dest: filepath.Join(store, rule.ID+ruleFileSuffix), data: data})}+ tmpDir, err := os.MkdirTemp(store, ".install-*")+ if err != nil {+ return nil, err+ }+ defer os.RemoveAll(tmpDir)++ for _, p := range todo {+ tmpPath := filepath.Join(tmpDir, p.id+ruleFileSuffix)+ if err := os.WriteFile(tmpPath, p.data, 0o644); err != nil {+ return nil, err+ }+ }+installed := make([]Installed, 0, len(todo))for _, p := range todo {- if err := os.WriteFile(p.dest, p.data, 0o644); err != nil {+ tmpPath := filepath.Join(tmpDir, p.id+ruleFileSuffix)+ backupPath, err := moveExistingRuleAside(p.dest, tmpDir, p.id)+ if err != nil {+ rollbackInstall(installed, tmpDir)+ return nil, err+ }+ if err := os.Rename(tmpPath, p.dest); err != nil {+ if backupPath != "" {+ _ = os.Rename(backupPath, p.dest)+ }+ rollbackInstall(installed, tmpDir)return nil, err}installed = append(installed, Installed{ID: p.id, Path: p.dest})@@ -220,6 +243,34 @@ func InstallFiles(userMode bool, src string, force bool) ([]Installed, error) {return installed, nil}+func moveExistingRuleAside(dest, tmpDir, id string) (string, error) {+ info, err := os.Lstat(dest)+ if err != nil {+ if os.IsNotExist(err) {+ return "", nil+ }+ return "", err+ }+ if info.IsDir() {+ return "", fmt.Errorf("%s exists and is a directory", dest)+ }+ backupPath := filepath.Join(tmpDir, id+".backup")+ if err := os.Rename(dest, backupPath); err != nil {+ return "", err+ }+ return backupPath, nil+}++func rollbackInstall(installed []Installed, tmpDir string) {+ for i := len(installed) - 1; i >= 0; i-- {+ backupPath := filepath.Join(tmpDir, installed[i].ID+".backup")+ _ = os.Remove(installed[i].Path)+ if _, err := os.Stat(backupPath); err == nil {+ _ = os.Rename(backupPath, installed[i].Path)+ }+ }+}+// Remove deletes a rule by id from the store. Returns the removed path. The id// is validated against the rule-id grammar before use so it can never contain// path separators or ".." segments that would resolve outside the store.