summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fledge/tentative/cross-origin.https.window.js
blob: a8cf93049fa05ced5c922f45890d3a3f680481aa (plain)
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
// META: script=/resources/testdriver.js
// META: script=/common/utils.js
// META: script=/common/subset-tests.js
// META: script=resources/fledge-util.sub.js
// META: timeout=long
// META: variant=?1-4
// META: variant=?5-8
// META: variant=?9-12
// META: variant=?13-last

"use strict;"

////////////////////////////////////////////////////////////////////////////////
// Join interest group in iframe tests.
////////////////////////////////////////////////////////////////////////////////

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);
  let iframe = await createIframe(test, document.location.origin);

  // Join a same-origin InterestGroup in a iframe navigated to its origin.
  await runInFrame(test, iframe, `await joinInterestGroup(test_instance, "${uuid}");`);

  // Run an auction using window.location.origin as a bidder. The IG should
  // make a bid and win an auction.
  await runBasicFledgeTestExpectingWinner(test, uuid);
}, 'Join interest group in same-origin iframe, default permissions.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);
  let iframe = await createIframe(test, OTHER_ORIGIN1);

  // Join a cross-origin InterestGroup in a iframe navigated to its origin.
  await runInFrame(test, iframe, `await joinInterestGroup(test_instance, "${uuid}");`);

  // Run an auction in this frame using the other origin as a bidder. The IG should
  // make a bid and win an auction.
  //
  // TODO: Once the permission defaults to not being able to join InterestGroups in
  // cross-origin iframes, this auction should have no winner.
  await runBasicFledgeTestExpectingWinner(
      test, uuid,
      { interestGroupBuyers: [OTHER_ORIGIN1],
        scoreAd: `if (browserSignals.interestGroupOwner !== "${OTHER_ORIGIN1}")
                    throw "Wrong owner: " + browserSignals.interestGroupOwner`
      });
}, 'Join interest group in cross-origin iframe, default permissions.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);
  let iframe = await createIframe(test, OTHER_ORIGIN1, 'join-ad-interest-group');

  // Join a cross-origin InterestGroup in a iframe navigated to its origin.
  await runInFrame(test, iframe, `await joinInterestGroup(test_instance, "${uuid}");`);

  // Run an auction in this frame using the other origin as a bidder. The IG should
  // make a bid and win an auction.
  await runBasicFledgeTestExpectingWinner(
      test, uuid,
      { interestGroupBuyers: [OTHER_ORIGIN1],
        scoreAd: `if (browserSignals.interestGroupOwner !== "${OTHER_ORIGIN1}")
                    throw "Wrong owner: " + browserSignals.interestGroupOwner`
      });
}, 'Join interest group in cross-origin iframe with join-ad-interest-group permission.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);
  let iframe = await createIframe(test, OTHER_ORIGIN1, "join-ad-interest-group 'none'");

  // Try to join an InterestGroup in a cross-origin iframe whose permissions policy
  // blocks joining interest groups. An exception should be thrown, and the interest
  // group should not be joined.
  await runInFrame(test, iframe,
                    `try {
                       await joinInterestGroup(test_instance, "${uuid}");
                     } catch (e) {
                       assert_true(e instanceof DOMException, "DOMException thrown");
                       assert_equals(e.name, "NotAllowedError", "NotAllowedError DOMException thrown");
                       return {result: "success"};
                     }
                     return "exception unexpectedly not thrown";`);

  // Run an auction in this frame using the other origin as a bidder. Since the join
  // should have failed, the auction should have no winner.
  await runBasicFledgeTestExpectingNoWinner(
      test, uuid,
      { interestGroupBuyers: [OTHER_ORIGIN1] });
}, 'Join interest group in cross-origin iframe with join-ad-interest-group permission denied.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);
  let iframe = await createIframe(test, OTHER_ORIGIN1, 'join-ad-interest-group');

  // Try to join an IG with the parent's origin as an owner in a cross-origin iframe.
  // This should require a .well-known fetch to the parents origin, which will not
  // grant permission. The case where permission is granted is not yet testable.
  let interestGroup = JSON.stringify(createInterestGroupForOrigin(uuid, window.location.origin));
  await runInFrame(test, iframe,
                   `try {
                      await joinInterestGroup(test_instance, "${uuid}", ${interestGroup});
                    } catch (e) {
                      assert_true(e instanceof DOMException, "DOMException thrown");
                      assert_equals(e.name, "NotAllowedError", "NotAllowedError DOMException thrown");
                      return {result: "success"};
                    }
                    return "exception unexpectedly not thrown";`);

  // Run an auction with this page's origin as a bidder. Since the join
  // should have failed, the auction should have no winner.
  await runBasicFledgeTestExpectingNoWinner(test, uuid);
}, "Join interest group owned by parent's origin in cross-origin iframe.");

////////////////////////////////////////////////////////////////////////////////
// Run auction in iframe tests.
////////////////////////////////////////////////////////////////////////////////

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);
  await joinInterestGroup(test, uuid);

  let iframe = await createIframe(test, document.location.origin);

  // Join a same-origin InterestGroup in a iframe navigated to its origin.
  await runInFrame(test, iframe, `await joinInterestGroup(test_instance, "${uuid}");`);

  // Run auction in same-origin iframe. This should succeed, by default.
  await runInFrame(
    test, iframe,
    `await runBasicFledgeTestExpectingWinner(test_instance, "${uuid}");`);
}, 'Run auction in same-origin iframe, default permissions.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);
  // Join an interest group owned by the the main frame's origin.
  await joinInterestGroup(test, uuid);

  let iframe = await createIframe(test, OTHER_ORIGIN1);

  // Run auction in cross-origin iframe. Currently, this is allowed by default.
  await runInFrame(
      test, iframe,
      `await runBasicFledgeTestExpectingWinner(
           test_instance, "${uuid}",
           {interestGroupBuyers: ["${window.location.origin}"]});`);
}, 'Run auction in cross-origin iframe, default permissions.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);
  // Join an interest group owned by the the main frame's origin.
  await joinInterestGroup(test, uuid);

  let iframe = await createIframe(test, OTHER_ORIGIN1, "run-ad-auction");

  // Run auction in cross-origin iframe that should allow the auction to occur.
  await runInFrame(
      test, iframe,
      `await runBasicFledgeTestExpectingWinner(
           test_instance, "${uuid}",
           {interestGroupBuyers: ["${window.location.origin}"]});`);
}, 'Run auction in cross-origin iframe with run-ad-auction permission.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);
  // No need to join any interest groups in this case - running an auction
  // should only throw an exception based on permissions policy, regardless
  // of whether there are any interest groups can participate.

  let iframe = await createIframe(test, OTHER_ORIGIN1, "run-ad-auction 'none'");

  // Run auction in cross-origin iframe that should not allow the auction to occur.
  await runInFrame(
      test, iframe,
      `try {
         await runBasicFledgeAuction(test_instance, "${uuid}");
       } catch (e) {
         assert_true(e instanceof DOMException, "DOMException thrown");
         assert_equals(e.name, "NotAllowedError", "NotAllowedError DOMException thrown");
         return {result: "success"};
       }
       throw "Attempting to run auction unexpectedly did not throw"`);
}, 'Run auction in cross-origin iframe with run-ad-auction permission denied.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);
  // Join an interest group owned by the the main frame's origin.
  await joinInterestGroup(test, uuid);

  let iframe = await createIframe(test, OTHER_ORIGIN1, `run-ad-auction ${OTHER_ORIGIN1}`);

  await runInFrame(
      test, iframe,
      `await runBasicFledgeTestExpectingWinner(
        test_instance, "${uuid}",
        { interestGroupBuyers: ["${window.location.origin}"],
          seller: "${OTHER_ORIGIN2}",
          decisionLogicURL: createDecisionScriptURL("${uuid}", {origin: "${OTHER_ORIGIN2}"})
        });`);
}, 'Run auction in cross-origin iframe with run-ad-auction for iframe origin, which is different from seller origin.');

////////////////////////////////////////////////////////////////////////////////
// Navigate fenced frame iframe tests.
////////////////////////////////////////////////////////////////////////////////

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  // Join an interest group and run an auction with a winner.
  await joinInterestGroup(test, uuid);
  let config = await runBasicFledgeTestExpectingWinner(test, uuid);

  // Try to navigate a fenced frame to the winning ad in a cross-origin iframe
  // with no fledge-related permissions.
  let iframe = await createIframe(
      test, OTHER_ORIGIN1, "join-ad-interest-group 'none'; run-ad-auction 'none'");
  await runInFrame(
      test, iframe,
      `await createAndNavigateFencedFrame(test_instance, param);`,
      /*param=*/config);
  await waitForObservedRequests(
      uuid, [createBidderReportURL(uuid), createSellerReportURL(uuid)]);
}, 'Run auction main frame, open winning ad in cross-origin iframe.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  let iframe = await createIframe(
      test, OTHER_ORIGIN1, "join-ad-interest-group; run-ad-auction");
  await runInFrame(
      test, iframe,
      `await joinInterestGroup(test_instance, "${uuid}");
       await runBasicFledgeAuctionAndNavigate(test_instance, "${uuid}");
       await waitForObservedRequests(
         "${uuid}", [createBidderReportURL("${uuid}"), createSellerReportURL("${uuid}")])`);
}, 'Run auction in cross-origin iframe and open winning ad in nested fenced frame.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  // Run an auction in an cross-origin iframe, and get the resulting FencedFrameConfig.
  let iframe = await createIframe(
      test, OTHER_ORIGIN1, "join-ad-interest-group; run-ad-auction");
  let config = await runInFrame(
      test, iframe,
      `await joinInterestGroup(test_instance, "${uuid}");
       let config = await runBasicFledgeTestExpectingWinner(test_instance, "${uuid}");
       return {result: "success", returnValue: config};`);
  assert_true(config != null, "Value not returned from auction in iframe");
  assert_true(config instanceof FencedFrameConfig,
    `Wrong value type returned from auction: ${config.constructor.type}`);

  // Loading the winning ad in a fenced frame that's a child of the main frame should
  // succeed.
  await createAndNavigateFencedFrame(test, config);
  await waitForObservedRequests(
      uuid,
      [ createBidderReportURL(uuid, '1', OTHER_ORIGIN1),
        createSellerReportURL(uuid, '1', OTHER_ORIGIN1)]);
}, 'Run auction in cross-origin iframe and open winning ad in a fenced frame child of the main frame.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  // Run an auction in an cross-origin iframe, and get the resulting FencedFrameConfig.
  let iframe = await createIframe(
      test, OTHER_ORIGIN1, "join-ad-interest-group; run-ad-auction");
  let config = await runInFrame(
      test, iframe,
      `await joinInterestGroup(test_instance, "${uuid}");
       let config = await runBasicFledgeTestExpectingWinner(test_instance, "${uuid}");
       return {result: "success", returnValue: config};`);
  assert_true(config != null, "Value not returned from auction in iframe");
  assert_true(config instanceof FencedFrameConfig,
    `Wrong value type returned from auction: ${config.constructor.type}`);

  // Try to navigate a fenced frame to the winning ad in a cross-origin iframe
  // with no fledge-related permissions. The iframe is a different origin from the
  // first cross-origin iframe.
  let iframe2 = await createIframe(
    test, OTHER_ORIGIN2, "join-ad-interest-group 'none'; run-ad-auction 'none'");
  await runInFrame(
      test, iframe2,
      `await createAndNavigateFencedFrame(test_instance, param);`,
      /*param=*/config);
  await waitForObservedRequests(
      uuid,
      [ createBidderReportURL(uuid, '1', OTHER_ORIGIN1),
        createSellerReportURL(uuid, '1', OTHER_ORIGIN1)]);
}, 'Run auction in cross-origin iframe and open winning ad in a fenced frame child of another cross-origin iframe.');

////////////////////////////////////////////////////////////////////////////////
// Other tests.
////////////////////////////////////////////////////////////////////////////////

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  let iframe = await createIframe(test, OTHER_ORIGIN1, "run-ad-auction");

  // Do everything in a cross-origin iframe, and make sure correct top-frame origin is used.
  await runInFrame(
      test, iframe,
      `const uuid = "${uuid}";
       const renderURL = createRenderURL(uuid, /*script=*/null, /*signalsParam=*/'hostname');

       await joinInterestGroup(
          test_instance, uuid,
          { trustedBiddingSignalsKeys: ['hostname'],
            trustedBiddingSignalsURL: TRUSTED_BIDDING_SIGNALS_URL,
            ads: [{ renderURL: renderURL }],
            biddingLogicURL: createBiddingScriptURL({
              generateBid:
                  \`if (browserSignals.topWindowHostname !== "${document.location.hostname}")
                      throw "Wrong topWindowHostname: " + browserSignals.topWindowHostname;
                    if (trustedBiddingSignals.hostname !== '${window.location.hostname}')
                      throw 'Wrong hostname: ' + trustedBiddingSignals.hostname;\`})});

       await runBasicFledgeTestExpectingWinner(
           test_instance, uuid,
           { trustedScoringSignalsURL: TRUSTED_SCORING_SIGNALS_URL,
            decisionLogicURL:
            createDecisionScriptURL(
              uuid,
              { scoreAd:
                    \`if (browserSignals.topWindowHostname !== "${document.location.hostname}")
                        throw "Wrong topWindowHostname: " + browserSignals.topWindowHostname;
                      if (trustedScoringSignals.renderURL["\${renderURL}"] !== '${window.location.hostname}')
                        throw 'Wrong hostname: ' + trustedScoringSignals.renderURL["\${renderURL}"];\` })});`);
}, 'Different top-frame origin.');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  let bidderOrigin = OTHER_ORIGIN1;
  let sellerOrigin = OTHER_ORIGIN2;
  let bidderSendReportToURL = createBidderReportURL(uuid, '1', OTHER_ORIGIN3);
  let sellerSendReportToURL = createSellerReportURL(uuid, '2', OTHER_ORIGIN4);
  let bidderBeaconURL = createBidderBeaconURL(uuid, '3', OTHER_ORIGIN5);
  let sellerBeaconURL = createSellerBeaconURL(uuid, '4', OTHER_ORIGIN6);
  let renderURL = createRenderURL(
      uuid,
      `window.fence.reportEvent({
         eventType: "beacon",
         eventData: window.location.href,
         destination: ["buyer", "seller"]
       })`,
       /*signalsParams=*/null, OTHER_ORIGIN7);

  let iframe = await createIframe(test, bidderOrigin, "join-ad-interest-group");
  let interestGroup = createInterestGroupForOrigin(
      uuid, bidderOrigin,
      {biddingLogicURL: createBiddingScriptURL(
        { origin: bidderOrigin,
          generateBid: `if (browserSignals.topWindowHostname !== "${document.location.hostname}")
                          throw "Wrong topWindowHostname: " + browserSignals.topWindowHostname;
                        if (interestGroup.owner !== "${bidderOrigin}")
                          throw "Wrong origin: " + interestGroup.owner;
                        if (!interestGroup.biddingLogicURL.startsWith("${bidderOrigin}"))
                          throw "Wrong origin: " + interestGroup.biddingLogicURL;
                        if (interestGroup.ads[0].renderURL != "${renderURL}")
                          throw "Wrong renderURL: " + interestGroup.ads[0].renderURL;
                        if (browserSignals.seller !== "${sellerOrigin}")
                          throw "Wrong origin: " + browserSignals.seller;`,
          reportWin: `if (browserSignals.topWindowHostname !== "${document.location.hostname}")
                        throw "Wrong topWindowHostname: " + browserSignals.topWindowHostname;
                      if (browserSignals.seller !== "${sellerOrigin}")
                        throw "Wrong seller: " + browserSignals.seller;
                      if (browserSignals.interestGroupOwner !== "${bidderOrigin}")
                        throw "Wrong interestGroupOwner: " + browserSignals.interestGroupOwner;
                      if (browserSignals.renderURL !== "${renderURL}")
                        throw "Wrong renderURL: " + browserSignals.renderURL;
                      if (browserSignals.seller !== "${sellerOrigin}")
                        throw "Wrong seller: " + browserSignals.seller;
                      sendReportTo("${bidderSendReportToURL}");
                      registerAdBeacon({beacon: "${bidderBeaconURL}"});` }),
       ads: [{ renderURL: renderURL }]});
  await runInFrame(
      test, iframe,
      `await joinInterestGroup(test_instance, "${uuid}", ${JSON.stringify(interestGroup)});`);

  await runBasicFledgeAuctionAndNavigate(test, uuid,
    { seller: sellerOrigin,
      interestGroupBuyers: [bidderOrigin],
      decisionLogicURL: createDecisionScriptURL(
        uuid,
        { origin: sellerOrigin,
          scoreAd: `if (browserSignals.topWindowHostname !== "${document.location.hostname}")
                      throw "Wrong topWindowHostname: " + browserSignals.topWindowHostname;
                    if (auctionConfig.seller !== "${sellerOrigin}")
                      throw "Wrong seller: " + auctionConfig.seller;
                    if (auctionConfig.interestGroupBuyers[0] !== "${bidderOrigin}")
                      throw "Wrong interestGroupBuyers: " + auctionConfig.interestGroupBuyers;
                    if (browserSignals.interestGroupOwner !== "${bidderOrigin}")
                      throw "Wrong interestGroupOwner: " + browserSignals.interestGroupOwner;
                    if (browserSignals.renderURL !== "${renderURL}")
                      throw "Wrong renderURL: " + browserSignals.renderURL;`,
          reportResult: `if (browserSignals.topWindowHostname !== "${document.location.hostname}")
                           throw "Wrong topWindowHostname: " + browserSignals.topWindowHostname;
                         if (browserSignals.interestGroupOwner !== "${bidderOrigin}")
                           throw "Wrong interestGroupOwner: " + browserSignals.interestGroupOwner;
                         if (browserSignals.renderURL !== "${renderURL}")
                           throw "Wrong renderURL: " + browserSignals.renderURL;
                         sendReportTo("${sellerSendReportToURL}");
                         registerAdBeacon({beacon: "${sellerBeaconURL}"});`})
     });

  await waitForObservedRequests(
      uuid,
      [ bidderSendReportToURL,
        sellerSendReportToURL,
        `${bidderBeaconURL}, body: ${renderURL}`,
        `${sellerBeaconURL}, body: ${renderURL}`
      ]);
}, 'Single seller auction with as many distinct origins as possible (except no component ads).');

subsetTest(promise_test, async test => {
  const uuid = generateUuid(test);

  // Join an interest group and run an auction with a winner. Use a tracking
  // URL for the ad, so that if it's incorrectly loaded in this test, the
  // waitForObservedRequests() at the end of the test will see it, and the
  // test will fail.
  await joinInterestGroup(
      test, uuid,
      {ads: [{renderURL: createTrackerURL(window.location.origin, uuid, 'track_get', 'renderURL')}]});
  let config = await runBasicFledgeTestExpectingWinner(test, uuid);

  // Try to navigate a fenced frame to the winning ad in a new same-origin
  // window. This should fail. Unfortunately, there's no assertion that
  // can be checked for, and can't communicate with the contents of the
  // fenced frame to make sure the load fails.
  //
  // So instead, join an interest group with a different sendReportTo-url,
  // overwriting the previously joined one, and run another auction, loading
  // the winner in another fenced frame.
  //
  // Then wait to see that only the reporting URLs from that second auction
  // are requested. They should almost always be requested after the URLs
  // from the first auction.
  let child_window =
      await createFrame(test, document.location.origin, /*is_iframe=*/false);
  await runInFrame(
      test, child_window,
      `await createAndNavigateFencedFrame(test_instance, param);
       await joinInterestGroup(
          test_instance, "${uuid}",
          {biddingLogicURL: createBiddingScriptURL(
              {reportWin: "sendReportTo('${createBidderReportURL(uuid, "2")}');" })});
       await runBasicFledgeAuctionAndNavigate(test_instance, "${uuid}");`,
      /*param=*/config);
  await waitForObservedRequests(
      uuid, [createBidderReportURL(uuid, "2"), createSellerReportURL(uuid)]);
}, 'Run auction in main frame, try to open winning ad in different same-origin main frame.');