fix(chromium): open LevelDB copies read-only (#662)

A read-write open replays the journal into new files and renames CURRENT in the copy;
on Windows that write can fail, which emptied one profile's session storage in CI.
This commit is contained in:
Roger authored and GitHub committed 2026-10-28 16:57:13 +08:00
1 parent 1592f7b7d6
commit 5f7bfbd455
2 files changed
+43 -2

No files matched your search

+6 -2
View File
@@ -9,6 +9,7 @@ import (
"unicode/utf16"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/record"
@@ -27,11 +28,14 @@ const (
const maxLocalStorageValueLength = 2048
// readOnly keeps the open from replaying the journal into new files in the copy, a write that can fail on Windows.
var readOnly = &opt.Options{ReadOnly: true}
func extractLocalStorage(path string, out browser.Collector) error {
if _, err := os.Stat(path); err != nil {
return fmt.Errorf("leveldb path %q: %w", path, err)
}
db, err := leveldb.OpenFile(path, nil)
db, err := leveldb.OpenFile(path, readOnly)
if err != nil {
return err
}
@@ -163,7 +167,7 @@ func extractSessionStorage(path string, out browser.Collector) error {
if _, err := os.Stat(path); err != nil {
return fmt.Errorf("leveldb path %q: %w", path, err)
}
db, err := leveldb.OpenFile(path, nil)
db, err := leveldb.OpenFile(path, readOnly)
if err != nil {
return err
}
@@ -2,12 +2,15 @@ package chromium
import (
"encoding/binary"
"os"
"path/filepath"
"testing"
"unicode/utf16"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/record"
)
@@ -232,6 +235,40 @@ func TestExtractSessionStorage(t *testing.T) {
assert.Equal(t, "abc123", byKey["https://example.com//token"])
}
// Opening the copy read-write replays its journal into new files; on Windows that write can fail and lose the profile.
func TestExtractStorage_LeavesTheCopyUnchanged(t *testing.T) {
tests := []struct {
name string
setup func(*testing.T) string
extract func(string, browser.Collector) error
}{
{name: "localstorage", setup: setupLocalStorageLevelDB, extract: extractLocalStorage},
{name: "sessionstorage", setup: setupSessionStorageLevelDB, extract: extractSessionStorage},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
dir := tt.setup(t)
before := levelDBFiles(t, dir)
require.NoError(t, tt.extract(dir, &testCollector{}))
assert.Equal(t, before, levelDBFiles(t, dir))
})
}
}
func levelDBFiles(t *testing.T, dir string) map[string]string {
t.Helper()
entries, err := os.ReadDir(dir)
require.NoError(t, err)
files := make(map[string]string, len(entries))
for _, e := range entries {
data, err := os.ReadFile(filepath.Join(dir, e.Name()))
require.NoError(t, err)
files[e.Name()] = string(data)
}
return files
}
func testEncodeLatin1(s string) []byte {
return append([]byte{chromiumStringLatin1Format}, []byte(s)...)
}