1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* import-globals-from head_cache.js */
8 /* import-globals-from head_cookies.js */
9 /* import-globals-from head_channels.js */
10 /* import-globals-from head_servers.js */
12 function makeChan(uri) {
14 Services.scriptSecurityManager.createContentPrincipalFromOrigin(
17 let chan = NetUtil.newChannel({
19 loadingPrincipal: principal,
20 securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT,
21 contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER,
22 }).QueryInterface(Ci.nsIHttpChannel);
23 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
27 function inChildProcess() {
28 return Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
31 async function setup() {
32 if (!inChildProcess()) {
33 Services.prefs.setBoolPref("browser.opaqueResponseBlocking", true);
35 let server = new NodeHTTPServer();
37 registerCleanupFunction(async () => {
40 await server.registerPathHandler("/dosniff", (req, resp) => {
42 "Content-Type": "application/json",
43 "Set-Cookie": "mycookie",
48 await server.registerPathHandler("/nosniff", (req, resp) => {
50 "Content-Type": "application/msword",
51 "Set-Cookie": "mycookie",
59 async function test_empty_header(server, doSniff) {
62 chan = makeChan(`${server.origin()}/dosniff`);
64 chan = makeChan(`${server.origin()}/nosniff`);
66 let req = await new Promise(resolve => {
67 chan.asyncOpen(new ChannelListener(resolve, null, CL_EXPECT_FAILURE));
69 equal(req.status, Cr.NS_ERROR_FAILURE);
70 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 500);
72 req.visitResponseHeaders({
73 visitHeader: function visit(_aName, _aValue) {
79 add_task(async function () {
80 let server = await setup();
81 await test_empty_header(server, true);
82 await test_empty_header(server, false);