File: indexed_db_leveldb_coding_encodeidbkey_fuzzer.cc

package info (click to toggle)
chromium 142.0.7444.175-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie-proposed-updates
  • size: 6,295,408 kB
  • sloc: cpp: 35,488,378; ansic: 7,479,680; javascript: 4,259,373; python: 1,466,843; xml: 757,444; asm: 710,716; pascal: 187,980; sh: 89,247; perl: 88,690; objc: 79,984; sql: 56,984; cs: 42,192; fortran: 24,137; makefile: 22,913; tcl: 15,277; php: 14,018; yacc: 9,005; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (114 lines) | stat: -rw-r--r-- 4,267 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <fuzzer/FuzzedDataProvider.h>
#include <stddef.h>
#include <stdint.h>

#include <string_view>
#include <tuple>

#include "base/check.h"
#include "base/strings/utf_string_conversions.h"
#include "content/browser/indexed_db/indexed_db_leveldb_coding.h"
#include "third_party/blink/public/common/indexeddb/indexeddb_key.h"

using blink::IndexedDBKey;
using blink::mojom::IDBKeyType;

// IDBKeyType has 7 possible values, so the lower 3 bits of |data| are used to
// determine the IDBKeyType to return.
IDBKeyType GetIDBKeyType(uint8_t data) {
  return static_cast<IDBKeyType>(data & 0x7);
}

// Parse |fuzzed_data| to create an IndexedDBKey. This method takes uses the
// first byte to determine the type of key to create. The remaining bytes in
// |fuzzed_data| will be consumed differently depending on the type of key.
IndexedDBKey CreateKey(FuzzedDataProvider* fuzzed_data,
                       size_t recursion_level = 0) {
  // Avoid a stack overflow by enforcing this limit. The added buffer here is to
  // allow this fuzzer to verify behavior for when the generated key is too
  // deep.
  if (recursion_level > IndexedDBKey::kMaximumDepth + 5) {
    fuzzed_data->ConsumeRemainingBytes<uint8_t>();
  }

  // If there is no more data to use, return a made-up key.
  if (fuzzed_data->remaining_bytes() < 1) {
    return IndexedDBKey(1.0, IDBKeyType::Number);
  }

  auto key_type = GetIDBKeyType(fuzzed_data->ConsumeIntegral<uint8_t>());

  switch (key_type) {
    case IDBKeyType::Array: {
      // Recursively create and add keys to |key_array| until there are no more
      // bytes to consume. Then, create the final key to return with this array.
      IndexedDBKey::KeyArray key_array;
      while (fuzzed_data->remaining_bytes() > 0) {
        key_array.push_back(CreateKey(fuzzed_data, 1 + recursion_level));
      }
      return IndexedDBKey(std::move(key_array));
    }
    // For keys of type |Binary| and |String|, consume sizeof(size_t) bytes to
    // determine the maximum length of the string to create.
    case IDBKeyType::Binary: {
      if (fuzzed_data->remaining_bytes() < 1)
        return IndexedDBKey("");
      auto str_size = fuzzed_data->ConsumeIntegral<size_t>();
      return IndexedDBKey(fuzzed_data->ConsumeBytesAsString(str_size));
    }
    case IDBKeyType::String: {
      if (fuzzed_data->remaining_bytes() < 1)
        return IndexedDBKey(base::UTF8ToUTF16(std::string()));
      auto str_size = fuzzed_data->ConsumeIntegral<size_t>();
      std::u16string data_str =
          base::UTF8ToUTF16(fuzzed_data->ConsumeBytesAsString(str_size));
      return IndexedDBKey(data_str);
    }
    case IDBKeyType::Date:
    case IDBKeyType::Number: {
      return IndexedDBKey(fuzzed_data->ConsumeFloatingPoint<double>(),
                          key_type);
    }
    case IDBKeyType::Invalid:
    case IDBKeyType::None:
    case IDBKeyType::Min:
    default:
      return IndexedDBKey(key_type);
  }
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  FuzzedDataProvider fuzzed_data(data, size);
  blink::IndexedDBKey key = CreateKey(&fuzzed_data);

  // Old encoding scheme.
  {
    // Encoding fails if the key is invalid or if the recursion depth is too
    // much. In prod, either of these cases will CHECK, but here we fail
    // gracefully.
    std::string result;
    if (content::indexed_db::MaybeEncodeIDBKey(key, &result)) {
      // Ensure that |result| can be decoded back into the original key.
      auto result_str_view = std::string_view(result);
      CHECK(content::indexed_db::DecodeIDBKey(&result_str_view).Equals(key));
    }
  }

  // New (sortable) encoding scheme.
  if (key.IsValid()) {
    std::string encoded = content::indexed_db::EncodeSortableIDBKey(key);
    IndexedDBKey decoded = content::indexed_db::DecodeSortableIDBKey(encoded);
    // If too deeply nested, decoding will reject the input by returning an
    // invalid key.
    if (decoded.IsValid()) {
      CHECK(decoded.Equals(key))
          << key.DebugString()
          << " could not be round-tripped through sortable encoding.";
    }
  }
  return 0;
}