summaryrefslogtreecommitdiffstats
path: root/netwerk/test/browser/browser_cookie_filtering_insecure.js
blob: 4f467736752d77cf940f41a25f987b66a3f7956a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */
"use strict";

// performing http and https testing within this file,
// and we do not want https-first to interfere with that test
Services.prefs.setBoolPref("dom.security.https_first", false);

registerCleanupFunction(function () {
  Services.prefs.clearUserPref("dom.security.https_first");
});

const {
  HTTPS_EXAMPLE_ORG,
  HTTPS_EXAMPLE_COM,
  HTTP_EXAMPLE_COM,
  browserTestPath,
  waitForAllExpectedTests,
  cleanupObservers,
  checkExpectedCookies,
  fetchHelper,
  preclean_test,
  cleanup_test,
} = ChromeUtils.importESModule(
  "resource://testing-common/cookie_filtering_helper.sys.mjs"
);

async function runSuiteWithContentListener(name, trigger_suite_func, expected) {
  return async function (browser) {
    info("Running content suite: " + name);
    await SpecialPowers.spawn(browser, [expected, name], checkExpectedCookies);
    await trigger_suite_func();
    await SpecialPowers.spawn(browser, [], waitForAllExpectedTests);
    await SpecialPowers.spawn(browser, [], cleanupObservers);
    info("Complete content suite: " + name);
  };
}

// TEST: In/Secure (insecure com)
// * secure example.com cookie do not go to insecure example.com process
// * insecure cookies go to insecure process
// * secure request with insecure cookie will go to insecure process
async function test_insecure_suite_insecure_com() {
  var expected = [];
  expected.push("test-cookie=png1");
  expected.push("test-cookie=png2");
  // insecure com will not recieve the secure com request with secure cookie
  expected.push(""); // insecure com will lose visibility of secure com cookie
  expected.push("test-cookie=png3");
  info(expected);

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: browserTestPath(HTTP_EXAMPLE_COM),
    },
    await runSuiteWithContentListener(
      "insecure suite insecure com",
      triggerInsecureSuite,
      expected
    )
  );
}

// TEST: In/Secure (secure com)
// * secure page will recieve all secure/insecure cookies
async function test_insecure_suite_secure_com() {
  var expected = [];
  expected.push("test-cookie=png1");
  expected.push("test-cookie=png2");
  expected.push("test-cookie=secure-png");
  expected.push("test-cookie=png3");
  info(expected);
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: browserTestPath(HTTPS_EXAMPLE_COM),
    },
    await runSuiteWithContentListener(
      "insecure suite secure com",
      triggerInsecureSuite,
      expected
    )
  );
}

async function triggerInsecureSuite() {
  const cookieSjsFilename = "cookie_filtering_resource.sjs";

  // insecure page, insecure cookie
  var url = browserTestPath(HTTP_EXAMPLE_COM) + cookieSjsFilename;
  await fetchHelper(url, "test-cookie=png1", false);

  // secure page req, insecure cookie
  url = browserTestPath(HTTPS_EXAMPLE_COM) + cookieSjsFilename;
  await fetchHelper(url, "test-cookie=png2", false);

  // secure page, secure cookie
  url = browserTestPath(HTTPS_EXAMPLE_COM) + cookieSjsFilename;
  await fetchHelper(url, "test-cookie=secure-png", true);

  // not testing insecure server returning secure cookie --

  // sentinel
  url = browserTestPath(HTTPS_EXAMPLE_COM) + cookieSjsFilename;
  await fetchHelper(url, "test-cookie=png3", false);
}

add_task(preclean_test);
add_task(test_insecure_suite_insecure_com); // 3
add_task(test_insecure_suite_secure_com); // 4
add_task(cleanup_test);