Files
HackBrowserData/internal/app/masterkeys.go
T
Roger f36790d4dd refactor: pass secrets as values, fetched once per scope (#713)
* refactor: pass secrets as values, fetched once per scope

A master key travelled through five layers of function values and three
sync.Once guarded reuse; holding each secret where its scope lives shows
where it comes from. Only the keychain prompt still waits for first use.

* docs(rfcs): fetch secrets per extraction, not per record
2026-11-22 23:52:12 +08:00

44 lines
1.6 KiB
Go

package app
import (
"context"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/browser/chromium"
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
"github.com/moond4rk/hackbrowserdata/internal/snapshot"
)
// keySource is where a run gets each Chromium dir's master keys: this machine, a key file, or nowhere.
type keySource interface {
MasterKeys(ctx context.Context, dir browser.UserDataDir, dirSnapshot snapshot.Snapshot) masterkey.Retrieval
}
// machineKeys asks this machine's secret stores; on macOS that includes the run's login keychain.
type machineKeys struct {
loginKeychain masterkey.LoginKeychain
}
func (k machineKeys) MasterKeys(ctx context.Context, dir browser.UserDataDir, dirSnapshot snapshot.Snapshot) masterkey.Retrieval {
locator, err := chromium.KeyLocator(dir, dirSnapshot)
if err != nil {
return masterkey.Retrieval{Err: err}
}
locator.Keychain = k.loginKeychain
return masterkey.RetrieveAll(ctx, locator)
}
// keyFileKeys are a key file's master keys by dir; a dir it lacks gets none, so its encrypted values stay empty.
type keyFileKeys map[browser.UserDataDirID]masterkey.Set
func (k keyFileKeys) MasterKeys(_ context.Context, dir browser.UserDataDir, _ snapshot.Snapshot) masterkey.Retrieval {
return masterkey.Retrieval{Keys: k[dir.ID]}
}
// noKeys lets list --detail count encrypted rows without retrieving a key, so counting never prompts.
type noKeys struct{}
func (noKeys) MasterKeys(context.Context, browser.UserDataDir, snapshot.Snapshot) masterkey.Retrieval {
return masterkey.Retrieval{}
}