Files
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

95 lines
2.3 KiB
Go

package safari
import (
"fmt"
"os"
"regexp"
"sort"
"strings"
"github.com/moond4rk/plist"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/record"
)
// extensionKeyPattern parses the "<bundleID> (<teamID>)" keys of the App Extensions and Web Extensions plists.
// Only Web Extensions carry Enabled; a listed App Extension is enabled.
var extensionKeyPattern = regexp.MustCompile(`^(\S+)\s+\(([^)]+)\)$`)
// extensionState decodes only the fields record.Extension needs.
type extensionState struct {
Enabled *bool `plist:"Enabled"`
}
// extractExtensions merges the plists in paths, sorted by key for deterministic output.
func extractExtensions(paths []string, out browser.Collector) error {
records, err := readExtensions(paths)
if err != nil {
return err
}
for _, entry := range records {
out.Add(&record.Extension{
Name: entry.bundleID,
ID: entry.key,
Enabled: entry.enabled,
})
}
return nil
}
type extensionRecord struct {
key string
bundleID string
enabled bool
}
func readExtensions(paths []string) ([]extensionRecord, error) {
var all []extensionRecord
for _, plistPath := range paths {
records, err := decodeExtensionsPlist(plistPath)
if err != nil {
return nil, err
}
all = append(all, records...)
}
sort.Slice(all, func(i, j int) bool { return all[i].key < all[j].key })
return all, nil
}
func decodeExtensionsPlist(path string) ([]extensionRecord, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var decoded map[string]extensionState
if err := plist.NewDecoder(file).Decode(&decoded); err != nil {
return nil, fmt.Errorf("decode extensions %s: %w", path, err)
}
records := make([]extensionRecord, 0, len(decoded))
for key, extension := range decoded {
enabled := true
if extension.Enabled != nil {
enabled = *extension.Enabled
}
records = append(records, extensionRecord{
key: key,
bundleID: bundleIDFromExtensionKey(key),
enabled: enabled,
})
}
return records, nil
}
// bundleIDFromExtensionKey falls back to the whole trimmed key when it does not match the pattern.
func bundleIDFromExtensionKey(key string) string {
if match := extensionKeyPattern.FindStringSubmatch(key); match != nil {
return match[1]
}
return strings.TrimSpace(key)
}