Skip to content

Commit

Permalink
[Chrome IndexedDB] Fix header processing (#1814)
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany authored Jul 31, 2024
1 parent be04b3c commit 5a6f49f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
22 changes: 16 additions & 6 deletions ee/indexeddb/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ func DeserializeChrome(ctx context.Context, slogger *slog.Logger, row map[string
}

// readHeader reads through the header bytes at the start of `srcReader`.
// It parses the version, if found. It stops as soon as it reaches the first
// object reference.
// It parses the version, if found. The header always ends with the byte
// 0xff (tokenVersion), followed by the version (a varint), followed by 0x6f (tokenObjectBegin)
// -- we stop reading the header at this point.
func readHeader(srcReader *bytes.Reader) (uint64, error) {
var version uint64
for {
Expand All @@ -84,16 +85,25 @@ func readHeader(srcReader *bytes.Reader) (uint64, error) {

switch nextByte {
case tokenVersion:
// Read the version first. It is fine to overwrite version if we saw the version token
// before -- the last instance of the version token is the correct one.
version, err = binary.ReadUvarint(srcReader)
if err != nil {
return 0, fmt.Errorf("decoding uint32: %w", err)
}
case tokenObjectBegin:
// Done reading header
return version, nil

// Next, determine whether this is the 0xff token at the end of the header
peekByte, err := srcReader.ReadByte()
if err != nil {
return 0, fmt.Errorf("peeking at next byte after version tag: %w", err)
}

if peekByte == tokenObjectBegin {
// We read the version followed by 0x6f -- we have completed reading the header.
return version, nil
}
default:
// Padding -- ignore
continue
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion ee/indexeddb/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func Test_deserializeIndexeddbValue(t *testing.T) {
0x00, // padding, ignore
0x00, // padding, ignore
0x00, // padding, ignore
0xff, // version tag (indicates end of header)
0x0f, // version
// object
0x6f, // object begin
0x22, // string tag
Expand Down Expand Up @@ -49,9 +51,11 @@ func Test_deserializeIndexeddbValue_InvalidType(t *testing.T) {
0x00, // padding, ignore
0x00, // padding, ignore
0x00, // padding, ignore
0xff, // version tag (indicates end of header)
0x0f, // version
// object
0x6f, // object begin
0xff, // version tag -- invalid data!
0x54, // boolean true tag -- invalid data!
0x7b, // object end
0x00, // properties_written
}
Expand Down

0 comments on commit 5a6f49f

Please sign in to comment.