mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-05-19 18:58:03 +02:00
feat: add CountEntries to skip decryption for list --detail (#562)
* feat: add CountEntries to skip decryption for list --detail (#549) * test: add CountEntries and countCategory tests at browser level * fix: address review feedback on CountRows and countLocalStorage * test: add CountRows unit tests
This commit is contained in:
@@ -95,6 +95,63 @@ func (b *Browser) Extract(categories []types.Category) (*types.BrowserData, erro
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// CountEntries copies browser files to a temp directory and counts entries
|
||||
// per category without decryption. Much faster than Extract for display-only
|
||||
// use cases like "list --detail".
|
||||
func (b *Browser) CountEntries(categories []types.Category) (map[types.Category]int, error) {
|
||||
session, err := filemanager.NewSession()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer session.Cleanup()
|
||||
|
||||
tempPaths := b.acquireFiles(session, categories)
|
||||
|
||||
counts := make(map[types.Category]int)
|
||||
for _, cat := range categories {
|
||||
path, ok := tempPaths[cat]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
counts[cat] = b.countCategory(cat, path)
|
||||
}
|
||||
return counts, nil
|
||||
}
|
||||
|
||||
// countCategory calls the appropriate count function for a category.
|
||||
func (b *Browser) countCategory(cat types.Category, path string) int {
|
||||
var count int
|
||||
var err error
|
||||
switch cat {
|
||||
case types.Password:
|
||||
count, err = countPasswords(path)
|
||||
case types.Cookie:
|
||||
count, err = countCookies(path)
|
||||
case types.History:
|
||||
count, err = countHistories(path)
|
||||
case types.Download:
|
||||
count, err = countDownloads(path)
|
||||
case types.Bookmark:
|
||||
count, err = countBookmarks(path)
|
||||
case types.CreditCard:
|
||||
count, err = countCreditCards(path)
|
||||
case types.Extension:
|
||||
if b.cfg.Kind == types.ChromiumOpera {
|
||||
count, err = countOperaExtensions(path)
|
||||
} else {
|
||||
count, err = countExtensions(path)
|
||||
}
|
||||
case types.LocalStorage:
|
||||
count, err = countLocalStorage(path)
|
||||
case types.SessionStorage:
|
||||
count, err = countSessionStorage(path)
|
||||
}
|
||||
if err != nil {
|
||||
log.Debugf("count %s for %s: %v", cat, b.BrowserName()+"/"+b.ProfileName(), err)
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// acquireFiles copies source files to the session temp directory.
|
||||
func (b *Browser) acquireFiles(session *filemanager.Session, categories []types.Category) map[types.Category]string {
|
||||
tempPaths := make(map[types.Category]string)
|
||||
|
||||
@@ -544,17 +544,9 @@ func TestGetMasterKey(t *testing.T) {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestExtract(t *testing.T) {
|
||||
// Shared fixture: profile with a real History database.
|
||||
dir := t.TempDir()
|
||||
mkFile(dir, "Default", "Preferences")
|
||||
|
||||
historyDB := createTestDB(t, "History", urlsSchema,
|
||||
insertURL("https://example.com", "Example", 5, 13350000000000000),
|
||||
)
|
||||
profileDir := filepath.Join(dir, "Default")
|
||||
data, err := os.ReadFile(historyDB)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(filepath.Join(profileDir, "History"), data, 0o644))
|
||||
installFile(t, filepath.Join(dir, "Default"), setupHistoryDB(t), "History")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -586,7 +578,8 @@ func TestExtract(t *testing.T) {
|
||||
result, err := browsers[0].Extract([]types.Category{types.History})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.Len(t, result.Histories, 1)
|
||||
require.Len(t, result.Histories, 3)
|
||||
// setupHistoryDB: Example(200) > GitHub(100) > Go Dev(50)
|
||||
assert.Equal(t, "Example", result.Histories[0].Title)
|
||||
|
||||
if tt.wantRetriever {
|
||||
@@ -598,6 +591,87 @@ func TestExtract(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CountEntries
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestCountEntries(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
mkFile(dir, "Default", "Preferences")
|
||||
installFile(t, filepath.Join(dir, "Default"), setupHistoryDB(t), "History")
|
||||
|
||||
browsers, err := NewBrowsers(types.BrowserConfig{
|
||||
Name: "Test", Kind: types.Chromium, UserDataDir: dir,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, browsers, 1)
|
||||
|
||||
// No retriever set — CountEntries should still work (no decryption needed).
|
||||
counts, err := browsers[0].CountEntries([]types.Category{types.History, types.Download})
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, 3, counts[types.History])
|
||||
// Download uses a different table in the same file; since we only
|
||||
// created the urls table (not downloads), the count query will fail
|
||||
// gracefully and return 0.
|
||||
assert.Equal(t, 0, counts[types.Download])
|
||||
}
|
||||
|
||||
func TestCountEntries_NoRetrieverNeeded(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
mkFile(dir, "Default", "Preferences")
|
||||
// Login Data normally needs master key to extract, but CountEntries skips decryption.
|
||||
installFile(t, filepath.Join(dir, "Default"), setupLoginDB(t), "Login Data")
|
||||
|
||||
browsers, err := NewBrowsers(types.BrowserConfig{
|
||||
Name: "Test", Kind: types.Chromium, UserDataDir: dir,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, browsers, 1)
|
||||
|
||||
// No retriever set — CountEntries succeeds without master key.
|
||||
counts, err := browsers[0].CountEntries([]types.Category{types.Password})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, counts[types.Password])
|
||||
}
|
||||
|
||||
func TestCountCategory(t *testing.T) {
|
||||
t.Run("History", func(t *testing.T) {
|
||||
path := setupHistoryDB(t)
|
||||
b := &Browser{cfg: types.BrowserConfig{Kind: types.Chromium}}
|
||||
assert.Equal(t, 3, b.countCategory(types.History, path))
|
||||
})
|
||||
|
||||
t.Run("Cookie", func(t *testing.T) {
|
||||
path := setupCookieDB(t)
|
||||
b := &Browser{cfg: types.BrowserConfig{Kind: types.Chromium}}
|
||||
assert.Equal(t, 2, b.countCategory(types.Cookie, path))
|
||||
})
|
||||
|
||||
t.Run("Bookmark", func(t *testing.T) {
|
||||
path := setupBookmarkJSON(t)
|
||||
b := &Browser{cfg: types.BrowserConfig{Kind: types.Chromium}}
|
||||
assert.Equal(t, 3, b.countCategory(types.Bookmark, path))
|
||||
})
|
||||
|
||||
t.Run("Extension_Opera", func(t *testing.T) {
|
||||
path := createTestJSON(t, "Secure Preferences", `{
|
||||
"extensions": {
|
||||
"opsettings": {
|
||||
"ext1": {"location": 1, "manifest": {"name": "Ext", "version": "1.0"}}
|
||||
}
|
||||
}
|
||||
}`)
|
||||
b := &Browser{cfg: types.BrowserConfig{Kind: types.ChromiumOpera}}
|
||||
assert.Equal(t, 1, b.countCategory(types.Extension, path))
|
||||
})
|
||||
|
||||
t.Run("FileNotFound", func(t *testing.T) {
|
||||
b := &Browser{cfg: types.BrowserConfig{Kind: types.Chromium}}
|
||||
assert.Equal(t, 0, b.countCategory(types.History, "/nonexistent/path"))
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// SetRetriever: verify *Browser satisfies the interface used by
|
||||
// browser.pickFromConfigs for post-construction retriever injection.
|
||||
|
||||
@@ -51,3 +51,32 @@ func walkBookmarks(node gjson.Result, folder string, out *[]types.BookmarkEntry)
|
||||
walkBookmarks(child, currentFolder, out)
|
||||
}
|
||||
}
|
||||
|
||||
func countBookmarks(path string) (int, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var count int
|
||||
roots := gjson.GetBytes(data, "roots")
|
||||
roots.ForEach(func(_, value gjson.Result) bool {
|
||||
count += walkCountBookmarks(value)
|
||||
return true
|
||||
})
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// walkCountBookmarks recursively counts URL nodes in the bookmark tree.
|
||||
func walkCountBookmarks(node gjson.Result) int {
|
||||
count := 0
|
||||
if node.Get("type").String() == "url" {
|
||||
count++
|
||||
}
|
||||
children := node.Get("children")
|
||||
if children.Exists() && children.IsArray() {
|
||||
for _, child := range children.Array() {
|
||||
count += walkCountBookmarks(child)
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
@@ -7,8 +7,9 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestExtractBookmarks(t *testing.T) {
|
||||
path := createTestJSON(t, "Bookmarks", `{
|
||||
func setupBookmarkJSON(t *testing.T) string {
|
||||
t.Helper()
|
||||
return createTestJSON(t, "Bookmarks", `{
|
||||
"roots": {
|
||||
"bookmark_bar": {
|
||||
"name": "Bookmarks Bar",
|
||||
@@ -33,6 +34,10 @@ func TestExtractBookmarks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}`)
|
||||
}
|
||||
|
||||
func TestExtractBookmarks(t *testing.T) {
|
||||
path := setupBookmarkJSON(t)
|
||||
|
||||
got, err := extractBookmarks(path)
|
||||
require.NoError(t, err)
|
||||
@@ -52,6 +57,22 @@ func TestExtractBookmarks(t *testing.T) {
|
||||
assert.Equal(t, "News", got[2].Folder) // parent folder name
|
||||
}
|
||||
|
||||
func TestCountBookmarks(t *testing.T) {
|
||||
path := setupBookmarkJSON(t)
|
||||
|
||||
count, err := countBookmarks(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 3, count) // 3 URLs, folders not counted
|
||||
}
|
||||
|
||||
func TestCountBookmarks_Empty(t *testing.T) {
|
||||
path := createTestJSON(t, "Bookmarks", `{"roots": {}}`)
|
||||
|
||||
count, err := countBookmarks(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, count)
|
||||
}
|
||||
|
||||
func TestExtractBookmarks_FoldersExcluded(t *testing.T) {
|
||||
path := createTestJSON(t, "Bookmarks", `{
|
||||
"roots": {
|
||||
|
||||
@@ -11,9 +11,12 @@ import (
|
||||
"github.com/moond4rk/hackbrowserdata/utils/sqliteutil"
|
||||
)
|
||||
|
||||
const defaultCookieQuery = `SELECT name, encrypted_value, host_key, path,
|
||||
creation_utc, expires_utc, is_secure, is_httponly,
|
||||
has_expires, is_persistent FROM cookies`
|
||||
const (
|
||||
defaultCookieQuery = `SELECT name, encrypted_value, host_key, path,
|
||||
creation_utc, expires_utc, is_secure, is_httponly,
|
||||
has_expires, is_persistent FROM cookies`
|
||||
countCookieQuery = `SELECT COUNT(*) FROM cookies`
|
||||
)
|
||||
|
||||
func extractCookies(masterKey []byte, path string) ([]types.CookieEntry, error) {
|
||||
var decryptFails int
|
||||
@@ -65,6 +68,10 @@ func extractCookies(masterKey []byte, path string) ([]types.CookieEntry, error)
|
||||
return cookies, nil
|
||||
}
|
||||
|
||||
func countCookies(path string) (int, error) {
|
||||
return sqliteutil.CountRows(path, false, countCookieQuery)
|
||||
}
|
||||
|
||||
// stripCookieHash removes the SHA256(host_key) prefix from a decrypted cookie value.
|
||||
// Chrome 130+ (Cookie DB schema version 24) prepends SHA256(domain) to the cookie
|
||||
// value before encryption to prevent cross-domain cookie replay attacks.
|
||||
|
||||
@@ -8,11 +8,16 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestExtractCookies(t *testing.T) {
|
||||
path := createTestDB(t, "Cookies", cookiesSchema,
|
||||
func setupCookieDB(t *testing.T) string {
|
||||
t.Helper()
|
||||
return createTestDB(t, "Cookies", cookiesSchema,
|
||||
insertCookie("session", ".old.com", "/", "", 13340000000000000, 13350000000000000, 1, 1),
|
||||
insertCookie("token", ".new.com", "/api", "", 13360000000000000, 13370000000000000, 1, 0),
|
||||
)
|
||||
}
|
||||
|
||||
func TestExtractCookies(t *testing.T) {
|
||||
path := setupCookieDB(t)
|
||||
|
||||
got, err := extractCookies(nil, path)
|
||||
require.NoError(t, err)
|
||||
@@ -34,6 +39,22 @@ func TestExtractCookies(t *testing.T) {
|
||||
assert.True(t, got[1].IsHTTPOnly)
|
||||
}
|
||||
|
||||
func TestCountCookies(t *testing.T) {
|
||||
path := setupCookieDB(t)
|
||||
|
||||
count, err := countCookies(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, count)
|
||||
}
|
||||
|
||||
func TestCountCookies_Empty(t *testing.T) {
|
||||
path := createTestDB(t, "Cookies", cookiesSchema)
|
||||
|
||||
count, err := countCookies(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, count)
|
||||
}
|
||||
|
||||
func TestStripCookieHash(t *testing.T) {
|
||||
googleHash := sha256.Sum256([]byte(".google.com"))
|
||||
shopifyHash := sha256.Sum256([]byte(".shopify.com"))
|
||||
|
||||
@@ -8,8 +8,11 @@ import (
|
||||
"github.com/moond4rk/hackbrowserdata/utils/sqliteutil"
|
||||
)
|
||||
|
||||
const defaultCreditCardQuery = `SELECT COALESCE(guid, ''), name_on_card, expiration_month, expiration_year,
|
||||
card_number_encrypted, COALESCE(nickname, ''), COALESCE(billing_address_id, '') FROM credit_cards`
|
||||
const (
|
||||
defaultCreditCardQuery = `SELECT COALESCE(guid, ''), name_on_card, expiration_month, expiration_year,
|
||||
card_number_encrypted, COALESCE(nickname, ''), COALESCE(billing_address_id, '') FROM credit_cards`
|
||||
countCreditCardQuery = `SELECT COUNT(*) FROM credit_cards`
|
||||
)
|
||||
|
||||
func extractCreditCards(masterKey []byte, path string) ([]types.CreditCardEntry, error) {
|
||||
var decryptFails int
|
||||
@@ -44,3 +47,7 @@ func extractCreditCards(masterKey []byte, path string) ([]types.CreditCardEntry,
|
||||
}
|
||||
return cards, nil
|
||||
}
|
||||
|
||||
func countCreditCards(path string) (int, error) {
|
||||
return sqliteutil.CountRows(path, false, countCreditCardQuery)
|
||||
}
|
||||
|
||||
@@ -7,11 +7,16 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestExtractCreditCards(t *testing.T) {
|
||||
path := createTestDB(t, "Web Data", creditCardsSchema,
|
||||
func setupCreditCardDB(t *testing.T) string {
|
||||
t.Helper()
|
||||
return createTestDB(t, "Web Data", creditCardsSchema,
|
||||
insertCreditCard("John Doe", 12, 2025, "", "Johnny", "addr-1"),
|
||||
insertCreditCard("Jane Smith", 6, 2027, "", "", ""),
|
||||
)
|
||||
}
|
||||
|
||||
func TestExtractCreditCards(t *testing.T) {
|
||||
path := setupCreditCardDB(t)
|
||||
|
||||
got, err := extractCreditCards(nil, path)
|
||||
require.NoError(t, err)
|
||||
@@ -28,3 +33,19 @@ func TestExtractCreditCards(t *testing.T) {
|
||||
assert.Equal(t, "6", got[1].ExpMonth)
|
||||
assert.Equal(t, "2027", got[1].ExpYear)
|
||||
}
|
||||
|
||||
func TestCountCreditCards(t *testing.T) {
|
||||
path := setupCreditCardDB(t)
|
||||
|
||||
count, err := countCreditCards(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, count)
|
||||
}
|
||||
|
||||
func TestCountCreditCards_Empty(t *testing.T) {
|
||||
path := createTestDB(t, "Web Data", creditCardsSchema)
|
||||
|
||||
count, err := countCreditCards(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, count)
|
||||
}
|
||||
|
||||
@@ -8,8 +8,11 @@ import (
|
||||
"github.com/moond4rk/hackbrowserdata/utils/sqliteutil"
|
||||
)
|
||||
|
||||
const defaultDownloadQuery = `SELECT target_path, tab_url, total_bytes, start_time, end_time,
|
||||
mime_type FROM downloads`
|
||||
const (
|
||||
defaultDownloadQuery = `SELECT target_path, tab_url, total_bytes, start_time, end_time,
|
||||
mime_type FROM downloads`
|
||||
countDownloadQuery = `SELECT COUNT(*) FROM downloads`
|
||||
)
|
||||
|
||||
func extractDownloads(path string) ([]types.DownloadEntry, error) {
|
||||
downloads, err := sqliteutil.QueryRows(path, false, defaultDownloadQuery,
|
||||
@@ -37,3 +40,7 @@ func extractDownloads(path string) ([]types.DownloadEntry, error) {
|
||||
})
|
||||
return downloads, nil
|
||||
}
|
||||
|
||||
func countDownloads(path string) (int, error) {
|
||||
return sqliteutil.CountRows(path, false, countDownloadQuery)
|
||||
}
|
||||
|
||||
@@ -7,11 +7,16 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestExtractDownloads(t *testing.T) {
|
||||
path := createTestDB(t, "History", downloadsSchema,
|
||||
func setupDownloadDB(t *testing.T) string {
|
||||
t.Helper()
|
||||
return createTestDB(t, "History", downloadsSchema,
|
||||
insertDownload("/tmp/old.zip", "https://old.com/file.zip", "application/zip", 1024, 13340000000000000, 13340000100000000),
|
||||
insertDownload("/tmp/new.pdf", "https://new.com/doc.pdf", "application/pdf", 2048, 13360000000000000, 13360000200000000),
|
||||
)
|
||||
}
|
||||
|
||||
func TestExtractDownloads(t *testing.T) {
|
||||
path := setupDownloadDB(t)
|
||||
|
||||
got, err := extractDownloads(path)
|
||||
require.NoError(t, err)
|
||||
@@ -29,3 +34,19 @@ func TestExtractDownloads(t *testing.T) {
|
||||
assert.False(t, got[0].EndTime.IsZero())
|
||||
assert.True(t, got[0].StartTime.Before(got[0].EndTime))
|
||||
}
|
||||
|
||||
func TestCountDownloads(t *testing.T) {
|
||||
path := setupDownloadDB(t)
|
||||
|
||||
count, err := countDownloads(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, count)
|
||||
}
|
||||
|
||||
func TestCountDownloads_Empty(t *testing.T) {
|
||||
path := createTestDB(t, "History", downloadsSchema)
|
||||
|
||||
count, err := countDownloads(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, count)
|
||||
}
|
||||
|
||||
@@ -85,3 +85,45 @@ func isExtensionEnabled(ext gjson.Result) bool {
|
||||
func extractOperaExtensions(path string) ([]types.ExtensionEntry, error) {
|
||||
return extractExtensionsWithKeys(path, []string{"extensions.opsettings"})
|
||||
}
|
||||
|
||||
func countExtensions(path string) (int, error) {
|
||||
return countExtensionsWithKeys(path, defaultExtensionKeys)
|
||||
}
|
||||
|
||||
func countOperaExtensions(path string) (int, error) {
|
||||
return countExtensionsWithKeys(path, []string{"extensions.opsettings"})
|
||||
}
|
||||
|
||||
// countExtensionsWithKeys counts non-system extensions without building
|
||||
// full ExtensionEntry structs. Mirrors the filtering logic in extractExtensionsWithKeys.
|
||||
func countExtensionsWithKeys(path string, keys []string) (int, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var settings gjson.Result
|
||||
for _, key := range keys {
|
||||
settings = gjson.GetBytes(data, key)
|
||||
if settings.Exists() {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !settings.Exists() {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
var count int
|
||||
settings.ForEach(func(_, ext gjson.Result) bool {
|
||||
location := ext.Get("location").Int()
|
||||
if location == 5 || location == 10 {
|
||||
return true
|
||||
}
|
||||
if !ext.Get("manifest").Exists() {
|
||||
return true
|
||||
}
|
||||
count++
|
||||
return true
|
||||
})
|
||||
return count, nil
|
||||
}
|
||||
|
||||
@@ -7,8 +7,9 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestExtractExtensions(t *testing.T) {
|
||||
path := createTestJSON(t, "Secure Preferences", `{
|
||||
func setupExtensionJSON(t *testing.T) string {
|
||||
t.Helper()
|
||||
return createTestJSON(t, "Secure Preferences", `{
|
||||
"extensions": {
|
||||
"settings": {
|
||||
"abc123": {
|
||||
@@ -38,6 +39,10 @@ func TestExtractExtensions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}`)
|
||||
}
|
||||
|
||||
func TestExtractExtensions(t *testing.T) {
|
||||
path := setupExtensionJSON(t)
|
||||
|
||||
got, err := extractExtensions(path)
|
||||
require.NoError(t, err)
|
||||
@@ -56,6 +61,45 @@ func TestExtractExtensions(t *testing.T) {
|
||||
assert.False(t, ids["system-ext"])
|
||||
}
|
||||
|
||||
func TestCountExtensions(t *testing.T) {
|
||||
path := setupExtensionJSON(t)
|
||||
|
||||
count, err := countExtensions(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, count) // system (5) and component (10) skipped
|
||||
}
|
||||
|
||||
func TestCountOperaExtensions(t *testing.T) {
|
||||
path := createTestJSON(t, "Secure Preferences", `{
|
||||
"extensions": {
|
||||
"opsettings": {
|
||||
"opera-ext-1": {
|
||||
"location": 1,
|
||||
"manifest": {"name": "Opera Ad Blocker", "version": "2.0.0"}
|
||||
},
|
||||
"system-ext": {
|
||||
"location": 5,
|
||||
"manifest": {"name": "System", "version": "1.0"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`)
|
||||
|
||||
count, err := countOperaExtensions(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, count)
|
||||
}
|
||||
|
||||
func TestCountExtensions_Empty(t *testing.T) {
|
||||
path := createTestJSON(t, "Secure Preferences", `{
|
||||
"extensions": {"settings": {}}
|
||||
}`)
|
||||
|
||||
count, err := countExtensions(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, count)
|
||||
}
|
||||
|
||||
func TestExtractExtensions_NoManifestSkipped(t *testing.T) {
|
||||
path := createTestJSON(t, "Secure Preferences", `{
|
||||
"extensions": {
|
||||
|
||||
@@ -8,7 +8,10 @@ import (
|
||||
"github.com/moond4rk/hackbrowserdata/utils/sqliteutil"
|
||||
)
|
||||
|
||||
const defaultHistoryQuery = `SELECT url, title, visit_count, last_visit_time FROM urls`
|
||||
const (
|
||||
defaultHistoryQuery = `SELECT url, title, visit_count, last_visit_time FROM urls`
|
||||
countHistoryQuery = `SELECT COUNT(*) FROM urls`
|
||||
)
|
||||
|
||||
func extractHistories(path string) ([]types.HistoryEntry, error) {
|
||||
histories, err := sqliteutil.QueryRows(path, false, defaultHistoryQuery,
|
||||
@@ -35,3 +38,7 @@ func extractHistories(path string) ([]types.HistoryEntry, error) {
|
||||
})
|
||||
return histories, nil
|
||||
}
|
||||
|
||||
func countHistories(path string) (int, error) {
|
||||
return sqliteutil.CountRows(path, false, countHistoryQuery)
|
||||
}
|
||||
|
||||
@@ -7,12 +7,17 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestExtractHistories(t *testing.T) {
|
||||
path := createTestDB(t, "History", urlsSchema,
|
||||
func setupHistoryDB(t *testing.T) string {
|
||||
t.Helper()
|
||||
return createTestDB(t, "History", urlsSchema,
|
||||
insertURL("https://github.com", "GitHub", 100, 13370000000000000),
|
||||
insertURL("https://go.dev", "Go Dev", 50, 13360000000000000),
|
||||
insertURL("https://example.com", "Example", 200, 13350000000000000),
|
||||
)
|
||||
}
|
||||
|
||||
func TestExtractHistories(t *testing.T) {
|
||||
path := setupHistoryDB(t)
|
||||
|
||||
got, err := extractHistories(path)
|
||||
require.NoError(t, err)
|
||||
@@ -29,6 +34,22 @@ func TestExtractHistories(t *testing.T) {
|
||||
assert.False(t, got[0].LastVisit.IsZero())
|
||||
}
|
||||
|
||||
func TestCountHistories(t *testing.T) {
|
||||
path := setupHistoryDB(t)
|
||||
|
||||
count, err := countHistories(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 3, count)
|
||||
}
|
||||
|
||||
func TestCountHistories_Empty(t *testing.T) {
|
||||
path := createTestDB(t, "History", urlsSchema)
|
||||
|
||||
count, err := countHistories(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, count)
|
||||
}
|
||||
|
||||
func TestExtractHistories_FileNotFound(t *testing.T) {
|
||||
_, err := extractHistories("/nonexistent/History")
|
||||
require.Error(t, err)
|
||||
|
||||
@@ -9,7 +9,10 @@ import (
|
||||
"github.com/moond4rk/hackbrowserdata/utils/sqliteutil"
|
||||
)
|
||||
|
||||
const defaultLoginQuery = `SELECT origin_url, username_value, password_value, date_created FROM logins`
|
||||
const (
|
||||
defaultLoginQuery = `SELECT origin_url, username_value, password_value, date_created FROM logins`
|
||||
countLoginQuery = `SELECT COUNT(*) FROM logins`
|
||||
)
|
||||
|
||||
func extractPasswords(masterKey []byte, path string) ([]types.LoginEntry, error) {
|
||||
return extractPasswordsWithQuery(masterKey, path, defaultLoginQuery)
|
||||
@@ -57,3 +60,7 @@ func extractYandexPasswords(masterKey []byte, path string) ([]types.LoginEntry,
|
||||
const yandexLoginQuery = `SELECT action_url, username_value, password_value, date_created FROM logins`
|
||||
return extractPasswordsWithQuery(masterKey, path, yandexLoginQuery)
|
||||
}
|
||||
|
||||
func countPasswords(path string) (int, error) {
|
||||
return sqliteutil.CountRows(path, false, countLoginQuery)
|
||||
}
|
||||
|
||||
@@ -7,11 +7,16 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestExtractPasswords(t *testing.T) {
|
||||
path := createTestDB(t, "Login Data", loginsSchema,
|
||||
func setupLoginDB(t *testing.T) string {
|
||||
t.Helper()
|
||||
return createTestDB(t, "Login Data", loginsSchema,
|
||||
insertLogin("https://old.com", "https://old.com/login", "alice", "", 13340000000000000),
|
||||
insertLogin("https://new.com", "https://new.com/login", "bob", "", 13360000000000000),
|
||||
)
|
||||
}
|
||||
|
||||
func TestExtractPasswords(t *testing.T) {
|
||||
path := setupLoginDB(t)
|
||||
|
||||
got, err := extractPasswords(nil, path)
|
||||
require.NoError(t, err)
|
||||
@@ -28,6 +33,22 @@ func TestExtractPasswords(t *testing.T) {
|
||||
assert.Empty(t, got[0].Password)
|
||||
}
|
||||
|
||||
func TestCountPasswords(t *testing.T) {
|
||||
path := setupLoginDB(t)
|
||||
|
||||
count, err := countPasswords(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, count)
|
||||
}
|
||||
|
||||
func TestCountPasswords_Empty(t *testing.T) {
|
||||
path := createTestDB(t, "Login Data", loginsSchema)
|
||||
|
||||
count, err := countPasswords(path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, count)
|
||||
}
|
||||
|
||||
func TestExtractYandexPasswords(t *testing.T) {
|
||||
path := createTestDB(t, "Ya Passman Data", loginsSchema,
|
||||
insertLogin("https://origin.yandex.ru", "https://action.yandex.ru/submit", "user", "", 13350000000000000),
|
||||
|
||||
@@ -238,6 +238,53 @@ func extractNamespaceOrigin(key string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func countLocalStorage(path string) (int, error) {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
db, err := leveldb.OpenFile(path, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
var count int
|
||||
iter := db.NewIterator(nil, nil)
|
||||
defer iter.Release()
|
||||
|
||||
for iter.Next() {
|
||||
if _, ok := parseLocalStorageEntry(iter.Key(), iter.Value()); ok {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count, iter.Error()
|
||||
}
|
||||
|
||||
func countSessionStorage(path string) (int, error) {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
db, err := leveldb.OpenFile(path, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
var count int
|
||||
iter := db.NewIterator(nil, nil)
|
||||
defer iter.Release()
|
||||
|
||||
mapPrefix := []byte("map-")
|
||||
for iter.Next() {
|
||||
if bytes.HasPrefix(iter.Key(), mapPrefix) {
|
||||
if sep := bytes.IndexByte(iter.Key()[len(mapPrefix):], '-'); sep >= 0 {
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
return count, iter.Error()
|
||||
}
|
||||
|
||||
// decodeSessionStorageValue decodes a session storage value.
|
||||
// Values are raw UTF-16 LE (no format byte prefix, unlike localStorage).
|
||||
func decodeSessionStorageValue(value []byte) string {
|
||||
|
||||
@@ -162,14 +162,31 @@ func TestParseLocalStorageEntry(t *testing.T) {
|
||||
// extractLocalStorage (integration with LevelDB)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestExtractLocalStorage(t *testing.T) {
|
||||
dir := createTestLevelDB(t, map[string]string{
|
||||
func setupLocalStorageLevelDB(t *testing.T) string {
|
||||
t.Helper()
|
||||
return createTestLevelDB(t, map[string]string{
|
||||
localStorageVersionKey: "1",
|
||||
localStorageMetaPrefix + "https://example.com": string([]byte{0x08, 0x96, 0x01}),
|
||||
localStorageMetaAccessKey + "https://example.com": string([]byte{0x10, 0x20}),
|
||||
string(append([]byte("_https://example.com\x00"), testEncodeLatin1("token")...)): string(testEncodeLatin1("abc123")),
|
||||
string(append([]byte("_https://example.com\x00"), testEncodeUTF16("テスト")...)): string(testEncodeUTF16("データ")),
|
||||
})
|
||||
}
|
||||
|
||||
func setupSessionStorageLevelDB(t *testing.T) string {
|
||||
t.Helper()
|
||||
return createTestLevelDB(t, map[string]string{
|
||||
"namespace-abcd1234_5678_9abc_def0_111111111111-https://github.com/": "100",
|
||||
"namespace-abcd1234_5678_9abc_def0_111111111111-https://example.com/": "101",
|
||||
"map-100-__darkreader__wasEnabledForHost": string(testEncodeUTF16Raw("false")),
|
||||
"map-101-token": string(testEncodeUTF16Raw("abc123")),
|
||||
"next-map-id": "200",
|
||||
"version": "1",
|
||||
})
|
||||
}
|
||||
|
||||
func TestExtractLocalStorage(t *testing.T) {
|
||||
dir := setupLocalStorageLevelDB(t)
|
||||
|
||||
got, err := extractLocalStorage(dir)
|
||||
require.NoError(t, err)
|
||||
@@ -196,17 +213,7 @@ func TestExtractLocalStorage(t *testing.T) {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestExtractSessionStorage(t *testing.T) {
|
||||
dir := createTestLevelDB(t, map[string]string{
|
||||
// Namespace entry: maps guid+origin → map_id
|
||||
"namespace-abcd1234_5678_9abc_def0_111111111111-https://github.com/": "100",
|
||||
"namespace-abcd1234_5678_9abc_def0_111111111111-https://example.com/": "101",
|
||||
// Map entries: actual data (values are raw UTF-16 LE)
|
||||
"map-100-__darkreader__wasEnabledForHost": string(testEncodeUTF16Raw("false")),
|
||||
"map-101-token": string(testEncodeUTF16Raw("abc123")),
|
||||
// Metadata: should be skipped
|
||||
"next-map-id": "200",
|
||||
"version": "1",
|
||||
})
|
||||
dir := setupSessionStorageLevelDB(t)
|
||||
|
||||
got, err := extractSessionStorage(dir)
|
||||
require.NoError(t, err)
|
||||
@@ -220,6 +227,41 @@ func TestExtractSessionStorage(t *testing.T) {
|
||||
assert.Equal(t, "abc123", byKey["https://example.com//token"])
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// countLocalStorage
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestCountLocalStorage(t *testing.T) {
|
||||
dir := setupLocalStorageLevelDB(t)
|
||||
|
||||
count, err := countLocalStorage(dir)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 4, count) // VERSION filtered, 2 META + 2 data entries kept
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// countSessionStorage
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestCountSessionStorage(t *testing.T) {
|
||||
dir := setupSessionStorageLevelDB(t)
|
||||
|
||||
count, err := countSessionStorage(dir)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, count) // only map- entries with key separator
|
||||
}
|
||||
|
||||
func TestCountSessionStorage_Empty(t *testing.T) {
|
||||
dir := createTestLevelDB(t, map[string]string{
|
||||
"next-map-id": "1",
|
||||
"version": "1",
|
||||
})
|
||||
|
||||
count, err := countSessionStorage(dir)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 0, count)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Test helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -196,6 +196,16 @@ func insertCreditCard(name string, month, year int, encNumberHex, nickName, addr
|
||||
// Test fixture builders
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// installFile copies a test fixture file into a profile directory.
|
||||
// This bridges per-category setup functions (which return standalone paths)
|
||||
// and browser-level integration tests (which need files inside a profile).
|
||||
func installFile(t *testing.T, profileDir, srcPath, dstName string) {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(srcPath)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(filepath.Join(profileDir, dstName), data, 0o644))
|
||||
}
|
||||
|
||||
// createTestDB creates a SQLite database with the given schema and insert statements.
|
||||
func createTestDB(t *testing.T, name, schema string, inserts ...string) string {
|
||||
t.Helper()
|
||||
|
||||
Reference in New Issue
Block a user