-
Notifications
You must be signed in to change notification settings - Fork 23
/
spec.bs
1424 lines (1192 loc) · 62.1 KB
/
spec.bs
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class='metadata'>
Title: Private Aggregation API
Shortname: private-aggregation-api
Level: 1
Status: UD
Group: patcg-id
Repository: patcg-individual-drafts/private-aggregation-api
URL: https://patcg-individual-drafts.github.io/private-aggregation-api
Editor: Alexander Turner, Google https://www.google.com, [email protected]
Abstract: A generic API for measuring aggregate, cross-site data in a privacy
preserving manner. The potentially identifying cross-site data is
encapsulated into <em>aggregatable reports</em>. To prevent leakage, this
data is encrypted, ensuring it can only be processed by an <em>aggregation
service</em>. During processing, this service will add noise and impose
limits on how many queries can be performed.
Markup Shorthands: markdown on
Complain About: accidental-2119 on, missing-example-ids on
Assume Explicit For: on
</pre>
<pre class="anchors">
urlPrefix: https://wicg.github.io/turtledove/; type: interface
text: InterestGroupBiddingScriptRunnerGlobalScope
text: InterestGroupScriptRunnerGlobalScope
text: InterestGroupScoringScriptRunnerGlobalScope
text: InterestGroupReportingScriptRunnerGlobalScope
urlPrefix: https://wicg.github.io/shared-storage/; type: interface
text: SharedStorageWorklet
text: SharedStorageWorkletGlobalScope
spec: hr-time; type: dfn; urlPrefix: https://w3c.github.io/hr-time/
text: current wall time; url: #dfn-current-wall-time
text: duration; url: #dfn-duration
text: duration from; url: #dfn-duration-from
text: moment; url: #dfn-moment
text: unix epoch; url: #dfn-unix-epoch
text: wall clock; url: #dfn-wall-clock
spec: html; type: dfn; urlPrefix: https://tc39.es/ecma262/
text: call; url: sec-call
spec: webdriver; urlPrefix: https://w3c.github.io/webdriver/
type: dfn
text: getting a property; url: dfn-getting-properties
text: error code; url: dfn-error-code
spec: fenced-frame; urlPrefix: https://wicg.github.io/fenced-frame/
type: dfn;
for: browsing context;
text: fenced frame config instance; url: browsing-context-fenced-frame-config-instance
</pre>
<pre class=link-defaults>
spec:infra; type:dfn; text:user agent
spec:webidl; type:dfn; text:attribute
</pre>
Introduction {#intro}
=====================
<em>This section is non-normative.</em>
Motivation {#motivation}
------------------------
Browsers are now working to prevent cross-site user tracking, including by
partitioning storage and removing third-party cookies. There are a range of API
proposals to continue supporting legitimate use cases in a way that respects
user privacy. Many of these APIs, including the
<a href="https://wicg.github.io/shared-storage/">Shared Storage API</a> and the
<a href="https://wicg.github.io/turtledove/">Protected Audience API</a>, isolate
potentially identifying cross-site data in special contexts, which ensures that
the data cannot escape the user agent.
Relative to cross-site data from an individual user, aggregate data about groups
of users can be less sensitive and yet would be sufficient for a wide range of
use cases. An aggregation service has been built to allow reporting noisy,
aggregated cross-site data. This service was originally created for use by the
<a href="https://wicg.github.io/attribution-reporting-api/">Attribution
Reporting API</a>, but allowing more general aggregation supports additional use
cases. In particular, the Protected Audience and Shared Storage APIs expect this
functionality to be available.
Overview {#overview}
--------------------
This document outlines a general-purpose API that can be called from isolated
contexts that have access to cross-site data (such as a Shared Storage worklet).
Within these contexts, potentially identifying data can be encapsulated into
"aggregatable reports". To prevent leakage, the cross-site data in these reports
is encrypted to ensure it can only be processed by the aggregation service.
During processing, this service adds noise and imposes limits on how many
queries can be performed.
This API provides functions that allow the origin to construct an aggregatable
report and specify the values to be embedded into its encrypted payload (for
later computation via the aggregation service). These calls result in the
aggregatable report being queued to be sent to the reporting endpoint of the
script's origin after a delay. After the endpoint receives the reports, it will
batch the reports and send them to the aggregation service for processing. The
output of that process is a summary report containing the (approximate) result,
which is dispatched back to the script's origin.
Alternative considered {#alternative-considered}
------------------------------------------------
Instead of the chosen API shape, we considered aligning with a design that is
much closer to {{WindowOrWorkerGlobalScope/fetch()}}. However, there are a few
key differences which make this unfavorable:
- This API is designed to be used in isolated contexts where
{{WindowOrWorkerGlobalScope/fetch()}} is not available.
- It's an anti-goal to give the developer control over when [=aggregatable
reports=] are being sent or knowledge that they were sent (outside of the
isolated context). Note, however, the exception when providing a context ID
from <em>outside</em> the isolated context, see [Protecting against leaks
via the number of
reports](#protecting-against-leaks-via-the-number-of-reports) below.
- The reports cannot be sent to arbitrary reporting endpoints, only a
particular <code>[[RFC8615|.well-known]]</code> path on the script origin.
- The report's input is very specific (an array of {{PAHistogramContribution}}s)
and is not amenable to {{WindowOrWorkerGlobalScope/fetch()}}'s general
purpose contents.
- There is no concept of a response.
So, we chose the more tailored API shape detailed below.
Exposed interface {#exposed-interface}
======================================
<xmp class="idl">
[Exposed=(InterestGroupScriptRunnerGlobalScope,SharedStorageWorklet),
SecureContext]
interface PrivateAggregation {
undefined contributeToHistogram(PAHistogramContribution contribution);
undefined enableDebugMode(optional PADebugModeOptions options = {});
};
dictionary PAHistogramContribution {
required bigint bucket;
required long value;
bigint filteringId = 0;
};
dictionary PADebugModeOptions {
required bigint debugKey;
};
</xmp>
Issue: Per the <a href=https://www.w3.org/TR/design-principles/#numeric-types>
Web Platform Design Principles</a>, we should consider switching `long` to
`[EnforceRange] long long`.
Issue: {{PrivateAggregation/enableDebugMode(options)}}'s argument should not
have a default value of `{}`. Alternatively, {{PADebugModeOptions/debugKey}}
should not be required in {{PADebugModeOptions}}.
Each {{PrivateAggregation}} object has the following fields:
<dl dfn-for="PrivateAggregation">
: <dfn>scoping details</dfn> (default null)
:: A [=scoping details=] or null
: <dfn>allowed to use</dfn> (default false)
:: A [=boolean=]
</dl>
Note: See [Exposing to global scopes](#exposing) below.
<div algorithm>
The <dfn method for="PrivateAggregation">
contributeToHistogram(PAHistogramContribution contribution)</dfn> method steps
are:
1. If |contribution|["{{PAHistogramContribution/bucket}}"] is not [=set/
contained=] in [=the exclusive range|the range=] 0 to 2<sup>128</sup>,
exclusive, [=exception/throw=] a {{RangeError}}.
1. If |contribution|["{{PAHistogramContribution/value}}"] is negative,
[=exception/throw=] a {{RangeError}}.
1. Let |scopingDetails| be [=this=]'s [=PrivateAggregation/scoping details=].
1. Let |batchingScope| be the result of running |scopingDetails|' [=scoping
details/get batching scope steps=].
1. Let |filteringIdMaxBytes| be the [=default filtering ID max bytes=].
1. If [=pre-specified report parameters map=][|batchingScope|] [=map/exists=]:
1. Set |filteringIdMaxBytes| to [=pre-specified report parameters
map=][|batchingScope|]'s [=pre-specified report parameters/filtering ID
max bytes=].
1. If |contribution|["{{PAHistogramContribution/filteringId}}"] is not [=set/
contained=] in [=the exclusive range|the range=] 0 to
256<sup>|filteringIdMaxBytes|</sup>, exclusive, [=exception/throw=] a
{{RangeError}}.
1. Let |entry| be a new [=contribution cache entry=] with the items:
: [=contribution cache entry/contribution=]
:: |contribution|
: [=contribution cache entry/batching scope=]
:: |batchingScope|
: [=contribution cache entry/debug scope=]
:: The result of running |scopingDetails|' [=scoping details/get debug scope
steps=].
1. [=Append an entry to the contribution cache|Append=] |entry| to the
[=contribution cache=].
Issue: Ensure errors are of an appropriate type, e.g. {{InvalidAccessError}} is
deprecated.
Issue(44): Consider accepting an array of contributions.
</div>
<div algorithm>
The <dfn method for="PrivateAggregation">
enableDebugMode(optional PADebugModeOptions options)</dfn> method steps are:
1. Let |scopingDetails| be [=this=]'s [=PrivateAggregation/scoping details=].
1. Let |debugScope| be the result of running |scopingDetails|' [=scoping
details/get debug scope steps=].
1. If [=debug scope map=][|debugScope|] [=map/exists=], [=exception/throw=] a
"{{DataError}}" {{DOMException}}.
Note: This would occur if `enableDebugMode()` has already been run for this
[=debug scope=].
1. Let |debugKey| be null.
1. If |options| was given:
1. If |options|["{{PADebugModeOptions/debugKey}}"] is not [=set/contained=]
in [=the exclusive range|the range=] 0 to 2<sup>64</sup>, exclusive,
[=exception/throw=] a "{{DataError}}" {{DOMException}}.
1. Set |debugKey| to |options|["{{PADebugModeOptions/debugKey}}"].
1. Let |debugDetails| be a new [=debug details=] with the items:
: [=debug details/enabled=]
:: true
: [=debug details/key=]
:: |debugKey|
1. Optionally, set |debugDetails| to a new [=debug details=].
Note: This allows the user agent to make debug mode unavailable globally or
just for certain callers.
1. [=map/Set=] [=debug scope map=][|debugScope|] to |debugDetails|.
Issue: Ensure errors are of an appropriate type, e.g. {{InvalidAccessError}} is
deprecated.
</div>
Exposing to global scopes {#exposing}
=====================================
To expose this API to a global scope, a [=read only=] [=attribute=]
`privateAggregation` of type {{PrivateAggregation}} should be exposed on the
global scope. Its [=getter steps=] should be set to the [=get the
privateAggregation=] steps given [=this=].
Each global scope should set the [=PrivateAggregation/allowed to use=] for the
{{PrivateAggregation}} object it exposes based on whether a relevant
[=document=] is [=allowed to use=] the "<code>[=private-aggregation=]</code>"
[=policy-controlled feature=].
Additionally, each global scope should set the [=PrivateAggregation/scoping
details=] for the {{PrivateAggregation}} object it exposes to a non-null value.
The global scope should wait to set the field until the API is intended to be
available.
<div class="example" id="shared-storage-only-within-operations">
Shared Storage only allows Private Aggregation when an operation is being
invoked, not in the top-level context:
<pre highlight="js">
class ExampleOperation {
async run(data) {
privateAggregation.contributeToHistogram(...) // This is allowed.
}
}
register('example-operation', ExampleOperation);
privateAggregation.contributeToHistogram(...) // This would cause an error.
</pre>
So, Shared Storage sets the [=PrivateAggregation/scoping details=]
immediately after the initial execution of the module script is complete.
</div>
For any [=batching scope=] returned by the [=scoping details/get batching scope
steps=], the [=process contributions for a batching scope=] steps should later
be performed given that same batching scope, the global scope's [=relevant
settings object=]'s [=environment settings object/origin=], some [=context
type=] and a timeout (or null).
Note: This last requirement means that global scopes with different origins
cannot share the same batching scope, see [Same-origin
policy](#same-origin-policy) discussion.
For any [=debug scope=] returned by the [=scoping details/get debug scope
steps=], the [=mark a debug scope complete=] steps should later be performed
given that same [=debug scope=].
Note: A later algorithm [=asserts=] that, for any [=contribution cache entry=]
in the [=contribution cache=], the [=mark a debug scope complete=] steps
were performed given the entry's [=contribution cache entry/debug scope=]
before the [=process contributions for a batching scope=] steps are
performed given the entry's [=contribution cache entry/batching scope=].
APIs exposing Private Aggregation {#apis-exposing-private-aggregation}
----------------------------------------------------------------------
<em>This section is non-normative.</em>
This API is currently exposed in global scopes defined in the specifications of
two APIs:
1. <a href="https://wicg.github.io/shared-storage/">Shared Storage</a> and
1. <a href="https://wicg.github.io/turtledove/">Protected Audience</a>.
Structures {#structures}
========================
<h3 dfn-type=dfn>Batching scope</h3>
A batching scope is a <a spec=HTML>unique internal value</a> that identifies
which {{PAHistogramContribution}}s should be sent in the same [=aggregatable
report=] unless their [=aggregatable report/debug details=] differ.
Issue: Unique internal value is not an exported definition. See
<a href="https://github.com/whatwg/infra/issues/583">infra/583</a>.
<h3 dfn-type=dfn>Debug scope</h3>
A debug scope is a <a spec=HTML>unique internal value</a> that identifies which
{{PAHistogramContribution}}s should have their [=debug details=] affected by the
presence or absence of a call to {{PrivateAggregation/enableDebugMode()}} in the
same period of execution.
<h3 dfn-type=dfn>Scoping details</h3>
A scoping details is a [=struct=] with the following items:
<dl dfn-for="scoping details">
: <dfn>get batching scope steps</dfn>
:: An algorithm returning a [=batching scope=]
: <dfn>get debug scope steps</dfn>
:: An algorithm returning a [=debug scope=]
</dl>
<h3 dfn-type=dfn>Debug details</h3>
A debug details is a [=struct=] with the following items:
<dl dfn-for="debug details">
: <dfn>enabled</dfn> (default false)
:: A [=boolean=]
: <dfn>key</dfn> (default null)
:: An unsigned 64-bit integer or null. The key must be null if [=debug
details/enabled=] is false.
</dl>
<h3 dfn-type=dfn>Contribution cache entry</h3>
A contribution cache entry is a [=struct=] with the following items:
<dl dfn-for="contribution cache entry">
: <dfn>contribution</dfn>
:: A {{PAHistogramContribution}}
: <dfn>batching scope</dfn>
:: A [=batching scope=]
: <dfn>debug scope</dfn>
:: A [=debug scope=]
: <dfn>debug details</dfn> (default null)
:: A [=debug details=] or null
</dl>
<h3 dfn-type=dfn>Aggregatable report</h3>
An aggregatable report is a [=struct=] with the following items:
<dl dfn-for="aggregatable report">
: <dfn>reporting origin</dfn>
:: An [=origin=]
: <dfn>original report time</dfn>
:: A [=moment=]
: <dfn>report time</dfn>
:: A [=moment=]
: <dfn>contributions</dfn>
:: A [=list=] of {{PAHistogramContribution}}s
: <dfn>api</dfn>
:: A [=context type=]
: <dfn>report ID</dfn>
:: A [=string=]
: <dfn>debug details</dfn>
:: A [=debug details=]
: <dfn>aggregation coordinator</dfn>
:: An [=aggregation coordinator=]
: <dfn>context ID</dfn>
:: A [=string=] or null
: <dfn>filtering ID max bytes</dfn>
:: A positive integer
: <dfn>queued</dfn>
:: A [=boolean=]
</dl>
Aggregation coordinator {#aggregation-coordinator-structure}
------------------------------------------------------------
An <dfn>aggregation coordinator</dfn> is an [=origin=] that the [=allowed
aggregation coordinator set=] [=set/contains=].
Issue: Consider switching to the <a spec="attribution-reporting-api">suitable
origin</a> concept used by the Attribution Reporting API here and elsewhere.
Issue: Move other structures to be defined inline instead of via a header.
Consider also removing all the subheadings.
<h3 dfn-type=dfn>Context type</h3>
A context type is a [=string=] indicating what kind of global scope the
{{PrivateAggregation}} object was exposed in. Each API exposing Private
Aggregation should pick a unique string (or multiple) for this.
Pre-specified report parameters {#pre-specified-report-parameters-structure}
----------------------------------------------------------------------------
A <dfn>pre-specified report parameters</dfn> is a [=struct=] with the following
items:
<dl dfn-for="pre-specified report parameters">
: <dfn>context ID</dfn> (default: null)
:: A [=string=] or null
: <dfn>filtering ID max bytes</dfn> (default: [=default filtering ID max bytes=])
:: A positive integer
</dl>
Storage {#storage}
==================
A [=user agent=] holds an <dfn>aggregatable report cache</dfn>, which is a
[=list=] of [=aggregatable reports=].
A [=user agent=] holds an <dfn>aggregation coordinator map</dfn>, which is a
[=map=] from [=batching scopes=] to [=aggregation coordinators=].
A [=user agent=] holds a <dfn>pre-specified report parameters map</dfn>, which
is a [=map=] from [=batching scopes=] to [=pre-specified report parameters=].
A [=user agent=] holds a <dfn>contribution cache</dfn>, which is a [=list=] of
[=contribution cache entries=].
A [=user agent=] holds a <dfn>debug scope map</dfn>, which is a [=map=] from
[=debug scopes=] to [=debug details=].
Issue: Elsewhere, link to definition when using [=user agent=].
Clearing storage {#clearing-storage}
----------------------------------------
The user agent must expose controls that allow the user to delete data from
the [=aggregatable report cache=] as well as any contribution history data
stored for the [=consume budget if permitted=] algorithm.
The user agent may expose controls that allow the user to delete data from the
[=contribution cache=], the [=debug scope map=] and the [=pre-specified report
parameters map=].
Constants {#constants}
======================
<dfn>Default filtering ID max bytes</dfn> is a positive integer controlling the
max bytes used if none is explicitly chosen. Its value is 1.
<dfn>Valid filtering ID max bytes range</dfn> is a [=set=] of positive integers
controlling the allowable values of max bytes. Its value is [=the inclusive
range|the range=] 1 to 8, inclusive.
Issue: Consider adding more constants.
[=Implementation-defined=] values {#implementation-defined-values}
==================================================================
<dfn>Allowed aggregation coordinator set</dfn> is a [=set=] of [=origins=] that
controls which [=origins=] are valid [=aggregation coordinators=]. Every
[=set/item=] in this [=set=] must be a [=potentially trustworthy origin=].
<dfn>Default aggregation coordinator</dfn> is an [=aggregation coordinator=]
that controls which is used for a report if none is explicitly selected.
<dfn>Maximum report contributions</dfn> is a [=map=] from [=context type=] to
positive integers. Semantically, it defines the maximum number of contributions
that can be present in a single report for every kind of calling context, e.g.
Shared Storage.
<dfn>Minimum report delay</dfn> is a non-negative [=duration=] that controls the
minimum delay to deliver an [=aggregatable report=].
<dfn>Randomized report delay</dfn> is a positive [=duration=] that controls the
random delay to deliver an [=aggregatable report=]. This delay is additional to
the [=minimum report delay=].
Permissions Policy integration {#permissions-policy-integration}
================================================================
This specification defines a [=policy-controlled feature=] identified by the
string "<code><dfn>private-aggregation</dfn></code>". Its [=policy-controlled
feature/default allowlist=] is "`*`".
Note: The [=PrivateAggregation/allowed to use=] field is set by other
specifications that integrate with this API according to this
[=policy-controlled feature=].
Algorithms {#algorithms}
====================
To <dfn>serialize an integer</dfn>, represent it as a [=string=] of the shortest
possible decimal number.
Issue: This would ideally be replaced by a more descriptive algorithm in Infra.
See <a href="https://github.com/whatwg/infra/issues/201">infra/201</a>.
Exported algorithms {#exported-algorithms}
------------------------------------------
Note: These algorithms allow other specifications to integrate with this API.
<div algorithm>
To <dfn export>get the privateAggregation</dfn> given a
{{PrivateAggregation}} |this|:
1. Let |scopingDetails| be |this|'s [=PrivateAggregation/scoping details=].
1. If |scopingDetails| is null, [=exception/throw=] a "{{NotAllowedError}}"
{{DOMException}}.
Note: This indicates the API is not yet available, for example, because the
initial execution of the script after loading is not complete.
Issue: Consider improving developer ergonomics here (e.g. a way to detect
this case).
1. If |this|'s [=PrivateAggregation/allowed to use=] is false, [=exception/
throw=] an "{{InvalidAccessError}}" {{DOMException}}.
1. Return |this|.
Issue: Ensure errors are of an appropriate type, e.g. {{InvalidAccessError}} is
deprecated.
</div>
<div algorithm>
To <dfn export>append an entry to the contribution cache</dfn> given a
[=contribution cache entry=] |entry|:
1. [=list/Append=] |entry| to the [=contribution cache=].
</div>
<div algorithm>
To <dfn export>get a debug details</dfn> given a [=debug scope=]
|debugScope|, perform the following steps. They return a [=debug details=].
1. If [=debug scope map=][|debugScope|] [=map/exists=], return
[=debug scope map=][|debugScope|].
1. Otherwise, return a new [=debug details=].
</div>
<div algorithm>
To <dfn export>mark a debug scope complete</dfn> given a [=debug
scope=] |debugScope| and an optional [=debug details=] or null
|debugDetailsOverride| (default null):
1. Let |debugDetails| be |debugDetailsOverride|.
1. If [=debug scope map=][|debugScope|] [=map/exists=]:
1. [=Assert=]: |debugDetailsOverride| is null.
Note: The override can be provided if the debug details have not been
set otherwise.
1. Set |debugDetails| to [=debug scope map=][|debugScope|].
1. [=map/Remove=] [=debug scope map=][|debugScope|].
1. If |debugDetails|'s [=debug details/key=] is not null, [=assert=]:
|debugDetails|'s [=debug details/enabled=] is true.
1. If |debugDetails| is null, set |debugDetails| to a new [=debug details=].
1. [=list/iterate|For each=] |entry| of the [=contribution cache=]:
1. If |entry|'s [=contribution cache entry/debug scope=] is |debugScope|,
set |entry|'s [=contribution cache entry/debug details=] to
|debugDetails|.
</div>
<div algorithm>
To <dfn>determine if a report should be sent deterministically</dfn> given a
[=pre-specified report parameters=] |preSpecifiedParams|, perform the following
steps. They return a [=boolean=]:
1. If |preSpecifiedParams|' [=pre-specified report parameters/context ID=] is
not null, return true.
1. If |preSpecifiedParams|' [=pre-specified report parameters/filtering ID max
bytes=] is not the [=default filtering ID max bytes=], return true.
1. Return false.
Note: If a context ID or non-default filtering ID max bytes was specified, a
report is sent, even if there are no contributions or there is insufficent
budget for the requested contributions. See [Protecting against leaks via
the number of reports](#protecting-against-leaks-via-the-number-of-reports).
</div>
<div algorithm>
To <dfn export>process contributions for a batching scope</dfn> given
a [=batching scope=] |batchingScope|, an [=origin=] |reportingOrigin|, a
[=context type=] |contextType| and a [=moment=] or null |timeout|:
1. Let |batchEntries| be a new [=list=].
1. [=list/iterate|For each=] |entry| of the [=contribution cache=]:
1. If |entry|'s [=contribution cache entry/batching scope=] is
|batchingScope|:
1. [=Assert=]: |entry|'s [=contribution cache entry/debug details=] is
not null.
Note: This asserts that the [=mark a debug scope complete=] steps
were run before the [=process contributions for a batching
scope=] steps.
1. [=list/Append=] |entry| to |batchEntries|.
1. Let |aggregationCoordinator| be the [=default aggregation coordinator=].
1. If [=aggregation coordinator map=][|batchingScope|] [=map/exists=]:
1. Set |aggregationCoordinator| to [=aggregation coordinator
map=][|batchingScope|].
1. [=map/Remove=] [=aggregation coordinator map=][|batchingScope|].
1. Let |preSpecifiedParams| be a new [=pre-specified report parameters=].
1. If [=pre-specified report parameters map=][|batchingScope|] [=map/exists=]:
1. Set |preSpecifiedParams| to [=pre-specified report parameters
map=][|batchingScope|].
1. [=map/Remove=] [=pre-specified report parameters map=][|batchingScope|].
1. Let |isDeterministicReport| be the result of [=determining if a report should
be sent deterministically=] given |preSpecifiedParams|.
1. If |isDeterministicReport| is false, [=assert=]: |timeout| is null.
Note: Timeouts can only be used for deterministic reports.
1. If |batchEntries| [=list/is empty=] and |isDeterministicReport| is false,
return.
1. Let |batchedContributions| be a new [=ordered map=].
1. [=list/iterate|For each=] |entry| of |batchEntries|:
1. [=list/Remove=] |entry| from the [=contribution cache=].
1. Let |debugDetails| be |entry|'s [=contribution cache entry/debug
details=].
1. If |batchedContributions|[|debugDetails|] does not [=map/exist=]:
1. [=map/Set=] |batchedContributions|[|debugDetails|] to a new [=list=].
1. [=list/Append=] |entry|'s [=contribution cache entry/contribution=] to
|batchedContributions|[|debugDetails|].
1. If |batchedContributions| [=map/is empty=]:
1. Let |debugDetails| be a new [=debug details=].
1. Set |batchedContributions|[|debugDetails|] to a new [=list=].
1. [=map/iterate|For each=] |debugDetails| → |contributions| of
|batchedContributions|:
1. Perform the [=report creation and scheduling steps=] with
|reportingOrigin|, |contextType|, |contributions|, |debugDetails|,
|aggregationCoordinator|, |preSpecifiedParams| and |timeout|.
Note: These steps break up the contributions based on their [=debug details=] as
each report can only have one set of metadata.
</div>
<div algorithm>
To <dfn export>determine if an origin is an aggregation coordinator</dfn> given
an [=origin=] |origin|, perform the following steps. They return a [=boolean=].
1. Return whether |origin| is an [=aggregation coordinator=].
</div>
<div algorithm>
To <dfn export>obtain the Private Aggregation coordinator</dfn> given a
{{USVString}} |originString|, perform the following steps. They return an
[=aggregation coordinator=] or a {{DOMException}}.
1. Let |url| be the result of running the [=URL parser=] on |originString|.
1. If |url| is failure or null, return a new {{DOMException}} with name
"`SyntaxError`".
Issue: Consider throwing an error if the path is not empty.
1. Let |origin| be |url|'s [=url/origin=].
1. If the result of [=determining if an origin is an aggregation coordinator=]
given |origin| is false, return a new {{DOMException}} with name
"`DataError`".
1. Return |origin|.
</div>
<div algorithm>
To <dfn export>set the aggregation coordinator for a batching scope</dfn> given
an [=origin=] |origin| and a [=batching scope=] |batchingScope|:
1. [=Assert=]: |origin| is an [=aggregation coordinator=].
1. [=map/Set=] [=aggregation coordinator map=][|batchingScope|] to |origin|.
</div>
Issue: Elsewhere, surround algorithms in a `<div algorithm>` block to match, and
add styling for all algorithms per
[bikeshed/1472](https://github.com/speced/bikeshed/issues/1472).
<div algorithm>
To <dfn export>set the pre-specified report parameters for a batching
scope</dfn> given a [=pre-specified report parameters=] |params| and a
[=batching scope=] |batchingScope|:
1. Let |contextId| be |params|' [=pre-specified report parameters/context ID=].
1. [=Assert=]: |contextId| is null or |contextId|'s [=string/length=] is not
larger than 64.
1. Let |filteringIdMaxBytes| be |params|' [=pre-specified report parameters/
filtering ID max bytes=].
1. [=Assert=]: |filteringIdMaxBytes| is [=set/contained=] in the [=valid
filtering ID max bytes range=]
1. [=map/Set=] [=pre-specified report parameters map=][|batchingScope|] to
|params|.
</div>
Scheduling reports {#scheduling-reports}
----------------------------------------
<div algorithm>
To perform the <dfn>report creation and scheduling steps</dfn> with an
[=origin=] |reportingOrigin|, a [=context type=] |api|, a [=list=] of
{{PAHistogramContribution}}s |contributions|, a [=debug details=]
|debugDetails|, an [=aggregation coordinator=] |aggregationCoordinator|, a
[=pre-specified report parameters=] |preSpecifiedParams| and a [=moment=] or
null |timeout|:
1. [=Assert=]: |reportingOrigin| is a [=potentially trustworthy origin=].
1. Optionally, return.
Note: This [=implementation-defined=] condition is intended to allow [=user
agents=] to drop reports for a number of reasons, for example user
opt-out or an origin not being
<a href="https://github.com/privacysandbox/attestation">enrolled</a>.
1. Let |mergedContributions| be a new [=list=].
1. [=list/For each=] |contribution| of |contributions|:
1. Let |hasProcessedContribution| be false.
1. [=list/For each=] |mergedContribution| of |mergedContributions|:
1. If |contribution| and |mergedContribution| have both the same
{{PAHistogramContribution/bucket}} and the same
{{PAHistogramContribution/filteringId}}:
1. Add |contribution|'s {{PAHistogramContribution/value}} to
|mergedContribution|'s {{PAHistogramContribution/value}}.
1. Set |hasProcessedContribution| to true.
1. [=iteration/Break=].
1. If |hasProcessedContribution| is false, [=list/append=] |contribution| to
|mergedContributions|.
1. Let |truncatedContributions| be a new [=list=].
1. If |mergedContributions| has a [=list/size=] greater than [=maximum report
contributions=][|api|]:
1. [=set/For each=] |n| of [=the exclusive range|the range=] 0 to [=maximum
report contributions=][|api|], exclusive:
1. [=set/Append=] |mergedContributions|[|n|] to
|truncatedContributions|.
1. Otherwise, set |truncatedContributions| to |mergedContributions|.
1. Let |contributionSum| be 0.
1. [=set/iterate|For each=] |contribution| of |truncatedContributions|:
1. [=Assert=]: |contribution|["{{PAHistogramContribution/value}}"] is
non-negative.
1. Add |contribution|["{{PAHistogramContribution/value}}"] to
|contributionSum|.
1. Let |currentWallTime| be the [=current wall time=].
1. Let |sufficientBudget| be the result of [=consuming budget if permitted=]
given |contributionSum|, |reportingOrigin|, |api| and |currentWallTime|.
Issue: Allow for some of the contributions to still be approved even if
there isn't enough budget for the entire report. Note that this change
would require the merging step to be postponed until after budgeting and
the truncation step modified to account for the later merging.
1. If |sufficientBudget| is false:
1. Let |isDeterministicReport| be the result of [=determining if a report
should be sent deterministically=] given |preSpecifiedParams|.
1. If |isDeterministicReport| is false, return.
1. [=list/Empty=] |truncatedContributions|.
1. Let |report| be the result of [=obtaining an aggregatable report=] given
|reportingOrigin|, |api|, |truncatedContributions|, |debugDetails|,
|aggregationCoordinator|, |preSpecifiedParams|, |timeout| and
|currentWallTime|.
1. [=set/Append=] |report| to the user agent's [=aggregatable report cache=].
</div>
<div algorithm>
To <dfn>consume budget if permitted</dfn> given an integer |value|, an
[=origin=] <var ignore>origin</var>, a [=context type=] <var ignore>api</var> and a
[=moment=] <var ignore>currentTime</var>, perform [=implementation-defined=] steps. They return
a [=boolean=], which indicates whether there is sufficient 'contribution budget'
left to send the requested contribution |value| (or multiple contributions with
a sum of values equal to |value|). This budget should be bound to
usage over time, e.g. the contribution sum over the last 24 hours. The algorithm
should assume that the contribution will be sent if and only if true is
returned, i.e. it should consume the budget in that case. If |value| is zero,
this algorithm should return true.
</div>
<div algorithm>
To <dfn>obtain an aggregatable report</dfn> given an [=origin=]
|reportingOrigin|, a [=context type=] |api|, a [=list=] of
{{PAHistogramContribution}}s |contributions|, a [=debug details=]
|debugDetails|, an [=aggregation coordinator=] |aggregationCoordinator|, a
[=pre-specified report parameters=] |preSpecifiedParams|, a [=moment] or null
|timeout| and a [=moment=] |currentTime|,
perform the following steps. They return an [=aggregatable report=].
1. [=Assert=]: |reportingOrigin| is a [=potentially trustworthy origin=].
1. Let |reportTime| be the result of running [=obtain a report delivery time=]
given |currentTime| and |timeout|.
1. Let |report| be a new [=aggregatable report=] with the items:
: [=aggregatable report/reporting origin=]
:: |reportingOrigin|
: [=aggregatable report/original report time=]
:: |reportTime|
: [=aggregatable report/report time=]
:: |reportTime|
: [=aggregatable report/contributions=]
:: |contributions|
: [=aggregatable report/api=]
:: |api|
: [=aggregatable report/report ID=]
:: The result of [=generating a random UUID=].
: [=aggregatable report/debug details=]
:: |debugDetails|
: [=aggregatable report/aggregation coordinator=]
:: |aggregationCoordinator|
: [=aggregatable report/context ID=]
:: |preSpecifiedParams|' [=pre-specified report parameters/context ID=]
: [=aggregatable report/filtering ID max bytes=]
:: |preSpecifiedParams|' [=pre-specified report parameters/filtering ID max
bytes=]
: [=aggregatable report/queued=]
:: false
1. Return |report|.
</div>
<div algorithm>
To <dfn>obtain a report delivery time</dfn> given a [=moment=]
|currentTime| and a [=moment=] or null |timeout|, perform the following steps.
They return a [=moment=].
1. If |timeout| is not null:
1. Return |timeout|.
1. If [=automation local testing mode enabled=] is true, return
|currentTime|.
1. Let |r| be a random double between 0 (inclusive) and 1 (exclusive) with
uniform probability.
1. Return |currentTime| + [=minimum report delay=] + |r| * [=randomized report
delay=].
</div>
Sending reports {#sending-reports}
----------------------------------
Note: This section is largely copied from the
<a href="https://wicg.github.io/attribution-reporting-api/">Attribution
Reporting API spec</a>, adapting as necessary.
Issue: Do we have to use the [=queue a task=] algorithm here?
The user agent must periodically [=attempt to queue reports for sending=] given
its [=aggregatable report cache=].
<div algorithm>
To <dfn>attempt to queue reports for sending</dfn> given a [=list=] of
[=aggregatable reports=] |reports|:
1. [=list/iterate|For each=] |report| of |reports|, run these steps [=in
parallel=]:
1. Run these steps, but [=abort when=] the [=user agent=] shuts down:
1. If |report|'s [=aggregatable report/queued=] value is true, return.
1. Set |report|'s [=aggregatable report/queued=] value to true.
1. Let |currentWallTime| be the [=current wall time=].
1. If |report|'s [=aggregatable report/report time=] is before
|currentWallTime|, set |report|'s [=aggregatable report/report
time=] to |currentWallTime| plus an [=implementation-defined=]
random non-negative [=duration=].
Note: On startup, it is possible the user agent will need to send
many reports whose report times passed while the browser was
closed. Adding random delay prevents temporal joining of
reports.
1. Wait until the [=current wall time=] is equal to or after |report|'s
[=aggregatable report/report time=].
1. Optionally, wait a further [=implementation-defined=] non-negative
[=duration=].
Note: This is intended to allow user agents to optimize device
resource usage and wait for the user agent to be online.
1. Run [=attempt to deliver a report=] with |report|.
1. [=If aborted=], set |report|'s [=aggregatable report/queued=] value to
false.
Note: It might be more practical to perform this step when the [=user
agent=] next starts up.
</div>
<div algorithm>
To <dfn>attempt to deliver a report</dfn> given an [=aggregatable report=]
|report|:
1. Let |url| be the result of [=obtaining a reporting endpoint=] given
|report|'s [=aggregatable report/reporting origin=] and |report|'s
[=aggregatable report/api=].
1. Let |data| be the result of [=serializing an aggregatable report=] given
|report|.
1. If |data| is an error, [=list/remove=] |report| from the [=aggregatable
report cache=].
Issue: Do we need to queue this task?
1. Let |request| be the result of [=creating a report request=] given |url| and
|data|.
1. [=Queue a task=] to [=fetch=] |request| with [=fetch/processResponse=] being
the following steps:
1. Let |shouldRetry| be an [=implementation-defined=] [=boolean=]. The value
should be false if no error occurred.
1. If |shouldRetry| is true:
1. Set |report|'s [=aggregatable report/report time=] to the [=current
wall time=] plus an [=implementation-defined=] non-negative
[=duration=].
1. Set |report|'s [=aggregatable report/queued=] value to false.
1. Otherwise, [=list/remove=] |report| from the [=aggregatable report
cache=].
</div>
<div algorithm>
To <dfn>obtain a reporting endpoint</dfn> given an [=origin=] |reportingOrigin|
and [=context type=] |api|, perform the following steps. They return a [=URL=].
1. [=Assert=]: |reportingOrigin| is a [=potentially trustworthy origin=].
1. Let |path| be the [=string/concatenation=] of
«"<code>[[RFC8615|.well-known]]/private-aggregation/report-</code>", |api|».
Issue(67): Register this well-known directory.
1. Let |base| be the result on running the [=URL parser=] on the [=serialization
of an origin|serialization=] of |reportingOrigin|.
1. [=Assert=]: |base| is not failure.
1. Let |result| be the result of running the [=URL parser=] on |path| with
|base|.
1. [=Assert=]: |result| is not failure.
1. Return |result|.
</div>
<div algorithm>
To <dfn>create a report request</dfn> given a [=URL=] |url| and a [=byte
sequence=] |body|:
1. Let |request| be a new [=request=] with the following properties:
: [=request/method=]
:: "`POST`"
: [=request/URL=]
:: |url|
: [=request/header list=]
:: «("`Content-Type`", "`application/json`")»
: [=request/unsafe-request flag=]
:: set
: [=request/body=]
:: |body|
: [=request/client=]
:: `null`
: [=request/window=]
:: "`no-window`"
: [=request/service-workers mode=]
:: "`none`"
: [=request/initiator=]
:: ""
: [=request/referrer=]
:: "`no-referrer`"
: [=request/mode=]
:: "`cors`"
: [=request/credentials mode=]
:: "`omit`"
: [=request/cache mode=]
:: "`no-store`"
1. Return |request|.
</div>
Serializing reports {#serializing-reports}
------------------------------------------
Note: This section is largely copied from the
<a href="https://wicg.github.io/attribution-reporting-api/">Attribution
Reporting API spec</a>, adapting as necessary.
<div algorithm>
To <dfn>serialize an aggregatable report</dfn> given an [=aggregatable report=]
|report|, perform the following steps. They return a [=byte sequence=] or an
error.
1. Let |aggregationServicePayloads| be the result of [=obtaining the aggregation
service payloads=] given |report|.
1. If |aggregationServicePayloads| is an error, return
|aggregationServicePayloads|.
1. Let |data| be an [=ordered map=] of the following key/value pairs:
: "`aggregation_coordinator_origin`"
:: |report|'s [=aggregatable report/aggregation coordinator=],
[=serialization of an origin|serialized=].
: "`aggregation_service_payloads`"
:: |aggregationServicePayloads|
: "`shared_info`"
:: The result of [=obtaining a report's shared info=] given |report|.
1. Let |debugKey| be |report|'s [=aggregatable report/debug details=]'s [=debug
details/key=].
1. If |debugKey| is not null, [=map/set=] |data|["`debug_key`"] to |debugKey|.
1. Let |contextId| be |report|'s [=aggregatable report/context ID=].
1. If |contextId| is not null, [=map/set=] |data|["`context_id`"] to
|contextId|.
1. Return the [=byte sequence=] resulting from executing [=serialize an infra
value to JSON bytes=] on |data|.
</div>
<div algorithm>
To <dfn>obtain the aggregation service payloads</dfn> given an [=aggregatable
report=] |report|, perform the following steps. They return a [=list=] of
[=maps=] or an error.
1. Let |publicKeyTuple| be the result of [=obtaining the public key for
encryption=] given |report|'s [=aggregatable report/aggregation
coordinator=].
1. If |publicKeyTuple| is an error, return |publicKeyTuple|.
1. Let (|pkR|, |keyId|) be |publicKeyTuple|.
1. Let |plaintextPayload| be the result of [=obtaining the plaintext payload=]
given |report|.
1. Let |sharedInfo| be the result of [=obtaining a report's shared info=] given
|report|.
1. Let |encryptedPayload| be the result of [=encrypting the payload=] given
|plaintextPayload|, |pkR| and |sharedInfo|.
1. If |encryptedPayload| is an error, return |encryptedPayload|.