tor-browser

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

test_baseDomain_publicsuffix.js (2867B)


      1 "use strict";
      2 
      3 add_task(async () => {
      4  const HOST = "www.bbc.co.uk";
      5  Assert.equal(
      6    Services.eTLD.getBaseDomainFromHost(HOST),
      7    "bbc.co.uk",
      8    "Sanity check: HOST is an eTLD + 1 with subdomain"
      9  );
     10 
     11  const tests = [
     12    {
     13      // Correct baseDomain: eTLD + 1.
     14      baseDomain: "bbc.co.uk",
     15      name: "originally_bbc_co_uk",
     16    },
     17    {
     18      // Incorrect baseDomain: Part of public suffix list.
     19      baseDomain: "uk",
     20      name: "originally_uk",
     21    },
     22    {
     23      // Incorrect baseDomain: Part of public suffix list.
     24      baseDomain: "co.uk",
     25      name: "originally_co_uk",
     26    },
     27    {
     28      // Incorrect baseDomain: eTLD + 2.
     29      baseDomain: "www.bbc.co.uk",
     30      name: "originally_www_bbc_co_uk",
     31    },
     32  ];
     33 
     34  do_get_profile();
     35 
     36  let dbFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
     37  dbFile.append("cookies.sqlite");
     38  let conn = Services.storage.openDatabase(dbFile);
     39 
     40  conn.schemaVersion = 10;
     41  conn.executeSimpleSQL("DROP TABLE IF EXISTS moz_cookies");
     42  conn.executeSimpleSQL(
     43    "CREATE TABLE moz_cookies (" +
     44      "id INTEGER PRIMARY KEY, " +
     45      "baseDomain TEXT, " +
     46      "originAttributes TEXT NOT NULL DEFAULT '', " +
     47      "name TEXT, " +
     48      "value TEXT, " +
     49      "host TEXT, " +
     50      "path TEXT, " +
     51      "expiry INTEGER, " +
     52      "lastAccessed INTEGER, " +
     53      "creationTime INTEGER, " +
     54      "isSecure INTEGER, " +
     55      "isHttpOnly INTEGER, " +
     56      "inBrowserElement INTEGER DEFAULT 0, " +
     57      "sameSite INTEGER DEFAULT 0, " +
     58      "rawSameSite INTEGER DEFAULT 0, " +
     59      "CONSTRAINT moz_uniqueid UNIQUE (name, host, path, originAttributes)" +
     60      ")"
     61  );
     62 
     63  function addCookie(baseDomain, host, name) {
     64    conn.executeSimpleSQL(
     65      "INSERT INTO moz_cookies(" +
     66        "baseDomain, host, name, value, path, expiry, " +
     67        "lastAccessed, creationTime, isSecure, isHttpOnly) VALUES (" +
     68        `'${baseDomain}', '${host}', '${name}', 'thevalue', '/', ` +
     69        (Date.now() + 3600000) +
     70        "," +
     71        Date.now() +
     72        "," +
     73        Date.now() +
     74        ", 1, 1)"
     75    );
     76  }
     77 
     78  // Prepare the database.
     79  for (let { baseDomain, name } of tests) {
     80    addCookie(baseDomain, HOST, name);
     81  }
     82  // Domain cookies are not supported for IP addresses.
     83  addCookie("127.0.0.1", ".127.0.0.1", "invalid_host");
     84  conn.close();
     85 
     86  let cs = Services.cookies;
     87 
     88  // Count excludes the invalid_host cookie.
     89  Assert.equal(cs.cookies.length, tests.length, "Expected number of cookies");
     90 
     91  // Check whether the database has the expected value,
     92  // despite the incorrect baseDomain.
     93  for (let { name } of tests) {
     94    Assert.ok(
     95      cs.cookieExists(HOST, "/", name, {}),
     96      "Should find cookie with name: " + name
     97    );
     98  }
     99 
    100  Assert.equal(
    101    cs.cookieExists("127.0.0.1", "/", "invalid_host", {}),
    102    false,
    103    "Should ignore database row with invalid host name"
    104  );
    105 });