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:
Roger
2026-04-07 22:28:39 +08:00
committed by GitHub
parent 5f42d4fe5f
commit b3bbc0dadf
40 changed files with 1009 additions and 101 deletions
+10 -2
View File
@@ -8,8 +8,12 @@ import (
"github.com/moond4rk/hackbrowserdata/utils/sqliteutil"
)
const firefoxBookmarkQuery = `SELECT id, url, type, dateAdded, COALESCE(title, '')
FROM (SELECT * FROM moz_bookmarks INNER JOIN moz_places ON moz_bookmarks.fk=moz_places.id)`
const (
firefoxBookmarkQuery = `SELECT id, url, type, dateAdded, COALESCE(title, '')
FROM (SELECT * FROM moz_bookmarks INNER JOIN moz_places ON moz_bookmarks.fk=moz_places.id)`
firefoxCountBookmarkQuery = `SELECT COUNT(*) FROM moz_bookmarks
INNER JOIN moz_places ON moz_bookmarks.fk=moz_places.id`
)
func extractBookmarks(path string) ([]types.BookmarkEntry, error) {
bookmarks, err := sqliteutil.QueryRows(path, true, firefoxBookmarkQuery,
@@ -45,3 +49,7 @@ func bookmarkType(bt int64) string {
return "folder"
}
}
func countBookmarks(path string) (int, error) {
return sqliteutil.CountRows(path, true, firefoxCountBookmarkQuery)
}
+23 -3
View File
@@ -7,14 +7,18 @@ import (
"github.com/stretchr/testify/require"
)
func TestExtractBookmarks(t *testing.T) {
// Bookmarks require JOIN: moz_bookmarks.fk = moz_places.id
path := createTestDB(t, "places.sqlite", []string{mozPlacesSchema, mozBookmarksSchema},
func setupMozBookmarkDB(t *testing.T) string {
t.Helper()
return createTestDB(t, "places.sqlite", []string{mozPlacesSchema, mozBookmarksSchema},
insertMozPlace(1, "https://go.dev", "Go", 0, 0),
insertMozPlace(2, "https://github.com", "GitHub", 0, 0),
insertMozBookmark(1, 1, 1, "Go Website", 1700000000000000),
insertMozBookmark(2, 2, 1, "GitHub", 1710000000000000),
)
}
func TestExtractBookmarks(t *testing.T) {
path := setupMozBookmarkDB(t)
got, err := extractBookmarks(path)
require.NoError(t, err)
@@ -29,3 +33,19 @@ func TestExtractBookmarks(t *testing.T) {
assert.Equal(t, "url", got[0].Folder) // type=1 → "url"
assert.False(t, got[0].CreatedAt.IsZero())
}
func TestCountBookmarks(t *testing.T) {
path := setupMozBookmarkDB(t)
count, err := countBookmarks(path)
require.NoError(t, err)
assert.Equal(t, 2, count)
}
func TestCountBookmarks_Empty(t *testing.T) {
path := createTestDB(t, "places.sqlite", []string{mozPlacesSchema, mozBookmarksSchema})
count, err := countBookmarks(path)
require.NoError(t, err)
assert.Equal(t, 0, count)
}
+9 -2
View File
@@ -8,8 +8,11 @@ import (
"github.com/moond4rk/hackbrowserdata/utils/sqliteutil"
)
const firefoxCookieQuery = `SELECT name, value, host, path,
creationTime, expiry, isSecure, isHttpOnly FROM moz_cookies`
const (
firefoxCookieQuery = `SELECT name, value, host, path,
creationTime, expiry, isSecure, isHttpOnly FROM moz_cookies`
firefoxCountCookieQuery = `SELECT COUNT(*) FROM moz_cookies`
)
func extractCookies(path string) ([]types.CookieEntry, error) {
cookies, err := sqliteutil.QueryRows(path, true, firefoxCookieQuery,
@@ -46,3 +49,7 @@ func extractCookies(path string) ([]types.CookieEntry, error) {
})
return cookies, nil
}
func countCookies(path string) (int, error) {
return sqliteutil.CountRows(path, true, firefoxCountCookieQuery)
}
+23 -2
View File
@@ -7,11 +7,16 @@ import (
"github.com/stretchr/testify/require"
)
func TestExtractCookies(t *testing.T) {
path := createTestDB(t, "cookies.sqlite", []string{mozCookiesSchema},
func setupMozCookieDB(t *testing.T) string {
t.Helper()
return createTestDB(t, "cookies.sqlite", []string{mozCookiesSchema},
insertMozCookie("session", "abc123", ".example.com", "/", 1700000000000000, 1800000000, 1, 1),
insertMozCookie("token", "xyz789", ".new.com", "/api", 1710000000000000, 1810000000, 1, 0),
)
}
func TestExtractCookies(t *testing.T) {
path := setupMozCookieDB(t)
got, err := extractCookies(path)
require.NoError(t, err)
@@ -33,3 +38,19 @@ func TestExtractCookies(t *testing.T) {
assert.Equal(t, "abc123", got[1].Value)
assert.True(t, got[1].IsHTTPOnly)
}
func TestCountCookies(t *testing.T) {
path := setupMozCookieDB(t)
count, err := countCookies(path)
require.NoError(t, err)
assert.Equal(t, 2, count)
}
func TestCountCookies_Empty(t *testing.T) {
path := createTestDB(t, "cookies.sqlite", []string{mozCookiesSchema})
count, err := countCookies(path)
require.NoError(t, err)
assert.Equal(t, 0, count)
}
+13 -3
View File
@@ -11,9 +11,15 @@ import (
"github.com/moond4rk/hackbrowserdata/utils/sqliteutil"
)
const firefoxDownloadQuery = `SELECT place_id, GROUP_CONCAT(content), url, dateAdded
FROM (SELECT * FROM moz_annos INNER JOIN moz_places ON moz_annos.place_id=moz_places.id)
t GROUP BY place_id`
const (
firefoxDownloadQuery = `SELECT place_id, GROUP_CONCAT(content), url, dateAdded
FROM (SELECT * FROM moz_annos INNER JOIN moz_places ON moz_annos.place_id=moz_places.id)
t GROUP BY place_id`
firefoxCountDownloadQuery = `SELECT COUNT(*) FROM
(SELECT place_id FROM moz_annos
INNER JOIN moz_places ON moz_annos.place_id=moz_places.id
GROUP BY place_id)`
)
func extractDownloads(path string) ([]types.DownloadEntry, error) {
downloads, err := sqliteutil.QueryRows(path, true, firefoxDownloadQuery,
@@ -52,3 +58,7 @@ func extractDownloads(path string) ([]types.DownloadEntry, error) {
})
return downloads, nil
}
func countDownloads(path string) (int, error) {
return sqliteutil.CountRows(path, true, firefoxCountDownloadQuery)
}
+23 -3
View File
@@ -7,14 +7,18 @@ import (
"github.com/stretchr/testify/require"
)
func TestExtractDownloads(t *testing.T) {
// Downloads require JOIN: moz_annos.place_id = moz_places.id
path := createTestDB(t, "places.sqlite", []string{mozPlacesSchema, mozAnnosSchema},
func setupMozDownloadDB(t *testing.T) string {
t.Helper()
return createTestDB(t, "places.sqlite", []string{mozPlacesSchema, mozAnnosSchema},
insertMozPlace(1, "https://example.com/old.zip", "Old File", 0, 0),
insertMozPlace(2, "https://example.com/new.pdf", "New File", 0, 0),
insertMozAnno(1, "/tmp/old.zip", 1700000000000000),
insertMozAnno(2, "/tmp/new.pdf", 1710000000000000),
)
}
func TestExtractDownloads(t *testing.T) {
path := setupMozDownloadDB(t)
got, err := extractDownloads(path)
require.NoError(t, err)
@@ -28,3 +32,19 @@ func TestExtractDownloads(t *testing.T) {
assert.Equal(t, "/tmp/new.pdf", got[0].TargetPath)
assert.False(t, got[0].StartTime.IsZero())
}
func TestCountDownloads(t *testing.T) {
path := setupMozDownloadDB(t)
count, err := countDownloads(path)
require.NoError(t, err)
assert.Equal(t, 2, count)
}
func TestCountDownloads_Empty(t *testing.T) {
path := createTestDB(t, "places.sqlite", []string{mozPlacesSchema, mozAnnosSchema})
count, err := countDownloads(path)
require.NoError(t, err)
assert.Equal(t, 0, count)
}
+15
View File
@@ -34,3 +34,18 @@ func extractExtensions(path string) ([]types.ExtensionEntry, error) {
return extensions, nil
}
func countExtensions(path string) (int, error) {
data, err := os.ReadFile(path)
if err != nil {
return 0, err
}
var count int
for _, v := range gjson.GetBytes(data, "addons").Array() {
if v.Get("location").String() == "app-profile" {
count++
}
}
return count, nil
}
+23 -2
View File
@@ -7,8 +7,9 @@ import (
"github.com/stretchr/testify/require"
)
func TestExtractExtensions(t *testing.T) {
path := createTestJSON(t, "extensions.json", `{
func setupMozExtensionJSON(t *testing.T) string {
t.Helper()
return createTestJSON(t, "extensions.json", `{
"addons": [
{
"id": "ublock@gorhill.org",
@@ -38,6 +39,10 @@ func TestExtractExtensions(t *testing.T) {
}
]
}`)
}
func TestExtractExtensions(t *testing.T) {
path := setupMozExtensionJSON(t)
got, err := extractExtensions(path)
require.NoError(t, err)
@@ -54,6 +59,22 @@ func TestExtractExtensions(t *testing.T) {
assert.False(t, ids["system@mozilla.org"])
}
func TestCountExtensions(t *testing.T) {
path := setupMozExtensionJSON(t)
count, err := countExtensions(path)
require.NoError(t, err)
assert.Equal(t, 2, count) // system addon filtered out
}
func TestCountExtensions_Empty(t *testing.T) {
path := createTestJSON(t, "extensions.json", `{"addons": []}`)
count, err := countExtensions(path)
require.NoError(t, err)
assert.Equal(t, 0, count)
}
func TestExtractExtensions_EmptyAddons(t *testing.T) {
path := createTestJSON(t, "extensions.json", `{"addons": []}`)
got, err := extractExtensions(path)
+9 -2
View File
@@ -8,8 +8,11 @@ import (
"github.com/moond4rk/hackbrowserdata/utils/sqliteutil"
)
const firefoxHistoryQuery = `SELECT url, COALESCE(last_visit_date, 0),
COALESCE(title, ''), visit_count FROM moz_places`
const (
firefoxHistoryQuery = `SELECT url, COALESCE(last_visit_date, 0),
COALESCE(title, ''), visit_count FROM moz_places`
firefoxCountHistoryQuery = `SELECT COUNT(*) FROM moz_places`
)
func extractHistories(path string) ([]types.HistoryEntry, error) {
histories, err := sqliteutil.QueryRows(path, true, firefoxHistoryQuery,
@@ -36,3 +39,7 @@ func extractHistories(path string) ([]types.HistoryEntry, error) {
})
return histories, nil
}
func countHistories(path string) (int, error) {
return sqliteutil.CountRows(path, true, firefoxCountHistoryQuery)
}
+23 -2
View File
@@ -7,12 +7,17 @@ import (
"github.com/stretchr/testify/require"
)
func TestExtractHistories(t *testing.T) {
path := createTestDB(t, "places.sqlite", []string{mozPlacesSchema},
func setupMozHistoryDB(t *testing.T) string {
t.Helper()
return createTestDB(t, "places.sqlite", []string{mozPlacesSchema},
insertMozPlace(1, "https://github.com", "GitHub", 100, 1700000000000000),
insertMozPlace(2, "https://go.dev", "Go", 50, 1710000000000000),
insertMozPlace(3, "https://example.com", "Example", 200, 1690000000000000),
)
}
func TestExtractHistories(t *testing.T) {
path := setupMozHistoryDB(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 := setupMozHistoryDB(t)
count, err := countHistories(path)
require.NoError(t, err)
assert.Equal(t, 3, count)
}
func TestCountHistories_Empty(t *testing.T) {
path := createTestDB(t, "places.sqlite", []string{mozPlacesSchema})
count, err := countHistories(path)
require.NoError(t, err)
assert.Equal(t, 0, count)
}
func TestExtractHistories_NullFields(t *testing.T) {
path := createTestDB(t, "places.sqlite", []string{mozPlacesSchema},
// last_visit_date=NULL, title=NULL — COALESCE should handle
+8
View File
@@ -13,6 +13,14 @@ import (
"github.com/moond4rk/hackbrowserdata/types"
)
func countPasswords(path string) (int, error) {
data, err := os.ReadFile(path)
if err != nil {
return 0, err
}
return len(gjson.GetBytes(data, "logins").Array()), nil
}
// decryptPBE combines base64 decode + ASN1 PBE parse + decrypt into one call.
func decryptPBE(encoded string, masterKey []byte) ([]byte, error) {
raw, err := base64.StdEncoding.DecodeString(encoded)
+23
View File
@@ -53,6 +53,29 @@ func TestExtractPasswords(t *testing.T) {
assert.False(t, got[0].CreatedAt.IsZero())
}
func TestCountPasswords(t *testing.T) {
json := `{
"logins": [
{"hostname": "https://a.com", "encryptedUsername": "", "encryptedPassword": "", "timeCreated": 1000},
{"hostname": "https://b.com", "encryptedUsername": "", "encryptedPassword": "", "timeCreated": 2000},
{"hostname": "https://c.com", "encryptedUsername": "", "encryptedPassword": "", "timeCreated": 3000}
]
}`
path := createTestJSON(t, "logins.json", json)
count, err := countPasswords(path)
require.NoError(t, err)
assert.Equal(t, 3, count)
}
func TestCountPasswords_Empty(t *testing.T) {
path := createTestJSON(t, "logins.json", `{"logins": []}`)
count, err := countPasswords(path)
require.NoError(t, err)
assert.Equal(t, 0, count)
}
func TestExtractPasswords_FormSubmitURLFallback(t *testing.T) {
encB64 := loginPBEBase64(t)
+8 -1
View File
@@ -9,7 +9,10 @@ import (
"github.com/moond4rk/hackbrowserdata/utils/sqliteutil"
)
const firefoxLocalStorageQuery = `SELECT originKey, key, value FROM webappsstore2`
const (
firefoxLocalStorageQuery = `SELECT originKey, key, value FROM webappsstore2`
firefoxCountLocalStorageQuery = `SELECT COUNT(*) FROM webappsstore2`
)
func extractLocalStorage(path string) ([]types.StorageEntry, error) {
return sqliteutil.QueryRows(path, true, firefoxLocalStorageQuery,
@@ -26,6 +29,10 @@ func extractLocalStorage(path string) ([]types.StorageEntry, error) {
})
}
func countLocalStorage(path string) (int, error) {
return sqliteutil.CountRows(path, true, firefoxCountLocalStorageQuery)
}
func reverseString(s string) string {
b := []byte(s)
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
+23 -2
View File
@@ -7,12 +7,17 @@ import (
"github.com/stretchr/testify/require"
)
func TestExtractLocalStorage(t *testing.T) {
path := createTestDB(t, "webappsstore.sqlite", []string{webappsstore2Schema},
func setupWebappsDB(t *testing.T) string {
t.Helper()
return createTestDB(t, "webappsstore.sqlite", []string{webappsstore2Schema},
insertWebappsstore("moc.buhtig.:https:443", "theme", "dark"),
insertWebappsstore("moc.buhtig.:https:443", "lang", "en"),
insertWebappsstore("moc.elpmaxe.:http:8080", "token", "abc123"),
)
}
func TestExtractLocalStorage(t *testing.T) {
path := setupWebappsDB(t)
got, err := extractLocalStorage(path)
require.NoError(t, err)
@@ -28,6 +33,22 @@ func TestExtractLocalStorage(t *testing.T) {
assert.Equal(t, "abc123", byKey["http://example.com:8080/token"])
}
func TestCountLocalStorage(t *testing.T) {
path := setupWebappsDB(t)
count, err := countLocalStorage(path)
require.NoError(t, err)
assert.Equal(t, 3, count)
}
func TestCountLocalStorage_Empty(t *testing.T) {
path := createTestDB(t, "webappsstore.sqlite", []string{webappsstore2Schema})
count, err := countLocalStorage(path)
require.NoError(t, err)
assert.Equal(t, 0, count)
}
func TestParseOriginKey(t *testing.T) {
tests := []struct {
name string
+51
View File
@@ -83,6 +83,57 @@ 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.Extension:
count, err = countExtensions(path)
case types.LocalStorage:
count, err = countLocalStorage(path)
case types.CreditCard, types.SessionStorage:
// Firefox does not support CreditCard or SessionStorage.
}
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)
+58
View File
@@ -176,6 +176,64 @@ func TestResolveSourcePaths_Partial(t *testing.T) {
assert.NotContains(t, resolved, types.Extension)
}
// ---------------------------------------------------------------------------
// CountEntries
// ---------------------------------------------------------------------------
func TestCountEntries(t *testing.T) {
dir := t.TempDir()
profileDir := filepath.Join(dir, "test-profile")
mkDir(profileDir)
installFile(t, profileDir, setupMozHistoryDB(t), "places.sqlite")
browsers, err := NewBrowsers(types.BrowserConfig{
Name: "Firefox", Kind: types.Firefox, UserDataDir: dir,
})
require.NoError(t, err)
require.Len(t, browsers, 1)
// CountEntries works without master key.
counts, err := browsers[0].CountEntries([]types.Category{types.History})
require.NoError(t, err)
assert.Equal(t, 3, counts[types.History])
}
func TestCountCategory(t *testing.T) {
t.Run("History", func(t *testing.T) {
path := setupMozHistoryDB(t)
b := &Browser{}
assert.Equal(t, 3, b.countCategory(types.History, path))
})
t.Run("Cookie", func(t *testing.T) {
path := setupMozCookieDB(t)
b := &Browser{}
assert.Equal(t, 2, b.countCategory(types.Cookie, path))
})
t.Run("Bookmark", func(t *testing.T) {
path := setupMozBookmarkDB(t)
b := &Browser{}
assert.Equal(t, 2, b.countCategory(types.Bookmark, path))
})
t.Run("Extension", func(t *testing.T) {
path := setupMozExtensionJSON(t)
b := &Browser{}
assert.Equal(t, 2, b.countCategory(types.Extension, path))
})
t.Run("UnsupportedCategory", func(t *testing.T) {
b := &Browser{}
assert.Equal(t, 0, b.countCategory(types.CreditCard, "unused"))
assert.Equal(t, 0, b.countCategory(types.SessionStorage, "unused"))
})
}
// ---------------------------------------------------------------------------
// extractCategory
// ---------------------------------------------------------------------------
// TestExtractCategory verifies that the switch dispatch works for each category.
func TestExtractCategory(t *testing.T) {
t.Run("History", func(t *testing.T) {
+8
View File
@@ -141,6 +141,14 @@ func insertWebappsstore(originKey, key, value string) string {
// Test fixture builders
// ---------------------------------------------------------------------------
// installFile copies a test fixture file into a profile directory.
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))
}
func createTestDB(t *testing.T, name string, schemas []string, inserts ...string) string {
t.Helper()
path := filepath.Join(t.TempDir(), name)