mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-05-19 18:58:03 +02:00
feat: add browserdata/datautil helpers (#513)
* feat: add browserdata/datautil helpers (QuerySQLite, QueryRows, DecryptChromiumValue) Phase 2 of architecture refactoring (RFC-002 Section 3): - datautil/sqlite.go: QuerySQLite() — shared SQLite open/query/scan helper with optional journal_mode=off for Firefox databases - datautil/query.go: QueryRows[T]() — generic helper (Go 1.20) that wraps QuerySQLite and collects results into a typed slice - datautil/decrypt.go: DecryptChromiumValue() — unified Chromium decryption (DPAPI first, then AES-GCM/CBC fallback) - datautil/sqlite_test.go: tests for all helpers * refactor: move DecryptChromiumValue from datautil to browser/chromium - Remove browserdata/datautil/decrypt.go (Chromium-specific, not a generic util) - Will be added as browser/chromium/decrypt.go (unexported decryptValue) in the chromium extract methods PR - Update RFCs to reflect the change - Remove decrypt test from datautil tests * refactor: move datautil to utils/sqliteutil for consistency - Rename browserdata/datautil/ → utils/sqliteutil/ - Aligns with existing utils/ convention (fileutil, typeutil, byteutil) - QuerySQLite/QueryRows are generic SQLite helpers, not browserdata-specific - Update package name from datautil to sqliteutil - Update both RFCs to reflect new location * fix: apply review suggestions for sqliteutil - QuerySQLite: validate dbPath exists before sql.Open to prevent silently creating empty databases - Tests: check db.Close() errors with require.NoError
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package sqliteutil
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
// sqlite3 driver for database/sql
|
||||
_ "modernc.org/sqlite"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/log"
|
||||
)
|
||||
|
||||
// QuerySQLite opens a SQLite database, optionally disables journal mode (required
|
||||
// for Firefox databases), runs the query, and calls scanFn for each row.
|
||||
//
|
||||
// It validates the database file exists before opening to prevent sql.Open from
|
||||
// silently creating an empty database.
|
||||
//
|
||||
// scanFn should return nil to continue iteration, or an error to skip the current
|
||||
// row (the error is logged at debug level and iteration continues).
|
||||
func QuerySQLite(dbPath string, journalOff bool, query string, scanFn func(*sql.Rows) error) error {
|
||||
if _, err := os.Stat(dbPath); err != nil {
|
||||
return fmt.Errorf("database file: %w", err)
|
||||
}
|
||||
db, err := sql.Open("sqlite", dbPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
if journalOff {
|
||||
if _, err := db.Exec("PRAGMA journal_mode=off"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
rows, err := db.Query(query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
if err := scanFn(rows); err != nil {
|
||||
log.Debugf("scan row error: %v", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user