summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_no_cookies_after_last_pb_exit.js
blob: 9a0b2fa4dc2397cdfddd58d5dc59a277caa42bf0 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"use strict";

do_get_profile();

// This test checks that active private-browsing HTTP channels, do not save
// cookies after the termination of the private-browsing session.

// This test consists in following steps:
// - starts a http server
// - no cookies at this point
// - does a beacon request in private-browsing mode
// - after the completion of the request, a cookie should be set (cookie cleanup)
// - does a beacon request in private-browsing mode and dispatch a
//   last-pb-context-exit notification
// - after the completion of the request, no cookies should be set

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");

let server;

function setupServer() {
  info("Starting the server...");

  function beaconHandler(metadata, response) {
    response.setHeader("Cache-Control", "max-age=10000", false);
    response.setStatusLine(metadata.httpVersion, 204, "No Content");
    response.setHeader("Set-Cookie", "a=b; path=/beacon; sameSite=lax", false);
    response.bodyOutputStream.write("", 0);
  }

  server = new HttpServer();
  server.registerPathHandler("/beacon", beaconHandler);
  server.start(-1);
  next();
}

function shutdownServer() {
  info("Terminating the server...");
  server.stop(next);
}

function sendRequest(notification) {
  info("Sending a request...");

  var privateLoadContext = Cu.createPrivateLoadContext();

  var path =
    "http://localhost:" +
    server.identity.primaryPort +
    "/beacon?" +
    Math.random();

  var uri = NetUtil.newURI(path);
  var securityFlags =
    Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL |
    Ci.nsILoadInfo.SEC_COOKIES_INCLUDE;
  var principal = Services.scriptSecurityManager.createContentPrincipal(uri, {
    privateBrowsingId: 1,
  });

  var chan = NetUtil.newChannel({
    uri,
    loadingPrincipal: principal,
    securityFlags,
    contentPolicyType: Ci.nsIContentPolicy.TYPE_BEACON,
  });

  chan.notificationCallbacks = Cu.createPrivateLoadContext();

  let loadGroup = Cc["@mozilla.org/network/load-group;1"].createInstance(
    Ci.nsILoadGroup
  );

  loadGroup.notificationCallbacks = Cu.createPrivateLoadContext();
  chan.loadGroup = loadGroup;

  chan.notificationCallbacks = privateLoadContext;
  var channelListener = new ChannelListener(next, null, CL_ALLOW_UNKNOWN_CL);

  if (notification) {
    info("Sending notification...");
    Services.obs.notifyObservers(null, "last-pb-context-exited");
  }

  chan.asyncOpen(channelListener);
}

function checkCookies(hasCookie) {
  let cm = Services.cookies;
  Assert.equal(
    cm.cookieExists("localhost", "/beacon", "a", { privateBrowsingId: 1 }),
    hasCookie
  );
  cm.removeAll();
  next();
}

const steps = [
  setupServer,

  // no cookie at startup
  () => checkCookies(false),

  // no last-pb-context-exit notification
  () => sendRequest(false),
  () => checkCookies(true),

  // last-pb-context-exit notification
  () => sendRequest(true),
  () => checkCookies(false),

  shutdownServer,
];

function next() {
  if (!steps.length) {
    do_test_finished();
    return;
  }

  steps.shift()();
}

function run_test() {
  // We don't want to have CookieJarSettings blocking this test.
  Services.prefs.setBoolPref(
    "network.cookieJarSettings.unblocked_for_testing",
    true
  );

  do_test_pending();
  next();
}