feat(safari): localstorage extraction (#582)

* feat(safari): localstorage extraction

Extracts Safari 17+ localStorage from WebKit's nested layout —
WebsiteDataStore/<uuid>/Origins/<top-hash>/<frame-hash>/LocalStorage/
localstorage.sqlite3 for named profiles, WebsiteData/Default for the
default profile. Parses the binary SecurityOrigin serialization
(length-prefixed scheme+host plus 0x00 default-port or 0x01 <uint16_le>
explicit-port section) and decodes UTF-16 LE ItemTable value BLOBs,
capping oversized values at 2048 bytes to match the Chromium extractor.
Reports the frame origin URL so partitioned third-party storage is
attributed to the iframe origin JavaScript actually sees.

Closes the remaining LocalStorage checkbox in #565.

* docs(safari): add RFC-011 data storage

Documents Safari's profile structure, per-category file layouts, and
storage formats including the Safari 17+ nested WebKit Origins
localStorage layout and binary SecurityOrigin serialization. Defers
Keychain credential extraction to RFC-006 §7 and notes the cross-browser
differences (plaintext cookies, plist bookmarks/downloads, Core Data
epoch timestamps, partitioned storage).

* fix(safari): latin-1 origin decoding, NULL key skip, count fast-path

- Decode originEncASCII via decodeLatin1 so high-byte records preserve
  their ISO-8859-1 meaning instead of being interpreted as UTF-8.
  Matches the pattern in chromium/extract_storage.go.
- Skip ItemTable rows where key is NULL — SQLite's UNIQUE constraint
  permits multiple NULLs, and silently lowering them to empty strings
  would collide with legitimate empty-string keys.
- countLocalStorage now walks origin dirs and runs SELECT COUNT(key)
  per localstorage.sqlite3 instead of fully decoding every value.
  COUNT(key) naturally excludes NULLs, keeping count and extract
  symmetric.

Addresses Copilot review feedback on #582.

* fix(safari): round-2 review — WAL replay, stable ordering, error context

- Drop immutable=1 on temp-copy SQLite opens in readLocalStorageFile /
  countLocalStorageFile. Session.Acquire copies the -wal / -shm sidecars,
  so mode=ro alone lets SQLite replay WAL on the ephemeral copy and
  surface entries Safari committed to WAL but hasn't checkpointed yet.
  Live-file reads in profiles.go keep immutable=1 as before.
- Order ItemTable query by (key, rowid) for deterministic exports across
  runs and SQLite versions.
- Wrap os.ReadFile / os.ReadDir errors with the offending path so
  multi-origin debug logs stay scannable.
- RFC-011 §7 rewritten to explain the live-vs-temp split.
- New regression test asserts ORDER BY surfaces rows in key order.

Addresses round-2 Copilot review on #582.
This commit is contained in:
Roger
2026-04-21 20:47:11 +08:00
committed by GitHub
parent d75738b90f
commit 7a5db25b4f
7 changed files with 1157 additions and 11 deletions
+344
View File
@@ -0,0 +1,344 @@
package safari
import (
"database/sql"
"encoding/binary"
"fmt"
"os"
"path/filepath"
"unicode/utf16"
_ "modernc.org/sqlite"
"github.com/moond4rk/hackbrowserdata/log"
"github.com/moond4rk/hackbrowserdata/types"
)
// Modern WebKit (Safari 17+) stores localStorage under a nested, partitioned layout rooted at
// either WebsiteDataStore/<uuid>/Origins (per named profile) or WebsiteData/Default
// (the pre-profile default store). Within that root:
//
// <root>/<top-frame-hash>/<frame-hash>/origin — binary; encodes top+frame origins
// <root>/<top-frame-hash>/<frame-hash>/LocalStorage/localstorage.sqlite3
//
// top-hash == frame-hash ⇒ first-party; they differ for third-party partitioned storage.
// We report the frame origin because that's what window.localStorage exposes to JS.
// ItemTable: (key TEXT UNIQUE ON CONFLICT REPLACE, value BLOB NOT NULL ON CONFLICT FAIL);
// value BLOBs are UTF-16 LE strings.
//
// The flat "LocalStorage/<scheme>_<host>_<port>.localstorage" directory that older builds used
// is empty on current Safari and is no longer a supported source.
const (
webkitOriginFile = "origin"
webkitLocalStorageSubdir = "LocalStorage"
webkitLocalStorageDB = "localstorage.sqlite3"
webkitOriginSaltName = "salt" // HMAC salt sibling of the <hash> dirs; not a data dir
maxLocalStorageValueLength = 2048
)
// origin file encoding-byte constants (WebCore SecurityOrigin serialization).
const (
originEncASCII = 0x01 // Latin-1 / ASCII
originEncUTF16 = 0x00 // UTF-16 LE
)
// Port marker values after the (scheme, host) pair in an origin block.
// 0x00 → port is the scheme default (stored as 0).
// 0x01 → next two bytes are a uint16_le port.
const (
originPortDefaultMarker = 0x00
originPortExplicitFlag = 0x01
)
func extractLocalStorage(root string) ([]types.StorageEntry, error) {
dirs, err := findOriginDataDirs(root)
if err != nil {
return nil, err
}
var entries []types.StorageEntry
for _, od := range dirs {
origin, err := readOriginFile(filepath.Join(od, webkitOriginFile))
if err != nil {
log.Debugf("safari localstorage: origin %s: %v", od, err)
continue
}
dbPath := filepath.Join(od, webkitLocalStorageSubdir, webkitLocalStorageDB)
items, err := readLocalStorageFile(dbPath)
if err != nil {
log.Debugf("safari localstorage: db %s: %v", dbPath, err)
continue
}
for _, it := range items {
entries = append(entries, types.StorageEntry{
URL: origin,
Key: it.key,
Value: it.value,
})
}
}
return entries, nil
}
// countLocalStorage sums ItemTable row counts across every origin DB under root without
// parsing origin files or decoding values — CountEntries callers only need the total, not the
// URLs or plaintext. COUNT(key) naturally excludes NULL keys, matching the same skip rule
// applied by readLocalStorageFile, so count and extract stay in sync.
func countLocalStorage(root string) (int, error) {
dirs, err := findOriginDataDirs(root)
if err != nil {
return 0, err
}
total := 0
for _, od := range dirs {
dbPath := filepath.Join(od, webkitLocalStorageSubdir, webkitLocalStorageDB)
n, err := countLocalStorageFile(dbPath)
if err != nil {
log.Debugf("safari localstorage: count %s: %v", dbPath, err)
continue
}
total += n
}
return total, nil
}
func countLocalStorageFile(path string) (int, error) {
// mode=ro (no immutable) so SQLite replays the copied -wal sidecar — this surfaces entries
// Safari has committed to WAL but not yet checkpointed to the main DB. Writes SQLite might
// make to the temp-copy's -shm during replay are harmless; the Session cleanup removes
// everything. Live-file reads (profiles.go) still use immutable=1 to stay off the real WAL.
dsn := "file:" + path + "?mode=ro"
db, err := sql.Open("sqlite", dsn)
if err != nil {
return 0, fmt.Errorf("open %s: %w", path, err)
}
defer db.Close()
if err := db.Ping(); err != nil {
return 0, fmt.Errorf("ping %s: %w", path, err)
}
var count int
if err := db.QueryRow(`SELECT COUNT(key) FROM ItemTable`).Scan(&count); err != nil {
return 0, fmt.Errorf("count ItemTable: %w", err)
}
return count, nil
}
// findOriginDataDirs returns <root>/<h1>/<h2>/ paths that contain both an "origin" file and
// a "LocalStorage/localstorage.sqlite3" database. Non-directory entries, the "salt" sibling,
// and partition dirs without localStorage data are silently skipped.
func findOriginDataDirs(root string) ([]string, error) {
topEntries, err := os.ReadDir(root)
if err != nil {
return nil, fmt.Errorf("read origins root %s: %w", root, err)
}
var out []string
for _, top := range topEntries {
if !top.IsDir() || top.Name() == webkitOriginSaltName {
continue
}
topPath := filepath.Join(root, top.Name())
frameEntries, err := os.ReadDir(topPath)
if err != nil {
continue
}
for _, frame := range frameEntries {
if !frame.IsDir() {
continue
}
framePath := filepath.Join(topPath, frame.Name())
if _, err := os.Stat(filepath.Join(framePath, webkitOriginFile)); err != nil {
continue
}
dbPath := filepath.Join(framePath, webkitLocalStorageSubdir, webkitLocalStorageDB)
if _, err := os.Stat(dbPath); err != nil {
continue
}
out = append(out, framePath)
}
}
return out, nil
}
// originEndpoint is one half of an origin file (top-frame or frame). Port 0 means the scheme
// default (443 for https, 80 for http) and is omitted from the URL rendering.
type originEndpoint struct {
scheme string
host string
port uint16
}
// readOriginFile parses WebKit's SecurityOrigin binary serialization and returns the frame
// origin URL (scheme://host[:port]). The file holds two origin blocks back-to-back: top-frame
// then frame. When the frame block is missing/unreadable we fall back to the top-frame so we
// can still attribute the data to *something* meaningful.
func readOriginFile(path string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("read origin file %s: %w", path, err)
}
top, pos, terr := readOriginBlock(data, 0)
if terr != nil {
return "", fmt.Errorf("parse top-frame origin: %w", terr)
}
frame, _, ferr := readOriginBlock(data, pos)
if ferr != nil {
// Partitioned info unavailable — attribute to the top-frame origin.
frame = top
}
if frame.scheme == "" || frame.host == "" {
return "", fmt.Errorf("origin file missing scheme/host")
}
return formatOriginURL(frame), nil
}
// readOriginBlock reads one origin block: scheme record, host record, port marker.
// Returns the parsed endpoint and the byte offset immediately after the block.
func readOriginBlock(data []byte, pos int) (originEndpoint, int, error) {
var ep originEndpoint
var err error
ep.scheme, pos, err = readOriginString(data, pos)
if err != nil {
return ep, pos, err
}
ep.host, pos, err = readOriginString(data, pos)
if err != nil {
return ep, pos, err
}
if pos >= len(data) {
return ep, pos, fmt.Errorf("unexpected EOF before port marker")
}
marker := data[pos]
pos++
switch marker {
case originPortDefaultMarker:
ep.port = 0
case originPortExplicitFlag:
if pos+2 > len(data) {
return ep, pos, fmt.Errorf("truncated port value at offset %d", pos)
}
ep.port = binary.LittleEndian.Uint16(data[pos : pos+2])
pos += 2
default:
return ep, pos, fmt.Errorf("unexpected port marker 0x%02x at offset %d", marker, pos-1)
}
return ep, pos, nil
}
// readOriginString consumes one length-prefixed record (uint32_le length + encoding byte + data).
func readOriginString(data []byte, pos int) (string, int, error) {
if pos+5 > len(data) {
return "", pos, fmt.Errorf("truncated string record at offset %d", pos)
}
length := int(binary.LittleEndian.Uint32(data[pos : pos+4]))
enc := data[pos+4]
pos += 5
if length < 0 || pos+length > len(data) {
return "", pos, fmt.Errorf("string record overruns buffer: length %d at offset %d", length, pos-5)
}
chunk := data[pos : pos+length]
pos += length
switch enc {
case originEncASCII:
return decodeLatin1(chunk), pos, nil
case originEncUTF16:
return decodeUTF16LE(chunk), pos, nil
default:
return decodeLatin1(chunk), pos, nil
}
}
// decodeLatin1 converts ISO-8859-1 bytes to a valid UTF-8 Go string. Latin-1 byte values map
// 1:1 to Unicode code points U+0000U+00FF. Mirrors the helper in chromium/extract_storage.go.
func decodeLatin1(b []byte) string {
runes := make([]rune, len(b))
for i, c := range b {
runes[i] = rune(c)
}
return string(runes)
}
func formatOriginURL(ep originEndpoint) string {
url := ep.scheme + "://" + ep.host
if ep.port != 0 {
url += fmt.Sprintf(":%d", ep.port)
}
return url
}
type localStorageItem struct {
key string
value string
}
func readLocalStorageFile(path string) ([]localStorageItem, error) {
// mode=ro (no immutable) — see countLocalStorageFile for the WAL-replay rationale; the same
// live-vs-temp split applies here. ORDER BY key, rowid makes exports byte-for-byte stable
// across runs and SQLite versions.
dsn := "file:" + path + "?mode=ro"
db, err := sql.Open("sqlite", dsn)
if err != nil {
return nil, fmt.Errorf("open %s: %w", path, err)
}
defer db.Close()
if err := db.Ping(); err != nil {
return nil, fmt.Errorf("ping %s: %w", path, err)
}
rows, err := db.Query(`SELECT key, value FROM ItemTable ORDER BY key, rowid`)
if err != nil {
return nil, fmt.Errorf("query ItemTable: %w", err)
}
defer rows.Close()
var items []localStorageItem
for rows.Next() {
var key sql.NullString
var value []byte
if err := rows.Scan(&key, &value); err != nil {
log.Debugf("safari localstorage: scan row in %s: %v", path, err)
continue
}
if !key.Valid {
// NULL keys would collide with legitimate empty-string keys in the output and are
// not meaningful localStorage entries. The UNIQUE constraint in ItemTable still
// permits multiple NULL rows in SQLite, so we filter them here.
log.Debugf("safari localstorage: skip row with NULL key in %s", path)
continue
}
items = append(items, localStorageItem{
key: key.String,
value: decodeLocalStorageValue(value),
})
}
return items, rows.Err()
}
// decodeLocalStorageValue treats the BLOB as UTF-16 LE. Values at or above the cap are replaced
// with a size marker to keep JSON/CSV output bounded, matching chromium/extract_storage.go.
func decodeLocalStorageValue(b []byte) string {
if len(b) >= maxLocalStorageValueLength {
return fmt.Sprintf(
"value is too long, length is %d, supported max length is %d",
len(b), maxLocalStorageValueLength,
)
}
return decodeUTF16LE(b)
}
// decodeUTF16LE returns the input as a Go string on odd-length (malformed) inputs; WebKit values
// are always even-length in practice but we don't want a stray byte to drop a whole row.
func decodeUTF16LE(b []byte) string {
if len(b) == 0 {
return ""
}
if len(b)%2 != 0 {
return string(b)
}
u16 := make([]uint16, len(b)/2)
for i := range u16 {
u16[i] = binary.LittleEndian.Uint16(b[i*2:])
}
return string(utf16.Decode(u16))
}
+335
View File
@@ -0,0 +1,335 @@
package safari
import (
"database/sql"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "modernc.org/sqlite"
)
// ---------------------------------------------------------------------------
// readOriginBlock / readOriginFile
// ---------------------------------------------------------------------------
func TestReadOriginBlock_FirstParty(t *testing.T) {
data := encodeOriginFile("https://example.com", "https://example.com")
top, pos, err := readOriginBlock(data, 0)
require.NoError(t, err)
assert.Equal(t, "https", top.scheme)
assert.Equal(t, "example.com", top.host)
assert.Equal(t, uint16(0), top.port, "port 0 ⇒ scheme default")
frame, _, err := readOriginBlock(data, pos)
require.NoError(t, err)
assert.Equal(t, "https://example.com", formatOriginURL(frame))
}
func TestReadOriginBlock_NonDefaultPort(t *testing.T) {
data := encodeOriginFile("https://example.com:8443", "https://example.com:8443")
top, _, err := readOriginBlock(data, 0)
require.NoError(t, err)
assert.Equal(t, uint16(8443), top.port)
assert.Equal(t, "https://example.com:8443", formatOriginURL(top))
}
func TestReadOriginBlock_Latin1HighByte(t *testing.T) {
// WebKit stores scheme/host records with encoding byte 0x01 = Latin-1. Verify high-byte
// bytes decode as Latin-1 (é = 0xE9) rather than being passed through as invalid UTF-8.
data := []byte{
0x04, 0x00, 0x00, 0x00, 0x01, 'h', 't', 't', 'p', // scheme "http"
0x04, 0x00, 0x00, 0x00, 0x01, 'c', 'a', 'f', 0xe9, // host "café" (Latin-1)
0x00, // port default
}
ep, _, err := readOriginBlock(data, 0)
require.NoError(t, err)
assert.Equal(t, "http", ep.scheme)
assert.Equal(t, "café", ep.host)
}
func TestDecodeLatin1(t *testing.T) {
assert.Equal(t, "café", decodeLatin1([]byte{'c', 'a', 'f', 0xe9}))
assert.Equal(t, "hello", decodeLatin1([]byte("hello")))
assert.Empty(t, decodeLatin1(nil))
}
func TestReadOriginFile_FramePreferred(t *testing.T) {
dir := t.TempDir()
originPath := filepath.Join(dir, "origin")
require.NoError(t, os.WriteFile(originPath,
encodeOriginFile("https://top.example.com", "https://iframe.example.com"), 0o644))
got, err := readOriginFile(originPath)
require.NoError(t, err)
assert.Equal(t, "https://iframe.example.com", got)
}
func TestReadOriginFile_FallbackToTop(t *testing.T) {
// Write only the top-frame block — no frame follows. Extractor should still succeed by
// falling back to the top-frame origin.
var buf []byte
buf = appendOriginBlock(buf, "https://example.com")
originPath := filepath.Join(t.TempDir(), "origin")
require.NoError(t, os.WriteFile(originPath, buf, 0o644))
got, err := readOriginFile(originPath)
require.NoError(t, err)
assert.Equal(t, "https://example.com", got)
}
func TestReadOriginFile_Malformed(t *testing.T) {
originPath := filepath.Join(t.TempDir(), "origin")
require.NoError(t, os.WriteFile(originPath, []byte{0x01, 0x02}, 0o644))
_, err := readOriginFile(originPath)
require.Error(t, err)
}
// ---------------------------------------------------------------------------
// decodeUTF16LE / decodeLocalStorageValue
// ---------------------------------------------------------------------------
func TestDecodeUTF16LE(t *testing.T) {
t.Run("ascii", func(t *testing.T) {
assert.Equal(t, "hello", decodeUTF16LE(encodeUTF16LE("hello")))
})
t.Run("cjk", func(t *testing.T) {
assert.Equal(t, "你好世界", decodeUTF16LE(encodeUTF16LE("你好世界")))
})
t.Run("mixed", func(t *testing.T) {
assert.Equal(t, "hello 世界 🌍", decodeUTF16LE(encodeUTF16LE("hello 世界 🌍")))
})
t.Run("empty", func(t *testing.T) {
assert.Empty(t, decodeUTF16LE(nil))
assert.Empty(t, decodeUTF16LE([]byte{}))
})
t.Run("odd length falls back to raw string", func(t *testing.T) {
assert.Equal(t, "abc", decodeUTF16LE([]byte("abc")))
})
}
func TestDecodeLocalStorageValue_Truncates(t *testing.T) {
// 1100 chars × 2 bytes = 2200 bytes, over the 2048 cap.
oversized := encodeUTF16LE(strings.Repeat("x", 1100))
got := decodeLocalStorageValue(oversized)
assert.Contains(t, got, "too long")
assert.Contains(t, got, "2048")
}
// ---------------------------------------------------------------------------
// extractLocalStorage — end-to-end over real nested layout fixtures
// ---------------------------------------------------------------------------
func TestExtractLocalStorage_SingleOrigin(t *testing.T) {
root := buildTestLocalStorageDir(t, map[string][]testLocalStorageItem{
"https://example.com": {{Key: "auth_token", Value: "abc123"}},
})
entries, err := extractLocalStorage(root)
require.NoError(t, err)
require.Len(t, entries, 1)
assert.Equal(t, "https://example.com", entries[0].URL)
assert.Equal(t, "auth_token", entries[0].Key)
assert.Equal(t, "abc123", entries[0].Value)
assert.False(t, entries[0].IsMeta)
}
func TestExtractLocalStorage_MultiOrigin(t *testing.T) {
root := buildTestLocalStorageDir(t, map[string][]testLocalStorageItem{
"https://github.com": {
{Key: "theme", Value: "dark"},
{Key: "lang", Value: "en"},
},
"https://example.com:8443": {
{Key: "session", Value: "xyz"},
},
})
entries, err := extractLocalStorage(root)
require.NoError(t, err)
require.Len(t, entries, 3)
byURL := make(map[string][]string)
for _, e := range entries {
byURL[e.URL] = append(byURL[e.URL], e.Key+"="+e.Value)
}
assert.ElementsMatch(t, []string{"theme=dark", "lang=en"}, byURL["https://github.com"])
assert.ElementsMatch(t, []string{"session=xyz"}, byURL["https://example.com:8443"])
}
func TestExtractLocalStorage_CJKAndEmoji(t *testing.T) {
root := buildTestLocalStorageDir(t, map[string][]testLocalStorageItem{
"https://example.com": {
{Key: "名字", Value: "张三"},
{Key: "status", Value: "hello 世界 🌍"},
},
})
entries, err := extractLocalStorage(root)
require.NoError(t, err)
require.Len(t, entries, 2)
values := make(map[string]string)
for _, e := range entries {
values[e.Key] = e.Value
}
assert.Equal(t, "张三", values["名字"])
assert.Equal(t, "hello 世界 🌍", values["status"])
}
func TestExtractLocalStorage_EmptyItemTable(t *testing.T) {
root := buildTestLocalStorageDir(t, map[string][]testLocalStorageItem{
"https://example.com": nil,
})
entries, err := extractLocalStorage(root)
require.NoError(t, err)
assert.Empty(t, entries)
}
func TestExtractLocalStorage_TruncatesOversizedValue(t *testing.T) {
root := buildTestLocalStorageDir(t, map[string][]testLocalStorageItem{
"https://example.com": {{Key: "big", Value: strings.Repeat("x", 1100)}},
})
entries, err := extractLocalStorage(root)
require.NoError(t, err)
require.Len(t, entries, 1)
assert.Contains(t, entries[0].Value, "too long")
}
func TestExtractLocalStorage_Partitioned(t *testing.T) {
// Manually construct a partitioned third-party entry: YouTube iframe inside Google top-frame.
root := filepath.Join(t.TempDir(), "Origins")
require.NoError(t, os.MkdirAll(root, 0o755))
writeTestOriginStore(t, root, "topHash", "frameHash",
"https://accounts.google.com", "https://accounts.youtube.com",
[]testLocalStorageItem{{Key: "yt-session", Value: "embedded"}},
)
entries, err := extractLocalStorage(root)
require.NoError(t, err)
require.Len(t, entries, 1)
assert.Equal(t, "https://accounts.youtube.com", entries[0].URL, "frame origin preferred over top-frame")
}
func TestExtractLocalStorage_SkipsSaltAndStrayFiles(t *testing.T) {
root := buildTestLocalStorageDir(t, map[string][]testLocalStorageItem{
"https://example.com": {{Key: "a", Value: "1"}},
})
// Drop a "salt" sibling that must not be traversed, plus a stray file at root.
require.NoError(t, os.WriteFile(filepath.Join(root, "salt"), []byte("pretend salt"), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(root, "README"), []byte("noise"), 0o644))
entries, err := extractLocalStorage(root)
require.NoError(t, err)
require.Len(t, entries, 1)
assert.Equal(t, "https://example.com", entries[0].URL)
}
func TestExtractLocalStorage_SkipsFrameDirsWithoutDB(t *testing.T) {
// Partition dirs that only have "origin" but no LocalStorage/ subdir must not error out —
// real Safari has plenty of these (cookies-only partitions).
root := filepath.Join(t.TempDir(), "Origins")
frameDir := filepath.Join(root, "topHash", "frameHash")
require.NoError(t, os.MkdirAll(frameDir, 0o755))
require.NoError(t, os.WriteFile(
filepath.Join(frameDir, "origin"),
encodeOriginFile("https://example.com", "https://example.com"), 0o644))
entries, err := extractLocalStorage(root)
require.NoError(t, err)
assert.Empty(t, entries)
}
func TestExtractLocalStorage_DirMissing(t *testing.T) {
_, err := extractLocalStorage(filepath.Join(t.TempDir(), "does-not-exist"))
require.Error(t, err)
}
func TestExtractLocalStorage_EmptyRoot(t *testing.T) {
entries, err := extractLocalStorage(t.TempDir())
require.NoError(t, err)
assert.Empty(t, entries)
}
// ---------------------------------------------------------------------------
// countLocalStorage
// ---------------------------------------------------------------------------
func TestCountLocalStorage(t *testing.T) {
root := buildTestLocalStorageDir(t, map[string][]testLocalStorageItem{
"https://a.com": {{Key: "k1", Value: "v1"}, {Key: "k2", Value: "v2"}},
"https://b.com": {{Key: "k3", Value: "v3"}},
"https://c.com:8443": {{Key: "k4", Value: "v4"}},
})
count, err := countLocalStorage(root)
require.NoError(t, err)
assert.Equal(t, 4, count)
}
func TestCountLocalStorage_DirMissing(t *testing.T) {
count, err := countLocalStorage(filepath.Join(t.TempDir(), "nope"))
require.Error(t, err)
assert.Equal(t, 0, count)
}
// ---------------------------------------------------------------------------
// NULL-key handling — readLocalStorageFile / countLocalStorageFile both skip NULL keys,
// keeping count and extract in sync.
// ---------------------------------------------------------------------------
func TestReadLocalStorageFile_SkipsNullKey(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "ls.sqlite3")
writeLocalStorageDB(t, dbPath, []testLocalStorageItem{
{Key: "real", Value: "keeper"},
}, true /*addNullKey*/)
items, err := readLocalStorageFile(dbPath)
require.NoError(t, err)
require.Len(t, items, 1)
assert.Equal(t, "real", items[0].key)
assert.Equal(t, "keeper", items[0].value)
}
func TestCountLocalStorageFile_SkipsNullKey(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "ls.sqlite3")
writeLocalStorageDB(t, dbPath, []testLocalStorageItem{
{Key: "k1", Value: "v1"},
{Key: "k2", Value: "v2"},
}, true /*addNullKey*/)
count, err := countLocalStorageFile(dbPath)
require.NoError(t, err)
assert.Equal(t, 2, count, "NULL keys are excluded from count to match extract's skip rule")
}
func TestReadLocalStorageFile_ReturnsRowsInKeyOrder(t *testing.T) {
// Rows are inserted in reverse alphabetical order; ORDER BY key, rowid in the extractor
// query must surface them ascending so exports are deterministic across runs.
dbPath := filepath.Join(t.TempDir(), "ls.sqlite3")
writeLocalStorageDB(t, dbPath, []testLocalStorageItem{
{Key: "zebra", Value: "z"},
{Key: "mango", Value: "m"},
{Key: "apple", Value: "a"},
}, false /*addNullKey*/)
items, err := readLocalStorageFile(dbPath)
require.NoError(t, err)
require.Len(t, items, 3)
assert.Equal(t, "apple", items[0].key)
assert.Equal(t, "mango", items[1].key)
assert.Equal(t, "zebra", items[2].key)
}
func TestCountLocalStorageFile_MissingTable(t *testing.T) {
// Real Safari has origin dirs with LocalStorage/localstorage.sqlite3 but no ItemTable yet
// (seen during live verification). countLocalStorageFile must surface the error so the
// caller can log-and-skip rather than counting 0 silently.
dbPath := filepath.Join(t.TempDir(), "empty.sqlite3")
db, err := sql.Open("sqlite", dbPath)
require.NoError(t, err)
require.NoError(t, db.Close())
_, err = countLocalStorageFile(dbPath)
require.Error(t, err)
}
+4
View File
@@ -136,6 +136,8 @@ func (b *Browser) extractCategory(data *types.BrowserData, cat types.Category, p
data.Bookmarks, err = extractBookmarks(path)
case types.Download:
data.Downloads, err = extractDownloads(path, b.profile.downloadOwnerUUID())
case types.LocalStorage:
data.LocalStorage, err = extractLocalStorage(path)
default:
return
}
@@ -158,6 +160,8 @@ func (b *Browser) countCategory(cat types.Category, path string) int {
count, err = countBookmarks(path)
case types.Download:
count, err = countDownloads(path, b.profile.downloadOwnerUUID())
case types.LocalStorage:
count, err = countLocalStorage(path)
default:
// Unsupported categories silently return 0.
}
+36
View File
@@ -3,6 +3,7 @@ package safari
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -78,6 +79,7 @@ func TestNewBrowsers(t *testing.T) {
func TestNewBrowsers_MultiProfile(t *testing.T) {
const uuid = "5604E6F5-02ED-4E40-8249-63DE7BC986C8"
uuidLower := strings.ToLower(uuid)
// Build a pretend ~/Library that mirrors a macOS 14+ layout.
library := t.TempDir()
@@ -91,6 +93,11 @@ func TestNewBrowsers_MultiProfile(t *testing.T) {
// Named profile data under the container.
mkFile(t, container, "Safari", "Profiles", uuid, "History.db")
// Named profile's Origins directory (Safari 17+ nested localStorage root) — must exist
// for resolveSourcePaths to register it.
namedOriginsDir := filepath.Join(container, "WebKit", "WebsiteDataStore", uuidLower, "Origins")
require.NoError(t, os.MkdirAll(namedOriginsDir, 0o755))
// SafariTabs.db registering the named profile with a human-readable title.
writeSafariTabsDB(t, filepath.Join(container, safariTabsDBRelPath), []tabRow{
{uuid: "DefaultProfile", title: ""},
@@ -112,12 +119,18 @@ func TestNewBrowsers_MultiProfile(t *testing.T) {
assert.Equal(t, legacyHome, b.ProfileDir())
assert.Contains(t, b.sourcePaths, types.History)
assert.Equal(t, filepath.Join(legacyHome, "History.db"), b.sourcePaths[types.History].absPath)
// Default profile's LocalStorage root (WebsiteData/Default) isn't created in this fixture,
// so it won't resolve — which is the point: resolveSourcePaths only registers paths that exist.
assert.NotContains(t, b.sourcePaths, types.LocalStorage)
case "work":
assert.Equal(t, filepath.Join(container, "Safari", "Profiles", uuid), b.ProfileDir())
assert.Contains(t, b.sourcePaths, types.History)
assert.Equal(t,
filepath.Join(container, "Safari", "Profiles", uuid, "History.db"),
b.sourcePaths[types.History].absPath)
require.Contains(t, b.sourcePaths, types.LocalStorage)
assert.Equal(t, namedOriginsDir, b.sourcePaths[types.LocalStorage].absPath)
assert.True(t, b.sourcePaths[types.LocalStorage].isDir)
}
}
}
@@ -216,6 +229,15 @@ func TestCountCategory(t *testing.T) {
assert.Equal(t, 1, b.countCategory(types.Download, path))
})
t.Run("LocalStorage", func(t *testing.T) {
dir := buildTestLocalStorageDir(t, map[string][]testLocalStorageItem{
"https://example.com": {{Key: "k1", Value: "v1"}, {Key: "k2", Value: "v2"}},
"https://go.dev": {{Key: "theme", Value: "dark"}},
})
b := &Browser{}
assert.Equal(t, 3, b.countCategory(types.LocalStorage, dir))
})
t.Run("UnsupportedCategory", func(t *testing.T) {
b := &Browser{}
assert.Equal(t, 0, b.countCategory(types.CreditCard, "unused"))
@@ -291,6 +313,20 @@ func TestExtractCategory(t *testing.T) {
assert.Equal(t, int64(1024), data.Downloads[0].TotalBytes)
})
t.Run("LocalStorage", func(t *testing.T) {
dir := buildTestLocalStorageDir(t, map[string][]testLocalStorageItem{
"https://github.com": {{Key: "theme", Value: "dark"}},
})
b := &Browser{}
data := &types.BrowserData{}
b.extractCategory(data, types.LocalStorage, dir)
require.Len(t, data.LocalStorage, 1)
assert.Equal(t, "https://github.com", data.LocalStorage[0].URL)
assert.Equal(t, "theme", data.LocalStorage[0].Key)
assert.Equal(t, "dark", data.LocalStorage[0].Value)
})
t.Run("UnsupportedCategory", func(t *testing.T) {
b := &Browser{}
data := &types.BrowserData{}
+16 -11
View File
@@ -12,6 +12,7 @@ type sourcePath struct {
}
func file(abs string) sourcePath { return sourcePath{abs: abs} }
func dir(abs string) sourcePath { return sourcePath{abs: abs, isDir: true} }
// buildSources dispatches between the default and named-profile path layouts.
//
@@ -28,33 +29,37 @@ func buildSources(p profileContext) map[types.Category][]sourcePath {
}
// defaultSources: cookies try macOS 14+ container first, then the ≤13 legacy path.
// LocalStorage for the default profile lives under WebsiteData/Default — the pre-profile-era
// WebKit store that stays readable even after profiles are introduced.
func defaultSources(p profileContext) map[types.Category][]sourcePath {
home := p.legacyHome
containerCookies := filepath.Join(p.container, "Cookies", "Cookies.binarycookies")
legacyCookies := filepath.Join(filepath.Dir(home), "Cookies", "Cookies.binarycookies")
defaultLocalStorage := filepath.Join(p.container, "WebKit", "WebsiteData", "Default")
return map[types.Category][]sourcePath{
types.History: {file(filepath.Join(home, "History.db"))},
types.Cookie: {file(containerCookies), file(legacyCookies)},
types.Bookmark: {file(filepath.Join(home, "Bookmarks.plist"))},
types.Download: {file(filepath.Join(home, "Downloads.plist"))},
types.History: {file(filepath.Join(home, "History.db"))},
types.Cookie: {file(containerCookies), file(legacyCookies)},
types.Bookmark: {file(filepath.Join(home, "Bookmarks.plist"))},
types.Download: {file(filepath.Join(home, "Downloads.plist"))},
types.LocalStorage: {dir(defaultLocalStorage)},
}
}
// namedSources omits Bookmark (shared plist with no per-entry profile tag, so attributed to default).
// Download is included because Downloads.plist carries DownloadEntryProfileUUIDStringKey per entry;
// extractDownloads filters by owner UUID so default and named profiles each see their own downloads.
//
// LocalStorage slot for a follow-up PR:
//
// file(filepath.Join(p.container, "WebKit/WebsiteDataStore", p.uuidLower, "LocalStorage"))
// LocalStorage lives under WebKit/WebsiteDataStore/<uuidLower>/Origins — Safari 17+ uses a nested
// <top-frame-hash>/<frame-hash>/LocalStorage/localstorage.sqlite3 layout; the flat
// WebsiteDataStore/<uuid>/LocalStorage directory from older builds is empty on modern Safari.
func namedSources(p profileContext) map[types.Category][]sourcePath {
profileDir := filepath.Join(p.container, "Safari", "Profiles", p.uuidUpper)
webkitStore := filepath.Join(p.container, "WebKit", "WebsiteDataStore", p.uuidLower)
return map[types.Category][]sourcePath{
types.History: {file(filepath.Join(profileDir, "History.db"))},
types.Cookie: {file(filepath.Join(webkitStore, "Cookies", "Cookies.binarycookies"))},
types.Download: {file(filepath.Join(p.legacyHome, "Downloads.plist"))},
types.History: {file(filepath.Join(profileDir, "History.db"))},
types.Cookie: {file(filepath.Join(webkitStore, "Cookies", "Cookies.binarycookies"))},
types.Download: {file(filepath.Join(p.legacyHome, "Downloads.plist"))},
types.LocalStorage: {dir(filepath.Join(webkitStore, "Origins"))},
}
}
+149
View File
@@ -2,10 +2,14 @@ package safari
import (
"database/sql"
"encoding/binary"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"unicode/utf16"
"github.com/stretchr/testify/require"
_ "modernc.org/sqlite"
@@ -119,3 +123,148 @@ func writeSafariTabsDB(t *testing.T, path string, rows []tabRow) {
require.NoError(t, err)
}
}
// ---------------------------------------------------------------------------
// LocalStorage fixtures — modern WebKit nested Origins layout
// ---------------------------------------------------------------------------
// testLocalStorageItem is one key/value pair written to an ItemTable row.
// Value is encoded as UTF-16 LE, matching WebKit's on-disk format.
type testLocalStorageItem struct {
Key, Value string
}
// buildTestLocalStorageDir creates a root dir that mirrors Safari 17+'s nested
// localStorage layout (<root>/<h1>/<h2>/origin + LocalStorage/localstorage.sqlite3)
// for each origin URL passed in. Origins are written as first-party (top == frame);
// for partitioned-origin coverage, use buildTestPartitionedLocalStorage.
func buildTestLocalStorageDir(t *testing.T, origins map[string][]testLocalStorageItem) string {
t.Helper()
root := filepath.Join(t.TempDir(), "Origins")
require.NoError(t, os.MkdirAll(root, 0o755))
i := 0
for origin, items := range origins {
hash := fmt.Sprintf("h%02d", i)
i++
writeTestOriginStore(t, root, hash, hash, origin, origin, items)
}
return root
}
// writeTestOriginStore writes one <root>/<topHash>/<frameHash>/ tree with the given
// origins encoded into the binary origin file and items inserted into localstorage.sqlite3.
func writeTestOriginStore(t *testing.T, root, topHash, frameHash, topOrigin, frameOrigin string, items []testLocalStorageItem) {
t.Helper()
frameDir := filepath.Join(root, topHash, frameHash)
require.NoError(t, os.MkdirAll(filepath.Join(frameDir, webkitLocalStorageSubdir), 0o755))
require.NoError(t, os.WriteFile(
filepath.Join(frameDir, webkitOriginFile),
encodeOriginFile(topOrigin, frameOrigin),
0o644,
))
dbPath := filepath.Join(frameDir, webkitLocalStorageSubdir, webkitLocalStorageDB)
db, err := sql.Open("sqlite", dbPath)
require.NoError(t, err)
_, err = db.Exec(`CREATE TABLE ItemTable (key TEXT UNIQUE ON CONFLICT REPLACE, value BLOB NOT NULL ON CONFLICT FAIL)`)
require.NoError(t, err)
for _, item := range items {
_, err = db.Exec(
`INSERT INTO ItemTable (key, value) VALUES (?, ?)`,
item.Key, encodeUTF16LE(item.Value),
)
require.NoError(t, err)
}
require.NoError(t, db.Close())
}
// encodeOriginFile mirrors WebKit's SecurityOrigin binary serialization. Layout per origin
// block: length-prefixed scheme record, length-prefixed host record, then a port marker
// (0x00 for the scheme default, or 0x01 + uint16_le port). Two blocks back-to-back: top-frame
// then frame.
func encodeOriginFile(topOrigin, frameOrigin string) []byte {
var buf []byte
buf = appendOriginBlock(buf, topOrigin)
buf = appendOriginBlock(buf, frameOrigin)
return buf
}
func appendOriginBlock(buf []byte, originURL string) []byte {
scheme, host, port := splitTestOriginURL(originURL)
buf = appendOriginRecord(buf, scheme)
buf = appendOriginRecord(buf, host)
if port == 0 {
buf = append(buf, originPortDefaultMarker)
return buf
}
buf = append(buf, originPortExplicitFlag)
portBytes := make([]byte, 2)
binary.LittleEndian.PutUint16(portBytes, port)
return append(buf, portBytes...)
}
func appendOriginRecord(buf []byte, s string) []byte {
lenBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(lenBytes, uint32(len(s)))
buf = append(buf, lenBytes...)
buf = append(buf, originEncASCII)
return append(buf, []byte(s)...)
}
// splitTestOriginURL parses "https://example.com[:port]" into (scheme, host, port).
// Port 0 means the URL had no explicit port (use scheme default).
func splitTestOriginURL(u string) (scheme, host string, port uint16) {
idx := strings.Index(u, "://")
if idx < 0 {
return "", u, 0
}
scheme = u[:idx]
rest := u[idx+3:]
if colon := strings.LastIndex(rest, ":"); colon >= 0 {
if p, err := strconv.ParseUint(rest[colon+1:], 10, 16); err == nil {
return scheme, rest[:colon], uint16(p)
}
}
return scheme, rest, 0
}
// writeLocalStorageDB creates a minimal localstorage.sqlite3 at path with ItemTable populated
// from items. When addNullKey is true, a NULL-key row is inserted first to exercise the
// skip-NULL-key logic in readLocalStorageFile / countLocalStorageFile. This is a direct-DB
// variant of buildTestLocalStorageDir — use it when the test targets one DB, not the full
// Origins nesting.
func writeLocalStorageDB(t *testing.T, path string, items []testLocalStorageItem, addNullKey bool) {
t.Helper()
db, err := sql.Open("sqlite", path)
require.NoError(t, err)
_, err = db.Exec(`CREATE TABLE ItemTable (key TEXT UNIQUE ON CONFLICT REPLACE, value BLOB NOT NULL ON CONFLICT FAIL)`)
require.NoError(t, err)
if addNullKey {
_, err = db.Exec(
`INSERT INTO ItemTable (key, value) VALUES (NULL, ?)`,
encodeUTF16LE("null-key-sentinel"),
)
require.NoError(t, err)
}
for _, item := range items {
_, err = db.Exec(
`INSERT INTO ItemTable (key, value) VALUES (?, ?)`,
item.Key, encodeUTF16LE(item.Value),
)
require.NoError(t, err)
}
require.NoError(t, db.Close())
}
// encodeUTF16LE is the inverse of extract_storage.go's decodeUTF16LE — it mirrors
// the WebKit encoding so test fixtures round-trip through the extractor.
func encodeUTF16LE(s string) []byte {
u16 := utf16.Encode([]rune(s))
buf := make([]byte, 2*len(u16))
for i, r := range u16 {
binary.LittleEndian.PutUint16(buf[i*2:], r)
}
return buf
}