Skip to content

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

final fixes

Justin D'Souzafdaac9692546
diff --git a/cli/beacon/cmd/rules.go b/cli/beacon/cmd/rules.go
index 1ab35c36..865f7742 100644
--- a/cli/beacon/cmd/rules.go
+++ b/cli/beacon/cmd/rules.go
@@ -174,7 +174,10 @@ func runRulesPull(cmd *cobra.Command, args []string) error {
}
// extractRuleTarball extracts *.rule.yaml entries from a gzipped tarball into dir. Entry
-// paths are flattened to their base name; path-traversal entries are rejected.
+// paths keep their relative archive layout so two entries that share a base name (e.g.
+// network/exfil.rule.yaml and secrets/exfil.rule.yaml) do not overwrite each other;
+// path-traversal entries are rejected. InstallFiles walks dir recursively and keys the
+// installed file on rule id, so preserving subdirectories here loses no rules.
func extractRuleTarball(r io.Reader, dir string) error {
gz, err := gzip.NewReader(r)
if err != nil {
@@ -201,13 +204,22 @@ func extractRuleTarball(r io.Reader, dir string) error {
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)))
+ // Preserve the entry's relative directory structure (rejecting absolute paths),
+ // then confirm the joined destination stays within cleanDir before any file
+ // operation (defense in depth; the same `dest` value is both validated and
+ // written). Keeping subdirectories prevents same-base-name entries from silently
+ // overwriting each other.
+ rel := filepath.Clean(filepath.FromSlash(hdr.Name))
+ if filepath.IsAbs(rel) {
+ return fmt.Errorf("archive entry %q is an absolute path", hdr.Name)
+ }
+ dest := filepath.Join(cleanDir, rel)
if dest != cleanDir && !strings.HasPrefix(dest, cleanDir+string(os.PathSeparator)) {
return fmt.Errorf("archive entry %q resolves outside the destination directory", hdr.Name)
}
+ if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil {
+ return err
+ }
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 778cea38..ab774d2a 100644
--- a/cli/beacon/cmd/rules_test.go
+++ b/cli/beacon/cmd/rules_test.go
@@ -133,8 +133,8 @@ func TestExtractRuleTarballExtractsCleanEntries(t *testing.T) {
if err := extractRuleTarball(bytes.NewReader(data), dir); err != nil {
t.Fatalf("extract: %v", err)
}
- // Rule entries are flattened into dir; archive directory structure is dropped.
- for _, name := range []string{"ok.rule.yaml", "nested.rule.yaml"} {
+ // Rule entries keep their relative archive layout inside dir.
+ for _, name := range []string{"ok.rule.yaml", filepath.Join("pack", "sub", "nested.rule.yaml")} {
if _, err := os.Stat(filepath.Join(dir, name)); err != nil {
t.Errorf("expected %s inside dest dir: %v", name, err)
}
@@ -145,6 +145,34 @@ func TestExtractRuleTarballExtractsCleanEntries(t *testing.T) {
}
}
+// TestExtractRuleTarballKeepsSameBaseNameEntries guards against the silent
+// overwrite where two archive paths sharing a base name flattened to the same
+// destination, so a pack could install fewer rules than it contained.
+func TestExtractRuleTarballKeepsSameBaseNameEntries(t *testing.T) {
+ dir := t.TempDir()
+ data := makeTarGz(t, map[string]string{
+ "network/exfil.rule.yaml": "id: network-exfil",
+ "secrets/exfil.rule.yaml": "id: secrets-exfil",
+ })
+ if err := extractRuleTarball(bytes.NewReader(data), dir); err != nil {
+ t.Fatalf("extract: %v", err)
+ }
+ // Both same-base-name entries must survive at their distinct relative paths.
+ cases := map[string]string{
+ filepath.Join("network", "exfil.rule.yaml"): "id: network-exfil",
+ filepath.Join("secrets", "exfil.rule.yaml"): "id: secrets-exfil",
+ }
+ for rel, want := range cases {
+ got, err := os.ReadFile(filepath.Join(dir, rel))
+ if err != nil {
+ t.Fatalf("expected %s to be extracted: %v", rel, err)
+ }
+ if string(got) != want {
+ t.Errorf("%s: got %q, want %q (entries overwrote each other)", rel, got, want)
+ }
+ }
+}
+
func TestRulesPullAcceptsURLWithQuery(t *testing.T) {
t.Setenv("HOME", t.TempDir())
prev := rulesOpts
diff --git a/cli/beacon/cmd/scan.go b/cli/beacon/cmd/scan.go
index a9e82cf2..4f673ab6 100644
--- a/cli/beacon/cmd/scan.go
+++ b/cli/beacon/cmd/scan.go
@@ -96,7 +96,7 @@ func runScan(cmd *cobra.Command, args []string) error {
sessionFilter := strings.TrimSpace(scanOpts.session)
var events []asymptoteobserve.Event
err = dashboard.StreamEvents(runtimeLog.EffectiveLogPath, func(e schema.Event) error {
- if sessionFilter != "" && (e.Session == nil || e.Session.ID != sessionFilter) {
+ if sessionFilter != "" && (e.Session == nil || !scanSessionMatches(e.Session.ID, sessionFilter)) {
return nil
}
events = append(events, e)
@@ -149,6 +149,10 @@ func runScan(cmd *cobra.Command, args []string) error {
return nil
}
+func scanSessionMatches(sessionID, query string) bool {
+ return strings.Contains(strings.ToLower(sessionID), strings.ToLower(strings.TrimSpace(query)))
+}
+
func filterBySeverity(findings []threatrules.Finding, minRank int) []threatrules.Finding {
out := findings[:0]
for _, f := range findings {
diff --git a/cli/beacon/cmd/scan_test.go b/cli/beacon/cmd/scan_test.go
index c35b2745..cda35c12 100644
--- a/cli/beacon/cmd/scan_test.go
+++ b/cli/beacon/cmd/scan_test.go
@@ -105,6 +105,27 @@ func TestScanJSON(t *testing.T) {
}
}
+func TestScanSessionFilterMatchesDashboardQuery(t *testing.T) {
+ secretReadMixedCaseSession := `{"timestamp":"2026-06-13T10:00:00Z","event":{"action":"file.read"},"file":{"path":".env"},"session":{"id":"Session-ABC-123"}}`
+ secretReadOtherSession := `{"timestamp":"2026-06-13T10:00:01Z","event":{"action":"file.read"},"file":{"path":".env"},"session":{"id":"session-other"}}`
+ scanFixture(t, []string{secretReadMixedCaseSession, secretReadOtherSession})
+ scanOpts.session = "abc"
+ cmd, buf := newCmd()
+ if err := runScan(cmd, nil); err != nil {
+ t.Fatalf("scan: %v", err)
+ }
+ out := buf.String()
+ if !strings.Contains(out, "1 finding(s) across 1 events") {
+ t.Fatalf("expected partial case-insensitive session filter to scan exactly one event, got:\n%s", out)
+ }
+ if !strings.Contains(out, "session=Session-ABC-123") {
+ t.Fatalf("expected finding from the matching session, got:\n%s", out)
+ }
+ if strings.Contains(out, "session=session-other") {
+ t.Fatalf("did not expect finding from the non-matching session, got:\n%s", out)
+ }
+}
+
func TestScanFailOn(t *testing.T) {
scanFixture(t, []string{evSecretRead})
scanOpts.failOn = "high"
diff --git a/cli/beacon/internal/endpoint/detect/detect_test.go b/cli/beacon/internal/endpoint/detect/detect_test.go
index 353d42c8..ff179c52 100644
--- a/cli/beacon/internal/endpoint/detect/detect_test.go
+++ b/cli/beacon/internal/endpoint/detect/detect_test.go
@@ -133,6 +133,74 @@ func TestInstallRejectsDuplicateWithoutForce(t *testing.T) {
}
}
+func TestInstallRollsBackWhenLaterWriteFails(t *testing.T) {
+ t.Setenv("HOME", t.TempDir())
+ srcDir := t.TempDir()
+ if err := os.WriteFile(filepath.Join(srcDir, "a.rule.yaml"), []byte(ruleWithID("a-rule")), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(srcDir, "b.rule.yaml"), []byte(ruleWithID("b-rule")), 0o644); err != nil {
+ t.Fatal(err)
+ }
+
+ store, err := EnsureStore(true)
+ if err != nil {
+ t.Fatal(err)
+ }
+ blockedDest := filepath.Join(store, "b-rule"+ruleFileSuffix)
+ if err := os.Mkdir(blockedDest, 0o755); err != nil {
+ t.Fatal(err)
+ }
+
+ if _, err := InstallFiles(true, srcDir, false); err == nil {
+ t.Fatal("expected install to fail on blocked destination")
+ }
+ if _, err := os.Stat(filepath.Join(store, "a-rule"+ruleFileSuffix)); !os.IsNotExist(err) {
+ t.Fatalf("first rule should have been rolled back, stat err=%v", err)
+ }
+ if info, err := os.Stat(blockedDest); err != nil || !info.IsDir() {
+ t.Fatalf("blocked destination directory should remain, info=%v err=%v", info, err)
+ }
+}
+
+func TestForceInstallRestoresOverwrittenRuleWhenLaterWriteFails(t *testing.T) {
+ t.Setenv("HOME", t.TempDir())
+ initial := strings.Replace(ruleWithID("a-rule"), "title: T", "title: Old", 1)
+ initialSrc := filepath.Join(t.TempDir(), "a.rule.yaml")
+ if err := os.WriteFile(initialSrc, []byte(initial), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := InstallFiles(true, initialSrc, false); err != nil {
+ t.Fatalf("initial install: %v", err)
+ }
+
+ srcDir := t.TempDir()
+ updated := strings.Replace(ruleWithID("a-rule"), "title: T", "title: New", 1)
+ if err := os.WriteFile(filepath.Join(srcDir, "a.rule.yaml"), []byte(updated), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(srcDir, "b.rule.yaml"), []byte(ruleWithID("b-rule")), 0o644); err != nil {
+ t.Fatal(err)
+ }
+
+ store := StoreDir(true)
+ blockedDest := filepath.Join(store, "b-rule"+ruleFileSuffix)
+ if err := os.Mkdir(blockedDest, 0o755); err != nil {
+ t.Fatal(err)
+ }
+
+ if _, err := InstallFiles(true, srcDir, true); err == nil {
+ t.Fatal("expected force install to fail on blocked destination")
+ }
+ got, err := os.ReadFile(filepath.Join(store, "a-rule"+ruleFileSuffix))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if string(got) != initial {
+ t.Fatalf("overwritten rule was not restored:\n%s", got)
+ }
+}
+
func TestRemove(t *testing.T) {
t.Setenv("HOME", t.TempDir())
src := filepath.Join(t.TempDir(), "r.rule.yaml")