Files
HackBrowserData/internal/browser/chromium/extract_cookie.go
T
Roger fed6c722e2 refactor: open SQLite in one place and stream records (#688)
* refactor: stream records through sqlitex and the collector

A row that failed to scan was logged at debug and dropped, so it never counted toward partial; it now reaches Collector.Fail.
Every SQLite open goes through sqlitex with options instead of a bool, and extractors add records as they read them.

* refactor: read SQLite rows with plain loops

EachRow hid each extractor's loop and its skip rule behind two callbacks; every extractor now opens, queries, and walks
its own rows, so the control flow reads top to bottom.

* refactor(browser): drop the memoizing key closures

Firefox derives its key once before the login loop, and Yandex loads its data key at the first row, so the state sits
in plain variables where the loop uses it instead of inside a closure.

* fix(safari): report unreadable localStorage origins

A broken origin, database or row was only logged at debug level, so it never counted toward a partial result.
2026-11-09 17:25:36 +08:00

91 lines
2.3 KiB
Go

package chromium
import (
"bytes"
"crypto/sha256"
"fmt"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/record"
"github.com/moond4rk/hackbrowserdata/internal/sqlitex"
"github.com/moond4rk/hackbrowserdata/internal/timeconv"
)
const cookieQuery = `SELECT name, encrypted_value, host_key, path,
creation_utc, expires_utc, is_secure, is_httponly,
has_expires, is_persistent, samesite FROM cookies`
func extractCookies(masterKeys masterKeysFunc, path string, out browser.Collector) error {
database, err := sqlitex.Open(path)
if err != nil {
return err
}
defer database.Close()
rows, err := database.Query(cookieQuery)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var (
name, host, cookiePath string
isSecure, isHTTPOnly int
hasExpire, isPersistent int
createdAt, expireAt int64
encryptedValue []byte
sameSite int
)
if err := rows.Scan(&name, &encryptedValue, &host, &cookiePath,
&createdAt, &expireAt, &isSecure, &isHTTPOnly,
&hasExpire, &isPersistent, &sameSite); err != nil {
out.Fail(err)
continue
}
value, err := masterKeys.decrypt(encryptedValue)
if err != nil {
out.Fail(fmt.Errorf("decrypt cookie %s on %s: %w", name, host, err))
}
value = stripCookieHash(value, host)
sameSiteName := "unspecified"
switch sameSite {
case 0:
sameSiteName = "none"
case 1:
sameSiteName = "lax"
case 2:
sameSiteName = "strict"
case -1:
// not specified by Set-Cookie
}
out.Add(&record.Cookie{
Name: name,
Host: host,
Path: cookiePath,
Value: string(value),
IsSecure: isSecure != 0,
IsHTTPOnly: isHTTPOnly != 0,
HasExpire: hasExpire != 0,
IsPersistent: isPersistent != 0,
ExpireAt: timeconv.Chromium(expireAt),
CreatedAt: timeconv.Chromium(createdAt),
SameSite: sameSiteName,
})
}
return rows.Err()
}
// stripCookieHash removes the SHA256(host_key) Chrome 130+ prepends; a value without that prefix is returned as is.
func stripCookieHash(value []byte, hostKey string) []byte {
if len(value) < sha256.Size {
return value
}
hash := sha256.Sum256([]byte(hostKey))
if bytes.Equal(value[:sha256.Size], hash[:]) {
return value[sha256.Size:] // empty slice if value was exactly 32 bytes
}
return value
}