From babb836beb23e5d51882b84b3a67def169d2e2bb Mon Sep 17 00:00:00 2001 From: Hoseong Liam Hwang <449334+thefron@users.noreply.github.com> Date: Sat, 14 Nov 2026 23:19:27 -0800 Subject: [PATCH] fix(secrets): re-judge recorded findings when the rule set changes A finding stood until its file changed again, so a rule fix never reached findings already on disk: slugs flagged by the pre-v0.18.1 OpenAI rule kept warning on every prompt. Record the rule set's fingerprint beside the findings and re-scan only the flagged paths once when it differs. Co-Authored-By: Claude Opus 5.5 --- internal/secrets/secrets.go | 17 ++++++++++++ internal/store/secrets.go | 29 ++++++++++++++++++++ internal/syncer/secrets.go | 23 ++++++++++++++++ internal/syncer/syncer.go | 8 ++++++ internal/syncer/syncer_test.go | 48 ++++++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+) diff --git a/internal/secrets/secrets.go b/internal/secrets/secrets.go index cd7db862..35a8ca11 100644 --- a/internal/secrets/secrets.go +++ b/internal/secrets/secrets.go @@ -14,6 +14,8 @@ package secrets import ( "bytes" + "crypto/sha256" + "encoding/hex" "regexp" "sort" ) @@ -65,6 +67,21 @@ var labels = map[string]string{ "gitlab_pat": "a GitLab token", } +// Fingerprint identifies the rule set. A finding recorded under a different +// fingerprint was judged by rules this binary no longer has, so the syncer +// re-reads those paths instead of reporting a verdict the current rules would +// not reach. +func Fingerprint() string { + h := sha256.New() + for _, r := range secretRules { + h.Write([]byte(r.id)) + h.Write([]byte{0}) + h.Write([]byte(r.re.String())) + h.Write([]byte{0}) + } + return hex.EncodeToString(h.Sum(nil))[:16] +} + // Label renders a rule id for a human. An id with no label renders as itself — // a bare id is ugly, but a rule that reports nothing is worse. func Label(rule string) string { diff --git a/internal/store/secrets.go b/internal/store/secrets.go index bfadf53e..f352a78d 100644 --- a/internal/store/secrets.go +++ b/internal/store/secrets.go @@ -55,3 +55,32 @@ func (s *Store) SaveSecrets(mountID string, f map[string][]secrets.Finding) erro } return WriteJSONAtomic(p, f) } + +// The rules record names the rule set the findings were judged by (see +// secrets.Fingerprint). It is a sibling file rather than a field so a record +// written before it existed still loads as a plain map. +func (s *Store) secretsRulesPath(mountID string) (string, error) { + return s.mountStatePath("secrets-rules-", mountID) +} + +// LoadSecretsRules returns the fingerprint the findings were recorded under, +// or "" when none was ever written. +func (s *Store) LoadSecretsRules(mountID string) (string, error) { + p, err := s.secretsRulesPath(mountID) + if err != nil { + return "", err + } + var fp string + if err := readJSON(p, &fp); err != nil { + return "", err + } + return fp, nil +} + +func (s *Store) SaveSecretsRules(mountID, fingerprint string) error { + p, err := s.secretsRulesPath(mountID) + if err != nil { + return err + } + return WriteJSONAtomic(p, fingerprint) +} diff --git a/internal/syncer/secrets.go b/internal/syncer/secrets.go index ebc2c172..898c6872 100644 --- a/internal/syncer/secrets.go +++ b/internal/syncer/secrets.go @@ -34,6 +34,29 @@ const secretsPerFile = 8 type secretLog struct { found map[string][]secrets.Finding dirty bool + // rulesChanged means the record was judged by another rule set and has + // been re-judged this cycle; finish writes the current fingerprint. + rulesChanged bool +} + +// revalidate re-judges every recorded path under the current rules when the +// record was written by a different rule set. Without it a rule fix never +// reaches findings already on disk: a path is only re-read when it changes, +// so a false positive from an older binary would stand forever. Only flagged +// paths are read, and only once per rule change. +func (l *secretLog) revalidate(st *store.Store, cache map[string]store.CachedFile, recorded string) { + if l == nil || recorded == secrets.Fingerprint() { + return + } + l.rulesChanged = true + for rel := range l.found { + c, ok := cache[rel] + if !ok || c.Blob == "" { + l.drop(rel) + continue + } + l.scanBlob(st, rel, c.Blob) + } } // scanBlob records what the blob just written for rel contains. Called only on diff --git a/internal/syncer/syncer.go b/internal/syncer/syncer.go index 2c667724..ad6a2df9 100644 --- a/internal/syncer/syncer.go +++ b/internal/syncer/syncer.go @@ -503,6 +503,9 @@ func (s *Session) cycleLocked(ctx context.Context, hint *ScanHint) (*Result, err if found, err := s.Store.LoadSecrets(s.mountID()); err == nil { sec.found = found } + if fp, err := s.Store.LoadSecretsRules(s.mountID()); err == nil { + sec.revalidate(s.Store, cache, fp) + } myOpCount, err := s.Store.OwnOpCount(s.Device.ID) if err != nil { return nil, fmt.Errorf("read own journal: %w", err) @@ -1107,6 +1110,11 @@ func (s *Session) finish(cache map[string]store.CachedFile, st store.SyncState, log.Printf("beardrive: could not record credential findings: %v", err) } } + if sec != nil && sec.rulesChanged { + if err := s.Store.SaveSecretsRules(s.mountID(), secrets.Fingerprint()); err != nil { + log.Printf("beardrive: could not record credential rule set: %v", err) + } + } if !store.SyncStateEqual(st, loadedSync) { return s.Store.SaveSync(st) } diff --git a/internal/syncer/syncer_test.go b/internal/syncer/syncer_test.go index 13b9c967..868918c7 100644 --- a/internal/syncer/syncer_test.go +++ b/internal/syncer/syncer_test.go @@ -1636,6 +1636,54 @@ func TestSecretScanSkipsUnchangedFiles(t *testing.T) { } } +// TestSecretRecordRejudgedWhenRulesChange: a finding recorded by an older rule +// set must not outlive the rule fix. The file never changes again, so without +// re-judging on a rule change the stale warning would stand forever. +func TestSecretRecordRejudgedWhenRulesChange(t *testing.T) { + a := newDevice(t, "deva", sharedRemote(t)) + + write(t, a.Folder, "blog.md", "see /blog/best-it-help-desk-tools-for-slack-teams\n") + write(t, a.Folder, "deploy.md", "key = "+testAWSKey+"\n") + cycle(t, a) + + // What an older binary left behind: a slug flagged as a key, under no + // recorded fingerprint. + stale := map[string][]secrets.Finding{ + "blog.md": {{Rule: "openai_api_key", Line: 1}}, + "deploy.md": {{Rule: "aws_access_key_id", Line: 1}}, + "gone.md": {{Rule: "openai_api_key", Line: 3}}, + } + if err := a.Store.SaveSecrets(a.mountID(), stale); err != nil { + t.Fatal(err) + } + if err := a.Store.SaveSecretsRules(a.mountID(), ""); err != nil { + t.Fatal(err) + } + + cycle(t, a) // nothing changed on disk + found, err := a.Store.LoadSecrets(a.mountID()) + if err != nil { + t.Fatal(err) + } + want := map[string][]secrets.Finding{"deploy.md": {{Rule: "aws_access_key_id", Line: 1}}} + if !reflect.DeepEqual(found, want) { + t.Fatalf("findings = %+v, want only the real key kept", found) + } + if fp, _ := a.Store.LoadSecretsRules(a.mountID()); fp != secrets.Fingerprint() { + t.Fatalf("rules fingerprint = %q, want %q", fp, secrets.Fingerprint()) + } + + // Once judged under the current rules, quiet cycles go back to reading + // nothing: a record cleared by hand stays cleared. + if err := a.Store.SaveSecrets(a.mountID(), map[string][]secrets.Finding{}); err != nil { + t.Fatal(err) + } + cycle(t, a) + if found, _ := a.Store.LoadSecrets(a.mountID()); len(found) != 0 { + t.Fatalf("findings = %+v — revalidation ran again with no rule change", found) + } +} + func TestPullRejectsSymlinkTargetOnlySlotRedefinition(t *testing.T) { be := sharedRemote(t) s := newDevice(t, "local", be)