Skip to content

Commit 15680c1

Browse files
authored
refactor(keyretriever): reduce duplicate master-key WARN logs (#589)
1 parent 50c4ea8 commit 15680c1

3 files changed

Lines changed: 36 additions & 25 deletions

File tree

browser/chromium/chromium.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package chromium
33
import (
44
"os"
55
"path/filepath"
6+
"sync"
67
"time"
78

89
"github.com/moond4rk/hackbrowserdata/crypto/keyretriever"
@@ -51,17 +52,7 @@ func NewBrowsers(cfg types.BrowserConfig) ([]*Browser, error) {
5152
return browsers, nil
5253
}
5354

54-
// SetKeyRetrievers wires the per-tier master-key retrievers used by Extract. Each slot
55-
// (V10 / V11 / V20) is populated only on platforms where that cipher tier is used:
56-
//
57-
// - Windows: V10 (DPAPI) + V20 (ABE). V11 nil — Chromium does not emit v11 prefix on Windows.
58-
// - Linux: V10 ("peanuts" kV10Key) + V11 (D-Bus Secret Service kV11Key). V20 nil.
59-
// - macOS: V10 (Keychain chain). V11 and V20 nil.
60-
//
61-
// Slots are independent — a failure or absence in one tier does not affect others. A single
62-
// Chromium profile can carry mixed cipher-prefix ciphertexts (the motivation for issue #578), so
63-
// every configured retriever runs at extract time and decryptValue picks the matching key per
64-
// ciphertext.
55+
// SetKeyRetrievers wires the per-tier master-key retrievers (V10/V11/V20) used by Extract; unused tiers stay nil.
6556
func (b *Browser) SetKeyRetrievers(r keyretriever.Retrievers) {
6657
b.retrievers = r
6758
}
@@ -178,12 +169,11 @@ func (b *Browser) acquireFiles(session *filemanager.Session, categories []types.
178169
return tempPaths
179170
}
180171

181-
// getMasterKeys retrieves the Chromium master keys for every configured tier. Chrome mixes
182-
// cipher tiers on the same profile — v20 for new cookies alongside v10 passwords on Windows; v10
183-
// (peanuts) alongside v11 (keyring) on Linux after session-mode changes — so every retriever in
184-
// b.retrievers runs independently and keyretriever.NewMasterKeys assembles the results. Any tier
185-
// key may be nil if its retriever failed or is not configured for this platform; decryptValue
186-
// treats a missing tier key as "that tier cannot decrypt" so partial success is still reported.
172+
// warnedMasterKeyFailure dedupes "master key retrieval" WARN per installation (BrowserName + UserDataDir);
173+
// profiles share one Safe Storage entry, but glob-expanded configs may yield multiple installations of the same browser.
174+
var warnedMasterKeyFailure sync.Map
175+
176+
// getMasterKeys retrieves master keys for all configured cipher tiers.
187177
func (b *Browser) getMasterKeys(session *filemanager.Session) keyretriever.MasterKeys {
188178
label := b.BrowserName() + "/" + b.ProfileName()
189179

@@ -207,7 +197,12 @@ func (b *Browser) getMasterKeys(session *filemanager.Session) keyretriever.Maste
207197

208198
keys, err := keyretriever.NewMasterKeys(b.retrievers, b.cfg.Storage, localStateDst)
209199
if err != nil {
210-
log.Warnf("%s: master key retrieval: %v", label, err)
200+
installKey := b.BrowserName() + "|" + b.cfg.UserDataDir
201+
if _, already := warnedMasterKeyFailure.LoadOrStore(installKey, struct{}{}); !already {
202+
log.Warnf("%s: master key retrieval: %v", b.BrowserName(), err)
203+
} else {
204+
log.Debugf("%s: master key retrieval: %v", label, err)
205+
}
211206
}
212207
return keys
213208
}

crypto/keyretriever/keyretriever.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c *ChainRetriever) RetrieveKey(storage, localStatePath string) ([]byte, er
4848
return key, nil
4949
}
5050
if err != nil {
51-
log.Warnf("keyretriever %T failed: %v", r, err)
51+
log.Debugf("keyretriever %T failed: %v", r, err)
5252
errs = append(errs, fmt.Errorf("%T: %w", r, err))
5353
}
5454
}

crypto/keyretriever/keyretriever_darwin.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"time"
1515

1616
"github.com/moond4rk/keychainbreaker"
17+
18+
"github.com/moond4rk/hackbrowserdata/log"
1719
)
1820

1921
// https://source.chromium.org/chromium/chromium/src/+/master:components/os_crypt/os_crypt_mac.mm;l=157
@@ -37,18 +39,25 @@ type GcoredumpRetriever struct {
3739
err error
3840
}
3941

42+
// RetrieveKey logs internal failures at Debug and returns (nil, nil) so ChainRetriever falls
43+
// through to the next retriever silently. The most common failure ("requires root privileges")
44+
// is documented expected behavior, not a warning-worthy condition; surfacing it on every profile
45+
// would drown out genuine warnings. The same pattern is used by ABERetriever (see abe_windows.go).
4046
func (r *GcoredumpRetriever) RetrieveKey(storage, _ string) ([]byte, error) {
4147
r.once.Do(func() {
4248
r.records, r.err = DecryptKeychainRecords()
43-
if r.err != nil {
44-
r.err = fmt.Errorf("gcoredump: %w", r.err)
45-
}
4649
})
4750
if r.err != nil {
48-
return nil, r.err
51+
log.Debugf("gcoredump: %v", r.err)
52+
return nil, nil //nolint:nilerr // intentional silent fallthrough
4953
}
5054

51-
return findStorageKey(r.records, storage)
55+
key, err := findStorageKey(r.records, storage)
56+
if err != nil {
57+
log.Debugf("gcoredump: %v", err)
58+
return nil, nil //nolint:nilerr // intentional silent fallthrough
59+
}
60+
return key, nil
5261
}
5362

5463
// loadKeychainRecords opens login.keychain-db and unlocks it with the given
@@ -141,7 +150,14 @@ func (r *SecurityCmdRetriever) retrieveKeyOnce(storage string) ([]byte, error) {
141150
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
142151
return nil, fmt.Errorf("security command timed out after %s", securityCmdTimeout)
143152
}
144-
return nil, fmt.Errorf("security command: %w (%s)", err, strings.TrimSpace(stderr.String()))
153+
// `security find-generic-password` exits non-zero with empty stderr when the user denies
154+
// the keychain access prompt or enters the wrong password. Surface that explicitly so the
155+
// error message is actionable instead of the cryptic "exit status 128 ()".
156+
stderrStr := strings.TrimSpace(stderr.String())
157+
if stderrStr == "" {
158+
return nil, fmt.Errorf("security command: %w (likely keychain access denied or wrong password)", err)
159+
}
160+
return nil, fmt.Errorf("security command: %w (%s)", err, stderrStr)
145161
}
146162
if stderr.Len() > 0 {
147163
return nil, fmt.Errorf("keychain: %s", strings.TrimSpace(stderr.String()))

0 commit comments

Comments
 (0)