go-sqlite / sqlite3

pure-Go sqlite3 file reader
BSD 3-Clause "New" or "Revised" License
140 stars 19 forks source link

Fails to even Open Chrome cookies database #4

Closed zellyn closed 6 years ago

zellyn commented 6 years ago

Heh. I know this is ancient code, but thought you might like to know. I'm trying to port this cookie-reading code to your go-native sqlite3 reader, since compiling and linking the full sqlite implementation for such a simple query is overkill.

But just opening the database fails. Here's what I'm trying:

usr, _ := user.Current()
cookiesFile := fmt.Sprintf("%s/Library/Application Support/Google/Chrome/Default/Cookies", usr.HomeDir)

db, err := sqlite3.Open(cookiesFile)
if err != nil {
    return err
}
defer db.Close()

I get this panic:

db: sqlite3.dbHeader{Magic:[16]uint8{0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x33, 0x0}, PageSize:0x400, WVersion:0x1, RVersion:0x1, NReserved:0x0, MaxFraction:0x40, MinFraction:0x20, LeafFraction:0x20, NFileChanges:429139, DbSize:1149, FreePage:657, NFreePages:53, SchemaCookie:[4]uint8{0x0, 0x0, 0x0, 0x4}, SchemaFormat:4, PageCacheSize:0, AutoVacuum:0, DbEncoding:1, UserVersion:0, IncrVacuum:0, ApplicationID:0, XXX_reserved:[20]uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, VersionValid:429139, SqliteVersion:3020000}
>>> bt-hdr: sqlite3.btheader{raw:struct { Kind sqlite3.PageKind; FreeBlockOffset int16; NCells int16; CellsOffset int16; NFreeBytes int8 }{Kind:0xd, FreeBlockOffset:1020, NCells:6, CellsOffset:174, NFreeBytes:0}}
>>> init... (ncells=6)
>>> def: "key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR" => ncols=2
panic: runtime error: slice bounds out of range

goroutine 1 [running]:
github.com/sbinet/sqlite3.(*page).Read(0xc4200640e0, 0xc4200b2400, 0x3dd, 0x3dd, 0x3dd, 0x3dd, 0x0)
    /Users/zellyn/Development/go/src/github.com/sbinet/sqlite3/page.go:138 +0xa9
github.com/sbinet/sqlite3.(*btreeTable).parseCell(0xc4200640c0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
    /Users/zellyn/Development/go/src/github.com/sbinet/sqlite3/btree.go:291 +0x520
github.com/sbinet/sqlite3.(*btreeTable).load(0xc4200640c0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
    /Users/zellyn/Development/go/src/github.com/sbinet/sqlite3/btree.go:116 +0x10a
github.com/sbinet/sqlite3.(*DbFile).init(0xc42008e0c0, 0x0, 0x2)
    /Users/zellyn/Development/go/src/github.com/sbinet/sqlite3/file.go:170 +0x420
github.com/sbinet/sqlite3.Open(0xc42001c2d0, 0x47, 0x0, 0x0, 0x0)
    /Users/zellyn/Development/go/src/github.com/sbinet/sqlite3/file.go:109 +0x5a9
main.run(0x0, 0x0)
    /Users/zellyn/Development/go/src/square/up/util/cookieutil/main.go:36 +0xe1
main.main()
    /Users/zellyn/Development/go/src/square/up/util/cookieutil/main.go:50 +0x26
exit status 2

Not sure if you'd still care, or have enough context to debug. :-)

Oh, Mac OS High Sierra, fwiw.

sbinet commented 6 years ago

@zellyn I think I fixed this particular panic in 81ef595. could you confirm ?

zellyn commented 6 years ago

The panic at github.com/sbinet/sqlite3/page.go:138 moves to github.com/sbinet/sqlite3/page.go:144, but otherwise the error remains unchanged.

Actually, that's not true. The NFileChanges:429337 changes to NFileChanges:429338, and the VersionValid:429337 changes to VersionValid:429338.

zellyn commented 6 years ago

What OS are you on? If you can find your Chrome cookie database, you can probably reproduce it yourself easily. Here's the whole program I'm running to reproduce it:

package main

import (
    "fmt"
    "os"
    "os/user"

    "github.com/sbinet/sqlite3"
)

func run() error {
    usr, _ := user.Current()
    cookiesFile := fmt.Sprintf("%s/Library/Application Support/Google/Chrome/Default/Cookies", usr.HomeDir)

    db, err := sqlite3.Open(cookiesFile)
    if err != nil {
        return err
    }
    defer db.Close()

    for _, table := range db.Tables() {
        fmt.Printf(">>> table=%#v\n", table)
    }

    return nil
}

func main() {
    if err := run(); err != nil {
        fmt.Fprintf(os.Stderr, "Got error: %v", err)
    }
}
sbinet commented 6 years ago

I am on Linux.

this worked for me:

$> go run ./n.go /home/binet/.config/chromium/Default/Cookies
db: sqlite3.dbHeader{Magic:[16]uint8{0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x33, 0x0}, PageSize:0x400, WVersion:0x1, RVersion:0x1, NReserved:0x0, MaxFraction:0x40, MinFraction:0x20, LeafFraction:0x20, NFileChanges:6067689, DbSize:4802, FreePage:2084, NFreePages:3750, SchemaCookie:[4]uint8{0x0, 0x0, 0x0, 0x7}, SchemaFormat:3, PageCacheSize:0, AutoVacuum:0, DbEncoding:1, UserVersion:0, IncrVacuum:0, ApplicationID:0, XXX_reserved:[20]uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, VersionValid:6067689, SqliteVersion:3020000}
>>> bt-hdr: sqlite3.btheader{raw:struct { Kind sqlite3.PageKind; FreeBlockOffset int16; NCells int16; CellsOffset int16; NFreeBytes int8 }{Kind:0xd, FreeBlockOffset:0, NCells:6, CellsOffset:193, NFreeBytes:0}}
>>> init... (ncells=6)
>>> def: "key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR" => ncols=2
2017/12/01 16:12:16 **WARNING** decoding overflow-page not implemented
>>> def: "creation_utc INTEGER NOT NULL UNIQUE PRIMARY KEY,host_key TEXT NOT NULL,name TEXT NOT NULL,value TEXT NOT NULL,path TEXT N" => ncols=5
>>> table=sqlite3.Table{name:"meta", pageid:2, cols:[]sqlite3.Column{sqlite3.Column{name:"", typ:reflect.Type(nil)}, sqlite3.Column{name:"", typ:reflect.Type(nil)}}}
>>> table=sqlite3.Table{name:"cookies", pageid:4, cols:[]sqlite3.Column{sqlite3.Column{name:"", typ:reflect.Type(nil)}, sqlite3.Column{name:"", typ:reflect.Type(nil)}, sqlite3.Column{name:"", typ:reflect.Type(nil)}, sqlite3.Column{name:"", typ:reflect.Type(nil)}, sqlite3.Column{name:"", typ:reflect.Type(nil)}}}
zellyn commented 6 years ago

Oh lovely. :-)

I created a new user, logged in to one site, then closed the window. Here's the cookie file. Does that work or break for you?

mostly-empty-cookie-db.zip

sbinet commented 6 years ago

ok. now I can reproduce :)