Files
HackBrowserData/internal/browser/extractor.go
T
Roger fed6c722e2 refactor: open SQLite in one place and stream records (#688)
* refactor: stream records through sqlitex and the collector

A row that failed to scan was logged at debug and dropped, so it never counted toward partial; it now reaches Collector.Fail.
Every SQLite open goes through sqlitex with options instead of a bool, and extractors add records as they read them.

* refactor: read SQLite rows with plain loops

EachRow hid each extractor's loop and its skip rule behind two callbacks; every extractor now opens, queries, and walks
its own rows, so the control flow reads top to bottom.

* refactor(browser): drop the memoizing key closures

Firefox derives its key once before the login loop, and Yandex loads its data key at the first row, so the state sits
in plain variables where the loop uses it instead of inside a closure.

* fix(safari): report unreadable localStorage origins

A broken origin, database or row was only logged at debug level, so it never counted toward a partial result.
2026-11-09 17:25:36 +08:00

85 lines
2.7 KiB
Go

package browser
import (
"context"
"errors"
"fmt"
"io/fs"
"path"
"strings"
"github.com/moond4rk/hackbrowserdata/internal/category"
"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 {
Path string // slash-separated, relative to the profile dir, or to the user data dir when Shared
Fallbacks []string // older locations, read only when Path 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
}
// Paths lists Path and then its Fallbacks, the order they are tried in.
func (s Source) Paths() []string {
return append([]string{s.Path}, s.Fallbacks...)
}
// CandidatePaths are the Paths for a profile, relative to the user data dir.
func (s Source) CandidatePaths(profile Profile) []string {
paths := s.Paths()
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, profileSnapshot ProfileSnapshot, 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)
}
type ProfileSnapshot struct {
Profile Profile
snapshot snapshot.Snapshot
masterKeys func(ctx context.Context) masterkey.Set
}
// NewProfileSnapshot takes masterKeys, the dir's call-once retrieval, nil for bases that keep no Chromium keys.
func NewProfileSnapshot(dirSnapshot snapshot.Snapshot, profile Profile, masterKeys func(context.Context) masterkey.Set) ProfileSnapshot {
return ProfileSnapshot{Profile: profile, snapshot: dirSnapshot, masterKeys: masterKeys}
}
// Path skips a candidate that was never captured but returns the error of one whose copy failed.
func (ps ProfileSnapshot) Path(source Source) (string, error) {
for _, relativePath := range source.CandidatePaths(ps.Profile) {
capturedPath, err := ps.snapshot.Path(relativePath)
if errors.Is(err, fs.ErrNotExist) {
continue
}
return capturedPath, err
}
return "", fmt.Errorf("%s: %w", strings.Join(source.Paths(), " or "), fs.ErrNotExist)
}
// MasterKeys may block on its first call; a key version that could not be retrieved is absent from the Set.
func (ps ProfileSnapshot) MasterKeys(ctx context.Context) masterkey.Set {
if ps.masterKeys == nil {
return nil
}
return ps.masterKeys(ctx)
}