mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
fix: report unreadable sources and tidy the pipeline (#675)
A Stat error other than not-exist was reported as absent, and the --zip archive was readable by other users. The rest is review follow-up: export no longer prints, one runner type, shared name helpers, a trimmed log API.
This commit is contained in:
1 parent
6d6191b4c6
commit
4e98ba7884
33 files changed
+303
-315
No files matched your search
+8
-17
@@ -134,7 +134,7 @@ func ListRequest() (Request, error) {
|
||||
if err != nil {
|
||||
return Request{}, err
|
||||
}
|
||||
return Request{Browser: "all", Categories: category.All(), Home: home}, nil
|
||||
return Request{Browser: allBrowsers, Categories: category.All(), Home: home}, nil
|
||||
}
|
||||
|
||||
func homeDir() (string, error) {
|
||||
@@ -145,20 +145,20 @@ func homeDir() (string, error) {
|
||||
return home, nil
|
||||
}
|
||||
|
||||
const allBrowsers = "all"
|
||||
|
||||
// parseBrowser accepts all or one of this OS's browser IDs; an ID from another OS's table has nothing to find here.
|
||||
func parseBrowser(s string) (string, error) {
|
||||
id := strings.ToLower(strings.TrimSpace(s))
|
||||
if id == "" || id == "all" {
|
||||
return "all", nil
|
||||
if id == "" || id == allBrowsers {
|
||||
return allBrowsers, nil
|
||||
}
|
||||
ids := []string{"all"}
|
||||
for _, def := range browser.Supported() {
|
||||
if def.ID == id {
|
||||
return id, nil
|
||||
}
|
||||
ids = append(ids, def.ID)
|
||||
}
|
||||
return "", fmt.Errorf("unknown browser %q, available: %s", s, strings.Join(ids, "|"))
|
||||
return "", fmt.Errorf("unknown browser %q, available: %s|%s", s, allBrowsers, strings.Join(browser.IDs(), "|"))
|
||||
}
|
||||
|
||||
// checkOutputDir creates nothing: -d must already be a directory, or its nearest existing ancestor must be one that a
|
||||
@@ -191,7 +191,7 @@ func checkProfilePath(id, path string) (string, error) {
|
||||
if path == "" {
|
||||
return "", nil
|
||||
}
|
||||
if id == "all" {
|
||||
if id == allBrowsers {
|
||||
log.Warnf("-p needs a specific -b; ignoring %s", path)
|
||||
return "", nil
|
||||
}
|
||||
@@ -221,7 +221,7 @@ func parseCategories(s string) ([]category.Category, error) {
|
||||
}
|
||||
c, err := category.Parse(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w, available: all|%s", err, categoryNames())
|
||||
return nil, fmt.Errorf("%w, available: all|%s", err, strings.Join(category.Names(), ","))
|
||||
}
|
||||
if !seen[c] {
|
||||
seen[c] = true
|
||||
@@ -233,12 +233,3 @@ func parseCategories(s string) ([]category.Category, error) {
|
||||
}
|
||||
return categories, nil
|
||||
}
|
||||
|
||||
func categoryNames() string {
|
||||
all := category.All()
|
||||
names := make([]string, len(all))
|
||||
for i, c := range all {
|
||||
names[i] = c.String()
|
||||
}
|
||||
return strings.Join(names, ",")
|
||||
}
|
||||
@@ -56,7 +56,7 @@ func archiveFiles(p Plan, dirs []browser.UserDataDir) []snapshot.File {
|
||||
for _, dir := range dirs {
|
||||
for _, prof := range dir.Profiles {
|
||||
for _, src := range chromium.ArchiveSources(dir) {
|
||||
if f, ok := firstOnDisk(dir, prof, src); ok {
|
||||
if f, ok, err := firstOnDisk(dir, prof, src); err == nil && ok {
|
||||
add(f)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,11 +20,11 @@ type Discovery struct {
|
||||
func Discover(req Request) Discovery {
|
||||
var d Discovery
|
||||
for _, def := range browser.Supported() {
|
||||
if req.Browser != "all" && def.ID != req.Browser {
|
||||
if req.Browser != allBrowsers && def.ID != req.Browser {
|
||||
continue
|
||||
}
|
||||
opts := browser.DiscoverOptions{Home: req.Home}
|
||||
if req.Browser != "all" {
|
||||
if req.Browser != allBrowsers {
|
||||
opts.ProfilePath = req.ProfilePath
|
||||
}
|
||||
dirs, err := discover(def, opts)
|
||||
@@ -37,7 +37,8 @@ func Discover(req Request) Discovery {
|
||||
}
|
||||
|
||||
// withData keeps the profiles holding a file some category reads, and the user data dirs with such a profile: a
|
||||
// Preferences marker alone is no data, so a profile created and never used is not listed, archived, or keyed.
|
||||
// Preferences marker alone is no data, so a profile created and never used is not listed, archived, or keyed. A
|
||||
// profile whose files cannot be checked stays, so the plan reports the error.
|
||||
func withData(dirs []browser.UserDataDir) []browser.UserDataDir {
|
||||
extractors := extractorsWith(nil)
|
||||
var out []browser.UserDataDir
|
||||
@@ -61,7 +62,7 @@ func hasData(dir browser.UserDataDir, p browser.Profile, ex browser.Extractors)
|
||||
if len(x.Sources) == 0 {
|
||||
continue
|
||||
}
|
||||
if _, ok := locate(dir, p, x.Sources); ok {
|
||||
if _, ok, err := locate(dir, p, x.Sources); ok || err != nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,3 +88,10 @@ func TestDiscover_UnreadableDirIsReported(t *testing.T) {
|
||||
assert.Equal(t, Extraction{DirID: "chrome"}, d.Failed[0].Extraction)
|
||||
assert.Equal(t, OutcomeError, d.Failed[0].Outcome)
|
||||
}
|
||||
|
||||
// Dropping a profile whose files cannot be checked would hide the failure; the plan reports it instead.
|
||||
func TestWithData_KeepsAProfileItCannotCheck(t *testing.T) {
|
||||
dir := chromeDir(t, "chrome", "Default")
|
||||
lock(t, filepath.Join(dir.Path, "Default"))
|
||||
assert.Len(t, withData([]browser.UserDataDir{dir}), 1)
|
||||
}
|
||||
+35
-12
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/internal/browser"
|
||||
@@ -44,14 +45,30 @@ func execute(ctx context.Context, p Plan, req Request, extractors extractorsFunc
|
||||
if err != nil {
|
||||
return Summary{}, err
|
||||
}
|
||||
s, err := runPlan(ctx, p, w, extractors, keys, true)
|
||||
return s, errors.Join(err, w.Close())
|
||||
s, err := runner{w: w, extractors: extractors, keys: keys, announce: true}.run(ctx, p)
|
||||
closeErr := w.Close()
|
||||
announceOutput(w.Output())
|
||||
return s, errors.Join(err, closeErr)
|
||||
}
|
||||
|
||||
func announceOutput(out export.Output) {
|
||||
if len(out.Files) == 0 {
|
||||
return
|
||||
}
|
||||
fmt.Fprintln(os.Stderr)
|
||||
log.Infof("Exported to %s/", out.Dir)
|
||||
for _, f := range out.Files {
|
||||
log.Infof(" %-24s %d entries", f.Name, f.Count)
|
||||
}
|
||||
if out.Zip != "" {
|
||||
log.Infof("Compressed: %s/%s", out.Dir, out.Zip)
|
||||
}
|
||||
}
|
||||
|
||||
// Count runs every planned extraction with no key and the keychain locked, and counts the records instead of exporting
|
||||
// them, which is all list --detail shows.
|
||||
func Count(ctx context.Context, p Plan, req Request) (Summary, error) {
|
||||
return runPlan(ctx, p, discard{}, extractorsWith(loginKeychain(req)), dumpKeys(nil), false)
|
||||
return runner{w: discard{}, extractors: extractorsWith(loginKeychain(req)), keys: dumpKeys(nil)}.run(ctx, p)
|
||||
}
|
||||
|
||||
// recordWriter takes each extraction's records as it finishes.
|
||||
@@ -63,13 +80,21 @@ type discard struct{}
|
||||
|
||||
func (discard) Write(category.Category, []record.Record) error { return nil }
|
||||
|
||||
func runPlan(ctx context.Context, p Plan, w recordWriter, extractors extractorsFunc, keys keySource, announce bool) (Summary, error) {
|
||||
// runner is what every user data dir of one run shares; announce logs each dir as it starts.
|
||||
type runner struct {
|
||||
w recordWriter
|
||||
extractors extractorsFunc
|
||||
keys keySource
|
||||
announce bool
|
||||
}
|
||||
|
||||
func (r runner) run(ctx context.Context, p Plan) (Summary, error) {
|
||||
results := append([]Result(nil), p.Skipped...)
|
||||
for _, dir := range p.Dirs {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return Summary{Results: results}, fmt.Errorf("run stopped: %w", err)
|
||||
}
|
||||
dirResults, err := executeDir(ctx, dir, p, w, extractors, keys, announce)
|
||||
dirResults, err := r.runDir(ctx, dir, p)
|
||||
results = append(results, dirResults...)
|
||||
if err != nil {
|
||||
return Summary{Results: results}, err
|
||||
@@ -78,14 +103,12 @@ func runPlan(ctx context.Context, p Plan, w recordWriter, extractors extractorsF
|
||||
return Summary{Results: results}, nil
|
||||
}
|
||||
|
||||
func executeDir(ctx context.Context, dir browser.UserDataDir, p Plan, w recordWriter, extractors extractorsFunc,
|
||||
keys keySource, announce bool,
|
||||
) ([]Result, error) {
|
||||
func (r runner) runDir(ctx context.Context, dir browser.UserDataDir, p Plan) ([]Result, error) {
|
||||
extractions := extractionsOf(p, dir.ID)
|
||||
if len(extractions) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
if announce {
|
||||
if r.announce {
|
||||
log.Infof("Extracting %s...", dir.Def.Name)
|
||||
}
|
||||
|
||||
@@ -107,7 +130,7 @@ func executeDir(ctx context.Context, dir browser.UserDataDir, p Plan, w recordWr
|
||||
masterKeys func(context.Context) masterkey.Set
|
||||
)
|
||||
if dir.Def.Base == browser.Chromium {
|
||||
dk = &dirKeys{retrieve: func(ctx context.Context) (masterkey.Set, error) { return keys(ctx, dir, snap) }}
|
||||
dk = &dirKeys{retrieve: func(ctx context.Context) (masterkey.Set, error) { return r.keys(ctx, dir, snap) }}
|
||||
masterKeys = dk.get
|
||||
}
|
||||
|
||||
@@ -115,7 +138,7 @@ func executeDir(ctx context.Context, dir browser.UserDataDir, p Plan, w recordWr
|
||||
byProfile := make(map[string]browser.Extractors, len(dir.Profiles))
|
||||
for _, prof := range dir.Profiles {
|
||||
profiles[prof.Name] = prof
|
||||
byProfile[prof.Name] = extractors(dir, prof)
|
||||
byProfile[prof.Name] = r.extractors(dir, prof)
|
||||
}
|
||||
|
||||
results := make([]Result, 0, len(extractions)+1)
|
||||
@@ -132,7 +155,7 @@ func executeDir(ctx context.Context, dir browser.UserDataDir, p Plan, w recordWr
|
||||
c := &collector{browser: dir.Def.Name, profile: prof.Name}
|
||||
err := x.Extract(ctx, browser.NewProfileSnapshot(snap, prof, masterKeys), c)
|
||||
// Records emitted before an extraction failed are kept, as they always were.
|
||||
if writeErr := w.Write(e.Category, c.records); writeErr != nil {
|
||||
if writeErr := r.w.Write(e.Category, c.records); writeErr != nil {
|
||||
return results, writeErr
|
||||
}
|
||||
results = append(results, c.result(e, err))
|
||||
|
||||
@@ -61,7 +61,7 @@ func retrieveKeys(ctx context.Context, dir browser.UserDataDir, keys keySource)
|
||||
defer closeSnapshot(d)
|
||||
var files []snapshot.File
|
||||
for _, src := range chromium.KeySources(dir) {
|
||||
if f, ok := firstOnDisk(dir, browser.Profile{}, src); ok {
|
||||
if f, ok, err := firstOnDisk(dir, browser.Profile{}, src); err == nil && ok {
|
||||
files = append(files, f)
|
||||
}
|
||||
}
|
||||
|
||||
+23
-16
@@ -1,8 +1,9 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/internal/browser"
|
||||
@@ -48,7 +49,11 @@ func newPlan(d Discovery, req Request, extractors extractorsFunc) Plan {
|
||||
p.Skipped = append(p.Skipped, Result{Extraction: e, Outcome: OutcomeUnsupported})
|
||||
continue
|
||||
}
|
||||
files, ok := locate(dir, prof, x.Sources)
|
||||
files, ok, err := locate(dir, prof, x.Sources)
|
||||
if err != nil {
|
||||
p.Skipped = append(p.Skipped, Result{Extraction: e, Outcome: OutcomeError, Err: err})
|
||||
continue
|
||||
}
|
||||
if !ok {
|
||||
p.Skipped = append(p.Skipped, Result{Extraction: e, Outcome: OutcomeAbsent})
|
||||
continue
|
||||
@@ -68,35 +73,37 @@ func newPlan(d Discovery, req Request, extractors extractorsFunc) Plan {
|
||||
|
||||
// locate finds each source's first candidate on disk. An extraction needs every required source and at least one of
|
||||
// the profile's own; a shared one such as Local State never makes it present by itself. An extraction that reads no
|
||||
// file, such as Safari's keychain passwords, always runs.
|
||||
func locate(dir browser.UserDataDir, p browser.Profile, sources []browser.Source) ([]snapshot.File, bool) {
|
||||
// file, such as Safari's keychain passwords, always runs. A candidate that cannot be checked is an error, not absence.
|
||||
func locate(dir browser.UserDataDir, p browser.Profile, sources []browser.Source) ([]snapshot.File, bool, error) {
|
||||
if len(sources) == 0 {
|
||||
return nil, true
|
||||
return nil, true, nil
|
||||
}
|
||||
var files []snapshot.File
|
||||
present := false
|
||||
for _, src := range sources {
|
||||
f, ok := firstOnDisk(dir, p, src)
|
||||
f, ok, err := firstOnDisk(dir, p, src)
|
||||
switch {
|
||||
case err != nil:
|
||||
return nil, false, err
|
||||
case ok:
|
||||
files = append(files, f)
|
||||
present = present || !src.Shared
|
||||
case !src.Optional:
|
||||
return nil, false
|
||||
return nil, false, nil
|
||||
}
|
||||
}
|
||||
return files, present
|
||||
return files, present, nil
|
||||
}
|
||||
|
||||
func firstOnDisk(dir browser.UserDataDir, p browser.Profile, src browser.Source) (snapshot.File, bool) {
|
||||
for _, rel := range src.Paths {
|
||||
if !src.Shared {
|
||||
rel = path.Join(p.Dir, rel)
|
||||
}
|
||||
func firstOnDisk(dir browser.UserDataDir, p browser.Profile, src browser.Source) (snapshot.File, bool, error) {
|
||||
for _, rel := range src.Candidates(p) {
|
||||
info, err := os.Stat(filepath.Join(dir.Path, filepath.FromSlash(rel)))
|
||||
if err == nil && info.IsDir() == src.IsDir {
|
||||
return snapshot.File{DirID: string(dir.ID), From: dir.Path, Rel: rel, IsDir: src.IsDir}, true
|
||||
switch {
|
||||
case err == nil && info.IsDir() == src.IsDir:
|
||||
return snapshot.File{DirID: string(dir.ID), From: dir.Path, Rel: rel, IsDir: src.IsDir}, true, nil
|
||||
case err != nil && !errors.Is(err, fs.ErrNotExist):
|
||||
return snapshot.File{}, false, err
|
||||
}
|
||||
}
|
||||
return snapshot.File{}, false
|
||||
return snapshot.File{}, false, nil
|
||||
}
|
||||
@@ -2,8 +2,10 @@ package app
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -115,3 +117,31 @@ func TestNewPlan_ExtractorWithoutSourcesRuns(t *testing.T) {
|
||||
assert.Empty(t, got.Files)
|
||||
assert.Empty(t, got.Skipped)
|
||||
}
|
||||
|
||||
// lock makes dir unsearchable until the test ends, so a Stat of anything under it fails with permission denied.
|
||||
func lock(t *testing.T, dir string) {
|
||||
t.Helper()
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("mode bits do not restrict Stat on Windows")
|
||||
}
|
||||
if os.Geteuid() == 0 {
|
||||
t.Skip("root ignores mode bits")
|
||||
}
|
||||
require.NoError(t, os.Chmod(dir, 0))
|
||||
t.Cleanup(func() { assert.NoError(t, os.Chmod(dir, 0o700)) })
|
||||
}
|
||||
|
||||
// A candidate that cannot be checked is not absence: the extraction is an error, and nothing is captured for it.
|
||||
func TestNewPlan_UnreadableSourceIsAnError(t *testing.T) {
|
||||
dir := chromeDir(t, "chrome", "Default")
|
||||
lock(t, filepath.Join(dir.Path, "Default"))
|
||||
base := fakeBase(map[category.Category]extractFunc{category.History: nil})
|
||||
|
||||
got := newPlan(Discovery{Dirs: []browser.UserDataDir{dir}}, Request{Categories: []category.Category{category.History}}, base)
|
||||
|
||||
require.Len(t, got.Skipped, 1)
|
||||
assert.Equal(t, OutcomeError, got.Skipped[0].Outcome)
|
||||
require.ErrorIs(t, got.Skipped[0].Err, fs.ErrPermission)
|
||||
assert.Empty(t, got.Extractions)
|
||||
assert.Empty(t, got.Files)
|
||||
}
|
||||
@@ -57,9 +57,9 @@ func ValidateRestore(f RestoreFlags) (Request, masterkey.Dump, error) {
|
||||
}
|
||||
filter := strings.ToLower(strings.TrimSpace(f.Browser))
|
||||
if filter == "" {
|
||||
filter = "all"
|
||||
filter = allBrowsers
|
||||
}
|
||||
if filter != "all" && len(vaultsOf(dump, filter)) == 0 {
|
||||
if filter != allBrowsers && len(vaultsOf(dump, filter)) == 0 {
|
||||
return Request{}, masterkey.Dump{}, fmt.Errorf("no vault for browser %q in keys (have: %s)", filter, vaultKeys(dump.Vaults))
|
||||
}
|
||||
req := Request{Browser: filter, Categories: categories, Format: format, Dir: f.Dir, Zip: f.Zip}
|
||||
@@ -115,7 +115,7 @@ func fromManifest(root string, m snapshot.Manifest, vaults []masterkey.Vault, fi
|
||||
var d Discovery
|
||||
keys := make(map[browser.DirID]masterkey.Set)
|
||||
for _, md := range m.Dirs {
|
||||
if filter != "all" && md.Browser != filter {
|
||||
if filter != allBrowsers && md.Browser != filter {
|
||||
continue
|
||||
}
|
||||
v, ok := vaultOf(vaults, md)
|
||||
@@ -247,7 +247,7 @@ func vaultDirID(v masterkey.Vault) browser.DirID {
|
||||
func vaultsOf(dump masterkey.Dump, filter string) []masterkey.Vault {
|
||||
var out []masterkey.Vault
|
||||
for _, v := range dump.Vaults {
|
||||
if filter == "all" || strings.EqualFold(v.Browser, filter) {
|
||||
if filter == allBrowsers || strings.EqualFold(v.Browser, filter) {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user