tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

requestStorageAccess.sub.https.window.js (4736B)


      1 // META: script=helpers.js
      2 // META: script=/cookies/resources/cookie-helper.sub.js
      3 // META: script=/resources/testdriver.js
      4 // META: script=/resources/testdriver-vendor.js
      5 'use strict';
      6 
      7 // Document-level test config flags:
      8 //
      9 // testPrefix: Prefix each test case with an indicator so we know what context
     10 // they are run in if they are used in multiple iframes.
     11 //
     12 // topLevelDocument: Keep track of if we run these tests in a nested context, we
     13 // don't want to recurse forever.
     14 const {testPrefix, topLevelDocument} = processQueryParams();
     15 
     16 const canUseAutogrant = topLevelDocument ||
     17          testPrefix.includes('same-site') ||
     18          testPrefix.includes('same-origin') ||
     19          testPrefix.includes('cross-origin') ||
     20          testPrefix.includes('ABA');
     21 
     22 const initialCookie = "initial-cookie=unpartitioned;Secure;SameSite=None;Path=/";
     23 
     24 if (!topLevelDocument) {
     25  // WPT synthesizes a top-level HTML test for this JS file, and in that case we
     26  // don't want to, or need to, call set_test_context.
     27  test_driver.set_test_context(window.top);
     28 }
     29 
     30 // Common tests to run in all frames.
     31 promise_test(async () => {
     32  assert_not_equals(document.requestStorageAccess, undefined);
     33 }, "[" + testPrefix + "] document.requestStorageAccess() should exist on the document interface");
     34 
     35 // Most tests need to start with the feature in "prompt" state.
     36 async function CommonSetup() {
     37  await SetFirstPartyCookie(location.origin, initialCookie);
     38  if (!canUseAutogrant) {
     39    await test_driver.set_permission({ name: 'storage-access' }, 'prompt');
     40  }
     41 }
     42 
     43 promise_test(
     44    async t => {
     45      await CommonSetup();
     46      if (canUseAutogrant) {
     47        await document.requestStorageAccess().catch(t.unreached_func(
     48            'document.requestStorageAccess() call should resolve in top-level frame or same-site iframe.'));
     49 
     50        assert_true(await CanAccessCookiesViaHTTP(), 'After obtaining storage access, subresource requests from the frame should send and set cookies.');
     51        assert_true(CanAccessCookiesViaJS(), 'After obtaining storage access, scripts in the frame should be able to access cookies.');
     52      } else {
     53        return promise_rejects_dom(
     54            t, "NotAllowedError", document.requestStorageAccess(),
     55            "document.requestStorageAccess() call without user gesture.");
     56      }
     57    },
     58    '[' + testPrefix +
     59        '] document.requestStorageAccess() should resolve in top-level frame or same-site iframe, otherwise reject with a NotAllowedError with no user gesture.');
     60 
     61 promise_test(
     62    async (t) => {
     63      await CommonSetup();
     64      await MaybeSetStorageAccess("*", "*", "blocked");
     65      await test_driver.set_permission({name: 'storage-access'}, 'granted');
     66      t.add_cleanup(async () => {
     67        await test_driver.delete_all_cookies();
     68      });
     69 
     70      await document.requestStorageAccess();
     71 
     72      assert_true(await CanAccessCookiesViaHTTP(), 'After obtaining storage access, subresource requests from the frame should send and set cookies.');
     73      assert_true(CanAccessCookiesViaJS(), 'After obtaining storage access, scripts in the frame should be able to access cookies.');
     74    },
     75    '[' + testPrefix +
     76        '] document.requestStorageAccess() should be resolved with no user gesture when a permission grant exists, and ' +
     77        'should allow cookie access');
     78 
     79 if (!canUseAutogrant) {
     80  promise_test(
     81      async t => {
     82        t.add_cleanup(async () => {
     83          await test_driver.set_permission({name: 'storage-access'}, 'prompt');
     84        });
     85        await SetFirstPartyCookie(location.origin, initialCookie);
     86        await test_driver.set_permission(
     87            {name: 'storage-access'}, 'denied');
     88 
     89        await RunCallbackWithGesture(() => {
     90          return promise_rejects_dom(t, "NotAllowedError", document.requestStorageAccess(),
     91            "document.requestStorageAccess() call with denied permission");
     92        });
     93      },
     94      '[' + testPrefix +
     95          '] document.requestStorageAccess() should be rejected with a NotAllowedError with denied permission');
     96 } else {
     97  promise_test(
     98      async t => {
     99        t.add_cleanup(async () => {
    100          await test_driver.set_permission({name: 'storage-access'}, 'prompt');
    101        });
    102        await SetFirstPartyCookie(location.origin, initialCookie);
    103        await document.requestStorageAccess();
    104 
    105        assert_true(await CanAccessCookiesViaHTTP(), 'After obtaining storage access, subresource requests from the frame should send and set cookies.');
    106        assert_true(CanAccessCookiesViaJS(), 'After obtaining storage access, scripts in the frame should be able to access cookies.');
    107      },
    108      `[${testPrefix}] document.requestStorageAccess() should resolve without permission grant or user gesture`);
    109 }