tor-browser

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

refresh-by-host.js (3083B)


      1 const kOriginTypeDescriptions = {
      2  true: "same-origin",
      3  false: "cross-origin",
      4 }
      5 
      6 const kRefreshOptionsByDescription = {
      7  "meta refresh": "resources/refresh-policy.sub.html",
      8  "header refresh": "resources/refresh-policy.py",
      9 };
     10 
     11 const kExpectEmptyString = "the empty string";
     12 const kExpectOrigin = "origin";
     13 const kExpectFullURL = "full url";
     14 
     15 function referrerPolicyExpectationValue(aExpected, aFrame) {
     16  let expectedReferrer;
     17  switch (aExpected) {
     18    case kExpectEmptyString:
     19      expectedReferrer = "";
     20      break;
     21    case kExpectOrigin:
     22      expectedReferrer = new URL(aFrame.src).origin + "/";
     23      break;
     24    case kExpectFullURL:
     25      expectedReferrer = aFrame.src;
     26      break;
     27    default:
     28      throw new Error(`unexpected referrer type ${aExpected}`);
     29  }
     30  return expectedReferrer;
     31 }
     32 
     33 function refreshWithPoliciesTest(aExpectedURL, aExpectationsByPolicy) {
     34  const isSameOrigin = location.origin === new URL(aExpectedURL).origin;
     35  Object.entries(aExpectationsByPolicy).forEach(([policy, expected]) =>
     36    Object.entries(kRefreshOptionsByDescription).forEach(([description, refreshFrom]) =>
     37      promise_test(async t => {
     38        const originalPath = refreshFrom + "?" + new URLSearchParams({url: aExpectedURL, policy});
     39        let expectedReferrer = location.href;
     40        let loadCount = 0;
     41        const { promise: frameLoaded, resolve: resolveFrameLoaded } = Promise.withResolvers();
     42        const { promise: messageHandled, resolve: resolveMessageHandled } = Promise.withResolvers();
     43 
     44        const frame = document.createElement("iframe");
     45        try {
     46          frame.addEventListener("load", t.step_func(() => {
     47            loadCount++;
     48            try {
     49              if (loadCount === 1) {
     50                assert_equals(frame.contentWindow.location.href, new URL(originalPath, location).href, "original page loads");
     51                assert_equals(frame.contentDocument.referrer, expectedReferrer, "referrer is parent frame");
     52 
     53                expectedReferrer = referrerPolicyExpectationValue(expected, frame);
     54              }
     55            } finally {
     56              if (loadCount === 1) {
     57                resolveFrameLoaded();
     58              }
     59            }
     60          }));
     61 
     62          addEventListener("message", t.step_func(msg => {
     63            const {referrer, documentReferrer, url} = msg.data;
     64            try {
     65              assert_equals(url, aExpectedURL, "refresh page has expected URL");
     66              assert_equals(referrer, expectedReferrer, "referrer header is previous page");
     67              assert_equals(documentReferrer, expectedReferrer, "document referrer is previous page");
     68            } finally {
     69              resolveMessageHandled();
     70            }
     71          }));
     72 
     73          frame.src = originalPath;
     74          document.body.appendChild(frame);
     75 
     76          await frameLoaded;
     77          await messageHandled;
     78        } finally {
     79          frame.remove();
     80          t.done();
     81        }
     82      }, `${kOriginTypeDescriptions[isSameOrigin]} ${description} with referrer policy "${policy}" refreshes with ${expected} as referrer`)))
     83 }