5 // This test checks that active private-browsing HTTP channels, do not save
6 // cookies after the termination of the private-browsing session.
8 // This test consists in following steps:
9 // - starts a http server
10 // - no cookies at this point
11 // - does a beacon request in private-browsing mode
12 // - after the completion of the request, a cookie should be set (cookie cleanup)
13 // - does a beacon request in private-browsing mode and dispatch a
14 // last-pb-context-exit notification
15 // - after the completion of the request, no cookies should be set
17 const { HttpServer } = ChromeUtils.importESModule(
18 "resource://testing-common/httpd.sys.mjs"
23 function setupServer() {
24 info("Starting the server...");
26 function beaconHandler(metadata, response) {
27 response.setHeader("Cache-Control", "max-age=10000", false);
28 response.setStatusLine(metadata.httpVersion, 204, "No Content");
29 response.setHeader("Set-Cookie", "a=b; path=/beacon; sameSite=lax", false);
30 response.bodyOutputStream.write("", 0);
33 server = new HttpServer();
34 server.registerPathHandler("/beacon", beaconHandler);
39 function shutdownServer() {
40 info("Terminating the server...");
44 function sendRequest(notification) {
45 info("Sending a request...");
47 var privateLoadContext = Cu.createPrivateLoadContext();
51 server.identity.primaryPort +
55 var uri = NetUtil.newURI(path);
57 Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL |
58 Ci.nsILoadInfo.SEC_COOKIES_INCLUDE;
59 var principal = Services.scriptSecurityManager.createContentPrincipal(uri, {
63 var chan = NetUtil.newChannel({
65 loadingPrincipal: principal,
67 contentPolicyType: Ci.nsIContentPolicy.TYPE_BEACON,
70 chan.notificationCallbacks = Cu.createPrivateLoadContext();
72 let loadGroup = Cc["@mozilla.org/network/load-group;1"].createInstance(
76 loadGroup.notificationCallbacks = Cu.createPrivateLoadContext();
77 chan.loadGroup = loadGroup;
79 chan.notificationCallbacks = privateLoadContext;
80 var channelListener = new ChannelListener(next, null, CL_ALLOW_UNKNOWN_CL);
83 info("Sending notification...");
84 Services.obs.notifyObservers(null, "last-pb-context-exited");
87 chan.asyncOpen(channelListener);
90 function checkCookies(hasCookie) {
91 let cm = Services.cookies;
93 cm.cookieExists("localhost", "/beacon", "a", { privateBrowsingId: 1 }),
103 // no cookie at startup
104 () => checkCookies(false),
106 // no last-pb-context-exit notification
107 () => sendRequest(false),
108 () => checkCookies(true),
110 // last-pb-context-exit notification
111 () => sendRequest(true),
112 () => checkCookies(false),
126 function run_test() {
127 // We don't want to have CookieJarSettings blocking this test.
128 Services.prefs.setBoolPref(
129 "network.cookieJarSettings.unblocked_for_testing",