File: request_headers_internal.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (277 lines) | stat: -rw-r--r-- 8,867 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "services/network/attribution/request_headers_internal.h"

#include <stdint.h>

#include <algorithm>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "net/http/structured_headers.h"
#include "services/network/public/mojom/attribution.mojom.h"

namespace network {

namespace {

using GreaseContext =
    ::network::AttributionReportingHeaderGreaseOptions::GreaseContext;

using ::network::mojom::AttributionReportingEligibility;

void AddTrueValuedDictMember(
    std::vector<net::structured_headers::DictionaryMember>& dict,
    std::string key) {
  dict.emplace_back(std::move(key),
                    net::structured_headers::ParameterizedMember(
                        net::structured_headers::Item(true),
                        net::structured_headers::Parameters()));
}

void ApplyGreaseContext(
    std::vector<net::structured_headers::DictionaryMember>& dict,
    GreaseContext context,
    bool use_front,
    const char* grease) {
  if (!grease) {
    return;
  }

  switch (context) {
    case GreaseContext::kNone:
      break;
    case GreaseContext::kKey:
      AddTrueValuedDictMember(dict, base::StrCat({"not-", grease}));
      break;
    case GreaseContext::kValue:
      if (!dict.empty()) {
        auto& [_, parameterized_member] =
            use_front ? dict.front() : dict.back();
        parameterized_member.member.front().item =
            net::structured_headers::Item(
                grease, net::structured_headers::Item::kTokenType);
      }
      break;
    case GreaseContext::kParamName:
      if (!dict.empty()) {
        auto& [_, parameterized_member] =
            use_front ? dict.front() : dict.back();
        parameterized_member.params.emplace_back(
            grease, net::structured_headers::Item(true));
      }
      break;
  }
}

void ApplyGrease(std::vector<net::structured_headers::DictionaryMember>& dict,
                 const AttributionReportingHeaderGreaseOptions& options,
                 const char* grease1,
                 const char* grease2) {
  // We "grease" the header with meaningless components to help ensure that
  // recipients are using a structured header parser, not naive string
  // operations on the serialized header, and to ensure that values and
  // parameters are ignored. We carefully choose the greases themselves to
  // minimize excessive bandwidth: each substring corresponding to a real key
  // will appear at most once in the serialized header.
  //
  // https://wicg.github.io/attribution-reporting-api/#set-attribution-reporting-headers

  // Allowing these to be swapped gives them equal chance of being used, since
  // otherwise a key is more likely to exist for the second grease than the
  // first.
  if (options.swap_greases) {
    std::swap(grease1, grease2);
  }

  ApplyGreaseContext(dict, options.context1, options.use_front1, grease1);
  ApplyGreaseContext(dict, options.context2, options.use_front2, grease2);

  if (dict.size() > 1 && options.reverse) {
    // Dictionaries retain order during serialization, so reordering helps
    // ensure that recipients do not depend on it.
    std::ranges::reverse(dict);
  }
}

}  // namespace

// static
AttributionReportingHeaderGreaseOptions
AttributionReportingHeaderGreaseOptions::FromBits(uint8_t bits) {
  AttributionReportingHeaderGreaseOptions options;

  options.reverse = bits & 0b1;
  bits >>= 1;

  options.swap_greases = bits & 0b1;
  bits >>= 1;

  static_assert(std::to_underlying(GreaseContext::kNone) == 0);
  static_assert(std::to_underlying(GreaseContext::kKey) == 1);
  static_assert(std::to_underlying(GreaseContext::kValue) == 2);
  static_assert(std::to_underlying(GreaseContext::kParamName) == 3);

  options.context1 = static_cast<GreaseContext>(bits & 0b11);
  bits >>= 2;

  options.context2 = static_cast<GreaseContext>(bits & 0b11);
  bits >>= 2;

  options.use_front1 = bits & 0b1;
  bits >>= 1;

  options.use_front2 = bits & 0b1;
  bits >>= 1;

  return options;
}

std::string SerializeAttributionReportingEligibleHeader(
    const AttributionReportingEligibility eligibility,
    const AttributionReportingHeaderGreaseOptions& options) {
  const char* const kEventSource = "event-source";
  const char* const kNavigationSource = "navigation-source";
  const char* const kTrigger = "trigger";

  std::vector<net::structured_headers::DictionaryMember> eligibilities;

  const char* grease1;
  const char* grease2;
  switch (eligibility) {
    case AttributionReportingEligibility::kUnset:
      NOTREACHED();
    case AttributionReportingEligibility::kEmpty:
      grease1 = kEventSource;
      grease2 = kTrigger;
      break;
    case AttributionReportingEligibility::kEventSource:
      AddTrueValuedDictMember(eligibilities, kEventSource);
      grease1 = kTrigger;
      grease2 = kNavigationSource;
      break;
    case AttributionReportingEligibility::kNavigationSource:
      AddTrueValuedDictMember(eligibilities, kNavigationSource);
      grease1 = kEventSource;
      grease2 = kTrigger;
      break;
    case AttributionReportingEligibility::kTrigger:
      AddTrueValuedDictMember(eligibilities, kTrigger);
      grease1 = kNavigationSource;
      grease2 = kEventSource;
      break;
    case AttributionReportingEligibility::kEventSourceOrTrigger:
      AddTrueValuedDictMember(eligibilities, kEventSource);
      AddTrueValuedDictMember(eligibilities, kTrigger);
      grease1 = kNavigationSource;
      grease2 = nullptr;
      break;
  }

  ApplyGrease(eligibilities, options, grease1, grease2);

  std::optional<std::string> eligible_header =
      net::structured_headers::SerializeDictionary(
          net::structured_headers::Dictionary(std::move(eligibilities)));
  DCHECK(eligible_header.has_value());

  return std::move(*eligible_header);
}

std::string GetAttributionSupportHeader(
    mojom::AttributionSupport attribution_support,
    const AttributionReportingHeaderGreaseOptions& options) {
  std::vector<net::structured_headers::DictionaryMember> registrars;

  const char* grease1;
  const char* grease2;
  switch (attribution_support) {
    case mojom::AttributionSupport::kWeb:
      AddTrueValuedDictMember(registrars, "web");
      grease1 = "os";
      grease2 = nullptr;
      break;
    case mojom::AttributionSupport::kOs:
      AddTrueValuedDictMember(registrars, "os");
      grease1 = "web";
      grease2 = nullptr;
      break;
    case mojom::AttributionSupport::kWebAndOs:
      AddTrueValuedDictMember(registrars, "os");
      AddTrueValuedDictMember(registrars, "web");
      grease1 = nullptr;
      grease2 = nullptr;
      break;
    case mojom::AttributionSupport::kNone:
      grease1 = "os";
      grease2 = "web";
      break;
    case mojom::AttributionSupport::kUnset:
      NOTREACHED();
  }

  ApplyGrease(registrars, options, grease1, grease2);

  std::optional<std::string> support_header =
      net::structured_headers::SerializeDictionary(
          net::structured_headers::Dictionary(std::move(registrars)));
  DCHECK(support_header.has_value());
  return std::move(*support_header);
}

std::string SerializeAdAuctionRegistrationEligibleHeader(
    mojom::AttributionReportingEligibility eligibility,
    const AttributionReportingHeaderGreaseOptions& options) {
  constexpr char kView[] = "view";
  constexpr char kClick[] = "click";

  std::vector<net::structured_headers::DictionaryMember> eligibilities;

  const char* grease1;
  const char* grease2;
  switch (eligibility) {
    case AttributionReportingEligibility::kUnset:
      NOTREACHED();
    case AttributionReportingEligibility::kEmpty:
      grease1 = kView;
      grease2 = kClick;
      break;
    case AttributionReportingEligibility::kEventSource:
      AddTrueValuedDictMember(eligibilities, kView);
      grease1 = kClick;
      grease2 = kView;
      break;
    case AttributionReportingEligibility::kNavigationSource:
      AddTrueValuedDictMember(eligibilities, kClick);
      grease1 = kView;
      grease2 = kClick;
      break;
    case AttributionReportingEligibility::kTrigger:
      grease1 = kView;
      grease2 = kClick;
      break;
    case AttributionReportingEligibility::kEventSourceOrTrigger:
      AddTrueValuedDictMember(eligibilities, kView);
      grease1 = kClick;
      grease2 = kView;
      break;
  }

  ApplyGrease(eligibilities, options, grease1, grease2);

  std::optional<std::string> eligible_header =
      net::structured_headers::SerializeDictionary(
          net::structured_headers::Dictionary(std::move(eligibilities)));
  DCHECK(eligible_header.has_value());

  return std::move(*eligible_header);
}

}  // namespace network