From 54d11adf679c0edb7737dfb481d75f9821de7576 Mon Sep 17 00:00:00 2001 From: moonD4rk Date: Sat, 21 Nov 2026 20:18:41 +0800 Subject: [PATCH 1/3] refactor(app): export one function per command Step 2 of #710. A command's logic was split between cmd and stage-shaped app calls; cmd now parses flags, calls one function and prints what it returns. An error means the command never ran, and a run that ended early says so in Summary.Err. --- cmd/hack-browser-data/archive.go | 19 +++-- cmd/hack-browser-data/dump.go | 26 +++---- cmd/hack-browser-data/dumpkeys.go | 12 ++-- cmd/hack-browser-data/flags.go | 15 ++++ cmd/hack-browser-data/list.go | 7 +- cmd/hack-browser-data/password.go | 48 +++++-------- cmd/hack-browser-data/password_test.go | 8 +-- cmd/hack-browser-data/report.go | 8 +-- cmd/hack-browser-data/restore.go | 39 ++--------- cmd/hack-browser-data/restore_test.go | 28 ++++++-- internal/app/archive.go | 31 +++++---- internal/app/archive_golden_test.go | 8 +-- internal/app/discover.go | 13 ++-- internal/app/discover_test.go | 10 +-- internal/app/doc.go | 3 +- internal/app/dump.go | 64 ++++++++++------- internal/app/dump_test.go | 41 ++++++----- internal/app/dumpkeys.go | 27 +++---- internal/app/execute.go | 65 +++++++---------- internal/app/execute_test.go | 24 ++++--- internal/app/extract_golden_test.go | 12 ++-- internal/app/keychain_darwin.go | 9 +-- internal/app/keychain_other.go | 4 +- internal/app/list.go | 32 ++++----- internal/app/options.go | 36 +++++----- internal/app/restore.go | 93 +++++++++++++++++-------- internal/app/restore_golden_test.go | 47 +++++-------- internal/app/restore_test.go | 45 +++++++++--- internal/app/result.go | 3 +- internal/browser/browsertest/archive.go | 26 +++++++ internal/snapshot/snapshot.go | 19 ----- internal/snapshot/snapshot_test.go | 47 ++----------- internal/snapshot/zip_test.go | 12 ++-- 33 files changed, 444 insertions(+), 437 deletions(-) create mode 100644 cmd/hack-browser-data/flags.go create mode 100644 internal/browser/browsertest/archive.go diff --git a/cmd/hack-browser-data/archive.go b/cmd/hack-browser-data/archive.go index c8315aa..b8edca3 100644 --- a/cmd/hack-browser-data/archive.go +++ b/cmd/hack-browser-data/archive.go @@ -8,10 +8,7 @@ import ( ) func newArchiveCommand() *cobra.Command { - var ( - flags app.ArchiveFlags - outputPath string - ) + var flags app.ArchiveFlags command := &cobra.Command{ Use: "archive", @@ -23,22 +20,22 @@ func newArchiveCommand() *cobra.Command { if err != nil { return err } - discovery := app.Discover(command.Context(), options.Target) - if warnIfNothingFound(command.ErrOrStderr(), discovery, "no browsers found") { - return nil - } - fileCount, err := app.Archive(command.Context(), discovery, options.Categories, outputPath) + discovery, fileCount, err := app.Archive(command.Context(), options) if err != nil { return err } - log.Infof("Archived %d entries to %s", fileCount, outputPath) + if len(discovery.Dirs) == 0 { + printNothingFound(command.ErrOrStderr(), discovery, "no browsers found") + return nil + } + log.Infof("Archived %d entries to %s", fileCount, options.ArchivePath) return nil }, } command.Flags().StringVarP(&flags.Browser, "browser", "b", "all", "target browser: all|"+browserNames()) command.Flags().StringVarP(&flags.Categories, "category", "c", "all", "data categories (comma-separated): all|"+categoryNames()) - command.Flags().StringVarP(&outputPath, "output", "o", "browser-data.zip", "output archive of decryption-relevant browser files") + command.Flags().StringVarP(&flags.OutputPath, "output", "o", "browser-data.zip", "output archive of decryption-relevant browser files") return command } diff --git a/cmd/hack-browser-data/dump.go b/cmd/hack-browser-data/dump.go index fba801b..c0751e5 100644 --- a/cmd/hack-browser-data/dump.go +++ b/cmd/hack-browser-data/dump.go @@ -13,8 +13,8 @@ import ( func newDumpCommand() *cobra.Command { var ( flags app.DumpFlags - keychain keychainFlags - firefox firefoxFlags + keychain passwordFlags + firefox passwordFlags ) command := &cobra.Command{ @@ -32,31 +32,31 @@ func newDumpCommand() *cobra.Command { command.Flags().StringVarP(&flags.Browser, "browser", "b", "all", "target browser: all|"+browserNames()) command.Flags().StringVarP(&flags.Categories, "category", "c", "all", "data categories (comma-separated): all|"+categoryNames()) - command.Flags().StringVarP(&flags.Format, "format", "f", "json", "output format: csv|json|cookie-editor") - command.Flags().StringVarP(&flags.OutputDir, "dir", "d", "results", "output directory") command.Flags().StringVarP(&flags.ProfilePath, "profile-path", "p", "", "custom profile dir path, get with chrome://version") - addKeychainPasswordFlags(command, &keychain) - addFirefoxPasswordFlags(command, &firefox) - command.Flags().BoolVar(&flags.Zip, "zip", false, "compress output to zip") - command.Flags().IntVar(&flags.MaxStorageValue, "max-storage-value", app.DefaultMaxStorageValue, "max storage value chars, 0=unlimited") + addOutputFlags(command, &flags.Output) + addPasswordFlags(command, &keychain, "keychain-pw", "macOS keychain password") + addPasswordFlags(command, &firefox, "firefox-pw", "Firefox primary password") command.MarkFlagsMutuallyExclusive("keychain-pw-stdin", "firefox-pw-stdin") return command } -func runDump(ctx context.Context, stderr io.Writer, flags app.DumpFlags, keychain keychainFlags, firefox firefoxFlags) error { +func runDump(ctx context.Context, stderr io.Writer, flags app.DumpFlags, keychain, firefox passwordFlags) error { options, err := app.ValidateDump(flags, keychainPasswordFunc(keychain, os.Stdin), firefoxPasswordFunc(firefox, os.Stdin)) if err != nil { return err } - discovery := app.Discover(ctx, options.Target) - if warnIfNothingFound(stderr, discovery, "no browsers found") { + discovery, summary, err := app.Dump(ctx, options) + if err != nil { + return err + } + if len(discovery.Dirs) == 0 { + printNothingFound(stderr, discovery, "no browsers found") return nil } - summary, err := app.Execute(ctx, discovery, options.Categories, options.Output, options.Keychain, options.FirefoxPassword) printExported(stderr, summary.Exported) if ctx.Err() == nil { printSummary(stderr, summary) } - return err + return summary.Err } diff --git a/cmd/hack-browser-data/dumpkeys.go b/cmd/hack-browser-data/dumpkeys.go index 2173e7c..a7e9762 100644 --- a/cmd/hack-browser-data/dumpkeys.go +++ b/cmd/hack-browser-data/dumpkeys.go @@ -11,9 +11,8 @@ import ( func newDumpKeysCommand() *cobra.Command { var ( - flags app.DumpKeysFlags - keychain keychainFlags - outputPath string + flags app.DumpKeysFlags + keychain passwordFlags ) command := &cobra.Command{ @@ -26,8 +25,7 @@ func newDumpKeysCommand() *cobra.Command { if err != nil { return err } - discovery := app.Discover(command.Context(), options.Target) - vaultCount, err := app.DumpKeys(command.Context(), discovery, options.Keychain, command.OutOrStdout(), outputPath) + vaultCount, err := app.DumpKeys(command.Context(), options, command.OutOrStdout()) if err != nil { return err } @@ -37,8 +35,8 @@ func newDumpKeysCommand() *cobra.Command { } command.Flags().StringVarP(&flags.Browser, "browser", "b", "all", "target browser: all|"+browserNames()) - command.Flags().StringVarP(&outputPath, "output", "o", "", "output file (default: stdout)") - addKeychainPasswordFlags(command, &keychain) + command.Flags().StringVarP(&flags.OutputPath, "output", "o", "", "output file (default: stdout)") + addPasswordFlags(command, &keychain, "keychain-pw", "macOS keychain password") return command } diff --git a/cmd/hack-browser-data/flags.go b/cmd/hack-browser-data/flags.go new file mode 100644 index 0000000..c80f71d --- /dev/null +++ b/cmd/hack-browser-data/flags.go @@ -0,0 +1,15 @@ +package main + +import ( + "github.com/spf13/cobra" + + "github.com/moond4rk/hackbrowserdata/internal/app" +) + +// addOutputFlags defines the output flags once, so dump and restore keep one wording and one set of defaults. +func addOutputFlags(command *cobra.Command, flags *app.OutputFlags) { + command.Flags().StringVarP(&flags.Format, "format", "f", "json", "output format: csv|json|cookie-editor") + command.Flags().StringVarP(&flags.Dir, "dir", "d", "results", "output directory") + command.Flags().BoolVar(&flags.Zip, "zip", false, "compress output to zip") + command.Flags().IntVar(&flags.MaxStorageValue, "max-storage-value", app.DefaultMaxStorageValue, "max storage value chars, 0=unlimited") +} diff --git a/cmd/hack-browser-data/list.go b/cmd/hack-browser-data/list.go index c53b89d..bb39e3b 100644 --- a/cmd/hack-browser-data/list.go +++ b/cmd/hack-browser-data/list.go @@ -16,11 +16,10 @@ func newListCommand() *cobra.Command { Example: ` hack-browser-data list hack-browser-data list --detail`, RunE: func(command *cobra.Command, args []string) error { - options, err := app.ValidateList() + discovery, summary, err := app.List(command.Context(), detail) if err != nil { return err } - discovery := app.Discover(command.Context(), options.Target) for _, failure := range discovery.DirFailures { log.Warnf("%s: %v", failure.UserDataDirID, failure.Err) } @@ -31,10 +30,6 @@ func newListCommand() *cobra.Command { if !detail { return printProfiles(command.OutOrStdout(), discovery) } - summary, err := app.CountRecords(command.Context(), discovery, options.Keychain) - if err != nil { - return err - } return printRecordCounts(command.OutOrStdout(), discovery, summary) }, } diff --git a/cmd/hack-browser-data/password.go b/cmd/hack-browser-data/password.go index 6ab3c57..3285bef 100644 --- a/cmd/hack-browser-data/password.go +++ b/cmd/hack-browser-data/password.go @@ -13,39 +13,27 @@ import ( "golang.org/x/term" ) -// keychainFlags say where the macOS login keychain password comes from; both empty means a prompt, or none. -type keychainFlags struct { - password string - passwordStdin bool +// passwordFlags say where one password comes from: its flag, or the first line of stdin. Neither leaves it to the +// password's own default: a prompt or nothing for the keychain, the empty password for Firefox. +type passwordFlags struct { + value string + fromStdin bool } -func addKeychainPasswordFlags(command *cobra.Command, flags *keychainFlags) { - command.Flags().StringVar(&flags.password, "keychain-pw", "", "macOS keychain password (visible in ps and shell history)") - command.Flags().BoolVar(&flags.passwordStdin, "keychain-pw-stdin", false, - "read the macOS keychain password from the first line of stdin") - command.MarkFlagsMutuallyExclusive("keychain-pw", "keychain-pw-stdin") -} - -// firefoxFlags say where a Firefox primary password comes from; both empty means the empty password, the common case. -type firefoxFlags struct { - password string - passwordStdin bool -} - -func addFirefoxPasswordFlags(command *cobra.Command, flags *firefoxFlags) { - command.Flags().StringVar(&flags.password, "firefox-pw", "", "Firefox primary password (visible in ps and shell history)") - command.Flags().BoolVar(&flags.passwordStdin, "firefox-pw-stdin", false, - "read the Firefox primary password from the first line of stdin") - command.MarkFlagsMutuallyExclusive("firefox-pw", "firefox-pw-stdin") +// addPasswordFlags defines -- and ---stdin for the password that what names. +func addPasswordFlags(command *cobra.Command, flags *passwordFlags, name, what string) { + command.Flags().StringVar(&flags.value, name, "", what+" (visible in ps and shell history)") + command.Flags().BoolVar(&flags.fromStdin, name+"-stdin", false, "read the "+what+" from the first line of stdin") + command.MarkFlagsMutuallyExclusive(name, name+"-stdin") } // firefoxPasswordFunc reads a flag, else the first line of stdin; nil means the empty password, and it never prompts. -func firefoxPasswordFunc(flags firefoxFlags, stdin *os.File) func(context.Context) (string, error) { +func firefoxPasswordFunc(flags passwordFlags, stdin *os.File) func(context.Context) (string, error) { switch { - case flags.password != "": - password := flags.password + case flags.value != "": + password := flags.value return func(context.Context) (string, error) { return password, nil } - case flags.passwordStdin: + case flags.fromStdin: return func(ctx context.Context) (string, error) { return untilDone(ctx, func() (string, error) { return firstLine(stdin) }) } @@ -57,12 +45,12 @@ func firefoxPasswordFunc(flags firefoxFlags, stdin *os.File) func(context.Contex var errNoPassword = errors.New("macOS login password not given and stdin is not a terminal") // keychainPasswordFunc is read lazily: a flag, else a no-echo prompt on a terminal, else none (metadata only). -func keychainPasswordFunc(flags keychainFlags, stdin *os.File) func(context.Context) (string, error) { +func keychainPasswordFunc(flags passwordFlags, stdin *os.File) func(context.Context) (string, error) { switch { - case flags.password != "": - password := flags.password + case flags.value != "": + password := flags.value return func(context.Context) (string, error) { return password, nil } - case flags.passwordStdin: + case flags.fromStdin: return func(ctx context.Context) (string, error) { return untilDone(ctx, func() (string, error) { return firstLine(stdin) }) } diff --git a/cmd/hack-browser-data/password_test.go b/cmd/hack-browser-data/password_test.go index a9a89ff..09b3962 100644 --- a/cmd/hack-browser-data/password_test.go +++ b/cmd/hack-browser-data/password_test.go @@ -14,14 +14,14 @@ import ( func TestKeychainPasswordFunc(t *testing.T) { tests := []struct { name string - flags keychainFlags + flags passwordFlags stdin string want string err error }{ - {name: "flag", flags: keychainFlags{password: "from-flag"}, stdin: "from-stdin\n", want: "from-flag"}, - {name: "stdin", flags: keychainFlags{passwordStdin: true}, stdin: "from-stdin\r\nnext\n", want: "from-stdin"}, - {name: "stdin without newline", flags: keychainFlags{passwordStdin: true}, stdin: "from-stdin", want: "from-stdin"}, + {name: "flag", flags: passwordFlags{value: "from-flag"}, stdin: "from-stdin\n", want: "from-flag"}, + {name: "stdin", flags: passwordFlags{fromStdin: true}, stdin: "from-stdin\r\nnext\n", want: "from-stdin"}, + {name: "stdin without newline", flags: passwordFlags{fromStdin: true}, stdin: "from-stdin", want: "from-stdin"}, {name: "none off a terminal", stdin: "from-stdin\n", err: errNoPassword}, } for _, testCase := range tests { diff --git a/cmd/hack-browser-data/report.go b/cmd/hack-browser-data/report.go index 2d10fb3..8b18b03 100644 --- a/cmd/hack-browser-data/report.go +++ b/cmd/hack-browser-data/report.go @@ -39,16 +39,12 @@ func resultDetail(result app.Result) string { } } -// warnIfNothingFound reports discovery's failures, the likely reason it found no browser data, then says so. -func warnIfNothingFound(stderr io.Writer, discovery app.Discovery, message string) bool { - if len(discovery.Dirs) > 0 { - return false - } +// printNothingFound lists discovery's failures first, the likely reason a run found no browser data. +func printNothingFound(stderr io.Writer, discovery app.Discovery, message string) { if len(discovery.DirFailures) > 0 { printSummary(stderr, app.Summary{DirFailures: discovery.DirFailures}) } log.Warnf("%s", message) - return true } // printExported lists the files a run wrote, even when it stopped early, so the user finds what it left. diff --git a/cmd/hack-browser-data/restore.go b/cmd/hack-browser-data/restore.go index d0e127b..bab0c66 100644 --- a/cmd/hack-browser-data/restore.go +++ b/cmd/hack-browser-data/restore.go @@ -2,15 +2,12 @@ package main import ( "context" - "fmt" "io" "os" "github.com/spf13/cobra" "github.com/moond4rk/hackbrowserdata/internal/app" - "github.com/moond4rk/hackbrowserdata/internal/log" - "github.com/moond4rk/hackbrowserdata/internal/snapshot" ) func newRestoreCommand() *cobra.Command { @@ -33,10 +30,7 @@ func newRestoreCommand() *cobra.Command { command.Flags().StringVar(&flags.DataZip, "data-zip", "", "zip produced by the archive command (alternative to --data-dir)") command.Flags().StringVarP(&flags.Browser, "browser", "b", "", "restore only this browser (optional; must match a vault in --keys)") command.Flags().StringVarP(&flags.Categories, "category", "c", "all", "data categories (comma-separated): all|"+categoryNames()) - command.Flags().StringVarP(&flags.Format, "format", "f", "json", "output format: csv|json|cookie-editor") - command.Flags().StringVarP(&flags.OutputDir, "dir", "d", "results", "output directory") - command.Flags().BoolVar(&flags.Zip, "zip", false, "compress output to zip") - command.Flags().IntVar(&flags.MaxStorageValue, "max-storage-value", app.DefaultMaxStorageValue, "max storage value chars, 0=unlimited") + addOutputFlags(command, &flags.Output) _ = command.MarkFlagRequired("keys") command.MarkFlagsMutuallyExclusive("data-dir", "data-zip") @@ -49,40 +43,17 @@ func runRestore(ctx context.Context, stderr io.Writer, flags app.RestoreFlags) e if err != nil { return err } - root, err := openRestoreData(flags.DataDir, flags.DataZip) + discovery, summary, err := app.Restore(ctx, options) if err != nil { return err } - defer closeRestoreData(root) - - discovery, keys, err := app.DiscoverRestore(root.Path(), options.Vaults) - if err != nil { - return err - } - if warnIfNothingFound(stderr, discovery, "no browsers to restore from the supplied keys and data") { + if len(discovery.Dirs) == 0 { + printNothingFound(stderr, discovery, "no browsers to restore from the supplied keys and data") return nil } - summary, err := app.ExecuteWithKeys(ctx, discovery, options.Categories, options.Output, keys) printExported(stderr, summary.Exported) if ctx.Err() == nil { printSummary(stderr, summary) } - return err -} - -func openRestoreData(dataDir, dataZip string) (*snapshot.Root, error) { - if dataDir != "" { - return snapshot.Open(dataDir), nil - } - root, err := snapshot.Unzip(dataZip) - if err != nil { - return nil, fmt.Errorf("extract %s: %w", dataZip, err) - } - return root, nil -} - -func closeRestoreData(root *snapshot.Root) { - if err := root.Close(); err != nil { - log.Warnf("restore: %v", err) - } + return summary.Err } diff --git a/cmd/hack-browser-data/restore_test.go b/cmd/hack-browser-data/restore_test.go index 1a9aaaa..751de54 100644 --- a/cmd/hack-browser-data/restore_test.go +++ b/cmd/hack-browser-data/restore_test.go @@ -14,15 +14,15 @@ import ( "github.com/moond4rk/hackbrowserdata/internal/app" "github.com/moond4rk/hackbrowserdata/internal/browser/browsertest" "github.com/moond4rk/hackbrowserdata/internal/masterkey" - "github.com/moond4rk/hackbrowserdata/internal/snapshot" ) -// TestRunRestore_FromADataZip goes through the command's own unzip, which the app restore goldens do themselves. -func TestRunRestore_FromADataZip(t *testing.T) { +// restoreFixture zips the Chromium fixture dirs and writes the key file that opens them. +func restoreFixture(t *testing.T) (keys, dataZip string) { + t.Helper() userData := t.TempDir() browsertest.WriteChromiumUserDataDirs(t, userData) - dataZip := filepath.Join(t.TempDir(), "data.zip") - require.NoError(t, snapshot.Open(userData).Zip(dataZip)) + dataZip = filepath.Join(t.TempDir(), "data.zip") + browsertest.ZipDir(t, userData, dataZip) keyFile := masterkey.NewKeyFile() for _, fixture := range browsertest.ChromiumUserDataDirs { @@ -30,13 +30,27 @@ func TestRunRestore_FromADataZip(t *testing.T) { } var buffer bytes.Buffer require.NoError(t, keyFile.Write(&buffer)) - keys := filepath.Join(t.TempDir(), "keys.json") + keys = filepath.Join(t.TempDir(), "keys.json") require.NoError(t, os.WriteFile(keys, buffer.Bytes(), 0o600)) + return keys, dataZip +} +// TestRunRestore_FromADataZip runs the command from its flags to the file it writes. +func TestRunRestore_FromADataZip(t *testing.T) { + keys, dataZip := restoreFixture(t) out := t.TempDir() - flags := app.RestoreFlags{KeyFile: keys, DataZip: dataZip, Categories: "cookie", Format: "json", OutputDir: out} + flags := app.RestoreFlags{KeyFile: keys, DataZip: dataZip, Categories: "cookie", Output: app.OutputFlags{Format: "json", Dir: out}} require.NoError(t, runRestore(context.Background(), io.Discard, flags)) cookies, err := os.ReadFile(filepath.Join(out, "cookie.json")) require.NoError(t, err) assert.Contains(t, string(cookies), `"value": "theme=dark"`) } + +// TestRunRestore_FailsWhenItsOutputCannotBeWritten: the run's own error is in its Summary, which the command returns. +func TestRunRestore_FailsWhenItsOutputCannotBeWritten(t *testing.T) { + keys, dataZip := restoreFixture(t) + out := t.TempDir() + require.NoError(t, os.Mkdir(filepath.Join(out, "cookie.json"), 0o700)) + flags := app.RestoreFlags{KeyFile: keys, DataZip: dataZip, Categories: "cookie", Output: app.OutputFlags{Format: "json", Dir: out}} + require.Error(t, runRestore(context.Background(), io.Discard, flags)) +} diff --git a/internal/app/archive.go b/internal/app/archive.go index 079142f..53cfa23 100644 --- a/internal/app/archive.go +++ b/internal/app/archive.go @@ -16,11 +16,14 @@ import ( type ArchiveFlags struct { Browser string Categories string + OutputPath string } type ArchiveOptions struct { - Target Target - Categories []category.Category + Browsers []browser.Browser + HomeDir string + Categories []category.Category + ArchivePath string } // ValidateArchive takes no keychain password: an archive copies files and reads no secret. @@ -37,35 +40,39 @@ func ValidateArchive(flags ArchiveFlags) (ArchiveOptions, error) { if err != nil { return ArchiveOptions{}, err } - return ArchiveOptions{Target: Target{Browsers: browsers, HomeDir: home}, Categories: categories}, nil + return ArchiveOptions{Browsers: browsers, HomeDir: home, Categories: categories, ArchivePath: flags.OutputPath}, nil } -// Archive returns how many files and directories it captured. -func Archive(ctx context.Context, discovery Discovery, categories []category.Category, archivePath string) (int, error) { +// Archive returns how many files and directories it captured. A Discovery without dirs means nothing was written. +func Archive(ctx context.Context, options ArchiveOptions) (Discovery, int, error) { + discovery := discover(ctx, options.Browsers, browser.DiscoverOptions{HomeDir: options.HomeDir}) + if len(discovery.Dirs) == 0 { + return discovery, 0, nil + } var dirs []browser.UserDataDir for _, dir := range discovery.Dirs { if dir.Browser.Base == browser.Chromium { dirs = append(dirs, dir) } } - files := archiveFiles(dirs, categories) + files := archiveFiles(dirs, options.Categories) root, err := snapshot.NewRoot() if err != nil { - return 0, err + return Discovery{}, 0, err } defer closeRoot(root) if err := root.Capture(ctx, files); err != nil { - return 0, err + return Discovery{}, 0, err } fileCount := countCaptured(root, files) if fileCount == 0 { - return 0, errors.New("no decryption-relevant files found to archive") + return Discovery{}, 0, errors.New("no decryption-relevant files found to archive") } - if err := root.Zip(archivePath); err != nil { - return 0, fmt.Errorf("write archive %s: %w", archivePath, err) + if err := root.Zip(options.ArchivePath); err != nil { + return Discovery{}, 0, fmt.Errorf("write archive %s: %w", options.ArchivePath, err) } - return fileCount, nil + return discovery, fileCount, nil } func archiveFiles(dirs []browser.UserDataDir, categories []category.Category) []snapshot.File { diff --git a/internal/app/archive_golden_test.go b/internal/app/archive_golden_test.go index d764871..9636539 100644 --- a/internal/app/archive_golden_test.go +++ b/internal/app/archive_golden_test.go @@ -93,11 +93,11 @@ func TestArchiveGolden(t *testing.T) { for _, testCase := range tests { testCase := testCase t.Run(testCase.name, func(t *testing.T) { - options, err := app.ValidateArchive(app.ArchiveFlags{Browser: archiveLayoutBrowser, Categories: testCase.categories}) - require.NoError(t, err) - options.Target.HomeDir = home out := filepath.Join(t.TempDir(), "browser-data.zip") - fileCount, err := app.Archive(context.Background(), app.Discover(context.Background(), options.Target), options.Categories, out) + options, err := app.ValidateArchive(app.ArchiveFlags{Browser: archiveLayoutBrowser, Categories: testCase.categories, OutputPath: out}) + require.NoError(t, err) + options.HomeDir = home + _, fileCount, err := app.Archive(context.Background(), options) require.NoError(t, err) golden.Assert(t, testCase.golden, fmt.Sprintf("entries=%d\n", fileCount)+golden.ZipListing(t, out)) diff --git a/internal/app/discover.go b/internal/app/discover.go index b3b1ca5..545a681 100644 --- a/internal/app/discover.go +++ b/internal/app/discover.go @@ -16,11 +16,10 @@ type Discovery struct { DirFailures []DirFailure } -func Discover(ctx context.Context, target Target) Discovery { +func discover(ctx context.Context, browsers []browser.Browser, options browser.DiscoverOptions) Discovery { var discovery Discovery - discoverOptions := browser.DiscoverOptions{HomeDir: target.HomeDir, ProfilePath: target.ProfilePath} - for _, targetBrowser := range target.Browsers { - dirs, err := discover(ctx, targetBrowser, discoverOptions) + for _, targetBrowser := range browsers { + dirs, err := discoverBrowser(ctx, targetBrowser, options) discovery.Dirs = append(discovery.Dirs, withData(dirs)...) if err != nil { discovery.DirFailures = append(discovery.DirFailures, DirFailure{UserDataDirID: browser.UserDataDirID(targetBrowser.Name), Err: err}) @@ -59,8 +58,8 @@ func hasData(dir browser.UserDataDir, profile browser.Profile, extractors browse return false } -// discover dispatches on Base here, not in browser, because the base packages import browser. -func discover(ctx context.Context, targetBrowser browser.Browser, options browser.DiscoverOptions) ([]browser.UserDataDir, error) { +// discoverBrowser dispatches on Base here, not in browser, because the base packages import browser. +func discoverBrowser(ctx context.Context, targetBrowser browser.Browser, options browser.DiscoverOptions) ([]browser.UserDataDir, error) { switch targetBrowser.Base { case browser.Chromium: return chromium.Discover(targetBrowser, options) @@ -73,7 +72,7 @@ func discover(ctx context.Context, targetBrowser browser.Browser, options browse } } -// extractorsFor dispatches on Base here for the same reason discover does. +// extractorsFor dispatches on Base here for the same reason discoverBrowser does. func extractorsFor(dir browser.UserDataDir, profile browser.Profile) browser.Extractors { switch dir.Browser.Base { case browser.Chromium: diff --git a/internal/app/discover_test.go b/internal/app/discover_test.go index 4ea43ab..e6c2fa6 100644 --- a/internal/app/discover_test.go +++ b/internal/app/discover_test.go @@ -36,11 +36,11 @@ func TestDiscover(t *testing.T) { writeUserDataDir(t, home, "edge") writeUserDataDir(t, home, "chrome") - discovery := Discover(context.Background(), Target{Browsers: browser.Supported(), HomeDir: home}) + discovery := discover(context.Background(), browser.Supported(), browser.DiscoverOptions{HomeDir: home}) assert.Equal(t, []browser.UserDataDirID{"chrome", "edge"}, userDataDirIDs(discovery), "table order, which is output order") assert.Empty(t, discovery.DirFailures) - discovery = Discover(context.Background(), Target{Browsers: []browser.Browser{lookup(t, "edge")}, HomeDir: home}) + discovery = discover(context.Background(), []browser.Browser{lookup(t, "edge")}, browser.DiscoverOptions{HomeDir: home}) assert.Equal(t, []browser.UserDataDirID{"edge"}, userDataDirIDs(discovery)) } @@ -51,7 +51,7 @@ func TestDiscover_ProfilePath(t *testing.T) { touch(t, custom, "Default", "Preferences") touch(t, custom, "Default", "History") - discovery := Discover(context.Background(), Target{Browsers: []browser.Browser{lookup(t, "chrome")}, HomeDir: home, ProfilePath: custom}) + discovery := discover(context.Background(), []browser.Browser{lookup(t, "chrome")}, browser.DiscoverOptions{HomeDir: home, ProfilePath: custom}) require.Len(t, discovery.Dirs, 1) assert.Equal(t, custom, discovery.Dirs[0].Path) } @@ -64,7 +64,7 @@ func TestDiscover_KeepsOnlyProfilesWithData(t *testing.T) { edge := writeUserDataDir(t, home, "edge") require.NoError(t, os.Remove(filepath.Join(edge, "Default", "History"))) - discovery := Discover(context.Background(), Target{Browsers: browser.Supported(), HomeDir: home}) + discovery := discover(context.Background(), browser.Supported(), browser.DiscoverOptions{HomeDir: home}) assert.Equal(t, []browser.UserDataDirID{"chrome"}, userDataDirIDs(discovery)) assert.Equal(t, []browser.Profile{{Name: "Default", RelativePath: "Default"}}, discovery.Dirs[0].Profiles) } @@ -78,7 +78,7 @@ func TestDiscover_UnreadableDirIsReported(t *testing.T) { require.NoError(t, os.Chmod(path, 0o000)) defer func() { require.NoError(t, os.Chmod(path, 0o700)) }() - discovery := Discover(context.Background(), Target{Browsers: browser.Supported(), HomeDir: home}) + discovery := discover(context.Background(), browser.Supported(), browser.DiscoverOptions{HomeDir: home}) assert.Empty(t, discovery.Dirs) require.Len(t, discovery.DirFailures, 1) assert.Equal(t, browser.UserDataDirID("chrome"), discovery.DirFailures[0].UserDataDirID) diff --git a/internal/app/doc.go b/internal/app/doc.go index 5b81a1c..c69544a 100644 --- a/internal/app/doc.go +++ b/internal/app/doc.go @@ -1,2 +1,3 @@ -// Package app runs the pipeline's stages: the Validate functions, Discover, and Execute; cmd prints what they return. +// Package app runs the commands: each has a Validate function, which turns its flags into options, and a function +// named for it; cmd prints what they return. package app diff --git a/internal/app/dump.go b/internal/app/dump.go index 482ae5c..5d56367 100644 --- a/internal/app/dump.go +++ b/internal/app/dump.go @@ -7,27 +7,26 @@ import ( "github.com/moond4rk/hackbrowserdata/internal/browser" "github.com/moond4rk/hackbrowserdata/internal/category" - "github.com/moond4rk/hackbrowserdata/internal/export" "github.com/moond4rk/hackbrowserdata/internal/log" ) // DumpFlags are dump's command-line values as cobra parsed them. type DumpFlags struct { - Browser string - Categories string - Format string - OutputDir string - Zip bool - ProfilePath string - MaxStorageValue int + Browser string + ProfilePath string + Categories string + Output OutputFlags } type DumpOptions struct { - Target Target - Categories []category.Category - Output OutputOptions - Keychain KeychainConfig - FirefoxPassword func(ctx context.Context) (string, error) // nil is the empty password; Execute reads it at most once + Browsers []browser.Browser + ProfilePath string // -p, for the one browser in Browsers; "" when not given + HomeDir string + Categories []category.Category + Output OutputOptions + // KeychainPassword runs when the macOS login keychain first opens; "" leaves it locked. Other systems ignore it. + KeychainPassword func(ctx context.Context) (string, error) + FirefoxPassword func(ctx context.Context) (string, error) // nil is the empty password; Dump reads it at most once } // ValidateDump checks every flag before any browser or secret is touched, so a bad flag fails fast. @@ -40,30 +39,26 @@ func ValidateDump(flags DumpFlags, keychainPassword, firefoxPassword func(contex if err != nil { return DumpOptions{}, err } - format, err := export.ParseFormat(flags.Format) + output, err := parseOutput(flags.Output) if err != nil { return DumpOptions{}, err } - if err := checkOutputDir(flags.OutputDir); err != nil { - return DumpOptions{}, err - } profilePath, err := checkProfilePath(browsers, flags.ProfilePath) if err != nil { return DumpOptions{}, err } - if err := checkMaxStorageValue(flags.MaxStorageValue); err != nil { - return DumpOptions{}, err - } home, err := homeDir() if err != nil { return DumpOptions{}, err } return DumpOptions{ - Target: Target{Browsers: browsers, ProfilePath: profilePath, HomeDir: home}, - Categories: categories, - Output: OutputOptions{Dir: flags.OutputDir, Format: format, Zip: flags.Zip, MaxStorageValue: flags.MaxStorageValue}, - Keychain: KeychainConfig{Path: loginKeychainPath(home), Password: keychainPassword}, - FirefoxPassword: firefoxPassword, + Browsers: browsers, + ProfilePath: profilePath, + HomeDir: home, + Categories: categories, + Output: output, + KeychainPassword: keychainPassword, + FirefoxPassword: firefoxPassword, }, nil } @@ -85,3 +80,22 @@ func checkProfilePath(browsers []browser.Browser, path string) (string, error) { } return path, nil } + +// Dump exports what it finds on this machine, retrieving each dir's master keys here. A Discovery without dirs means +// nothing ran and nothing was created. An error means the run never started; one that ended early sets Summary.Err. +func Dump(ctx context.Context, options DumpOptions) (Discovery, Summary, error) { + discovery := discover(ctx, options.Browsers, browser.DiscoverOptions{HomeDir: options.HomeDir, ProfilePath: options.ProfilePath}) + if len(discovery.Dirs) == 0 { + return discovery, Summary{}, nil + } + keychain := newLoginKeychain(loginKeychainPath(options.HomeDir), options.KeychainPassword) + fromMachine := runner{ + extractors: extractorsFor, retrieveKeys: keysFromMachine(keychain), loginKeychain: keychain, + firefoxPassword: readOnce(options.FirefoxPassword), + } + summary, err := execute(ctx, discovery, options.Categories, options.Output, fromMachine) + if err != nil { + return Discovery{}, Summary{}, err + } + return discovery, summary, nil +} diff --git a/internal/app/dump_test.go b/internal/app/dump_test.go index 3ed6fc1..f0e12cd 100644 --- a/internal/app/dump_test.go +++ b/internal/app/dump_test.go @@ -19,26 +19,25 @@ import ( func TestValidateDump(t *testing.T) { keychainPassword := func(context.Context) (string, error) { return "pw", nil } firefoxPassword := func(context.Context) (string, error) { return "ff", nil } - options, err := ValidateDump(DumpFlags{Browser: " Chrome ", Categories: "cookie, password,cookie", Format: "csv", OutputDir: "out"}, + options, err := ValidateDump(DumpFlags{Browser: " Chrome ", Categories: "cookie, password,cookie", Output: OutputFlags{Format: "csv", Dir: "out"}}, keychainPassword, firefoxPassword) require.NoError(t, err) - require.Len(t, options.Target.Browsers, 1) - assert.Equal(t, "chrome", options.Target.Browsers[0].Name) + require.Len(t, options.Browsers, 1) + assert.Equal(t, "chrome", options.Browsers[0].Name) assert.Equal(t, []category.Category{category.Cookie, category.Password}, options.Categories, "a repeated category runs once") assert.Equal(t, export.CSV, options.Output.Format) assert.Equal(t, "out", options.Output.Dir) - assert.NotEmpty(t, options.Target.HomeDir) - assert.Equal(t, loginKeychainPath(options.Target.HomeDir), options.Keychain.Path) - password, err := options.Keychain.Password(context.Background()) + assert.NotEmpty(t, options.HomeDir) + password, err := options.KeychainPassword(context.Background()) require.NoError(t, err) assert.Equal(t, "pw", password) password, err = options.FirefoxPassword(context.Background()) require.NoError(t, err) assert.Equal(t, "ff", password) - options, err = ValidateDump(DumpFlags{Categories: "all", Format: "json"}, nil, nil) + options, err = ValidateDump(DumpFlags{Categories: "all", Output: OutputFlags{Format: "json"}}, nil, nil) require.NoError(t, err) - assert.Equal(t, browser.Supported(), options.Target.Browsers) + assert.Equal(t, browser.Supported(), options.Browsers) assert.Equal(t, category.All(), options.Categories) } @@ -47,15 +46,15 @@ func TestValidateDump_RejectsBadInput(t *testing.T) { require.NoError(t, os.WriteFile(file, nil, 0o600)) missing := filepath.Join(t.TempDir(), "missing") for name, flags := range map[string]DumpFlags{ - "unknown category": {Categories: "cookie,bogus", Format: "json"}, - "no category": {Categories: " , ", Format: "json"}, - "unknown format": {Categories: "all", Format: "xml"}, - "unknown browser": {Browser: "bogus", Categories: "all", Format: "json"}, - "another OS's browser": {Browser: otherOSBrowser(t), Categories: "all", Format: "json"}, - "-d below a file": {Categories: "all", Format: "json", OutputDir: filepath.Join(file, "out")}, - "-d is a file": {Categories: "all", Format: "json", OutputDir: file}, - "missing -p": {Browser: "chrome", Categories: "all", Format: "json", ProfilePath: missing}, - "-p is a file": {Browser: "chrome", Categories: "all", Format: "json", ProfilePath: file}, + "unknown category": {Categories: "cookie,bogus", Output: OutputFlags{Format: "json"}}, + "no category": {Categories: " , ", Output: OutputFlags{Format: "json"}}, + "unknown format": {Categories: "all", Output: OutputFlags{Format: "xml"}}, + "unknown browser": {Browser: "bogus", Categories: "all", Output: OutputFlags{Format: "json"}}, + "another OS's browser": {Browser: otherOSBrowser(t), Categories: "all", Output: OutputFlags{Format: "json"}}, + "-d below a file": {Categories: "all", Output: OutputFlags{Format: "json", Dir: filepath.Join(file, "out")}}, + "-d is a file": {Categories: "all", Output: OutputFlags{Format: "json", Dir: file}}, + "missing -p": {Browser: "chrome", Categories: "all", Output: OutputFlags{Format: "json"}, ProfilePath: missing}, + "-p is a file": {Browser: "chrome", Categories: "all", Output: OutputFlags{Format: "json"}, ProfilePath: file}, } { _, err := ValidateDump(flags, nil, nil) assert.Error(t, err, name) @@ -77,7 +76,7 @@ func otherOSBrowser(t *testing.T) string { func TestValidateDump_CreatesNoOutputDir(t *testing.T) { root := t.TempDir() - options, err := ValidateDump(DumpFlags{Categories: "all", Format: "json", OutputDir: filepath.Join(root, "a", "b")}, nil, nil) + options, err := ValidateDump(DumpFlags{Categories: "all", Output: OutputFlags{Format: "json", Dir: filepath.Join(root, "a", "b")}}, nil, nil) require.NoError(t, err) assert.Equal(t, filepath.Join(root, "a", "b"), options.Output.Dir) _, err = os.Stat(filepath.Join(root, "a")) @@ -92,13 +91,13 @@ func TestValidateDump_RejectsAnOutputDirThatCannotBeCreated(t *testing.T) { require.NoError(t, os.Chmod(root, 0o500)) defer func() { require.NoError(t, os.Chmod(root, 0o700)) }() - _, err := ValidateDump(DumpFlags{Categories: "all", Format: "json", OutputDir: filepath.Join(root, "results")}, nil, nil) + _, err := ValidateDump(DumpFlags{Categories: "all", Output: OutputFlags{Format: "json", Dir: filepath.Join(root, "results")}}, nil, nil) assert.Error(t, err) } // TestValidateDump_IgnoresProfilePathForAllBrowsers keeps -p without a named browser ignored, as before; it is reported. func TestValidateDump_IgnoresProfilePathForAllBrowsers(t *testing.T) { - options, err := ValidateDump(DumpFlags{Browser: "all", Categories: "all", Format: "json", ProfilePath: filepath.Join(t.TempDir(), "missing")}, nil, nil) + options, err := ValidateDump(DumpFlags{Browser: "all", Categories: "all", Output: OutputFlags{Format: "json"}, ProfilePath: filepath.Join(t.TempDir(), "missing")}, nil, nil) require.NoError(t, err) - assert.Empty(t, options.Target.ProfilePath) + assert.Empty(t, options.ProfilePath) } diff --git a/internal/app/dumpkeys.go b/internal/app/dumpkeys.go index 6925154..6f691d2 100644 --- a/internal/app/dumpkeys.go +++ b/internal/app/dumpkeys.go @@ -15,12 +15,16 @@ import ( // DumpKeysFlags are dumpkeys' command-line values as cobra parsed them. type DumpKeysFlags struct { - Browser string + Browser string + OutputPath string } type DumpKeysOptions struct { - Target Target - Keychain KeychainConfig + Browsers []browser.Browser + HomeDir string + OutputPath string // "" writes the key file to stdout + // KeychainPassword runs when the macOS login keychain first opens; "" leaves it locked. Other systems ignore it. + KeychainPassword func(ctx context.Context) (string, error) } func ValidateDumpKeys(flags DumpKeysFlags, keychainPassword func(context.Context) (string, error)) (DumpKeysOptions, error) { @@ -32,23 +36,22 @@ func ValidateDumpKeys(flags DumpKeysFlags, keychainPassword func(context.Context if err != nil { return DumpKeysOptions{}, err } - return DumpKeysOptions{ - Target: Target{Browsers: browsers, HomeDir: home}, - Keychain: KeychainConfig{Path: loginKeychainPath(home), Password: keychainPassword}, - }, nil + return DumpKeysOptions{Browsers: browsers, HomeDir: home, OutputPath: flags.OutputPath, KeychainPassword: keychainPassword}, nil } -// DumpKeys writes the key file to outputPath, or to stdout when it is "", and returns how many vaults it holds. +// DumpKeys writes the key file to options.OutputPath, or to stdout when it is "", and returns how many vaults it holds. // A stopped run writes nothing, so it never leaves a keys.json behind. -func DumpKeys(ctx context.Context, discovery Discovery, keychainConfig KeychainConfig, stdout io.Writer, outputPath string) (int, error) { - keyFile, err := dumpKeysWith(ctx, discovery, keysFromMachine(newLoginKeychain(keychainConfig))) +func DumpKeys(ctx context.Context, options DumpKeysOptions, stdout io.Writer) (int, error) { + discovery := discover(ctx, options.Browsers, browser.DiscoverOptions{HomeDir: options.HomeDir}) + keychain := newLoginKeychain(loginKeychainPath(options.HomeDir), options.KeychainPassword) + keyFile, err := dumpKeysWith(ctx, discovery, keysFromMachine(keychain)) if err != nil { return 0, err } - if outputPath == "" { + if options.OutputPath == "" { return len(keyFile.Vaults), keyFile.Write(stdout) } - if err := writeKeyFile(outputPath, keyFile); err != nil { + if err := writeKeyFile(options.OutputPath, keyFile); err != nil { return 0, err } return len(keyFile.Vaults), nil diff --git a/internal/app/execute.go b/internal/app/execute.go index 070f9de..0926d5a 100644 --- a/internal/app/execute.go +++ b/internal/app/execute.go @@ -10,26 +10,10 @@ import ( "github.com/moond4rk/hackbrowserdata/internal/category" "github.com/moond4rk/hackbrowserdata/internal/export" "github.com/moond4rk/hackbrowserdata/internal/log" - "github.com/moond4rk/hackbrowserdata/internal/masterkey" "github.com/moond4rk/hackbrowserdata/internal/record" "github.com/moond4rk/hackbrowserdata/internal/snapshot" ) -// Execute retrieves each dir's master keys from this machine. It handles one user data dir at a time, so at most one -// dir's copies are on disk. Its error means the run ended early; like os.ReadDir, it still returns the Summary of what -// ran before it, with the files written, so the caller can list them. -func Execute( - ctx context.Context, discovery Discovery, categories []category.Category, output OutputOptions, keychainConfig KeychainConfig, - firefoxPassword func(context.Context) (string, error), -) (Summary, error) { - keychain := newLoginKeychain(keychainConfig) - fromMachine := runner{ - extractors: extractorsFor, retrieveKeys: keysFromMachine(keychain), loginKeychain: keychain, - firefoxPassword: readOnce(firefoxPassword), - } - return execute(ctx, discovery, categories, output, fromMachine) -} - // readOnce lets every protected Firefox profile of a run share one read, since stdin yields the password only once. func readOnce(read func(context.Context) (string, error)) func(context.Context) (string, error) { if read == nil { @@ -44,15 +28,8 @@ func readOnce(read func(context.Context) (string, error)) func(context.Context) } } -// ExecuteWithKeys decrypts with a key file's keys, so it never reads this machine's secret stores. -func ExecuteWithKeys( - ctx context.Context, discovery Discovery, categories []category.Category, output OutputOptions, - keys map[browser.UserDataDirID]masterkey.Set, -) (Summary, error) { - return execute(ctx, discovery, categories, output, runner{extractors: extractorsFor, retrieveKeys: keysFromFile(keys)}) -} - // execute gives the runner the run's writer; the caller sets where its extractors, keys, and keychain come from. +// It handles one user data dir at a time, so at most one dir's copies are on disk. Its error means nothing ran. func execute( ctx context.Context, discovery Discovery, categories []category.Category, output OutputOptions, dirRunner runner, ) (Summary, error) { @@ -62,10 +39,10 @@ func execute( } dirRunner.writer, dirRunner.logProgress = writer, true dirRunner.maxStorageValue = output.MaxStorageValue - summary, err := dirRunner.run(ctx, discovery, categories) - closeErr := writer.Close() + summary := dirRunner.run(ctx, discovery, categories) + summary.Err = errors.Join(summary.Err, writer.Close()) summary.Exported = writer.Listing() - return summary, errors.Join(err, closeErr) + return summary, nil } type recordWriter interface { @@ -83,27 +60,30 @@ type runner struct { maxStorageValue int } -func (r runner) run(ctx context.Context, discovery Discovery, categories []category.Category) (Summary, error) { +// run ends at the first dir whose Summary has an Err, keeping what ran before it. +func (r runner) run(ctx context.Context, discovery Discovery, categories []category.Category) Summary { summary := Summary{DirFailures: append([]DirFailure(nil), discovery.DirFailures...)} for _, dir := range discovery.Dirs { if err := ctx.Err(); err != nil { - return summary, fmt.Errorf("run stopped: %w", err) + summary.Err = fmt.Errorf("run stopped: %w", err) + return summary } - dirSummary, err := r.runDir(ctx, dir, categories) + dirSummary := r.runDir(ctx, dir, categories) summary.Results = append(summary.Results, dirSummary.Results...) summary.DirFailures = append(summary.DirFailures, dirSummary.DirFailures...) - if err != nil { - return summary, err + if dirSummary.Err != nil { + summary.Err = dirSummary.Err + return summary } } - return summary, nil + return summary } -func (r runner) runDir(ctx context.Context, dir browser.UserDataDir, categories []category.Category) (Summary, error) { +func (r runner) runDir(ctx context.Context, dir browser.UserDataDir, categories []category.Category) Summary { plan := planDir(dir, categories, r.extractors) summary := Summary{Results: plan.skipped} if len(plan.extractions) == 0 { - return summary, nil + return summary } if r.logProgress { log.Infof("Extracting %s...", dir.Browser.DisplayName) @@ -112,15 +92,16 @@ func (r runner) runDir(ctx context.Context, dir browser.UserDataDir, categories root, err := snapshot.NewRoot() if err != nil { summary.Results = append(summary.Results, failAll(plan.extractions, err)...) - return summary, nil + return summary } defer closeRoot(root) if err := root.Capture(ctx, plan.files); err != nil { if ctx.Err() != nil { - return summary, err + summary.Err = err + return summary } summary.Results = append(summary.Results, failAll(plan.extractions, err)...) - return summary, nil + return summary } dirSnapshot := root.Snapshot(string(dir.ID)) @@ -130,12 +111,14 @@ func (r runner) runDir(ctx context.Context, dir browser.UserDataDir, categories } for _, extraction := range plan.extractions { if err := ctx.Err(); err != nil { - return summary, fmt.Errorf("run stopped: %w", err) + summary.Err = fmt.Errorf("run stopped: %w", err) + return summary } input := browser.NewInput(dirSnapshot, extraction.profile, keys.get, r.loginKeychain).WithFirefoxPassword(r.firefoxPassword) result, err := r.runExtraction(ctx, extraction, input, dir.Browser.DisplayName) if err != nil { - return summary, err + summary.Err = err + return summary } summary.Results = append(summary.Results, result) } @@ -143,7 +126,7 @@ func (r runner) runDir(ctx context.Context, dir browser.UserDataDir, categories keyErr := fmt.Errorf("master key retrieval: %w", keys.err) summary.DirFailures = []DirFailure{{UserDataDirID: dir.ID, Err: keyErr}} } - return summary, nil + return summary } // runExtraction exports the records emitted before an extraction failed, as they always were; its error stops the run. diff --git a/internal/app/execute_test.go b/internal/app/execute_test.go index 33c1510..ea7e4dc 100644 --- a/internal/app/execute_test.go +++ b/internal/app/execute_test.go @@ -59,6 +59,7 @@ func run(t *testing.T, base extractorsFunc, keys dirKeysFunc, categories []categ output := OutputOptions{Dir: t.TempDir()} summary, err := execute(context.Background(), Discovery{Dirs: dirs}, categories, output, runner{extractors: base, retrieveKeys: keys}) require.NoError(t, err) + require.NoError(t, summary.Err) return summary, output.Dir } @@ -138,6 +139,7 @@ func TestExecute_ReportsEachDirWhatNeverRanFirst(t *testing.T) { summary, err := execute(context.Background(), discovery, categories, OutputOptions{Dir: t.TempDir()}, runner{extractors: base, retrieveKeys: noKeys(t)}) require.NoError(t, err) + require.NoError(t, summary.Err) assert.Equal(t, []DirFailure{failed}, summary.DirFailures, "discovery's failures are reported with the run's") var got []string for _, result := range summary.Results { @@ -233,8 +235,10 @@ func TestExecute_StopEndsTheRunBeforeTheNextExtractionAndRemovesTheCopy(t *testi categories := []category.Category{category.History, category.Download} discovery := Discovery{Dirs: []browser.UserDataDir{chromeDir(t, "chrome", "Default"), chromeDir(t, "chrome-2", "Default")}} - _, err := execute(ctx, discovery, categories, OutputOptions{Dir: t.TempDir()}, runner{extractors: base, retrieveKeys: noKeys(t)}) - require.ErrorIs(t, err, context.Canceled) + summary, err := execute(ctx, discovery, categories, OutputOptions{Dir: t.TempDir()}, runner{extractors: base, retrieveKeys: noKeys(t)}) + require.NoError(t, err) + require.ErrorIs(t, summary.Err, context.Canceled) + require.Len(t, summary.Results, 1, "what ran before the stop is still reported") require.Len(t, copied, 1, "the next user data dir is never captured") _, statErr := os.Stat(copied[0]) assert.ErrorIs(t, statErr, fs.ErrNotExist) @@ -263,15 +267,16 @@ func TestExecute_OutputFailureStopsTheRunAndLeavesNoCopies(t *testing.T) { defer func() { require.NoError(t, os.Chmod(output.Dir, 0o700)) }() discovery := Discovery{Dirs: []browser.UserDataDir{chromeDir(t, "chrome", "Default")}} - _, err := execute(context.Background(), discovery, []category.Category{category.History, category.Download}, output, runner{extractors: base, retrieveKeys: noKeys(t)}) - require.Error(t, err) + summary, err := execute(context.Background(), discovery, []category.Category{category.History, category.Download}, output, runner{extractors: base, retrieveKeys: noKeys(t)}) + require.NoError(t, err) + require.Error(t, summary.Err) require.NotEmpty(t, copied) _, statErr := os.Stat(copied) assert.ErrorIs(t, statErr, fs.ErrNotExist) } -// TestExecute_FirefoxPasswordReadOnce: stdin yields the password once, yet every protected profile of the run needs it. -func TestExecute_FirefoxPasswordReadOnce(t *testing.T) { +// TestDump_FirefoxPasswordReadOnce: stdin yields the password once, yet every protected profile of the run needs it. +func TestDump_FirefoxPasswordReadOnce(t *testing.T) { home := t.TempDir() firefoxBrowser, ok := browser.Lookup("firefox") require.True(t, ok) @@ -286,9 +291,12 @@ func TestExecute_FirefoxPasswordReadOnce(t *testing.T) { return "hunter2", nil } - discovery := Discover(context.Background(), Target{Browsers: []browser.Browser{firefoxBrowser}, HomeDir: home}) - summary, err := Execute(context.Background(), discovery, []category.Category{category.Password}, OutputOptions{Dir: t.TempDir()}, KeychainConfig{}, password) + _, summary, err := Dump(context.Background(), DumpOptions{ + Browsers: []browser.Browser{firefoxBrowser}, HomeDir: home, Categories: []category.Category{category.Password}, + Output: OutputOptions{Dir: t.TempDir()}, FirefoxPassword: password, + }) require.NoError(t, err) + require.NoError(t, summary.Err) require.Len(t, summary.Results, 2) for _, result := range summary.Results { assert.Equal(t, OutcomeOK, result.Outcome, result.ProfileName) diff --git a/internal/app/extract_golden_test.go b/internal/app/extract_golden_test.go index d3dd69b..141c88a 100644 --- a/internal/app/extract_golden_test.go +++ b/internal/app/extract_golden_test.go @@ -1,4 +1,4 @@ -package app_test +package app import ( "context" @@ -8,7 +8,6 @@ import ( "github.com/stretchr/testify/require" - "github.com/moond4rk/hackbrowserdata/internal/app" "github.com/moond4rk/hackbrowserdata/internal/browser/browsertest" "github.com/moond4rk/hackbrowserdata/internal/golden" @@ -59,12 +58,15 @@ func TestExtractGoldenSafari(t *testing.T) { func extractGoldenRun(t *testing.T, home string, keys map[browser.UserDataDirID]masterkey.Set, format string) string { t.Helper() out := t.TempDir() - options, err := app.ValidateDump(app.DumpFlags{Browser: "all", Categories: "all", Format: format, OutputDir: out, MaxStorageValue: app.DefaultMaxStorageValue}, nil, nil) + flags := DumpFlags{Browser: "all", Categories: "all", Output: OutputFlags{Format: format, Dir: out, MaxStorageValue: DefaultMaxStorageValue}} + options, err := ValidateDump(flags, nil, nil) require.NoError(t, err) - options.Target.HomeDir = home - _, err = app.ExecuteWithKeys(context.Background(), app.Discover(context.Background(), options.Target), options.Categories, options.Output, keys) + discovery := discover(context.Background(), options.Browsers, browser.DiscoverOptions{HomeDir: home}) + fromFixture := runner{extractors: extractorsFor, retrieveKeys: keysFromFile(keys)} + summary, err := execute(context.Background(), discovery, options.Categories, options.Output, fromFixture) require.NoError(t, err) + require.NoError(t, summary.Err) return out } diff --git a/internal/app/keychain_darwin.go b/internal/app/keychain_darwin.go index a9b534b..ec81103 100644 --- a/internal/app/keychain_darwin.go +++ b/internal/app/keychain_darwin.go @@ -12,11 +12,12 @@ import ( ) // newLoginKeychain builds the run's login keychain without opening it, so a run that never decrypts never prompts. -func newLoginKeychain(config KeychainConfig) loginKeychain { - if config.Password == nil { - return keychain.New(config.Path, func(context.Context) (string, error) { return "", nil }) +// A nil password, as list has it, leaves the keychain locked without a word. +func newLoginKeychain(path string, password func(context.Context) (string, error)) loginKeychain { + if password == nil { + return keychain.New(path, func(context.Context) (string, error) { return "", nil }) } - return &reportingKeychain{Keychain: keychain.New(config.Path, keychain.PasswordFunc(config.Password))} + return &reportingKeychain{Keychain: keychain.New(path, keychain.PasswordFunc(password))} } func loginKeychainPath(home string) string { diff --git a/internal/app/keychain_other.go b/internal/app/keychain_other.go index 75a4936..506df74 100644 --- a/internal/app/keychain_other.go +++ b/internal/app/keychain_other.go @@ -2,6 +2,8 @@ package app -func newLoginKeychain(KeychainConfig) loginKeychain { return nil } +import "context" + +func newLoginKeychain(string, func(context.Context) (string, error)) loginKeychain { return nil } func loginKeychainPath(string) string { return "" } diff --git a/internal/app/list.go b/internal/app/list.go index 414d35e..218a316 100644 --- a/internal/app/list.go +++ b/internal/app/list.go @@ -8,28 +8,24 @@ import ( "github.com/moond4rk/hackbrowserdata/internal/record" ) -type ListOptions struct { - Target Target - Keychain KeychainConfig -} - -// ValidateList targets every browser and leaves the keychain without a password, so counting never prompts. -func ValidateList() (ListOptions, error) { +// List finds every browser of this OS. With detail it also counts each profile's records, running every category with +// no key and the keychain locked, so counting never prompts. +func List(ctx context.Context, detail bool) (Discovery, Summary, error) { home, err := homeDir() if err != nil { - return ListOptions{}, err + return Discovery{}, Summary{}, err } - return ListOptions{ - Target: Target{Browsers: browser.Supported(), HomeDir: home}, - Keychain: KeychainConfig{Path: loginKeychainPath(home)}, - }, nil -} - -// CountRecords runs every category with no key and the keychain locked, counting records instead of exporting them. -func CountRecords(ctx context.Context, discovery Discovery, keychainConfig KeychainConfig) (Summary, error) { - keychain := newLoginKeychain(keychainConfig) + discovery := discover(ctx, browser.Supported(), browser.DiscoverOptions{HomeDir: home}) + if !detail || len(discovery.Dirs) == 0 { + return discovery, Summary{}, nil + } + keychain := newLoginKeychain(loginKeychainPath(home), nil) counter := runner{writer: discard{}, extractors: extractorsFor, retrieveKeys: keysFromFile(nil), loginKeychain: keychain} - return counter.run(ctx, discovery, category.All()) + summary := counter.run(ctx, discovery, category.All()) + if summary.Err != nil { + return Discovery{}, Summary{}, summary.Err + } + return discovery, summary, nil } type discard struct{} diff --git a/internal/app/options.go b/internal/app/options.go index fd4e150..2a603cb 100644 --- a/internal/app/options.go +++ b/internal/app/options.go @@ -1,7 +1,6 @@ package app import ( - "context" "errors" "fmt" "io/fs" @@ -14,14 +13,15 @@ import ( "github.com/moond4rk/hackbrowserdata/internal/export" ) -// Target is what Discover looks for. -type Target struct { - Browsers []browser.Browser - ProfilePath string // a custom profile dir; set only when Browsers holds one browser - HomeDir string +// OutputFlags are the output flags dump and restore share, as cobra parsed them. +type OutputFlags struct { + Format string + Dir string + Zip bool + MaxStorageValue int } -// OutputOptions is where and how Execute writes records. +// OutputOptions is where and how a dump or a restore writes records. type OutputOptions struct { Dir string Format export.Format @@ -32,18 +32,18 @@ type OutputOptions struct { // DefaultMaxStorageValue bounds a stored value's characters unless --max-storage-value overrides it; 0 disables the bound. const DefaultMaxStorageValue = 2048 -func checkMaxStorageValue(value int) error { - if value < 0 { - return fmt.Errorf("--max-storage-value must be >= 0 (0 means unlimited), got %d", value) +func parseOutput(flags OutputFlags) (OutputOptions, error) { + format, err := export.ParseFormat(flags.Format) + if err != nil { + return OutputOptions{}, err } - return nil -} - -// KeychainConfig locates the macOS login keychain and says how to get its password; other systems ignore it. -type KeychainConfig struct { - Path string - // Password runs when the keychain first opens; "" leaves it locked, and nil, as list has it, silently. - Password func(ctx context.Context) (string, error) + if err := checkOutputDir(flags.Dir); err != nil { + return OutputOptions{}, err + } + if flags.MaxStorageValue < 0 { + return OutputOptions{}, fmt.Errorf("--max-storage-value must be >= 0 (0 means unlimited), got %d", flags.MaxStorageValue) + } + return OutputOptions{Dir: flags.Dir, Format: format, Zip: flags.Zip, MaxStorageValue: flags.MaxStorageValue}, nil } func homeDir() (string, error) { diff --git a/internal/app/restore.go b/internal/app/restore.go index d7dfc37..faec54d 100644 --- a/internal/app/restore.go +++ b/internal/app/restore.go @@ -1,6 +1,7 @@ package app import ( + "context" "errors" "fmt" "io" @@ -11,26 +12,25 @@ import ( "github.com/moond4rk/hackbrowserdata/internal/browser" "github.com/moond4rk/hackbrowserdata/internal/browser/chromium" "github.com/moond4rk/hackbrowserdata/internal/category" - "github.com/moond4rk/hackbrowserdata/internal/export" "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 - Format string - OutputDir string - Zip bool - MaxStorageValue int + 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 } @@ -48,16 +48,10 @@ func ValidateRestore(flags RestoreFlags, stdin io.Reader) (RestoreOptions, error if err != nil { return RestoreOptions{}, err } - format, err := export.ParseFormat(flags.Format) + output, err := parseOutput(flags.Output) if err != nil { return RestoreOptions{}, err } - if err := checkOutputDir(flags.OutputDir); err != nil { - return RestoreOptions{}, err - } - if err := checkMaxStorageValue(flags.MaxStorageValue); err != nil { - return RestoreOptions{}, err - } if err := checkRestoreData(flags.DataDir, flags.DataZip); err != nil { return RestoreOptions{}, err } @@ -69,11 +63,52 @@ func ValidateRestore(flags RestoreFlags, stdin io.Reader) (RestoreOptions, error if err != nil { return RestoreOptions{}, err } - return RestoreOptions{ - Vaults: vaults, - Categories: categories, - Output: OutputOptions{Dir: flags.OutputDir, Format: format, Zip: flags.Zip, MaxStorageValue: flags.MaxStorageValue}, - }, nil + 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{extractors: extractorsFor, retrieveKeys: keysFromFile(keys)} + summary, err := execute(ctx, discovery, options.Categories, options.Output, fromKeyFile) + 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. @@ -124,12 +159,12 @@ func readKeyFile(path string, stdin io.Reader) (masterkey.KeyFile, error) { return keyFile, nil } -// DiscoverRestore rebuilds the user data dirs under root, and their keys, from the vaults: each vault names a -// subdirectory of root, or root is one browser's User Data. -func DiscoverRestore(root string, vaults []masterkey.Vault) (Discovery, map[browser.UserDataDirID]masterkey.Set, error) { +// 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(root, string(vaultUserDataDirID(vault)))) { + if dirExists(filepath.Join(archiveDir, string(vaultUserDataDirID(vault)))) { archived = true break } @@ -137,7 +172,7 @@ func DiscoverRestore(root string, vaults []masterkey.Vault) (Discovery, map[brow 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 for one browser's User Data (have: %s)", - root, len(vaults), vaultBrowserNames(vaults)) + archiveDir, len(vaults), vaultBrowserNames(vaults)) } var discovery Discovery keys := make(map[browser.UserDataDirID]masterkey.Set) @@ -147,9 +182,9 @@ func DiscoverRestore(root string, vaults []masterkey.Vault) (Discovery, map[brow log.Warnf("restore: keys hold %s twice, restoring the first", id) continue } - path := root + path := archiveDir if archived { - path = filepath.Join(root, string(id)) + path = filepath.Join(archiveDir, string(id)) if !dirExists(path) { log.Warnf("restore: %s has no data under %s, skipping", vault.Browser, path) continue diff --git a/internal/app/restore_golden_test.go b/internal/app/restore_golden_test.go index aec46ca..c2af5c1 100644 --- a/internal/app/restore_golden_test.go +++ b/internal/app/restore_golden_test.go @@ -17,7 +17,7 @@ import ( "github.com/moond4rk/hackbrowserdata/internal/golden" "github.com/moond4rk/hackbrowserdata/internal/masterkey" - "github.com/moond4rk/hackbrowserdata/internal/snapshot" + "github.com/moond4rk/hackbrowserdata/internal/zipfile" ) // TestRestoreGolden checks that restore writes what dump writes: the extract golden, with each browser's display name. @@ -28,13 +28,13 @@ func TestRestoreGolden(t *testing.T) { want := golden.Read(t, "extract-json.golden") t.Run("data-dir", func(t *testing.T) { - out := restoreGolden(t, app.RestoreFlags{KeyFile: keys, DataDir: data, OutputDir: t.TempDir()}) + out := restoreGolden(t, app.RestoreFlags{KeyFile: keys, DataDir: data, Output: app.OutputFlags{Dir: t.TempDir()}}) assert.Equal(t, want, golden.ConcatDir(t, out)) }) t.Run("zip", func(t *testing.T) { out := filepath.Join(t.TempDir(), "results") - restoreGolden(t, app.RestoreFlags{KeyFile: keys, DataDir: data, OutputDir: out, Zip: true}) + restoreGolden(t, app.RestoreFlags{KeyFile: keys, DataDir: data, Output: app.OutputFlags{Dir: out, Zip: true}}) entries, err := os.ReadDir(out) require.NoError(t, err) @@ -43,18 +43,17 @@ func TestRestoreGolden(t *testing.T) { archive := filepath.Join(out, "results.zip") golden.Assert(t, "restore-zip.golden", golden.ZipListing(t, archive)) - unzipped, err := snapshot.Unzip(archive) - require.NoError(t, err) - defer unzipped.Close() - assert.Equal(t, want, golden.ConcatDir(t, unzipped.Path())) + unzipped := t.TempDir() + require.NoError(t, zipfile.Extract(archive, unzipped)) + assert.Equal(t, want, golden.ConcatDir(t, unzipped)) }) // An archive v1.1.0 wrote is the same / layout, which the vaults resolve. t.Run("data-zip", func(t *testing.T) { dataZip := filepath.Join(t.TempDir(), "data.zip") - require.NoError(t, snapshot.Open(data).Zip(dataZip)) + browsertest.ZipDir(t, data, dataZip) - out := restoreGolden(t, app.RestoreFlags{KeyFile: keys, DataZip: dataZip, OutputDir: t.TempDir()}) + out := restoreGolden(t, app.RestoreFlags{KeyFile: keys, DataZip: dataZip, Output: app.OutputFlags{Dir: t.TempDir()}}) assert.Equal(t, want, golden.ConcatDir(t, out)) }) } @@ -67,14 +66,14 @@ func TestArchiveRestoreRoundTrip(t *testing.T) { require.True(t, ok, fixture.Name) browsertest.WriteChromiumUserDataDir(t, filepath.Join(home, filepath.FromSlash(namedBrowser.Platform().UserDataDirPaths[0])), fixture.Name) } - options, err := app.ValidateArchive(app.ArchiveFlags{Browser: "all", Categories: "all"}) - require.NoError(t, err) - options.Target.HomeDir = home archive := filepath.Join(t.TempDir(), "data.zip") - _, err = app.Archive(context.Background(), app.Discover(context.Background(), options.Target), options.Categories, archive) + options, err := app.ValidateArchive(app.ArchiveFlags{Browser: "all", Categories: "all", OutputPath: archive}) + require.NoError(t, err) + options.HomeDir = home + _, _, err = app.Archive(context.Background(), options) require.NoError(t, err) - out := restoreGolden(t, app.RestoreFlags{KeyFile: chromiumKeyFile(t), DataZip: archive, OutputDir: t.TempDir()}) + out := restoreGolden(t, app.RestoreFlags{KeyFile: chromiumKeyFile(t), DataZip: archive, Output: app.OutputFlags{Dir: t.TempDir()}}) assert.Equal(t, golden.Read(t, "extract-json.golden"), golden.ConcatDir(t, out)) } @@ -112,23 +111,15 @@ func restoreGolden(t *testing.T, flags app.RestoreFlags) string { if flags.Categories == "" { flags.Categories = "all" } - if flags.Format == "" { - flags.Format = "json" + if flags.Output.Format == "" { + flags.Output.Format = "json" } - flags.MaxStorageValue = app.DefaultMaxStorageValue // match the extract goldens, which cap at the default + flags.Output.MaxStorageValue = app.DefaultMaxStorageValue // match the extract goldens, which cap at the default options, err := app.ValidateRestore(flags, nil) require.NoError(t, err) - root := snapshot.Open(flags.DataDir) - if flags.DataZip != "" { - root, err = snapshot.Unzip(flags.DataZip) - require.NoError(t, err) - } - defer root.Close() - - discovery, keys, err := app.DiscoverRestore(root.Path(), options.Vaults) - require.NoError(t, err) - _, err = app.ExecuteWithKeys(context.Background(), discovery, options.Categories, options.Output, keys) + _, summary, err := app.Restore(context.Background(), options) require.NoError(t, err) - return flags.OutputDir + require.NoError(t, summary.Err) + return flags.Output.Dir } diff --git a/internal/app/restore_test.go b/internal/app/restore_test.go index 8a16a21..e2a7986 100644 --- a/internal/app/restore_test.go +++ b/internal/app/restore_test.go @@ -1,6 +1,7 @@ package app import ( + "archive/zip" "bytes" "os" "path/filepath" @@ -37,7 +38,7 @@ func layoutProfile(t *testing.T, root, relativePath string) { func TestValidateRestore_RejectsBadInput(t *testing.T) { keys := keysFile(t, testVault("chrome")) data := t.TempDir() - base := RestoreFlags{KeyFile: keys, DataDir: data, Categories: "all", Format: "json", OutputDir: t.TempDir()} + base := RestoreFlags{KeyFile: keys, DataDir: data, Categories: "all", Output: OutputFlags{Format: "json", Dir: t.TempDir()}} for name, edit := range map[string]func(flags *RestoreFlags){ "no keys": func(flags *RestoreFlags) { flags.KeyFile = "" }, "unreadable keys": func(flags *RestoreFlags) { flags.KeyFile = filepath.Join(data, "missing.json") }, @@ -45,7 +46,7 @@ func TestValidateRestore_RejectsBadInput(t *testing.T) { "both data inputs": func(flags *RestoreFlags) { flags.DataZip = filepath.Join(data, "data.zip") }, "missing data dir": func(flags *RestoreFlags) { flags.DataDir = filepath.Join(data, "missing") }, "browser not in it": func(flags *RestoreFlags) { flags.Browser = "edge" }, - "unknown format": func(flags *RestoreFlags) { flags.Format = "xml" }, + "unknown format": func(flags *RestoreFlags) { flags.Output.Format = "xml" }, } { flags := base edit(&flags) @@ -53,7 +54,7 @@ func TestValidateRestore_RejectsBadInput(t *testing.T) { require.Error(t, err, name) } - options, err := ValidateRestore(RestoreFlags{KeyFile: keys, DataDir: data, Browser: "Chrome", Categories: "all", Format: "json", OutputDir: t.TempDir()}, nil) + options, err := ValidateRestore(RestoreFlags{KeyFile: keys, DataDir: data, Browser: "Chrome", Categories: "all", Output: OutputFlags{Format: "json", Dir: t.TempDir()}}, nil) require.NoError(t, err) require.Len(t, options.Vaults, 1) assert.Equal(t, "chrome", options.Vaults[0].Browser) @@ -62,20 +63,20 @@ func TestValidateRestore_RejectsBadInput(t *testing.T) { func TestValidateRestore_ReadsKeysFromTheGivenStdin(t *testing.T) { keys, err := os.ReadFile(keysFile(t, testVault("chrome"), testVault("edge"))) require.NoError(t, err) - flags := RestoreFlags{KeyFile: "-", DataDir: t.TempDir(), Categories: "all", Format: "json", OutputDir: t.TempDir()} + flags := RestoreFlags{KeyFile: "-", DataDir: t.TempDir(), Categories: "all", Output: OutputFlags{Format: "json", Dir: t.TempDir()}} options, err := ValidateRestore(flags, bytes.NewReader(keys)) require.NoError(t, err) assert.Len(t, options.Vaults, 2) } -func TestDiscoverRestore_SecondUserDataDir(t *testing.T) { +func TestDiscoverArchive_SecondUserDataDir(t *testing.T) { root := t.TempDir() layoutProfile(t, root, "arc") layoutProfile(t, root, "arc-2") second := testVault("arc") second.ID = "arc-2" second.Keys = masterkey.Set{masterkey.V10: []byte("arc-2")} - discovery, keys, err := DiscoverRestore(root, []masterkey.Vault{testVault("arc"), second}) + discovery, keys, err := discoverArchive(root, []masterkey.Vault{testVault("arc"), second}) require.NoError(t, err) assert.Equal(t, []browser.UserDataDirID{"arc", "arc-2"}, userDataDirIDs(discovery), "a vault's ID names its directory") assert.Equal(t, filepath.Join(root, "arc-2"), discovery.Dirs[1].Path) @@ -83,7 +84,7 @@ func TestDiscoverRestore_SecondUserDataDir(t *testing.T) { assert.Equal(t, masterkey.Set{masterkey.V10: []byte("arc-2")}, keys["arc-2"]) } -func TestDiscoverRestore_FromLayout(t *testing.T) { +func TestDiscoverArchive_FromLayout(t *testing.T) { root := t.TempDir() layoutProfile(t, root, "chrome") layoutProfile(t, root, "nightly") @@ -91,7 +92,7 @@ func TestDiscoverRestore_FromLayout(t *testing.T) { nightly.Kind = "chromium-yandex" vaults := []masterkey.Vault{testVault("chrome"), nightly, testVault("chrome"), testVault("edge")} - discovery, keys, err := DiscoverRestore(root, vaults) + discovery, keys, err := discoverArchive(root, vaults) require.NoError(t, err) assert.Equal(t, []browser.UserDataDirID{"chrome", "nightly"}, userDataDirIDs(discovery), "a repeated vault restores once, one without data not at all") assert.Len(t, keys, 2) @@ -100,15 +101,37 @@ func TestDiscoverRestore_FromLayout(t *testing.T) { assert.Equal(t, "chromium-yandex", discovery.Dirs[1].VaultKind, "and its vault's kind picks the format") } -func TestDiscoverRestore_OneUserDataNeedsOneVault(t *testing.T) { +func TestDiscoverArchive_OneUserDataNeedsOneVault(t *testing.T) { root := t.TempDir() touch(t, root, "Default", "Preferences") touch(t, root, "Default", "History") - _, _, err := DiscoverRestore(root, []masterkey.Vault{testVault("chrome"), testVault("edge")}) + _, _, err := discoverArchive(root, []masterkey.Vault{testVault("chrome"), testVault("edge")}) require.Error(t, err) - discovery, _, err := DiscoverRestore(root, []masterkey.Vault{testVault("edge")}) + discovery, _, err := discoverArchive(root, []masterkey.Vault{testVault("edge")}) require.NoError(t, err) require.Equal(t, []browser.UserDataDirID{"edge"}, userDataDirIDs(discovery)) assert.Equal(t, root, discovery.Dirs[0].Path) } + +// TestUnzipArchive_RejectedArchiveLeavesNothingBehind: a restore must not leave part of an archive in the temp dir. +func TestUnzipArchive_RejectedArchiveLeavesNothingBehind(t *testing.T) { + tempRoot := t.TempDir() + for _, variable := range []string{"TMPDIR", "TMP", "TEMP"} { + t.Setenv(variable, tempRoot) + } + archive := filepath.Join(t.TempDir(), "data.zip") + file, err := os.Create(archive) + require.NoError(t, err) + zipWriter := zip.NewWriter(file) + _, err = zipWriter.Create("../escape.txt") + require.NoError(t, err) + require.NoError(t, zipWriter.Close()) + require.NoError(t, file.Close()) + + _, err = unzipArchive(archive) + require.Error(t, err) + entries, err := os.ReadDir(tempRoot) + require.NoError(t, err) + assert.Empty(t, entries) +} diff --git a/internal/app/result.go b/internal/app/result.go index fd17823..c978c6e 100644 --- a/internal/app/result.go +++ b/internal/app/result.go @@ -60,5 +60,6 @@ type DirFailure struct { type Summary struct { Results []Result DirFailures []DirFailure - Exported export.Listing // the files Execute wrote; empty when nothing was exported + Exported export.Listing // the files the run wrote; empty when nothing was exported + Err error // why the run ended early: it was stopped, or its output could not be written } diff --git a/internal/browser/browsertest/archive.go b/internal/browser/browsertest/archive.go new file mode 100644 index 0000000..55e2fd3 --- /dev/null +++ b/internal/browser/browsertest/archive.go @@ -0,0 +1,26 @@ +package browsertest + +import ( + "io/fs" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/moond4rk/hackbrowserdata/internal/zipfile" +) + +// ZipDir packs every file under dir into archivePath, the way an archive carries a restore's data. +func ZipDir(t *testing.T, dir, archivePath string) { + t.Helper() + var names []string + require.NoError(t, filepath.WalkDir(dir, func(path string, entry fs.DirEntry, err error) error { + if err != nil || entry.IsDir() { + return err + } + relativePath, err := filepath.Rel(dir, path) + names = append(names, filepath.ToSlash(relativePath)) + return err + })) + require.NoError(t, zipfile.Write(archivePath, dir, names)) +} diff --git a/internal/snapshot/snapshot.go b/internal/snapshot/snapshot.go index af39662..151509a 100644 --- a/internal/snapshot/snapshot.go +++ b/internal/snapshot/snapshot.go @@ -3,13 +3,10 @@ package snapshot import ( "context" - "errors" "fmt" "os" "path/filepath" "strings" - - "github.com/moond4rk/hackbrowserdata/internal/zipfile" ) type File struct { @@ -40,22 +37,6 @@ func Open(path string) *Root { return &Root{path: path, failed: make(map[string]error)} } -// Unzip extracts an archive into a temporary snapshot directory, rejecting entries that would land outside it. -func Unzip(archivePath string) (*Root, error) { - root, err := NewRoot() - if err != nil { - return nil, err - } - if err := zipfile.Extract(archivePath, root.path); err != nil { - return nil, errors.Join(err, root.Close()) - } - return root, nil -} - -func (r *Root) Path() string { - return r.path -} - // Capture keeps a file's own failure for Snapshot.Path instead of stopping the rest of the capture. func (r *Root) Capture(ctx context.Context, files []File) error { if err := os.MkdirAll(r.path, 0o700); err != nil { diff --git a/internal/snapshot/snapshot_test.go b/internal/snapshot/snapshot_test.go index 2f156e9..4cc645b 100644 --- a/internal/snapshot/snapshot_test.go +++ b/internal/snapshot/snapshot_test.go @@ -1,7 +1,6 @@ package snapshot import ( - "archive/zip" "context" "io/fs" "os" @@ -34,14 +33,14 @@ func userDataDir(t *testing.T) string { func TestTemp_IsPrivateAndCloseRemovesIt(t *testing.T) { root, err := NewRoot() require.NoError(t, err) - info, err := os.Stat(root.Path()) + info, err := os.Stat(root.path) require.NoError(t, err) if runtime.GOOS != "windows" { assert.Equal(t, fs.FileMode(0o700), info.Mode().Perm()) } require.NoError(t, root.Close()) - assert.NoDirExists(t, root.Path()) + assert.NoDirExists(t, root.path) } func TestOpen_ReadsInPlaceAndCloseLeavesIt(t *testing.T) { @@ -72,7 +71,7 @@ func TestCapture_FilesWithSidecarsAndDirsWithoutLocks(t *testing.T) { dirSnapshot := root.Snapshot("chrome") cookies, err := dirSnapshot.Path("Default/Network/Cookies") require.NoError(t, err) - assert.Equal(t, filepath.Join(root.Path(), "chrome", "Default", "Network", "Cookies"), cookies) + assert.Equal(t, filepath.Join(root.path, "chrome", "Default", "Network", "Cookies"), cookies) for suffix, want := range map[string]string{"-wal": "wal", "-shm": "shm"} { got, err := os.ReadFile(cookies + suffix) require.NoError(t, err) @@ -141,7 +140,7 @@ func TestCapture_StaysInsideItsOwnSnapshot(t *testing.T) { _, err = root.Snapshot("chrome").Path("../edge/Local State") require.Error(t, err) - entries, err := os.ReadDir(root.Path()) + entries, err := os.ReadDir(root.path) require.NoError(t, err) assert.Empty(t, entries, "nothing may be written outside a file's own snapshot") } @@ -175,44 +174,6 @@ func TestCapture_ALaterCaptureReplacesAnEarlierFailure(t *testing.T) { require.NoError(t, err) } -func TestUnzip_RejectsZipSlipAndCleansUp(t *testing.T) { - tempRoot := privateTempRoot(t) - archive := zipWith(t, "../escape.txt") - - _, err := Unzip(archive) - require.Error(t, err) - entries, err := os.ReadDir(tempRoot) - require.NoError(t, err) - assert.Empty(t, entries, "a rejected archive leaves neither the escaped file nor the snapshot directory behind") -} - -// privateTempRoot points os.TempDir at a fresh directory, so a test can see everything Temp leaves behind. -func privateTempRoot(t *testing.T) string { - t.Helper() - dir := t.TempDir() - for _, variable := range []string{"TMPDIR", "TMP", "TEMP"} { - t.Setenv(variable, dir) - } - return dir -} - -func zipWith(t *testing.T, names ...string) string { - t.Helper() - archive := filepath.Join(t.TempDir(), "test.zip") - file, err := os.Create(archive) - require.NoError(t, err) - zipWriter := zip.NewWriter(file) - for _, name := range names { - writer, err := zipWriter.Create(name) - require.NoError(t, err) - _, err = writer.Write([]byte(name)) - require.NoError(t, err) - } - require.NoError(t, zipWriter.Close()) - require.NoError(t, file.Close()) - return archive -} - func writeFile(t *testing.T, path string, data []byte) { t.Helper() require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) diff --git a/internal/snapshot/zip_test.go b/internal/snapshot/zip_test.go index 24fc5dc..822da1e 100644 --- a/internal/snapshot/zip_test.go +++ b/internal/snapshot/zip_test.go @@ -9,9 +9,11 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/moond4rk/hackbrowserdata/internal/zipfile" ) -func TestZipUnzip_RoundTrip(t *testing.T) { +func TestZip_RoundTrip(t *testing.T) { files := map[string][]byte{ "small.txt": []byte("hello"), "chrome/Default/Network/Cookies": []byte("cookie-bytes"), @@ -31,15 +33,13 @@ func TestZipUnzip_RoundTrip(t *testing.T) { } require.NoError(t, zipReader.Close()) - root, err := Unzip(archive) - require.NoError(t, err) + unzipped := t.TempDir() + require.NoError(t, zipfile.Extract(archive, unzipped)) for relativePath, want := range files { - got, err := os.ReadFile(filepath.Join(root.Path(), filepath.FromSlash(relativePath))) + got, err := os.ReadFile(filepath.Join(unzipped, filepath.FromSlash(relativePath))) require.NoError(t, err, relativePath) assert.True(t, bytes.Equal(want, got), "%s: got %d bytes, want %d", relativePath, len(got), len(want)) } - require.NoError(t, root.Close()) - assert.NoDirExists(t, root.Path()) } func TestZip_MissingSnapshotDirFails(t *testing.T) { From 85cf7a30417ca7c6755f294d5bfef3d272e0c1a3 Mon Sep 17 00:00:00 2001 From: moonD4rk Date: Sat, 21 Nov 2026 20:18:53 +0800 Subject: [PATCH 2/3] fix(app): check dumpkeys and archive -o up front A missing output directory was reported only after the keys were retrieved or the files copied. --- internal/app/archive.go | 3 +++ internal/app/dumpkeys.go | 5 +++++ internal/app/options.go | 21 ++++++++++++++++++++ internal/app/options_test.go | 38 ++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 internal/app/options_test.go diff --git a/internal/app/archive.go b/internal/app/archive.go index 53cfa23..263d503 100644 --- a/internal/app/archive.go +++ b/internal/app/archive.go @@ -36,6 +36,9 @@ func ValidateArchive(flags ArchiveFlags) (ArchiveOptions, error) { if err != nil { return ArchiveOptions{}, err } + if err := checkOutputFile(flags.OutputPath); err != nil { + return ArchiveOptions{}, err + } home, err := homeDir() if err != nil { return ArchiveOptions{}, err diff --git a/internal/app/dumpkeys.go b/internal/app/dumpkeys.go index 6f691d2..6c5c694 100644 --- a/internal/app/dumpkeys.go +++ b/internal/app/dumpkeys.go @@ -32,6 +32,11 @@ func ValidateDumpKeys(flags DumpKeysFlags, keychainPassword func(context.Context if err != nil { return DumpKeysOptions{}, err } + if flags.OutputPath != "" { + if err := checkOutputFile(flags.OutputPath); err != nil { + return DumpKeysOptions{}, err + } + } home, err := homeDir() if err != nil { return DumpKeysOptions{}, err diff --git a/internal/app/options.go b/internal/app/options.go index 2a603cb..e8faa74 100644 --- a/internal/app/options.go +++ b/internal/app/options.go @@ -121,3 +121,24 @@ func checkOutputDir(outputDir string) error { path = parent } } + +// checkOutputFile creates nothing: -o is written whole and renamed into place, so its directory must already exist. +func checkOutputFile(path string) error { + parent := filepath.Dir(path) + info, err := os.Stat(parent) + switch { + case errors.Is(err, fs.ErrNotExist): + return fmt.Errorf("output file %s: directory %s does not exist", path, parent) + case err != nil: + return fmt.Errorf("output file %s: %w", path, err) + case !info.IsDir(): + return fmt.Errorf("output file %s: %s is not a directory", path, parent) + } + if err := canCreateIn(parent); err != nil { + return fmt.Errorf("output file %s: %w", path, err) + } + if existing, err := os.Stat(path); err == nil && existing.IsDir() { + return fmt.Errorf("output file %s: is a directory", path) + } + return nil +} diff --git a/internal/app/options_test.go b/internal/app/options_test.go new file mode 100644 index 0000000..a3f367c --- /dev/null +++ b/internal/app/options_test.go @@ -0,0 +1,38 @@ +package app + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCheckOutputFile(t *testing.T) { + directory := t.TempDir() + file := filepath.Join(directory, "file") + require.NoError(t, os.WriteFile(file, nil, 0o600)) + + require.NoError(t, checkOutputFile(filepath.Join(directory, "keys.json"))) + require.NoError(t, checkOutputFile(file), "an existing file is replaced") + for name, path := range map[string]string{ + "missing directory": filepath.Join(directory, "missing", "keys.json"), + "directory is a file": filepath.Join(file, "keys.json"), + "path is a directory": directory, + } { + assert.Error(t, checkOutputFile(path), name) + } +} + +// TestValidate_RejectsAnOutputFileItCannotWrite: -o fails before any browser is read, not after the work is done. +func TestValidate_RejectsAnOutputFileItCannotWrite(t *testing.T) { + missing := filepath.Join(t.TempDir(), "missing", "out") + _, err := ValidateDumpKeys(DumpKeysFlags{Browser: "all", OutputPath: missing}, nil) + require.Error(t, err) + _, err = ValidateArchive(ArchiveFlags{Browser: "all", Categories: "all", OutputPath: missing}) + require.Error(t, err) + + _, err = ValidateDumpKeys(DumpKeysFlags{Browser: "all"}, nil) + require.NoError(t, err, "without -o the key file goes to stdout") +} From d480af22ec3061bbe79cdc04bf91243b1c59a014 Mon Sep 17 00:00:00 2001 From: moonD4rk Date: Sat, 21 Nov 2026 20:18:54 +0800 Subject: [PATCH 3/3] docs(rfcs): move restore's input out of the command layer Restore now discovers its directories in an archive itself, so the command layer no longer holds the data it reads. --- rfcs/001-architecture.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rfcs/001-architecture.md b/rfcs/001-architecture.md index 280f05e..5a176ef 100644 --- a/rfcs/001-architecture.md +++ b/rfcs/001-architecture.md @@ -12,7 +12,7 @@ A run is a fixed, one-way sequence of stages, and no stage reaches back: 6. **Export** — append each extraction's records to the output files. 7. **Report** — summarize every outcome. -Plan, Capture, Extract, and Export run once per data directory: a directory is planned right before its files are copied, and at most one directory's copies are on disk at a time. Every command is a cut of this pipeline: `list` stops after discovery, `list --detail` counts records instead of exporting them, `archive` captures and zips, `dumpkeys` retrieves keys only, and `restore` extracts from a snapshot it is given. +Plan, Capture, Extract, and Export run once per data directory: a directory is planned right before its files are copied, and at most one directory's copies are on disk at a time. Every command is a cut of this pipeline: `list` stops after discovery, `list --detail` counts records instead of exporting them, `archive` captures and zips, `dumpkeys` retrieves keys only, and `restore` discovers its directories in an archive and takes its keys from a file. **Why.** When each browser runs the whole flow itself, every concern is solved several times, and bugs hide where nothing else checks them. Named stages implement each concern once; a browser contributes only where its files are and how to read them. @@ -22,7 +22,7 @@ Every extraction — one category from one profile of one data directory — end ## Layers -- The command layer wires flags into the pipeline, prints what it returns, and holds what lives as long as the process: signals, the console window, the data a restore reads. +- The command layer wires flags into the pipeline, prints what it returns, and holds what lives as long as the process: signals and the console window. - The pipeline orchestrates, and it is the only place that knows every browser base. - Each browser base knows its own discovery and file formats. - Shared services sit below them: secret retrieval, pure crypto primitives, snapshot capture, and export.