Skip to content

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

Ignore macOS metadata in threat-rule packs

Justin D'Souza01de04a260d8
diff --git a/cli/beacon/.goreleaser.yaml b/cli/beacon/.goreleaser.yaml
index 8bb0afd5..ab2af42b 100644
--- a/cli/beacon/.goreleaser.yaml
+++ b/cli/beacon/.goreleaser.yaml
@@ -15,7 +15,7 @@ before:
- cp ../../collector-builder/dist/beacon-otelcol/linux_arm64/beacon-otelcol ./release-collectors/linux_arm64/beacon-otelcol
# Package the external threat-rule pack (repo rules/, not embedded in the binary)
# as a release asset users install with `beacon rules pull <asset-url>`.
- - bash -c 'mkdir -p release-rules && tar -czf release-rules/threat-rules.tar.gz -C ../.. rules'
+ - bash -c 'mkdir -p release-rules && COPYFILE_DISABLE=1 tar --exclude="._*" --exclude=".DS_Store" -czf release-rules/threat-rules.tar.gz -C ../.. rules'
builds:
- id: beacon
diff --git a/cli/beacon/Makefile b/cli/beacon/Makefile
index 59c519c6..b66c7c59 100644
--- a/cli/beacon/Makefile
+++ b/cli/beacon/Makefile
@@ -129,5 +129,5 @@ tidy:
# release build produces the same artifact via the goreleaser before-hook.
rules-pack:
mkdir -p release-rules
- tar -czf release-rules/threat-rules.tar.gz -C ../.. rules
+ COPYFILE_DISABLE=1 tar --exclude='._*' --exclude='.DS_Store' -czf release-rules/threat-rules.tar.gz -C ../.. rules
@echo "wrote release-rules/threat-rules.tar.gz"
diff --git a/cli/beacon/cmd/rules.go b/cli/beacon/cmd/rules.go
index 865f7742..1b862941 100644
--- a/cli/beacon/cmd/rules.go
+++ b/cli/beacon/cmd/rules.go
@@ -195,7 +195,7 @@ func extractRuleTarball(r io.Reader, dir string) error {
if err != nil {
return fmt.Errorf("tar: %w", err)
}
- if hdr.Typeflag != tar.TypeReg || !strings.HasSuffix(hdr.Name, ".rule.yaml") {
+ if hdr.Typeflag != tar.TypeReg || !strings.HasSuffix(hdr.Name, ".rule.yaml") || isAppleDoubleArchiveEntry(hdr.Name) {
continue
}
// Reject any entry whose name contains a path-traversal element before it is
@@ -231,6 +231,15 @@ func extractRuleTarball(r io.Reader, dir string) error {
return nil
}
+func isAppleDoubleArchiveEntry(name string) bool {
+ for _, part := range strings.Split(path.Clean(name), "/") {
+ if part == "__MACOSX" || strings.HasPrefix(part, "._") {
+ return true
+ }
+ }
+ return false
+}
+
func writeReaderToFile(r io.Reader, dest string) error {
f, err := os.Create(dest)
if err != nil {
diff --git a/cli/beacon/cmd/rules_test.go b/cli/beacon/cmd/rules_test.go
index ab774d2a..6ff28112 100644
--- a/cli/beacon/cmd/rules_test.go
+++ b/cli/beacon/cmd/rules_test.go
@@ -145,6 +145,24 @@ func TestExtractRuleTarballExtractsCleanEntries(t *testing.T) {
}
}
+func TestExtractRuleTarballIgnoresAppleDoubleEntries(t *testing.T) {
+ dir := t.TempDir()
+ data := makeTarGz(t, map[string]string{
+ "rules/approval-abuse/approval-denied-command-executed-anyway.rule.yaml": "id: ok",
+ "rules/approval-abuse/._approval-denied-command-executed-anyway.rule.yaml": "\x00\x05metadata",
+ "__MACOSX/rules/._approval-denied-command-executed-anyway.rule.yaml": "\x00\x05metadata",
+ })
+ if err := extractRuleTarball(bytes.NewReader(data), dir); err != nil {
+ t.Fatalf("extract: %v", err)
+ }
+ if _, err := os.Stat(filepath.Join(dir, "rules", "approval-abuse", "approval-denied-command-executed-anyway.rule.yaml")); err != nil {
+ t.Fatalf("expected real rule to be extracted: %v", err)
+ }
+ if _, err := os.Stat(filepath.Join(dir, "rules", "approval-abuse", "._approval-denied-command-executed-anyway.rule.yaml")); !os.IsNotExist(err) {
+ t.Fatalf("AppleDouble entry should not be extracted, stat err=%v", err)
+ }
+}
+
// 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.
diff --git a/cli/beacon/internal/endpoint/detect/detect.go b/cli/beacon/internal/endpoint/detect/detect.go
index f0c62d5f..6702c2e2 100644
--- a/cli/beacon/internal/endpoint/detect/detect.go
+++ b/cli/beacon/internal/endpoint/detect/detect.go
@@ -300,7 +300,7 @@ func ruleFilesAt(src string) ([]string, error) {
return nil, err
}
if !info.IsDir() {
- if !strings.HasSuffix(src, ruleFileSuffix) {
+ if !isRuleFile(src) {
return nil, fmt.Errorf("%s is not a %s file", src, ruleFileSuffix)
}
return []string{src}, nil
@@ -310,7 +310,7 @@ func ruleFilesAt(src string) ([]string, error) {
if err != nil {
return err
}
- if !d.IsDir() && strings.HasSuffix(path, ruleFileSuffix) {
+ if !d.IsDir() && isRuleFile(path) {
paths = append(paths, path)
}
return nil
@@ -322,6 +322,10 @@ func ruleFilesAt(src string) ([]string, error) {
return paths, nil
}
+func isRuleFile(path string) bool {
+ return strings.HasSuffix(path, ruleFileSuffix) && !strings.HasPrefix(filepath.Base(path), "._")
+}
+
func storeIDs(store string) (map[string]bool, error) {
ids := map[string]bool{}
entries, err := os.ReadDir(store)
diff --git a/cli/beacon/internal/endpoint/detect/detect_test.go b/cli/beacon/internal/endpoint/detect/detect_test.go
index 240760f2..100b9a22 100644
--- a/cli/beacon/internal/endpoint/detect/detect_test.go
+++ b/cli/beacon/internal/endpoint/detect/detect_test.go
@@ -87,6 +87,24 @@ func TestInstallThenLoadActiveUsesStore(t *testing.T) {
}
}
+func TestInstallFilesIgnoresAppleDoubleRuleFiles(t *testing.T) {
+ t.Setenv("HOME", t.TempDir())
+ srcDir := t.TempDir()
+ if err := os.WriteFile(filepath.Join(srcDir, "ok.rule.yaml"), []byte(ruleWithID("custom-rule")), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(srcDir, "._ok.rule.yaml"), []byte("\x00\x05metadata"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ installed, err := InstallFiles(true, srcDir, false)
+ if err != nil {
+ t.Fatalf("install: %v", err)
+ }
+ if len(installed) != 1 || installed[0].ID != "custom-rule" {
+ t.Fatalf("unexpected install result: %+v", installed)
+ }
+}
+
func TestInstallRejectsInvalidRule(t *testing.T) {
t.Setenv("HOME", t.TempDir())
src := filepath.Join(t.TempDir(), "bad.rule.yaml")
diff --git a/pkg/asymptoteobserve/threatrules/loader.go b/pkg/asymptoteobserve/threatrules/loader.go
index 2900adc9..cbc060f9 100644
--- a/pkg/asymptoteobserve/threatrules/loader.go
+++ b/pkg/asymptoteobserve/threatrules/loader.go
@@ -56,7 +56,7 @@ func LoadDir(root string) ([]*Rule, error) {
if d.IsDir() {
return nil
}
- if strings.HasSuffix(path, ruleFileSuffix) {
+ if isRuleFile(path) {
paths = append(paths, path)
}
return nil
@@ -81,3 +81,7 @@ func LoadDir(root string) ([]*Rule, error) {
}
return rules, nil
}
+
+func isRuleFile(path string) bool {
+ return strings.HasSuffix(path, ruleFileSuffix) && !strings.HasPrefix(filepath.Base(path), "._")
+}
diff --git a/pkg/asymptoteobserve/threatrules/loader_test.go b/pkg/asymptoteobserve/threatrules/loader_test.go
index 19858cac..feb6acbe 100644
--- a/pkg/asymptoteobserve/threatrules/loader_test.go
+++ b/pkg/asymptoteobserve/threatrules/loader_test.go
@@ -55,6 +55,19 @@ func TestLoadDir(t *testing.T) {
}
}
+func TestLoadDirIgnoresAppleDoubleRuleFiles(t *testing.T) {
+ root := t.TempDir()
+ writeRule(t, root, "ok.rule.yaml", strings.Replace(sampleRule, "%s", "rule-one", 1))
+ writeRule(t, root, "._ok.rule.yaml", "\x00\x05metadata")
+ rules, err := LoadDir(root)
+ if err != nil {
+ t.Fatalf("LoadDir: %v", err)
+ }
+ if len(rules) != 1 || rules[0].ID != "rule-one" {
+ t.Fatalf("unexpected rules: %+v", rules)
+ }
+}
+
func TestLoadDirDuplicateID(t *testing.T) {
root := t.TempDir()
writeRule(t, filepath.Join(root, "a"), "x.rule.yaml", strings.Replace(sampleRule, "%s", "dup", 1))