test_orb_empty_header.js (2464B)
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/. */ 4 5 "use strict"; 6 7 const { NodeHTTPServer } = ChromeUtils.importESModule( 8 "resource://testing-common/NodeServer.sys.mjs" 9 ); 10 11 /* import-globals-from head_cache.js */ 12 /* import-globals-from head_cookies.js */ 13 /* import-globals-from head_channels.js */ 14 15 function makeChan(uri) { 16 var principal = 17 Services.scriptSecurityManager.createContentPrincipalFromOrigin( 18 "http://example.com" 19 ); 20 let chan = NetUtil.newChannel({ 21 uri, 22 loadingPrincipal: principal, 23 securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT, 24 contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER, 25 }).QueryInterface(Ci.nsIHttpChannel); 26 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI; 27 return chan; 28 } 29 30 function inChildProcess() { 31 return Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT; 32 } 33 34 async function setup() { 35 if (!inChildProcess()) { 36 Services.prefs.setBoolPref("browser.opaqueResponseBlocking", true); 37 } 38 let server = new NodeHTTPServer(); 39 await server.start(); 40 registerCleanupFunction(async () => { 41 await server.stop(); 42 }); 43 await server.registerPathHandler("/dosniff", (req, resp) => { 44 resp.writeHead(500, { 45 "Content-Type": "application/json", 46 "Set-Cookie": "mycookie", 47 }); 48 resp.write("good"); 49 resp.end("done"); 50 }); 51 await server.registerPathHandler("/nosniff", (req, resp) => { 52 resp.writeHead(500, { 53 "Content-Type": "application/msword", 54 "Set-Cookie": "mycookie", 55 }); 56 resp.write("good"); 57 resp.end("done"); 58 }); 59 60 return server; 61 } 62 async function test_empty_header(server, doSniff) { 63 let chan; 64 if (doSniff) { 65 chan = makeChan(`${server.origin()}/dosniff`); 66 } else { 67 chan = makeChan(`${server.origin()}/nosniff`); 68 } 69 let req = await new Promise(resolve => { 70 chan.asyncOpen(new ChannelListener(resolve, null, CL_EXPECT_FAILURE)); 71 }); 72 equal(req.status, Cr.NS_BINDING_ABORTED); 73 equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 500); 74 75 req.visitResponseHeaders({ 76 visitHeader: function visit(_aName, _aValue) { 77 ok(false); 78 }, 79 }); 80 } 81 82 add_task(async function () { 83 let server = await setup(); 84 await test_empty_header(server, true); 85 await test_empty_header(server, false); 86 });