|
| 1 | +package safari |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "math" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// buildTestBinaryCookies constructs a minimal valid Cookies.binarycookies file |
| 15 | +// containing the given cookies. Each cookie is placed in its own page. |
| 16 | +func buildTestBinaryCookies(t *testing.T, cookies []testCookie) string { |
| 17 | + t.Helper() |
| 18 | + |
| 19 | + var pages [][]byte |
| 20 | + for _, c := range cookies { |
| 21 | + pages = append(pages, buildPage(c)) |
| 22 | + } |
| 23 | + |
| 24 | + path := filepath.Join(t.TempDir(), "Cookies.binarycookies") |
| 25 | + f, err := os.Create(path) |
| 26 | + require.NoError(t, err) |
| 27 | + defer f.Close() |
| 28 | + |
| 29 | + // File header: magic + numPages (big-endian) |
| 30 | + _, err = f.WriteString("cook") |
| 31 | + require.NoError(t, err) |
| 32 | + require.NoError(t, binary.Write(f, binary.BigEndian, uint32(len(pages)))) |
| 33 | + |
| 34 | + // Page sizes (big-endian) |
| 35 | + for _, p := range pages { |
| 36 | + require.NoError(t, binary.Write(f, binary.BigEndian, uint32(len(p)))) |
| 37 | + } |
| 38 | + |
| 39 | + // Page data |
| 40 | + for _, p := range pages { |
| 41 | + _, err = f.Write(p) |
| 42 | + require.NoError(t, err) |
| 43 | + } |
| 44 | + |
| 45 | + // Checksum (8 bytes, not validated by decoder) |
| 46 | + _, err = f.Write(make([]byte, 8)) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + return path |
| 50 | +} |
| 51 | + |
| 52 | +type testCookie struct { |
| 53 | + domain, name, path, value string |
| 54 | + secure, httpOnly bool |
| 55 | + expires, creation float64 // Core Data epoch seconds |
| 56 | +} |
| 57 | + |
| 58 | +func buildPage(c testCookie) []byte { |
| 59 | + // Cookie string data: domain\0 name\0 path\0 value\0 |
| 60 | + domain := c.domain + "\x00" |
| 61 | + name := c.name + "\x00" |
| 62 | + cpath := c.path + "\x00" |
| 63 | + value := c.value + "\x00" |
| 64 | + |
| 65 | + // Cookie binary layout (all offsets are from cookie start): |
| 66 | + // size(4) + unknown1(4) + flags(4) + unknown2(4) |
| 67 | + // + domainOff(4) + nameOff(4) + pathOff(4) + valueOff(4) + commentOff(4) |
| 68 | + // + endHeader(4) + expires(8) + creation(8) |
| 69 | + // = 56 bytes header, then string data |
| 70 | + const headerSize = 56 |
| 71 | + domainOffset := uint32(headerSize) |
| 72 | + nameOffset := domainOffset + uint32(len(domain)) |
| 73 | + pathOffset := nameOffset + uint32(len(name)) |
| 74 | + valueOffset := pathOffset + uint32(len(cpath)) |
| 75 | + cookieSize := valueOffset + uint32(len(value)) |
| 76 | + |
| 77 | + var flags uint32 |
| 78 | + switch { |
| 79 | + case c.secure && c.httpOnly: |
| 80 | + flags = 0x5 |
| 81 | + case c.httpOnly: |
| 82 | + flags = 0x4 |
| 83 | + case c.secure: |
| 84 | + flags = 0x1 |
| 85 | + } |
| 86 | + |
| 87 | + // Build cookie bytes (little-endian) |
| 88 | + cookie := make([]byte, cookieSize) |
| 89 | + binary.LittleEndian.PutUint32(cookie[0:], cookieSize) |
| 90 | + // cookie[4:8] = unknown1 (zero) |
| 91 | + binary.LittleEndian.PutUint32(cookie[8:], flags) |
| 92 | + // cookie[12:16] = unknown2 (zero) |
| 93 | + binary.LittleEndian.PutUint32(cookie[16:], domainOffset) |
| 94 | + binary.LittleEndian.PutUint32(cookie[20:], nameOffset) |
| 95 | + binary.LittleEndian.PutUint32(cookie[24:], pathOffset) |
| 96 | + binary.LittleEndian.PutUint32(cookie[28:], valueOffset) |
| 97 | + // cookie[32:36] = commentOffset (zero = no comment) |
| 98 | + // cookie[36:40] = endHeader marker (zero) |
| 99 | + binary.LittleEndian.PutUint64(cookie[40:], math.Float64bits(c.expires)) |
| 100 | + binary.LittleEndian.PutUint64(cookie[48:], math.Float64bits(c.creation)) |
| 101 | + copy(cookie[domainOffset:], domain) |
| 102 | + copy(cookie[nameOffset:], name) |
| 103 | + copy(cookie[pathOffset:], cpath) |
| 104 | + copy(cookie[valueOffset:], value) |
| 105 | + |
| 106 | + // Page layout: marker(4) + cookieCount(4) + offsets(4*N) + endMarker(4) + cookies |
| 107 | + const pageHeaderSize = 16 // marker + count + 1 offset + end marker |
| 108 | + page := make([]byte, pageHeaderSize+len(cookie)) |
| 109 | + copy(page[0:4], []byte{0x00, 0x00, 0x01, 0x00}) // page start marker |
| 110 | + binary.LittleEndian.PutUint32(page[4:], 1) // 1 cookie |
| 111 | + binary.LittleEndian.PutUint32(page[8:], pageHeaderSize) |
| 112 | + // page[12:16] = page end marker (zero) |
| 113 | + copy(page[pageHeaderSize:], cookie) |
| 114 | + |
| 115 | + return page |
| 116 | +} |
| 117 | + |
| 118 | +func TestExtractCookies(t *testing.T) { |
| 119 | + path := buildTestBinaryCookies(t, []testCookie{ |
| 120 | + { |
| 121 | + domain: ".example.com", name: "session", path: "/", value: "abc123", |
| 122 | + secure: true, httpOnly: true, |
| 123 | + expires: 2000000000.0, creation: 700000000.0, |
| 124 | + }, |
| 125 | + { |
| 126 | + domain: ".go.dev", name: "lang", path: "/", value: "en", |
| 127 | + secure: false, httpOnly: false, |
| 128 | + expires: 2000000000.0, creation: 750000000.0, |
| 129 | + }, |
| 130 | + }) |
| 131 | + |
| 132 | + cookies, err := extractCookies(path) |
| 133 | + require.NoError(t, err) |
| 134 | + require.Len(t, cookies, 2) |
| 135 | + |
| 136 | + // Sorted by CreatedAt descending (newest first) |
| 137 | + assert.Equal(t, ".go.dev", cookies[0].Host) |
| 138 | + assert.Equal(t, ".example.com", cookies[1].Host) |
| 139 | + |
| 140 | + // Verify field mapping |
| 141 | + c := cookies[1] // .example.com cookie |
| 142 | + assert.Equal(t, "session", c.Name) |
| 143 | + assert.Equal(t, "abc123", c.Value) |
| 144 | + assert.Equal(t, "/", c.Path) |
| 145 | + assert.True(t, c.IsSecure) |
| 146 | + assert.True(t, c.IsHTTPOnly) |
| 147 | + assert.False(t, c.CreatedAt.IsZero()) |
| 148 | + assert.False(t, c.ExpireAt.IsZero()) |
| 149 | +} |
| 150 | + |
| 151 | +func TestCountCookies(t *testing.T) { |
| 152 | + path := buildTestBinaryCookies(t, []testCookie{ |
| 153 | + {domain: ".a.com", name: "a", path: "/", value: "1", expires: 2000000000.0, creation: 700000000.0}, |
| 154 | + {domain: ".b.com", name: "b", path: "/", value: "2", expires: 2000000000.0, creation: 700000000.0}, |
| 155 | + {domain: ".c.com", name: "c", path: "/", value: "3", expires: 2000000000.0, creation: 700000000.0}, |
| 156 | + }) |
| 157 | + |
| 158 | + count, err := countCookies(path) |
| 159 | + require.NoError(t, err) |
| 160 | + assert.Equal(t, 3, count) |
| 161 | +} |
| 162 | + |
| 163 | +func TestExtractCookies_InvalidFile(t *testing.T) { |
| 164 | + path := filepath.Join(t.TempDir(), "bad.binarycookies") |
| 165 | + require.NoError(t, os.WriteFile(path, []byte("not a cookies file"), 0o644)) |
| 166 | + |
| 167 | + _, err := extractCookies(path) |
| 168 | + assert.Error(t, err) |
| 169 | +} |
| 170 | + |
| 171 | +func TestExtractCookies_FileNotFound(t *testing.T) { |
| 172 | + _, err := extractCookies("/nonexistent/Cookies.binarycookies") |
| 173 | + assert.Error(t, err) |
| 174 | +} |
0 commit comments