Files
HackBrowserData/internal/app/discover.go
T
Roger 6e5a6455ac refactor(browser): consolidate decryption and extractor tables (#687)
* refactor(chromium): consolidate decryption and fork variants

Keys are fetched by the first value that needs one, as the secrets design says, so an empty store retrieves none.
One version type, one override table (browsers, kind, extractors), and all Yandex code in yandex.go replace lists that had to agree.

* refactor(chromium): build the password row with a constructor

Every other row of the extractor table is a constructor call; Safari already names its row newPasswordExtractor.

* refactor(browser): build password rows like every other row

A decrypting extractor now reads each file it lists, so Chromium's two login stores and Firefox's key4.db need no
hand-built row. Parse functions lead each call because Go puts the variadic file specs last.

* refactor(browser): declare each browser's sources in one block

Files were declared four ways: literals, three newFileSpec signatures, newDirectorySpec, and withKeyFiles. FileSpec
becomes Source with Path and Fallbacks, so a fallback no longer reads like a second file to read.
2026-11-09 17:25:35 +08:00

98 lines
3.1 KiB
Go

package app
import (
"fmt"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/browser/chromium"
"github.com/moond4rk/hackbrowserdata/internal/browser/firefox"
"github.com/moond4rk/hackbrowserdata/internal/browser/safari"
)
// Discovery holds an error result per browser whose data could not be read, so it is reported, not taken as absence.
type Discovery struct {
Dirs []browser.UserDataDir
Errors []Result
}
// Discover applies -p only when one browser is named.
func Discover(options Options) Discovery {
var discovery Discovery
for _, supportedBrowser := range browser.Supported() {
if options.BrowserName != allBrowsers && supportedBrowser.Name != options.BrowserName {
continue
}
discoverOptions := browser.DiscoverOptions{HomeDir: options.HomeDir}
if options.BrowserName != allBrowsers {
discoverOptions.ProfilePath = options.ProfilePath
}
dirs, err := discover(supportedBrowser, discoverOptions)
discovery.Dirs = append(discovery.Dirs, withData(dirs)...)
if err != nil {
discovery.Errors = append(discovery.Errors, dirFailure(browser.UserDataDirID(supportedBrowser.Name), err))
}
}
return discovery
}
// withData drops profiles holding no file any category reads, such as a bare Preferences, but keeps unreadable ones.
func withData(dirs []browser.UserDataDir) []browser.UserDataDir {
extractors := extractorsWith(nil)
var out []browser.UserDataDir
for _, dir := range dirs {
var profiles []browser.Profile
for _, profile := range dir.Profiles {
if hasData(dir, profile, extractors(dir, profile)) {
profiles = append(profiles, profile)
}
}
if len(profiles) > 0 {
dir.Profiles = profiles
out = append(out, dir)
}
}
return out
}
func hasData(dir browser.UserDataDir, profile browser.Profile, extractors browser.Extractors) bool {
for _, extractor := range extractors {
if len(extractor.Sources) == 0 {
continue
}
if _, ok, err := locate(dir, profile, extractor.Sources); ok || err != nil {
return true
}
}
return false
}
// discover dispatches on Base here, not in browser, because the base packages import browser.
func discover(supportedBrowser browser.Browser, options browser.DiscoverOptions) ([]browser.UserDataDir, error) {
switch supportedBrowser.Base {
case browser.Chromium:
return chromium.Discover(supportedBrowser, options)
case browser.Firefox:
return firefox.Discover(supportedBrowser, options)
case browser.Safari:
return safari.Discover(supportedBrowser, options)
default:
return nil, fmt.Errorf("%s: no base package", supportedBrowser.Name)
}
}
// extractorsWith hands Safari the run's login keychain, which its default profile's password extractor reads.
func extractorsWith(keychain safari.LoginKeychain) extractorsFunc {
return func(dir browser.UserDataDir, profile browser.Profile) browser.Extractors {
switch dir.Browser.Base {
case browser.Chromium:
return chromium.Extractors(dir, profile)
case browser.Firefox:
return firefox.Extractors(dir, profile)
case browser.Safari:
return safari.Extractors(dir, profile, keychain)
default:
return nil
}
}
}