Skip to content

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

bugfixg

Justin D'Souzad23b266c207c
diff --git a/cli/beacon/cmd/rules.go b/cli/beacon/cmd/rules.go
index 79774f43..37210d88 100644
--- a/cli/beacon/cmd/rules.go
+++ b/cli/beacon/cmd/rules.go
@@ -172,6 +172,10 @@ func extractRuleTarball(r io.Reader, dir string) error {
return fmt.Errorf("gzip: %w", err)
}
defer gz.Close()
+ dirAbs, err := filepath.Abs(dir)
+ if err != nil {
+ return fmt.Errorf("resolve destination dir: %w", err)
+ }
tr := tar.NewReader(gz)
count := 0
for {
@@ -185,8 +189,18 @@ func extractRuleTarball(r io.Reader, dir string) error {
if hdr.Typeflag != tar.TypeReg || !strings.HasSuffix(hdr.Name, ".rule.yaml") {
continue
}
- name := filepath.Base(hdr.Name) // flatten; ignore archive directory structure
+ // Flatten to the base name (archive directory structure is ignored) and confirm
+ // the resolved destination stays inside dir before writing — defense in depth
+ // against archive path traversal ("Zip Slip").
+ name := filepath.Base(filepath.ToSlash(hdr.Name))
dest := filepath.Join(dir, name)
+ destAbs, err := filepath.Abs(dest)
+ if err != nil {
+ return fmt.Errorf("resolve destination path: %w", err)
+ }
+ if destAbs != dirAbs && !strings.HasPrefix(destAbs, dirAbs+string(os.PathSeparator)) {
+ return fmt.Errorf("archive entry %q resolves outside the destination directory", hdr.Name)
+ }
if err := writeReaderToFile(io.LimitReader(tr, 1<<20), dest); err != nil {
return err
}
diff --git a/cli/beacon/cmd/rules_test.go b/cli/beacon/cmd/rules_test.go
index 72b59323..4a3d74d3 100644
--- a/cli/beacon/cmd/rules_test.go
+++ b/cli/beacon/cmd/rules_test.go
@@ -1,7 +1,9 @@
package cmd
import (
+ "archive/tar"
"bytes"
+ "compress/gzip"
"os"
"path/filepath"
"strings"
@@ -79,6 +81,57 @@ func TestRulesLintEmptyDir(t *testing.T) {
}
}
+func makeTarGz(t *testing.T, entries map[string]string) []byte {
+ t.Helper()
+ var buf bytes.Buffer
+ gz := gzip.NewWriter(&buf)
+ tw := tar.NewWriter(gz)
+ for name, body := range entries {
+ if err := tw.WriteHeader(&tar.Header{Name: name, Mode: 0o644, Size: int64(len(body)), Typeflag: tar.TypeReg}); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := tw.Write([]byte(body)); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := tw.Close(); err != nil {
+ t.Fatal(err)
+ }
+ if err := gz.Close(); err != nil {
+ t.Fatal(err)
+ }
+ return buf.Bytes()
+}
+
+func TestExtractRuleTarballNeutralizesTraversal(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",
+ })
+ 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"} {
+ 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 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.go
index fc53ee66..e522f653 100644
--- a/cli/beacon/internal/endpoint/detect/detect.go
+++ b/cli/beacon/internal/endpoint/detect/detect.go
@@ -189,9 +189,15 @@ func InstallFiles(userMode bool, src string, force bool) ([]Installed, error) {
if err != nil {
return nil, fmt.Errorf("%s: %w", p, err)
}
- if _, err := threatrules.CheckRule(rule); err != nil {
+ results, err := threatrules.CheckRule(rule)
+ if err != nil {
return nil, fmt.Errorf("%s: %w", p, err)
}
+ for _, res := range results {
+ if !res.OK() {
+ return nil, fmt.Errorf("%s: fixture %s", p, res.String())
+ }
+ }
if (existing[rule.ID] || staged[rule.ID]) && !force {
return nil, fmt.Errorf("rule %q already installed (use --force to overwrite)", rule.ID)
}
diff --git a/cli/beacon/internal/endpoint/detect/detect_test.go b/cli/beacon/internal/endpoint/detect/detect_test.go
index fa76666c..d7329eda 100644
--- a/cli/beacon/internal/endpoint/detect/detect_test.go
+++ b/cli/beacon/internal/endpoint/detect/detect_test.go
@@ -97,6 +97,25 @@ func TestInstallRejectsInvalidRule(t *testing.T) {
}
}
+func TestInstallRejectsFailingFixture(t *testing.T) {
+ t.Setenv("HOME", t.TempDir())
+ src := filepath.Join(t.TempDir(), "mismatch.rule.yaml")
+ // Valid, compilable rule whose embedded fixture asserts the wrong verdict:
+ // the match condition only fires on file.read, but the fixture feeds a
+ // file.write event while expecting a match. CheckRule returns no error here,
+ // only a failing FixtureResult.
+ bad := strings.Replace(ruleWithID("mismatch-rule"), "action: file.read }", "action: file.write }", 1)
+ if err := os.WriteFile(src, []byte(bad), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := InstallFiles(true, src, false); err == nil {
+ t.Fatal("expected install to reject a rule with a failing fixture")
+ }
+ if hasRuleFiles(StoreDir(true)) {
+ t.Fatal("rule with a failing fixture must not be written to the store")
+ }
+}
+
func TestInstallRejectsDuplicateWithoutForce(t *testing.T) {
t.Setenv("HOME", t.TempDir())
src := filepath.Join(t.TempDir(), "dup.rule.yaml")