Files
HackBrowserData/internal/app/restore.go
T
Roger c823d83e57 refactor(browser): give each base Sources and Extract (#714)
* refactor(browser): give each base Sources and Extract

browser.Input carried every base's secret, so no base said what it needs.
Each base now takes its own secret as a typed parameter, and app holds the
one switch that knows all three. Safari's files no longer need ../ paths.

* refactor(app): make execute the runner's export method

* docs: add a package map to CONTRIBUTING.md

* refactor(app): keep each base's secret on the runner

The bases interface had one real implementation and existed for the test
fake, so it held two of a run's three secrets. The runner holds them all,
and the run tests use real fixtures.

* refactor(chromium): pass data where a function was passed

DiscoverAt took a UserDataDir field by field, the Opera extension list was
a second function for one JSON key, and NeedsMasterKeys repeated by hand
which extractors decrypt.

* refactor(app): set both passwords after the flags are valid

The keychain password went in as a ValidateDump argument and the Firefox
one was assigned afterwards; cmd now sets both the same way.

* refactor: rename dumpKeysWith and CandidatePaths

buildKeyFile says what it returns, and DirRelativePaths says what its paths
are relative to. A failed browser's dir ID no longer comes from a cast.

* refactor(app): plan extractions without a plan struct

dirPlan and plannedExtraction existed to carry a profile and three lists.
An Extraction now holds its Profile, and planExtractions returns the lists.

* refactor(browser): rename profileAt and downloadsOf

profileAt returned a user data dir in two shapes, and downloadsOf returned
a function. Both bases now have dirFromProfilePath with one shape, and
Safari has newDownloadExtractor beside its other constructors.

* refactor: use one short name for a user data dir in app

Fields said UserDataDirID beside Dirs and DirFailure, and runDir did not
say what it runs. They are DirID and runExtractions; the type keeps its
full name.

* refactor(app): rename withData to dropEmptyProfiles

With reads as adding something, and the function removes profiles. hasData
loses a parameter that always held sourcesFor's result.

* refactor: rename five functions that misstate what they do

untilDone had no verb, canCreateIn asked a yes/no question but returned an
error, failAll only built results, sortKeyOf was the last xxxOf, and
checkProfilePath returned a value where its siblings return only an error.

* refactor: name forks as forks and drop the For lookups

The fork table was typed override while every variable said fork, and six
lookups were named for a preposition, not for what they return. The other
renames make one name mean one thing: extractors, dataSources, extractFile.

* refactor(browser): drop parameters Sources and Extract ignore

No interface makes the three bases share a signature, so the blank
parameters only hid that Chromium's sources vary by dir, Safari's by
profile, and that only Safari's extraction can be cancelled.

* refactor(browser): ask a base for one category's sources

Sources returned every category's files, so its name had no object and
each base carried a loop projecting its table. Keyed by category it is
the same lookup as Extract and NeedsMasterKeys.

* refactor: report a locked keychain as data, not a log line

Dump and DumpKeys return results for cmd to print, yet warned about the
keychain themselves, with the sentence passed in as an argument. Now cmd
says every sentence; warnIfLocked and printNothingFound take no message.

* refactor(cmd): rename printExported to printExportedFiles

Exported is an adjective; the function lists the files a run wrote.
2026-11-22 23:52:12 +08:00

249 lines
8.3 KiB
Go

package app
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/browser/chromium"
"github.com/moond4rk/hackbrowserdata/internal/category"
"github.com/moond4rk/hackbrowserdata/internal/log"
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
"github.com/moond4rk/hackbrowserdata/internal/zipfile"
)
// RestoreFlags are restore's command-line values as cobra parsed them.
type RestoreFlags struct {
KeyFile string // a keys.json path, or "-" for stdin
DataDir string
DataZip string
Browser string
Categories string
Output OutputFlags
}
type RestoreOptions struct {
Vaults []masterkey.Vault // the key file's vaults that -b selects
DataDir string // the copied data: an archive's layout or one browser's User Data; "" with DataZip
DataZip string // an archive from the archive command; "" with DataDir
Categories []category.Category
Output OutputOptions
}
// ValidateRestore checks restore's flags and reads its keys, from stdin for "-", before any data is opened. A restore's
// browsers are the key file's vaults, not this OS's table, so -b selects among them.
func ValidateRestore(flags RestoreFlags, stdin io.Reader) (RestoreOptions, error) {
if flags.KeyFile == "" {
return RestoreOptions{}, errors.New("requires --keys <file> (or - for stdin)")
}
if (flags.DataDir == "") == (flags.DataZip == "") {
return RestoreOptions{}, errors.New("exactly one of --data-dir or --data-zip is required")
}
categories, err := parseCategories(flags.Categories)
if err != nil {
return RestoreOptions{}, err
}
output, err := parseOutput(flags.Output)
if err != nil {
return RestoreOptions{}, err
}
if err := checkRestoreData(flags.DataDir, flags.DataZip); err != nil {
return RestoreOptions{}, err
}
keyFile, err := readKeyFile(flags.KeyFile, stdin)
if err != nil {
return RestoreOptions{}, err
}
vaults, err := selectVaults(keyFile.Vaults, flags.Browser)
if err != nil {
return RestoreOptions{}, err
}
return RestoreOptions{Vaults: vaults, DataDir: flags.DataDir, DataZip: flags.DataZip, Categories: categories, Output: output}, nil
}
// Restore exports the copied data with the key file's keys, so it never reads this machine's secret stores. It returns
// what Dump returns: a Discovery without dirs means nothing ran, and a run that ended early says so in Summary.Err.
func Restore(ctx context.Context, options RestoreOptions) (Discovery, Summary, error) {
archiveDir := options.DataDir
if options.DataZip != "" {
unzipped, err := unzipArchive(options.DataZip)
if err != nil {
return Discovery{}, Summary{}, err
}
defer removeUnzipped(unzipped)
archiveDir = unzipped
}
discovery, keys, err := discoverArchive(archiveDir, options.Vaults)
if err != nil {
return Discovery{}, Summary{}, err
}
if len(discovery.Dirs) == 0 {
return discovery, Summary{}, nil
}
fromKeyFile := runner{keySource: keyFileKeys(keys)}
summary, err := fromKeyFile.export(ctx, discovery, options.Categories, options.Output)
if err != nil {
return Discovery{}, Summary{}, err
}
return discovery, summary, nil
}
// unzipArchive extracts into a private temporary directory, rejecting entries that would land outside it.
func unzipArchive(archivePath string) (string, error) {
unzipped, err := os.MkdirTemp("", "hbd-restore-*")
if err != nil {
return "", fmt.Errorf("extract %s: %w", archivePath, err)
}
if err := zipfile.Extract(archivePath, unzipped); err != nil {
return "", errors.Join(fmt.Errorf("extract %s: %w", archivePath, err), os.RemoveAll(unzipped))
}
return unzipped, nil
}
func removeUnzipped(unzipped string) {
if err := os.RemoveAll(unzipped); err != nil {
log.Warnf("restore: remove %s: %v", unzipped, err)
}
}
// selectVaults keeps the vaults of the browser -b names, or every vault when it names none.
func selectVaults(vaults []masterkey.Vault, browserName string) ([]masterkey.Vault, error) {
name := strings.ToLower(strings.TrimSpace(browserName))
if name == "" || name == allBrowsers {
return vaults, nil
}
var selected []masterkey.Vault
for _, vault := range vaults {
if strings.EqualFold(vault.Browser, name) {
selected = append(selected, vault)
}
}
if len(selected) == 0 {
return nil, fmt.Errorf("no vault for browser %q in keys (have: %s)", name, vaultBrowserNames(vaults))
}
return selected, nil
}
func checkRestoreData(dataDir, dataZip string) error {
if dataDir != "" {
if !dirExists(dataDir) {
return fmt.Errorf("data dir %q does not exist", dataDir)
}
return nil
}
if _, err := os.Stat(dataZip); err != nil {
return fmt.Errorf("data zip: %w", err)
}
return nil
}
func readKeyFile(path string, stdin io.Reader) (masterkey.KeyFile, error) {
reader := stdin
if path != "-" {
file, err := os.Open(path)
if err != nil {
return masterkey.KeyFile{}, fmt.Errorf("open keys file %q: %w", path, err)
}
defer file.Close()
reader = file
}
keyFile, err := masterkey.ReadKeyFile(reader)
if err != nil {
return masterkey.KeyFile{}, fmt.Errorf("read keys file %q: %w", path, err)
}
return keyFile, nil
}
// discoverArchive rebuilds the user data dirs under archiveDir, and their keys, from the vaults: each vault names a
// subdirectory of archiveDir, or archiveDir is one browser's User Data.
func discoverArchive(archiveDir string, vaults []masterkey.Vault) (Discovery, map[browser.UserDataDirID]masterkey.Set, error) {
archived := false
for _, vault := range vaults {
if dirExists(filepath.Join(archiveDir, string(vaultDirID(vault)))) {
archived = true
break
}
}
if !archived && len(vaults) > 1 {
return Discovery{}, nil, fmt.Errorf("--data-dir %q has no per-browser subdir but keys has %d browsers; "+
"point it at the archive root, or use -b <browser> for one browser's User Data (have: %s)",
archiveDir, len(vaults), vaultBrowserNames(vaults))
}
var discovery Discovery
keys := make(map[browser.UserDataDirID]masterkey.Set)
for _, vault := range vaults {
id := vaultDirID(vault)
if _, duplicate := keys[id]; duplicate {
log.Warnf("restore: keys hold %s twice, restoring the first", id)
continue
}
path := archiveDir
if archived {
path = filepath.Join(archiveDir, string(id))
if !dirExists(path) {
log.Warnf("restore: %s has no data under %s, skipping", vault.Browser, path)
continue
}
}
dir, err := restoredDir(vault, id, path)
if err != nil {
log.Warnf("restore: %s: %v", vault.Browser, err)
continue
}
discovery.Dirs = append(discovery.Dirs, dir)
keys[id] = vault.Keys
}
return discovery, keys, nil
}
func restoredDir(vault masterkey.Vault, id browser.UserDataDirID, path string) (browser.UserDataDir, error) {
restoredBrowser, vaultKind := vaultBrowser(vault)
dir := browser.UserDataDir{ID: id, Browser: restoredBrowser, Path: path, VaultKind: vaultKind}
profiles, err := chromium.DiscoverProfiles(dir)
if err != nil {
return browser.UserDataDir{}, err
}
dir.Profiles = profiles
found := dropEmptyProfiles([]browser.UserDataDir{dir})
if len(found) == 0 {
return browser.UserDataDir{}, fmt.Errorf("found no profiles under %s", path)
}
return found[0], nil
}
// vaultBrowser searches the whole table, so a fork this OS lacks still restores. A browser this build does not know
// keeps its own name, and its vault's kind picks the on-disk format.
func vaultBrowser(vault masterkey.Vault) (restored browser.Browser, vaultKind string) {
name := strings.ToLower(vault.Browser)
if namedBrowser, ok := browser.Lookup(name); ok && namedBrowser.Base == browser.Chromium {
return namedBrowser, chromium.VaultKind(namedBrowser)
}
return browser.Browser{Name: name, DisplayName: vault.Browser, Base: browser.Chromium}, vault.Kind
}
// vaultDirID is the vault's directory in an archive: its ID for a second user data dir, else its browser name.
func vaultDirID(vault masterkey.Vault) browser.UserDataDirID {
if vault.ID != "" {
return browser.UserDataDirID(vault.ID)
}
return browser.UserDataDirID(strings.ToLower(vault.Browser))
}
func vaultBrowserNames(vaults []masterkey.Vault) string {
keys := make([]string, 0, len(vaults))
for _, vault := range vaults {
keys = append(keys, strings.ToLower(vault.Browser))
}
return strings.Join(keys, ", ")
}
func dirExists(path string) bool {
info, err := os.Stat(path)
return err == nil && info.IsDir()
}