Files
HackBrowserData/internal/browser/extractor.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

107 lines
3.5 KiB
Go

package browser
import (
"context"
"errors"
"fmt"
"io/fs"
"path"
"strings"
"github.com/moond4rk/hackbrowserdata/internal/category"
"github.com/moond4rk/hackbrowserdata/internal/keychain"
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
"github.com/moond4rk/hackbrowserdata/internal/record"
"github.com/moond4rk/hackbrowserdata/internal/snapshot"
)
// Source is one file or directory an extractor reads.
type Source struct {
RelativePath string // slash-separated, relative to the profile dir, or to the user data dir when Shared
Fallbacks []string // older locations, read only when RelativePath is absent
IsDir bool
Shared bool // belongs to the whole user data dir, such as Local State, so it is captured once per dir
Optional bool // the extraction still runs without it
}
// RelativePaths lists RelativePath and then its Fallbacks, the order they are tried in.
func (s Source) RelativePaths() []string {
return append([]string{s.RelativePath}, s.Fallbacks...)
}
// CandidatePaths are the RelativePaths for a profile, relative to the user data dir.
func (s Source) CandidatePaths(profile Profile) []string {
paths := s.RelativePaths()
if !s.Shared {
for i, relativePath := range paths {
paths[i] = path.Join(profile.RelativePath, relativePath)
}
}
return paths
}
type Extractor struct {
Sources []Source
Extract func(ctx context.Context, input Input, out Collector) error
}
type Extractors map[category.Category]Extractor
// Collector sets every record's Browser and Profile; Fail reports a bad record and the extraction goes on.
type Collector interface {
Add(row record.Record)
Fail(err error)
}
// LoginKeychain is what an extractor reads from the run's macOS login keychain.
type LoginKeychain interface {
InternetPasswords(ctx context.Context) ([]keychain.InternetPassword, error)
}
// Input is what an extractor reads for one profile: its captured files, the dir's master keys, and the run's secrets.
type Input struct {
Profile Profile
snapshot snapshot.Snapshot
masterKeys masterkey.Set
loginKeychain LoginKeychain
firefoxPassword string
}
// NewInput takes the dir's master keys, nil for bases that keep none, and the run's login keychain, nil off macOS and
// in a restore.
func NewInput(dirSnapshot snapshot.Snapshot, profile Profile, masterKeys masterkey.Set, loginKeychain LoginKeychain) Input {
return Input{Profile: profile, snapshot: dirSnapshot, masterKeys: masterKeys, loginKeychain: loginKeychain}
}
// Path skips a candidate that was never captured but returns the error of one whose copy failed.
func (in Input) Path(source Source) (string, error) {
for _, relativePath := range source.CandidatePaths(in.Profile) {
capturedPath, err := in.snapshot.Path(relativePath)
if errors.Is(err, fs.ErrNotExist) {
continue
}
return capturedPath, err
}
return "", fmt.Errorf("%s: %w", strings.Join(source.RelativePaths(), " or "), fs.ErrNotExist)
}
// MasterKeys lacks each key version that could not be retrieved.
func (in Input) MasterKeys() masterkey.Set {
return in.masterKeys
}
func (in Input) LoginKeychain() LoginKeychain {
return in.loginKeychain
}
// WithFirefoxPassword sets the Firefox primary password; only the dump path supplies one.
func (in Input) WithFirefoxPassword(password string) Input {
in.firefoxPassword = password
return in
}
// FirefoxPrimaryPassword is the user's primary password, "" when none was given.
func (in Input) FirefoxPrimaryPassword() string {
return in.firefoxPassword
}