tor-browser

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

test_cookies_privatebrowsing.js (5329B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test private browsing mode.
      5 
      6 "use strict";
      7 
      8 function make_channel(url) {
      9  return NetUtil.newChannel({
     10    uri: url,
     11    loadUsingSystemPrincipal: true,
     12  }).QueryInterface(Ci.nsIHttpChannel);
     13 }
     14 
     15 function getCookieStringFromPrivateDocument(uriSpec) {
     16  return CookieXPCShellUtils.getCookieStringFromDocument(uriSpec, {
     17    privateBrowsing: true,
     18  });
     19 }
     20 
     21 add_task(async () => {
     22  // Set up a profile.
     23  do_get_profile();
     24 
     25  // We don't want to have CookieJarSettings blocking this test.
     26  Services.prefs.setBoolPref(
     27    "network.cookieJarSettings.unblocked_for_testing",
     28    true
     29  );
     30 
     31  // Test with cookies enabled.
     32  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
     33  Services.prefs.setBoolPref("dom.security.https_first", false);
     34 
     35  // Test with https-first-mode disabled in PBM
     36  Services.prefs.setBoolPref("dom.security.https_first_pbm", false);
     37 
     38  CookieXPCShellUtils.createServer({ hosts: ["foo.com", "bar.com"] });
     39 
     40  // We need to keep a private-browsing window active, otherwise the
     41  // 'last-pb-context-exited' notification will be dispatched.
     42  const privateBrowsingHolder = await CookieXPCShellUtils.loadContentPage(
     43    "http://bar.com/",
     44    { privateBrowsing: true }
     45  );
     46 
     47  // Create URIs pointing to foo.com and bar.com.
     48  let uri1 = NetUtil.newURI("http://foo.com/foo.html");
     49  let uri2 = NetUtil.newURI("http://bar.com/bar.html");
     50 
     51  // Set a cookie for host 1.
     52  Services.cookies.setCookieStringFromHttp(
     53    uri1,
     54    "oh=hai; max-age=1000",
     55    make_channel(uri1.spec)
     56  );
     57  Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 1);
     58 
     59  // Enter private browsing mode, set a cookie for host 2, and check the counts.
     60  var chan1 = make_channel(uri1.spec);
     61  chan1.QueryInterface(Ci.nsIPrivateBrowsingChannel);
     62  chan1.setPrivate(true);
     63 
     64  var chan2 = make_channel(uri2.spec);
     65  chan2.QueryInterface(Ci.nsIPrivateBrowsingChannel);
     66  chan2.setPrivate(true);
     67 
     68  Services.cookies.setCookieStringFromHttp(uri2, "oh=hai; max-age=1000", chan2);
     69  Assert.equal(await getCookieStringFromPrivateDocument(uri1.spec), "");
     70  Assert.equal(await getCookieStringFromPrivateDocument(uri2.spec), "oh=hai");
     71 
     72  // Remove cookies and check counts.
     73  Services.obs.notifyObservers(null, "last-pb-context-exited");
     74  Assert.equal(await getCookieStringFromPrivateDocument(uri1.spec), "");
     75  Assert.equal(await getCookieStringFromPrivateDocument(uri2.spec), "");
     76 
     77  Services.cookies.setCookieStringFromHttp(uri2, "oh=hai; max-age=1000", chan2);
     78  Assert.equal(await getCookieStringFromPrivateDocument(uri2.spec), "oh=hai");
     79 
     80  // Leave private browsing mode and check counts.
     81  Services.obs.notifyObservers(null, "last-pb-context-exited");
     82  Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 1);
     83  Assert.equal(Services.cookies.countCookiesFromHost(uri2.host), 0);
     84 
     85  // Fake a profile change.
     86  await promise_close_profile();
     87  do_load_profile();
     88 
     89  // Check that the right cookie persisted.
     90  Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 1);
     91  Assert.equal(Services.cookies.countCookiesFromHost(uri2.host), 0);
     92 
     93  // Enter private browsing mode, set a cookie for host 2, and check the counts.
     94  Assert.equal(await getCookieStringFromPrivateDocument(uri1.spec), "");
     95  Assert.equal(await getCookieStringFromPrivateDocument(uri2.spec), "");
     96  Services.cookies.setCookieStringFromHttp(uri2, "oh=hai; max-age=1000", chan2);
     97  Assert.equal(await getCookieStringFromPrivateDocument(uri2.spec), "oh=hai");
     98 
     99  // on android fission the privateBrowsingHolder prevents
    100  // the cookies on the content process from being updated
    101  // Let's release the last PB window.
    102  await privateBrowsingHolder.close();
    103 
    104  // Fake a profile change.
    105  await promise_close_profile();
    106  do_load_profile();
    107 
    108  // keep the private browsing window open again
    109  const privateBrowsingHolder2 = await CookieXPCShellUtils.loadContentPage(
    110    "http://bar.com/",
    111    { privateBrowsing: true }
    112  );
    113 
    114  // We're still in private browsing mode, but should have a new session.
    115  // Check counts.
    116  Assert.equal(await getCookieStringFromPrivateDocument(uri1.spec), "");
    117  Assert.equal(await getCookieStringFromPrivateDocument(uri2.spec), "");
    118 
    119  // Leave private browsing mode and check counts.
    120  Services.obs.notifyObservers(null, "last-pb-context-exited");
    121  Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 1);
    122  Assert.equal(Services.cookies.countCookiesFromHost(uri2.host), 0);
    123 
    124  // Enter private browsing mode.
    125 
    126  // Fake a profile change, but wait for async read completion.
    127  await promise_close_profile();
    128  await promise_load_profile();
    129 
    130  // We're still in private browsing mode, but should have a new session.
    131  // Check counts.
    132  Assert.equal(await getCookieStringFromPrivateDocument(uri1.spec), "");
    133  Assert.equal(await getCookieStringFromPrivateDocument(uri2.spec), "");
    134 
    135  // Leave private browsing mode and check counts.
    136  Services.obs.notifyObservers(null, "last-pb-context-exited");
    137  Assert.equal(Services.cookies.countCookiesFromHost(uri1.host), 1);
    138  Assert.equal(Services.cookies.countCookiesFromHost(uri2.host), 0);
    139 
    140  // Let's release the last PB window.
    141  await privateBrowsingHolder2.close();
    142  Services.prefs.clearUserPref("dom.security.https_first");
    143 });