Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions cmd/hack-browser-data/pipeline_golden_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"context"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

"github.com/moond4rk/hackbrowserdata/internal/app"
ibrowser "github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
)

// Until the switch-over the pipeline runs beside today's extract path: on the same fixtures it must write the same
// goldens byte for byte.
func TestPipelineGolden(t *testing.T) {
home := t.TempDir()
keys := make(map[ibrowser.DirID]masterkey.Set)
for _, inst := range chromiumFixtureInstalls {
def, ok := ibrowser.Lookup(inst.key)
require.True(t, ok, inst.key)
require.Equal(t, inst.name, def.Name, "records are stamped with the table's display name")
chromiumFixtureUserData(t, filepath.Join(home, filepath.FromSlash(def.Platform().UserDataDirs[0])), inst.key)
keys[ibrowser.DirID(inst.key)] = chromiumFixtureKeys
}

tests := []struct {
format string
golden string
jsonFallback bool
}{
{format: "csv", golden: "extract-csv.golden"},
{format: "json", golden: "extract-json.golden"},
{format: "cookie-editor", golden: "extract-cookie-editor.golden", jsonFallback: true},
}
for _, tt := range tests {
tt := tt
t.Run(tt.format, func(t *testing.T) {
out := pipelineGoldenRun(t, home, keys, tt.format)
got := bundleDir(t, out)
if tt.jsonFallback {
assertJSONFallback(t, out, pipelineGoldenRun(t, home, keys, "json"))
got = bundleFiles(t, out, "cookie.json")
}
assertGolden(t, tt.golden, got)
})
}
}

func pipelineGoldenRun(t *testing.T, home string, keys map[ibrowser.DirID]masterkey.Set, format string) string {
t.Helper()
out := t.TempDir()
req, err := app.Validate(app.Flags{Browser: "all", Categories: "all", Format: format, Dir: out})
require.NoError(t, err)
req.Home, req.Keys = home, keys

plan := app.NewPlan(app.Discover(req), req)
_, err = app.Execute(context.Background(), plan, req)
require.NoError(t, err)
return out
}
109 changes: 109 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Package app runs the pipeline: Validate turns flags into a Request, Discover finds the user data dirs, NewPlan crosses
// them with the requested categories, Execute captures, extracts, and exports one user data dir at a time, and Report
// prints what every extraction got.
package app

import (
"context"
"fmt"
"os"
"strings"

"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/category"
"github.com/moond4rk/hackbrowserdata/internal/export"
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
)

// Flags are a run's command-line values as cobra parsed them.
type Flags struct {
Browser string
Categories string
Format string
Dir string
ProfilePath string
KeychainPassword string
}

// Request is a validated run.
type Request struct {
Browser string // "all" or one Def.ID
Categories []category.Category
Format export.Format
Dir string
ProfilePath string
Home string

// KeychainPassword yields the macOS login password when the login keychain is first opened; "" leaves it locked.
KeychainPassword func(ctx context.Context) (string, error)
// Keys are a dump's keys by user data dir; nil retrieves every dir's keys from this machine.
Keys map[browser.DirID]masterkey.Set
}

// Validate checks the flags and resolves the home directory, so nothing later reads the environment.
func Validate(f Flags) (Request, error) {
categories, err := parseCategories(f.Categories)
if err != nil {
return Request{}, err
}
format, err := export.ParseFormat(f.Format)
if err != nil {
return Request{}, err
}
home, err := os.UserHomeDir()
if err != nil {
return Request{}, fmt.Errorf("home directory: %w", err)
}
name := strings.ToLower(strings.TrimSpace(f.Browser))
if name == "" {
name = "all"
}
password := f.KeychainPassword
return Request{
Browser: name,
Categories: categories,
Format: format,
Dir: f.Dir,
ProfilePath: f.ProfilePath,
Home: home,
KeychainPassword: func(context.Context) (string, error) { return password, nil },
}, nil
}

// parseCategories reads a comma-separated -c value; a repeated category is kept once, or its records would be
// exported twice.
func parseCategories(s string) ([]category.Category, error) {
s = strings.TrimSpace(s)
if strings.EqualFold(s, "all") {
return category.All(), nil
}
var categories []category.Category
seen := make(map[category.Category]bool)
for _, name := range strings.Split(s, ",") {
name = strings.TrimSpace(strings.ToLower(name))
if name == "" {
continue
}
c, err := category.Parse(name)
if err != nil {
return nil, fmt.Errorf("%w, available: all|%s", err, categoryNames())
}
if !seen[c] {
seen[c] = true
categories = append(categories, c)
}
}
if len(categories) == 0 {
return nil, fmt.Errorf("no categories specified")
}
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, ",")
}
41 changes: 41 additions & 0 deletions internal/app/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package app

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/moond4rk/hackbrowserdata/internal/category"
"github.com/moond4rk/hackbrowserdata/internal/export"
)

func TestValidate(t *testing.T) {
req, err := Validate(Flags{Browser: " Chrome ", Categories: "cookie, password,cookie", Format: "csv", Dir: "out", KeychainPassword: "pw"})
require.NoError(t, err)
assert.Equal(t, "chrome", req.Browser)
assert.Equal(t, []category.Category{category.Cookie, category.Password}, req.Categories, "a repeated category runs once")
assert.Equal(t, export.CSV, req.Format)
assert.Equal(t, "out", req.Dir)
assert.NotEmpty(t, req.Home)
password, err := req.KeychainPassword(context.Background())
require.NoError(t, err)
assert.Equal(t, "pw", password)

req, err = Validate(Flags{Categories: "all", Format: "json"})
require.NoError(t, err)
assert.Equal(t, "all", req.Browser)
assert.Equal(t, category.All(), req.Categories)
}

func TestValidate_RejectsBadInput(t *testing.T) {
for name, f := range map[string]Flags{
"unknown category": {Categories: "cookie,bogus", Format: "json"},
"no category": {Categories: " , ", Format: "json"},
"unknown format": {Categories: "all", Format: "xml"},
} {
_, err := Validate(f)
assert.Error(t, err, name)
}
}
53 changes: 53 additions & 0 deletions internal/app/discover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package app

import (
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/browser/chromium"
)

// Discovery is what Discover found: the user data dirs, and an error result for each browser whose data could not be
// read, which is reported rather than mistaken for absence.
type Discovery struct {
Dirs []browser.UserDataDir
Failed []Result
}

// Discover looks for the requested browsers on this machine. -p applies only when one browser is named.
func Discover(req Request) Discovery {
var d Discovery
for _, def := range browser.Supported() {
if req.Browser != "all" && def.ID != req.Browser {
continue
}
opts := browser.DiscoverOptions{Home: req.Home}
if req.Browser != "all" {
opts.ProfilePath = req.ProfilePath
}
dirs, err := discover(def, opts)
d.Dirs = append(d.Dirs, dirs...)
if err != nil {
d.Failed = append(d.Failed, dirFailure(browser.DirID(def.ID), err))
}
}
return d
}

// discover and extractorsOf dispatch on the base here, because the base packages import browser and so browser cannot
// call them.
func discover(def browser.Def, opts browser.DiscoverOptions) ([]browser.UserDataDir, error) {
switch def.Base {
case browser.Chromium:
return chromium.Discover(def, opts)
default:
return nil, nil // Firefox and Safari are not on the pipeline yet
}
}

func extractorsOf(dir browser.UserDataDir, p browser.Profile) browser.Extractors {
switch dir.Def.Base {
case browser.Chromium:
return chromium.Extractors(dir, p)
default:
return nil
}
}
75 changes: 75 additions & 0 deletions internal/app/discover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package app

import (
"os"
"path/filepath"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/moond4rk/hackbrowserdata/internal/browser"
)

// installAt creates a profile where this OS's table expects the browser's user data dir under home.
func installAt(t *testing.T, home, id string) string {
t.Helper()
def := lookup(t, id)
require.NotEmpty(t, def.Platform().UserDataDirs, id)
path := filepath.Join(home, filepath.FromSlash(def.Platform().UserDataDirs[0]))
touch(t, path, "Default", "Preferences")
return path
}

func dirIDs(d Discovery) []browser.DirID {
var ids []browser.DirID
for _, dir := range d.Dirs {
ids = append(ids, dir.ID)
}
return ids
}

func TestDiscover(t *testing.T) {
home := t.TempDir()
installAt(t, home, "edge")
installAt(t, home, "chrome")

d := Discover(Request{Browser: "all", Home: home})
assert.Equal(t, []browser.DirID{"chrome", "edge"}, dirIDs(d), "table order, which is output order")
assert.Empty(t, d.Failed)

d = Discover(Request{Browser: "edge", Home: home})
assert.Equal(t, []browser.DirID{"edge"}, dirIDs(d))
}

func TestDiscover_ProfilePathNeedsOneBrowser(t *testing.T) {
home := t.TempDir()
installAt(t, home, "chrome")
custom := t.TempDir()
touch(t, custom, "Default", "Preferences")

d := Discover(Request{Browser: "chrome", Home: home, ProfilePath: custom})
require.Len(t, d.Dirs, 1)
assert.Equal(t, custom, d.Dirs[0].Path)

d = Discover(Request{Browser: "all", Home: home, ProfilePath: custom})
require.Len(t, d.Dirs, 1)
assert.NotEqual(t, custom, d.Dirs[0].Path, "-p is ignored without a specific browser")
}

func TestDiscover_UnreadableDirIsReported(t *testing.T) {
if runtime.GOOS == "windows" || os.Geteuid() == 0 {
t.Skip("directory permissions do not deny reads here")
}
home := t.TempDir()
path := installAt(t, home, "chrome")
require.NoError(t, os.Chmod(path, 0o000))
defer func() { require.NoError(t, os.Chmod(path, 0o700)) }()

d := Discover(Request{Browser: "all", Home: home})
assert.Empty(t, d.Dirs)
require.Len(t, d.Failed, 1)
assert.Equal(t, Extraction{DirID: "chrome"}, d.Failed[0].Extraction)
assert.Equal(t, OutcomeError, d.Failed[0].Outcome)
}
Loading
Loading