test_redirect_baduri.js (1492B)
1 "use strict"; 2 3 const { HttpServer } = ChromeUtils.importESModule( 4 "resource://testing-common/httpd.sys.mjs" 5 ); 6 7 /* 8 * Test whether we fail bad URIs in HTTP redirect as CORRUPTED_CONTENT. 9 */ 10 11 async function test_redirect(location) { 12 const PATH = "/BadRedirect"; 13 14 function handler(metadata, response) { 15 response.setStatusLine(metadata.httpVersion, 301, "Moved"); 16 response.setHeader("Location", location, false); 17 } 18 19 let httpServer = new HttpServer(); 20 httpServer.registerPathHandler(PATH, handler); 21 httpServer.start(-1); 22 23 const url = "http://localhost:" + httpServer.identity.primaryPort + PATH; 24 const chan = NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true }); 25 const request = await new Promise(resolve => { 26 chan.asyncOpen(new ChannelListener(resolve, null, CL_EXPECT_FAILURE)); 27 }); 28 29 Assert.equal(request.status, Cr.NS_ERROR_CORRUPTED_CONTENT); 30 31 await new Promise(resolve => httpServer.stop(resolve)); 32 } 33 34 add_task(async function test_bad_http_uri() { 35 // '>' in URI will fail to parse: we should not render response 36 await test_redirect("http://localhost:4444>BadRedirect"); 37 }); 38 39 add_task(async function test_bad_resource_1() { 40 await test_redirect("resource://"); 41 }); 42 43 add_task(async function test_bad_resource_2() { 44 await test_redirect("resource://xxx/"); 45 }); 46 47 add_task(async function test_bad_ws() { 48 await test_redirect("ws://localhost/"); 49 }); 50 51 add_task(async function test_bad_wss() { 52 await test_redirect("wss://localhost/"); 53 });