tor-browser

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

browser_persist_cookies.js (4082B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const TEST_PATH = getRootDirectory(gTestPath).replace(
      7  "chrome://mochitests/content",
      8  "https://example.org"
      9 );
     10 const TEST_PATH2 = getRootDirectory(gTestPath).replace(
     11  "chrome://mochitests/content",
     12  "https://example.com"
     13 );
     14 
     15 var MockFilePicker = SpecialPowers.MockFilePicker;
     16 MockFilePicker.init(window.browsingContext);
     17 
     18 registerCleanupFunction(async function () {
     19  info("Running the cleanup code");
     20  MockFilePicker.cleanup();
     21  Services.obs.removeObserver(checkRequest, "http-on-modify-request");
     22  SpecialPowers.clearUserPref("network.cookie.sameSite.laxByDefault");
     23  if (gTestDir && gTestDir.exists()) {
     24    // On Windows, sometimes nsIFile.remove() throws, probably because we're
     25    // still writing to the directory we're trying to remove, despite
     26    // waiting for the download to complete. Just retry a bit later...
     27    let succeeded = false;
     28    while (!succeeded) {
     29      try {
     30        gTestDir.remove(true);
     31        succeeded = true;
     32      } catch (ex) {
     33        await new Promise(requestAnimationFrame);
     34      }
     35    }
     36  }
     37 });
     38 
     39 let gTestDir = null;
     40 
     41 function checkRequest(subject) {
     42  let httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
     43  let spec = httpChannel.URI.spec;
     44  // Ignore initial requests for page that sets cookies and its favicon, which may not have
     45  // cookies.
     46  if (
     47    httpChannel.URI.host == "example.org" &&
     48    !spec.endsWith("favicon.ico") &&
     49    !spec.includes("redirect.sjs")
     50  ) {
     51    let cookie = httpChannel.getRequestHeader("cookie");
     52    is(
     53      cookie.trim(),
     54      "normalCookie=true",
     55      "Should have correct cookie in request for " + spec
     56    );
     57  }
     58 }
     59 
     60 function createTemporarySaveDirectory() {
     61  var saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
     62  saveDir.append("testsavedir");
     63  if (!saveDir.exists()) {
     64    info("create testsavedir!");
     65    saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
     66  }
     67  info("return from createTempSaveDir: " + saveDir.path);
     68  return saveDir;
     69 }
     70 
     71 add_task(async function () {
     72  // Use nsICookieService.BEHAVIOR_REJECT_TRACKER to avoid cookie partitioning.
     73  // In this test case, if the cookie is partitioned, there will be no cookie
     74  // nsICookieServicebeing sent to compare.
     75  await SpecialPowers.pushPrefEnv({
     76    set: [
     77      ["network.cookie.cookieBehavior", 4],
     78      // Bug 1617611: Fix all the tests broken by "cookies SameSite=lax by default"
     79      ["network.cookie.sameSite.laxByDefault", false],
     80    ],
     81  });
     82 
     83  await BrowserTestUtils.withNewTab("about:blank", async function (browser) {
     84    Services.obs.addObserver(checkRequest, "http-on-modify-request");
     85    BrowserTestUtils.startLoadingURIString(
     86      browser,
     87      TEST_PATH + "set-samesite-cookies-and-redirect.sjs"
     88    );
     89    // Test that the original document load doesn't send same-site cookies.
     90    await BrowserTestUtils.browserLoaded(
     91      browser,
     92      true,
     93      TEST_PATH2 + "set-samesite-cookies-and-redirect.sjs"
     94    );
     95    // Now check the saved page.
     96    // Create the folder the link will be saved into.
     97    gTestDir = createTemporarySaveDirectory();
     98    let destFile = gTestDir.clone();
     99 
    100    MockFilePicker.displayDirectory = gTestDir;
    101    let fileName;
    102    MockFilePicker.showCallback = function (fp) {
    103      info("showCallback");
    104      fileName = fp.defaultString;
    105      info("fileName: " + fileName);
    106      destFile.append(fileName);
    107      info("path: " + destFile.path);
    108      MockFilePicker.setFiles([destFile]);
    109      MockFilePicker.filterIndex = 0; // kSaveAsType_Complete
    110      info("done showCallback");
    111    };
    112    saveBrowser(browser);
    113    let dls = await Downloads.getList(Downloads.PUBLIC);
    114    await new Promise((resolve, reject) => {
    115      dls.addView({
    116        onDownloadChanged(download) {
    117          if (download.succeeded) {
    118            dls.removeView(this);
    119            dls.removeFinished();
    120            resolve();
    121          } else if (download.error) {
    122            reject("Download failed");
    123          }
    124        },
    125      });
    126    });
    127  });
    128 });