File: padding_key.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (81 lines) | stat: -rw-r--r-- 2,900 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
// 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 "storage/common/quota/padding_key.h"

#include <inttypes.h>

#include <cstdint>

#include "base/numerics/byte_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "crypto/hash.h"
#include "crypto/hmac.h"
#include "crypto/random.h"
#include "net/base/schemeful_site.h"
#include "net/http/http_request_headers.h"
#include "services/network/public/mojom/url_response_head.mojom-shared.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"

namespace storage {

namespace {

// The range of the padding added to response sizes for opaque resources.
// Increment the CacheStorage padding version if changed.
constexpr uint64_t kPaddingRange = 14431 * 1024;

}  // namespace

bool ShouldPadResponseType(network::mojom::FetchResponseType type) {
  return type == network::mojom::FetchResponseType::kOpaque ||
         type == network::mojom::FetchResponseType::kOpaqueRedirect;
}

int64_t ComputeRandomResponsePadding() {
  uint64_t raw_random = 0;
  crypto::RandBytes(base::byte_span_from_ref(raw_random));
  return raw_random % kPaddingRange;
}

int64_t ComputeStableResponsePadding(const blink::StorageKey& storage_key,
                                     const std::string& response_url,
                                     const base::Time& response_time,
                                     const std::string& request_method,
                                     int64_t side_data_size) {
  static std::array<uint8_t, 16> s_padding_key;
  static bool s_padding_key_generated = false;

  if (!s_padding_key_generated) {
    // This just needs to be consistent within a single browser session, so we
    // generate it the first time we need it.
    crypto::RandBytes(s_padding_key);
    s_padding_key_generated = true;
  }

  DCHECK(!response_url.empty());

  net::SchemefulSite site(storage_key.origin());

  DCHECK_GT(response_time, base::Time::UnixEpoch());
  int64_t microseconds =
      (response_time - base::Time::UnixEpoch()).InMicroseconds();

  // It should only be possible to have a CORS safe-listed method here since
  // the spec does not permit other methods for no-cors requests.
  DCHECK(request_method == net::HttpRequestHeaders::kGetMethod ||
         request_method == net::HttpRequestHeaders::kHeadMethod ||
         request_method == net::HttpRequestHeaders::kPostMethod);

  std::string key = base::StringPrintf(
      "%s-%" PRId64 "-%s-%s-%" PRId64, response_url.c_str(), microseconds,
      site.Serialize().c_str(), request_method.c_str(), side_data_size);

  auto hmac = crypto::hmac::SignSha256(s_padding_key, base::as_byte_span(key));
  return base::U64FromNativeEndian(base::as_byte_span(hmac).first<8>()) %
         kPaddingRange;
}

}  // namespace storage