tor-browser

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

test_cookies_partition_migration.js (5750B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 add_task(async function test_chips_migration() {
      7  // Set up a profile.
      8  let profile = do_get_profile();
      9 
     10  // Start the cookieservice, to force creation of a database.
     11  Services.cookies.sessionCookies;
     12 
     13  // Close the profile.
     14  await promise_close_profile();
     15 
     16  // Remove the cookie file in order to create another database file.
     17  do_get_cookie_file(profile).remove(false);
     18 
     19  // Create a schema 14 database.
     20  let database = new CookieDatabaseConnection(do_get_cookie_file(profile), 14);
     21 
     22  let now = Date.now() * 1000;
     23  let expiry = Math.round(now / 1e6 + 1000);
     24 
     25  // Populate db with a first-party unpartitioned cookies
     26  let cookie = new Cookie(
     27    "test",
     28    "Some data",
     29    "example.com",
     30    "/",
     31    expiry,
     32    now,
     33    now,
     34    false,
     35    false,
     36    false,
     37    false,
     38    {},
     39    Ci.nsICookie.SAMESITE_UNSET,
     40    Ci.nsICookie.SCHEME_UNSET,
     41    false // isPartitioned
     42  );
     43  database.insertCookie(cookie);
     44 
     45  // Populate db with a first-party unpartitioned cookies with the partitioned attribute
     46  cookie = new Cookie(
     47    "test partitioned",
     48    "Some data",
     49    "example.com",
     50    "/",
     51    expiry,
     52    now,
     53    now,
     54    false,
     55    false,
     56    false,
     57    false,
     58    {},
     59    Ci.nsICookie.SAMESITE_UNSET,
     60    Ci.nsICookie.SCHEME_UNSET,
     61    true // isPartitioned
     62  );
     63  database.insertCookie(cookie);
     64 
     65  // Populate db with a first-party unpartitioned cookies with the partitioned attribute
     66  cookie = new Cookie(
     67    "test overwrite",
     68    "Overwritten",
     69    "example.com",
     70    "/",
     71    expiry,
     72    now,
     73    now,
     74    false,
     75    false,
     76    false,
     77    false,
     78    {},
     79    Ci.nsICookie.SAMESITE_UNSET,
     80    Ci.nsICookie.SCHEME_UNSET,
     81    true // isPartitioned
     82  );
     83  database.insertCookie(cookie);
     84 
     85  // Populate db with a first-party unpartitioned cookies with the partitioned attribute
     86  cookie = new Cookie(
     87    "test overwrite",
     88    "Did not overwrite",
     89    "example.com",
     90    "/",
     91    expiry,
     92    now,
     93    now,
     94    false,
     95    false,
     96    false,
     97    false,
     98    { partitionKey: "(https,example.com)" },
     99    Ci.nsICookie.SAMESITE_UNSET,
    100    Ci.nsICookie.SCHEME_UNSET,
    101    true // isPartitioned
    102  );
    103  database.insertCookie(cookie);
    104 
    105  database.close();
    106  database = null;
    107 
    108  registerCleanupFunction(() => {
    109    Services.prefs.clearUserPref("network.cookie.CHIPS.enabled");
    110    Services.prefs.clearUserPref("network.cookie.CHIPS.migrateDatabase");
    111  });
    112 
    113  // Reload profile.
    114  Services.prefs.setBoolPref("network.cookie.CHIPS.enabled", true);
    115  Services.prefs.setIntPref("network.cookie.CHIPS.lastMigrateDatabase", 0);
    116  Services.prefs.setIntPref("network.cookie.CHIPS.migrateDatabaseTarget", 0);
    117  await promise_load_profile();
    118 
    119  // Make sure there were no changes
    120  Assert.equal(
    121    Services.cookies.getCookiesFromHost("example.com", {}).length,
    122    3
    123  );
    124  Assert.equal(
    125    Services.cookies
    126      .getCookiesFromHost("example.com", {})
    127      .filter(cookie => cookie.name == "test").length,
    128    1
    129  );
    130  Assert.equal(
    131    Services.cookies
    132      .getCookiesFromHost("example.com", {})
    133      .filter(cookie => cookie.name == "test partitioned").length,
    134    1
    135  );
    136  Assert.equal(
    137    Services.cookies
    138      .getCookiesFromHost("example.com", {})
    139      .filter(cookie => cookie.name == "test overwrite").length,
    140    1
    141  );
    142  Assert.equal(
    143    Services.cookies.getCookiesFromHost("example.com", {
    144      partitionKey: "(https,example.com)",
    145    }).length,
    146    1
    147  );
    148  Assert.equal(
    149    Services.cookies
    150      .getCookiesFromHost("example.com", {})
    151      .filter(cookie => cookie.name == "test overwrite").length,
    152    1
    153  );
    154 
    155  // Close the profile.
    156  await promise_close_profile();
    157 
    158  // Reload profile.
    159  await Services.prefs.setBoolPref("network.cookie.CHIPS.enabled", true);
    160  await Services.prefs.setIntPref(
    161    "network.cookie.CHIPS.migrateDatabaseTarget",
    162    1000
    163  );
    164  await promise_load_profile();
    165 
    166  // Check if the first-party unpartitioned cookie is still there
    167  Assert.equal(
    168    Services.cookies
    169      .getCookiesFromHost("example.com", {})
    170      .filter(cookie => cookie.name == "test").length,
    171    1
    172  );
    173 
    174  // Check that we no longer have Partitioned cookies in the unpartitioned storage
    175  Assert.equal(
    176    Services.cookies.getCookiesFromHost("example.com", {}).length,
    177    1
    178  );
    179 
    180  // Check that we only have our two partitioned cookies
    181  Assert.equal(
    182    Services.cookies.getCookiesFromHost("example.com", {
    183      partitionKey: "(https,example.com)",
    184    }).length,
    185    2
    186  );
    187  Assert.equal(
    188    Services.cookies
    189      .getCookiesFromHost("example.com", {
    190        partitionKey: "(https,example.com)",
    191      })
    192      .filter(cookie => cookie.name == "test").length,
    193    0
    194  );
    195  Assert.equal(
    196    Services.cookies
    197      .getCookiesFromHost("example.com", {
    198        partitionKey: "(https,example.com)",
    199      })
    200      .filter(cookie => cookie.name == "test partitioned").length,
    201    1
    202  );
    203  Assert.equal(
    204    Services.cookies
    205      .getCookiesFromHost("example.com", {
    206        partitionKey: "(https,example.com)",
    207      })
    208      .filter(cookie => cookie.name == "test overwrite").length,
    209    1
    210  );
    211 
    212  // Test that we overwrote the value of the cookie in the partition with the
    213  // value that was not partitioned
    214  Assert.equal(
    215    Services.cookies
    216      .getCookiesFromHost("example.com", {
    217        partitionKey: "(https,example.com)",
    218      })
    219      .filter(cookie => cookie.name == "test overwrite")[0].value,
    220    "Overwritten"
    221  );
    222 
    223  // Make sure we cleared the migration pref as part of the migration
    224  Assert.equal(
    225    Services.prefs.getIntPref("network.cookie.CHIPS.lastMigrateDatabase"),
    226    1000
    227  );
    228 
    229  // Cleanup
    230  Services.cookies.removeAll();
    231  do_close_profile();
    232 });