blob: d69c8ceac30a816d5b0c8040da9d933968358262 [file] [log] [blame] [view]
Ramin Halavati2e7ffe4f2017-11-13 11:19:351# Network Traffic Annotations
2
3[TOC]
4
5This document presents a practical guide to using network traffic annotations in
6Chrome.
7
8
9## Problem Statement
10
11To make Chrome’s network communication transparent, we would need to be able to
12provide the following answers:
13* What is the intent behind each network request?
14* What user data is sent in the request, and where does it go?
15
Ramin Halavati6c1526e2018-04-06 05:44:3816Besides these requirements, the following information helps users, admins, and
17help desk:
Ramin Halavati2e7ffe4f2017-11-13 11:19:3518* How can a network communication be stopped or controlled?
19* What are the traces of the communication on the client?
20
21It should be noted that the technical details of requests are not necessarily
22important to the users, but in order to provide the intended transparency, we
23need to show that we have covered all bases and there are no back doors.
24
25
26## The Solution
27
28We can provide up to date, in-line documentation on origin, intent, payload, and
29control mechanisms of each network communication. This is done by adding a
30`NetworkTrafficAnnotationTag` to all network communication functions.
31Please note that as the goal is to specify the intent behind each network
32request and its payload, this metadata does not need to be transmitted with the
33request during runtime and it is sufficient to have it in appropriate positions
34in the code. Having that as an argument of all network communication functions
35is a mechanism to enforce its existence and showing the users our intent to
36cover the whole repository.
37
38
39## Best Practices
40
41### Where to add annotation?
42All network requests are ultimately sending data through sockets or native API
43functions, but we should note that the concern is about the main intent of the
44communication and not the implementation details. Therefore we do not need to
45specify this data separately for each call to each function that is used in the
46process and it is sufficient that the most rational point of origin would be
47annotated and the annotation would be passed through the downstream steps.
48Best practices for choosing annotation code site include where:
49 1. The origin of user’s intent or internal requirement for the request is
50 stated.
51 2. The controls over stopping or limiting the request (Chrome settings or
52 policies) are enforced.
53 3. The data that is sent is specified.
54If there is a conflict between where is the best annotation point, please refer
55to the `Partial Annotations` section for an approach to split annotation.
56
57### Merged Requests
58There are cases where requests are received from multiple sources and merged
59into one connection, like when a socket merges several data frames and sends
60them together, or a device location is requested by different components, and
61just one network request is made to fetch it. In these cases, the merge point
62can ensure that all received requests are properly annotated and just pass one
Ramin Halavati6c1526e2018-04-06 05:44:3863of them to the downstream step. It can also pass a local annotation stating that
64it is a merged request on behalf of other requests of type X, which were ensured
65to all have annotations.
Ramin Halavati2e7ffe4f2017-11-13 11:19:3566This decision is driven from the fact that we do not need to transmit the
67annotation metadata in runtime and enforced annotation arguments are just to
68ensure that the request is annotated somewhere upstream.
69
70
71## Coverage
Ramin Halavati6c1526e2018-04-06 05:44:3872Network traffic annotations are currently enforced on all url requests and
73socket writes, except for the code which is not compiled on Windows or Linux.
74This effort may expand to ChromeOS in future and currently there is no plan to
75expand it to other platforms.
Ramin Halavati2e7ffe4f2017-11-13 11:19:3576
77
78## Network Traffic Annotation Tag
79
80`net::NetworkTrafficAnnotationTag` is the main definition for annotations. There
81are few variants of it that are specified in later sections. The goal is to have
82one object of this type or its variants as an argument of all functions that
83create a network request.
84
85### Content of Annotation Tag
Glenn Hartmanndb9723b32022-01-07 14:57:2586Each network traffic annotation should specify the following items, as defined
87in the `NetworkTrafficAnnotation` message of
88`chrome/browser/privacy/traffic_annotation.proto`:
Ramin Halavati2e7ffe4f2017-11-13 11:19:3589* `uniqueـid`: A globally unique identifier that must stay unchanged while the
90 network request carries the same semantic meaning. If the network request gets
91 a new meaning, this ID needs to be changed. The purpose of this ID is to give
92 humans a chance to reference NetworkTrafficAnnotations externally even when
93 those change a little bit (e.g. adding a new piece of data that is sent along
94 with a network request). IDs of one component should have a shared prefix so
95 that sorting all NetworkTrafficAnnotations by unique_id groups those that
96 belong to the same component together.
Glenn Hartmanndb9723b32022-01-07 14:57:2597* `source`: These set of fields specify the location of annotation in
Ramin Halavati2e7ffe4f2017-11-13 11:19:3598 the source code. These fields are automatically set and do not need
99 specification.
Glenn Hartmanndb9723b32022-01-07 14:57:25100* `semantics`: These set of fields specify meta information about the
Ramin Halavati2e7ffe4f2017-11-13 11:19:35101 network request’s content and reason.
102 * `sender`: What component triggers the request. The components should be
103 human readable and don’t need to reflect the components/ directory. Avoid
Ramin Halavati6c1526e2018-04-06 05:44:38104 abbreviations, and use a common value for all annotations in one component.
Ramin Halavati2e7ffe4f2017-11-13 11:19:35105 * `description`: Plaintext description of the network request in language
106 that is understandable by admins (ideally also users). Please avoid
107 acronyms and describe the feature and the feature's value proposition as
108 well.
109 * `trigger`: What user action triggered the network request. Use a textual
110 description. This should be a human readable string.
Sugandha Goyal0ccc7402022-10-21 13:35:55111 * `user_data`: What nature of data is being sent, as enums.
112 Any personally identifiable (PII) data, provided by user or generated
113 by Google, should be pointed out. You can include multiple
114 values, and you may want to supplement this with the data field.
115 All available User data enums can be found [here](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/privacy/traffic_annotation.proto?q=UserDataType).
116 * `data`: Textual description of data being sent, for things that aren't
117 covered by user_data enum values. You can also use this field if
118 more context needs to be provided to describe user_data.
Ramin Halavati2e7ffe4f2017-11-13 11:19:35119 * `destination`: Target of the network request. It can be either the website
120 that user visits and interacts with, a Google service, a request that does
121 not go to network and just fetches a local resource, or other endpoints
122 like a service hosting PAC scripts. The distinction between a Google owned
123 service and website can be difficult when the user navigates to google.com
124 or searches with the omnibar. Therefore follow the following guideline: If
125 the source code has hardcoded that the request goes to Google (e.g. for
126 ZeroSuggest), use `GOOGLE_OWNED_SERVICE`. If the request can go to other
127 domains and is perceived as a part of a website rather than a native
Nicolas Arciniega15745e22019-10-09 20:42:35128 browser feature, use `WEBSITE`. Use `LOCAL` if the request is processed
Ramin Halavati2e7ffe4f2017-11-13 11:19:35129 locally and doesn't go to network, otherwise use `OTHER`. If `OTHER` is
130 used, please add plain text description in `destination_other`
131 field.
132 * `destination_other`: Human readable description in case the destination
133 points to `OTHER`.
Sugandha Goyalfcdaf212022-11-22 15:07:40134 * `internal`: Data that is meant to be visible internally, example point of contacts,
135 should be placed inside internal field. This field should not be used in any
Sugandha Goyal18152792022-10-19 22:01:43136 external reports.
Sugandha Goyalfcdaf212022-11-22 15:07:40137 * `contacts`: A person's or team's email address who are point-of-contact
138 for questions, issues, or bugs related to this network request.
Sugandha Goyal0ccc7402022-10-21 13:35:55139 * `last_reviewed`: Date when this annotation was last reviewed in YYYY-MM-DD format.
Glenn Hartmanndb9723b32022-01-07 14:57:25140* `policy`: These set of fields specify the controls that a user may have
Ramin Halavati2e7ffe4f2017-11-13 11:19:35141 on disabling or limiting the network request and its trace.
Ramin Halavati2e7ffe4f2017-11-13 11:19:35142 * `cookies_allowed`: Specifies if this request stores and uses cookies or
143 not. Use values `YES` or `NO`.
144 * `cookies_store`: If a request sends or stores cookies/channel IDs/... (i.e.
145 if `cookies_allowed` is true), we want to know which cookie store is being
146 used. The answer to this question can typically be derived from the
147 URLRequestContext that is being used. The three most common cases will be:
148 * If `cookies_allowed` is false, leave this field unset.
149 * If the profile's default URLRequestContext is being used (e.g. from
150 `Profile::GetRequestContext())`, this means that the user's normal
151 cookies sent. In this case, put `user` here.
152 * If the system URLRequestContext is being used (for example via
153 `io_thread()->system_url_request_context_getter())`, put `system` here.
154 * Otherwise, please explain (e.g. SafeBrowsing uses a separate cookie
155 store).
156 * `setting`: Human readable description of how to enable/disable a feature
157 that triggers this network request by a user (e.g. “Disable ‘Use a web
158 service to help resolve spelling errors.’ in settings under Advanced”).
159 Note that settings look different on different platforms, make sure your
160 description works everywhere!
161 * `chrome_policy`: Policy configuration that disables or limits this network
Aiden Chiavatti479d874a2023-01-25 23:25:35162 request. This would be a text serialized protobuf of any **non-device**
163 enterprise policy. See policy list or
Glenn Hartmanndb9723b32022-01-07 14:57:25164 `out/Debug/gen/components/policy/proto/chrome_settings.proto` for the full
165 list of policies.
Aiden Chiavatti479d874a2023-01-25 23:25:35166 * Note: Use `chrome_device_policy` instead for device policies.
167 * `chrome_device_policy`: Policy configuration that disables or limits this
168 network request. This would be a text serialized protobuf of any
169 **device** enterprise policy. See
170 `components/policy/proto/chrome_device_policy.proto` for the full list of
171 policies.
172 * Note: Use `chrome_policy` instead for non-device policies (e.g. user
173 policies).
Ramin Halavati2e7ffe4f2017-11-13 11:19:35174 * `policy_exception_justification`: If there is no policy to disable or limit
Ramin Halavatic7c0bb032018-01-30 16:44:48175 this request, a justification can be presented here.
Sugandha Goyal0ccc7402022-10-21 13:35:55176 * `deprecated_policies`: Policy names disabling or limiting this network request
177 which are currently deprecated. These should be a subset of the policies in the
178 `chrome_policy` field. If a policy is removed from the `chrome_policy` field,
179 then it should be removed from this field also.
Ramin Halavati2e7ffe4f2017-11-13 11:19:35180* `comments`: If required, any human readable extra comments.
181
182### Format and Examples
183Traffic annotations are kept in code as serialized protobuf. To define a
184`NetworkTrafficAnnotationTag`, you may use the function
185`net::DefineNetworkTrafficAnnotation`, with two arguments, the unique id, and
186all other fields bundled together as a serialized protobuf string.
187
188#### Good examples
189```cpp
190 net::NetworkTrafficAnnotationTag traffic_annotation =
191 net::DefineNetworkTrafficAnnotation("spellcheck_lookup", R"(
192 semantics {
193 sender: "Online Spellcheck"
194 description:
195 "Chrome can provide smarter spell-checking by sending text you "
196 "type into the browser to Google's servers, allowing you to use "
197 "the same spell-checking technology used by Google products, such "
198 "as Docs. If the feature is enabled, Chrome will send the entire "
199 "contents of text fields as you type in them to Google along with "
200 "the browser’s default language. Google returns a list of "
201 "suggested spellings, which will be displayed in the context menu."
202 trigger: "User types text into a text field or asks to correct a "
203 "misspelled word."
Nicolas Ouellet-Payeur7a9724ae2023-02-09 16:03:27204 internal {
205 contacts {
206 email: "[email protected]"
207 }
208 }
Sugandha Goyal0ccc7402022-10-21 13:35:55209 user_data {
210 type: USER_CONTENT
211 }
Ramin Halavati2e7ffe4f2017-11-13 11:19:35212 data: "Text a user has typed into a text field. No user identifier "
213 "is sent along with the text."
214 destination: GOOGLE_OWNED_SERVICE
Sugandha Goyal0ccc7402022-10-21 13:35:55215 last_reviewed: "2022-10-17"
Ramin Halavati2e7ffe4f2017-11-13 11:19:35216 }
217 policy {
218 cookies_allowed: NO
219 setting:
220 "You can enable or disable this feature via 'Use a web service to "
221 "help resolve spelling errors.' in Chrome's settings under "
222 "Advanced. The feature is disabled by default."
223 chrome_policy {
224 SpellCheckServiceEnabled {
225 SpellCheckServiceEnabled: false
226 }
227 }
228 })");
229```
230
231```cpp
232 net::NetworkTrafficAnnotationTag traffic_annotation2 =
233 net::DefineNetworkTrafficAnnotation(
234 "safe_browsing_chunk_backup_request",
235 R"(
236 semantics {
237 sender: "Safe Browsing"
238 description:
239 "Safe Browsing updates its local database of bad sites every 30 "
240 "minutes or so. It aims to keep all users up-to-date with the same "
241 "set of hash-prefixes of bad URLs."
242 trigger:
243 "On a timer, approximately every 30 minutes."
244 data:
245 "The state of the local DB is sent so the server can send just the "
246 "changes. This doesn't include any user data."
247 destination: GOOGLE_OWNED_SERVICE
Sugandha Goyalfcdaf212022-11-22 15:07:40248 internal {
249 contacts {
250 email: "[email protected]"
251 }
252 }
Nicolas Ouellet-Payeur7a9724ae2023-02-09 16:03:27253 user_data {
254 type: NONE
255 }
256 last_reviewed: "2023-01-01"
Ramin Halavati2e7ffe4f2017-11-13 11:19:35257 }
258 policy {
259 cookies_allowed: YES
260 cookies_store: "Safe Browsing cookie store"
261 setting:
262 "Users can disable Safe Browsing by unchecking 'Protect you and "
263 "your device from dangerous sites' in Chromium settings under "
264 "Privacy. The feature is enabled by default."
265 chrome_policy {
266 SafeBrowsingEnabled {
267 policy_options {mode: MANDATORY}
268 SafeBrowsingEnabled: false
269 }
270 }
271 })");
272```
273
274#### Bad Examples
275```cpp
276 net::NetworkTrafficAnnotationTag bad_traffic_annotation =
277 net::DefineNetworkTrafficAnnotation(
278 ...
279 trigger: "Chrome sends this when [obscure event that is not related to "
280 "anything user-perceivable]."
281 // Please specify the exact user action that results in this request.
282 data: "This sends everything the feature needs to know."
283 // Please be precise, name the data items. If they are too many, name
284 // the sensitive user data and general classes of other data and refer
285 // to a document specifying the details.
286 ...
287 policy_exception_justification: "None."
288 // Check again! Most features can be disabled or limited by a policy.
289 ...
290 })");
291```
292
293#### Empty Template
294You can copy/paste the following template to define an annotation.
295```cpp
296 net::NetworkTrafficAnnotationTag traffic_annotation =
297 net::DefineNetworkTrafficAnnotation("...", R"(
298 semantics {
299 sender: "..."
300 description: "..."
301 trigger: "..."
302 data: "..."
303 destination: WEBSITE/GOOGLE_OWNED_SERVICE/OTHER
304 }
305 policy {
Ramin Halavati2e7ffe4f2017-11-13 11:19:35306 cookies_allowed: NO/YES
307 cookies_store: "..."
308 setting: "..."
309 chrome_policy {
310 [POLICY_NAME] {
311 [POLICY_NAME]: ...
312 }
313 }
314 policy_exception_justification = "..."
315 }
316 comments: "..."
317 )");
318```
319
320
321## Testing for errors
322
323There are several checks that should be done on annotations before submitting a
324change list. These checks include:
325* The annotations are syntactically correct.
326* They have all required fields.
327* Partial annotations and completing parts match (please refer to the next
328 section).
329* Annotations are not incorrectly defined.
330 * e.g., traffic_annotation = NetworkTrafficAnnotation({1}).
331* All usages from Chrome have annotation.
Bella Bahaf76f4a2020-08-03 22:22:56332* That the annotation appears in
333 `tools/traffic_annotation/summary/grouping.xml`. When adding a new annotation,
334 it must also be included in `grouping.xml` for reporting purposes (please
335 refer to the **Annotations Review**).
Bella Bah2b772c132020-08-05 17:01:37336
Ramin Halavati2e7ffe4f2017-11-13 11:19:35337
Ramin Halavati6c1526e2018-04-06 05:44:38338### Presubmit tests
Nicolas Ouellet-Payeur35662942021-08-03 16:01:37339To perform tests prior to submit, one can use the `auditor.py`
340script. It runs over the whole repository, extracts
341all the annotations from C++ code, and then checks them for correctness.
Ehsan Kia735f6b42020-02-27 18:14:59342
Nicolas Ouellet-Payeur35662942021-08-03 16:01:37343Running the `auditor.py` script requires a build directory in which you just
344built the `chrome` target. You can invoke it like this:
345`vpython3 tools/traffic_annotation/scripts/auditor/auditor.py
346 --build-path=out/Default`
Ramin Halavati6c1526e2018-04-06 05:44:38347
348### Waterfall tests
349Two commit queue trybots test traffic annotations on changed files using the
350scripts in `tools/traffic_annotation/scripts`. To run these tests faster and to
Nicolas Arciniega15745e22019-10-09 20:42:35351avoid spamming the commit queue if an unforeseen error has happened in
352downstream scripts or tools, they are run in error resilient mode, only on
353changed files, and using heuristics to decide which files to process.
Ramin Halavati6c1526e2018-04-06 05:44:38354An FYI bot runs more detailed tests on the whole repository and with different
355switches, to make sure that the heuristics that trybot tests use and the limited
356scope of tests have not neglected any issues.
Ramin Halavati2e7ffe4f2017-11-13 11:19:35357
358
359## Annotations Review
360
Ramin Halavati6c1526e2018-04-06 05:44:38361Network traffic annotations require review before landing in code and this is
362enforced through keeping a summary of annotations in
Bella Bahaf76f4a2020-08-03 22:22:56363`tools/traffic_annotation/summary/annotations.xml`. Once a new annotation is added,
364one is updated, or deleted, this file should also be updated. To update the
Nicolas Ouellet-Payeur35662942021-08-03 16:01:37365`annotations.xml` file automatically, one can run `auditor.py`
Bella Bahaf76f4a2020-08-03 22:22:56366as specified in presubmit tests. But if it is not possible to do so (e.g., if
367you are changing the code from an unsupported platform or you don’t have a
368compiled build directory), the code can be submitted to the trybot and the test
Bella Bah2b772c132020-08-05 17:01:37369on trybot will tell you the required modifications.
Ramin Halavati2e7ffe4f2017-11-13 11:19:35370
Bella Bahaf76f4a2020-08-03 22:22:56371In order to help make external reports easier, annotation unique ids should be
372mentioned in `tools/traffic_annotation/summary/grouping.xml`. Once a new
373annotation is added, or a preexisting annotation's unique id changes, this file
374should also be updated. When adding a new annotation, make sure it is placed
375within an appropriate group of `grouping.xml`. In the rare case that none of
376the groups are appropriate, one can create a new group for the annotation; the
377arrangement of annotations and group names in `grouping.xml` may be later
378updated by a technical writer to better coincide with the external reports.
Ramin Halavati2e7ffe4f2017-11-13 11:19:35379
380## Partial Annotations (Advanced)
381
382There are cases where the network traffic annotation cannot be fully specified
383in one place. For example, in one place we know the trigger of a network request
384and in another place we know the data that will be sent. In these cases, we
385prefer that both parts of the annotation appear in context so that they are
386updated if code changes. Partial annotations help splitting the network traffic
387annotation into two pieces. In these cases, we call the first part, the partial
388annotation, and the part the completes it, the completing annotation. Partial
389annotations and completing annotations do not need to have all annotation
390fields, but their composition should have all required fields.
391
392### Defining a Partial Annotation
393To define a partial annotation, one can use
394`net::DefinePartialNetworkTrafficAnnotation` function. Besides the unique id and
395annotation text, this function requires the unique id of the completing part.
396For example, a partial annotation that only specifies the semantics part or a
397request with unique id "omnibox_prefetch_image", and is completed later using an
398annotation with unique id "bitmap_fetcher", can be defined as follows:
399
400```cpp
401 net::PartialNetworkTrafficAnnotationTag partial_traffic_annotation =
402 net::DefinePartialNetworkTrafficAnnotation("omnibox_prefetch_image",
403 "bitmap_fetcher", R"(
404 semantics {
405 sender: "Omnibox"
406 Description: "..."
407 Trigger: "..."
408 Data: "..."
409 destination: WEBSITE
410 })");
411```
412
413### Nx1 Partial Annotations
414The cases where several partial annotations may be completed by one completing
415annotation are called Nx1. This also matches where N=1. To define a completing
416annotation for such cases, one can use net::CompleteNetworkTrafficAnnotation
417function. This function receives a unique id, the annotation text, and a
418`net::PartialNetworkTrafficAnnotationTag` object. Here is an example of a
419completing part for the previous example:
420
421```cpp
422 net::NetworkTrafficAnnotationTag traffic_annotation =
423 net::CompleteNetworkTrafficAnnotation("bitmap_fetcher",
424 partial_traffic_annotation, R"(
425 policy {
426 cookies_allowed: YES
427 cookies_store: "user"
428 setting: "..."
429 chrome_policy {...}
430 })");
431```
432
433### 1xN Partial Annotations
434There are cases where one partial traffic annotation may be completed by
435different completing annotations. In these cases,
436`net::BranchedCompleteNetworkTrafficAnnotation` function can be used. This
437function has an extra argument that is common between all branches and is
438referred to by the partial annotation. For the above examples, if there would be
439two different ways of completing the received partial annotation, the following
440the definition can be used:
441
442```cpp
443if (...) {
444 return net::BranchedCompleteNetworkTrafficAnnotation(
445 "bitmap_fetcher_type1", "bitmap_fetcher",
446 partial_traffic_annotation, R"(
447 policy {
448 cookies_allowed: YES
449 cookies_store: "user"
450 setting: "..."
451 chrome_policy {...}
452 })");
453 } else {
454 return net::BranchedCompleteNetworkTrafficAnnotation(
455 "bitmap_fetcher_type2", "bitmap_fetcher",
456 partial_traffic_annotation, R"(
457 policy {
458 cookies_allowed: YES
459 cookies_store: "system"
460 setting: "..."
461 chrome_policy {...}
462 })");
463```
464
465Please refer to `tools/traffic_annotation/sample_traffic_annotation.cc` for more
466detailed examples.
467
468
469## Mutable Annotations (Advanced)
470
471`net::NetworkTrafficAnnotationTag` and `net::PartialNetworkTrafficAnnotationTag`
472are defined with constant internal argument(s), so that once they are created,
473they cannot be modified. There are very few exceptions that may require
474modification of the annotation value, like the ones used by mojo interfaces
475where after serialization, the annotation object is first created, then receives
476value. In these cases, `net::MutableNetworkTrafficAnnotationTag` and
477`net::MutablePartialNetworkTrafficAnnotationTag` can be used which do not have
[email protected]d7c93b3b2018-01-19 08:39:07478this limitation.
Nicolas Ouellet-Payeur35662942021-08-03 16:01:37479
[email protected]d7c93b3b2018-01-19 08:39:07480Mutable annotations have a run time check before being converted into normal
481annotations to ensure their content is valid. Therefore it is suggested that
482they would be used only if there is no other way around it. Use cases are
Nicolas Ouellet-Payeur35662942021-08-03 16:01:37483checked with `auditor.py` to ensure proper initialization values for the
484mutable annotations.
Ramin Halavatic56d1702017-11-30 09:23:01485
486
487## Mojo Interfaces (Advanced)
488
489For serialization of network traffic annotation and partial network traffic
490annotation tags, you can use the mutable mojo interfaces defined in
Nicolas Arciniega15745e22019-10-09 20:42:35491`/services/network/public/mojom`.