tor-browser

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

browser_navigation.js (5666B)


      1 "use strict";
      2 
      3 const REQUEST_URL =
      4  "https://example.com/browser/dom/security/test/sec-fetch/file_no_cache.sjs";
      5 
      6 let gTestCounter = 0;
      7 let gExpectedHeader = {};
      8 
      9 async function setup() {
     10  waitForExplicitFinish();
     11 }
     12 
     13 function checkSecFetchUser(subject) {
     14  let channel = subject.QueryInterface(Ci.nsIHttpChannel);
     15  if (!channel.URI.spec.startsWith("https://example.com/")) {
     16    return;
     17  }
     18 
     19  info(`testing headers for load of ${channel.URI.spec}`);
     20 
     21  const secFetchHeaders = [
     22    "sec-fetch-mode",
     23    "sec-fetch-dest",
     24    "sec-fetch-user",
     25    "sec-fetch-site",
     26  ];
     27 
     28  secFetchHeaders.forEach(header => {
     29    const expectedValue = gExpectedHeader[header];
     30    try {
     31      is(
     32        channel.getRequestHeader(header),
     33        expectedValue,
     34        `${header} is set to ${expectedValue}`
     35      );
     36    } catch (e) {
     37      if (expectedValue) {
     38        ok(false, "required headers are set");
     39      } else {
     40        ok(true, `${header} should not be set`);
     41      }
     42    }
     43  });
     44 
     45  gTestCounter++;
     46 }
     47 
     48 async function testNavigations() {
     49  gTestCounter = 0;
     50 
     51  // Load initial site
     52  let loaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
     53  BrowserTestUtils.startLoadingURIString(gBrowser, REQUEST_URL + "?test1");
     54  await loaded;
     55 
     56  // Load another site
     57  loaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
     58  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
     59    content.document.notifyUserGestureActivation(); // simulate user activation
     60    let test2Button = content.document.getElementById("test2_button");
     61    test2Button.click();
     62    content.document.clearUserGestureActivation();
     63  });
     64  await loaded;
     65  // Load another site
     66  loaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
     67  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
     68    content.document.notifyUserGestureActivation(); // simulate user activation
     69    let test3Button = content.document.getElementById("test3_button");
     70    test3Button.click();
     71    content.document.clearUserGestureActivation();
     72  });
     73  await loaded;
     74 
     75  gExpectedHeader = {
     76    "sec-fetch-mode": "navigate",
     77    "sec-fetch-dest": "document",
     78    "sec-fetch-site": "same-origin",
     79    "sec-fetch-user": "?1",
     80  };
     81 
     82  // Register the http request observer.
     83  // All following actions should cause requests with the sec-fetch-user header
     84  // set.
     85  Services.obs.addObserver(checkSecFetchUser, "http-on-stop-request");
     86 
     87  // Go back one site by clicking the back button
     88  info("Clicking back button");
     89  loaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
     90  document.notifyUserGestureActivation(); // simulate user activation
     91  let backButton = document.getElementById("back-button");
     92  backButton.click();
     93  document.clearUserGestureActivation();
     94  await loaded;
     95 
     96  // Reload the site by clicking the reload button
     97  info("Clicking reload button");
     98  loaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
     99  document.notifyUserGestureActivation(); // simulate user activation
    100  let reloadButton = document.getElementById("reload-button");
    101  await TestUtils.waitForCondition(() => {
    102    return !reloadButton.disabled;
    103  });
    104  reloadButton.click();
    105  document.clearUserGestureActivation();
    106  await loaded;
    107 
    108  // Go forward one site by clicking the forward button
    109  info("Clicking forward button");
    110  loaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
    111  document.notifyUserGestureActivation(); // simulate user activation
    112  let forwardButton = document.getElementById("forward-button");
    113  forwardButton.click();
    114  document.clearUserGestureActivation();
    115  await loaded;
    116 
    117  // Testing history.back/forward...
    118 
    119  info("going back with history.back");
    120  loaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
    121  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    122    content.document.notifyUserGestureActivation(); // simulate user activation
    123    content.history.back();
    124    content.document.clearUserGestureActivation();
    125  });
    126  await loaded;
    127 
    128  info("going forward with history.forward");
    129  loaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
    130  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    131    content.document.notifyUserGestureActivation(); // simulate user activation
    132    content.history.forward();
    133    content.document.clearUserGestureActivation();
    134  });
    135  await loaded;
    136 
    137  gExpectedHeader = {
    138    "sec-fetch-mode": "navigate",
    139    "sec-fetch-dest": "document",
    140    "sec-fetch-site": "same-origin",
    141  };
    142 
    143  info("going back with history.back without user activation");
    144  loaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
    145  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    146    content.history.back();
    147  });
    148  await loaded;
    149 
    150  info("going forward with history.forward without user activation");
    151  loaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
    152  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    153    content.history.forward();
    154  });
    155  await loaded;
    156 
    157  Assert.strictEqual(
    158    gTestCounter,
    159    7,
    160    "testing that all five actions have been tested."
    161  );
    162 
    163  Services.obs.removeObserver(checkSecFetchUser, "http-on-stop-request");
    164 }
    165 
    166 add_task(async function () {
    167  waitForExplicitFinish();
    168 
    169  await testNavigations();
    170 
    171  // If fission is enabled we also want to test the navigations with the bfcache
    172  // in the parent.
    173  if (SpecialPowers.getBoolPref("fission.autostart")) {
    174    await SpecialPowers.pushPrefEnv({
    175      set: [["fission.bfcacheInParent", true]],
    176    });
    177 
    178    await testNavigations();
    179  }
    180 
    181  finish();
    182 });