browser_subdocument_downgrade.js (2389B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const EMPTY_URL = 7 "http://example.com/browser/dom/security/test/https-first/file_empty.html"; 8 const SUBDOCUMENT_URL = 9 "https://example.com/browser/dom/security/test/https-first/file_subdocument_downgrade.sjs"; 10 11 add_task(async function test_subdocument_downgrade() { 12 await SpecialPowers.pushPrefEnv({ 13 set: [ 14 // We want to test HTTPS-First 15 ["dom.security.https_first", true], 16 // Makes it easier to detect the error 17 ["security.mixed_content.block_active_content", false], 18 // Avoid error page for empty file (blank page with 429) 19 ["browser.http.blank_page_with_error_response.enabled", true], 20 ], 21 }); 22 23 // Open a empty document with origin http://example.com, which gets upgraded 24 // to https://example.com by HTTPS-First and thus is marked as 25 // HTTPS_ONLY_UPGRADED_HTTPS_FIRST. 26 await BrowserTestUtils.withNewTab(EMPTY_URL, async browser => { 27 await SpecialPowers.spawn( 28 browser, 29 [SUBDOCUMENT_URL], 30 async SUBDOCUMENT_URL => { 31 function isCrossOriginIframe(iframe) { 32 try { 33 return !iframe.contentDocument; 34 } catch (e) { 35 return true; 36 } 37 } 38 const subdocument = content.document.createElement("iframe"); 39 // We open https://example.com/.../file_subdocument_downgrade.sjs in a 40 // iframe, which sends a invalid response if the scheme is https. Thus 41 // we should get an error. But if we accidentally copy the 42 // HTTPS_ONLY_UPGRADED_HTTPS_FIRST flag from the parent into the iframe 43 // loadinfo, HTTPS-First will try to downgrade the iframe. We test that 44 // this doesn't happen. 45 subdocument.src = SUBDOCUMENT_URL; 46 const loadPromise = new Promise(resolve => { 47 subdocument.addEventListener("load", () => { 48 ok( 49 // If the iframe got downgraded, it should now have the origin 50 // http://example.com, which we can detect as being cross-origin. 51 !isCrossOriginIframe(subdocument), 52 "Subdocument should not be downgraded" 53 ); 54 resolve(); 55 }); 56 }); 57 content.document.body.appendChild(subdocument); 58 await loadPromise; 59 } 60 ); 61 }); 62 });