Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions internal/secrets/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ package secrets

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"regexp"
"sort"
)
Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions internal/store/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
23 changes: 23 additions & 0 deletions internal/syncer/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions internal/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
48 changes: 48 additions & 0 deletions internal/syncer/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading