browser_system_principal_initial_document.js (4581B)
1 const CHROME_URI = "chrome://global/content/aboutSupport.xhtml"; 2 3 // The goal of this test is to check for crashes and document current behavior 4 5 add_task(async function test_transient_about_blank_in_chrome_iframe() { 6 // open a tab with system principal due to chrome URI 7 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, CHROME_URI); 8 let browser = tab.linkedBrowser; 9 10 await SpecialPowers.spawn(browser, [CHROME_URI], async CHROME_URI => { 11 let bc = content.browsingContext; 12 let contentPrincipal = content.document.nodePrincipal; 13 Assert.ok(contentPrincipal.isSystemPrincipal, "tab has system principal"); 14 Assert.ok(bc.isContent, "tab BC is content"); 15 16 // within a system context, add a new iframe 17 let iframe = content.document.createElement("iframe"); 18 iframe.src = CHROME_URI; 19 content.document.documentElement.appendChild(iframe); 20 21 // iframe will start with some different principal 22 let ifrBC = iframe.browsingContext; 23 let aboutBlankPrincipal = iframe.contentDocument.nodePrincipal; 24 Assert.ok( 25 iframe.contentDocument.isUncommittedInitialDocument, 26 "iframe at transient about:blank" 27 ); 28 Assert.ok( 29 !aboutBlankPrincipal.isSystemPrincipal, 30 "transient about:blank doesn't have system principal" 31 ); 32 Assert.ok( 33 aboutBlankPrincipal.isNullPrincipal, 34 "transient about:blank starts out with null principal" 35 ); 36 Assert.ok(ifrBC.isContent, "iframe BC is content"); 37 38 // test inner window will be replaced 39 iframe.contentWindow.foo = "bar"; 40 iframe.contentWindow.addEventListener("load", () => { 41 Assert.ok(false, "load event never fired on initial iframe inner window"); 42 }); 43 44 await new Promise(res => iframe.addEventListener("load", res)); 45 46 // after load, iframe has system principal and inner window was replaced 47 let chromeDocPrincipal = iframe.contentDocument.nodePrincipal; 48 Assert.ok( 49 chromeDocPrincipal.isSystemPrincipal, 50 "after load, iframe has system principal" 51 ); 52 Assert.ok(ifrBC.isContent, "iframe BC stays content"); 53 Assert.equal( 54 iframe.contentWindow.foo, 55 undefined, 56 "iframe inner window replaced" 57 ); 58 59 iframe.remove(); 60 }); 61 62 BrowserTestUtils.removeTab(tab); 63 }); 64 65 add_task(async function test_about_blank_iframe_in_chrome_doc() { 66 // open a tab with system principal due to chrome URI 67 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, CHROME_URI); 68 let browser = tab.linkedBrowser; 69 70 await SpecialPowers.spawn(browser, [], async () => { 71 let bc = content.browsingContext; 72 let contentPrincipal = content.document.nodePrincipal; 73 Assert.ok(contentPrincipal.isSystemPrincipal, "tab has system principal"); 74 Assert.ok(bc.isContent, "tab BC is content"); 75 76 // within a system context, add an about:blank iframe 77 let iframe = content.document.createElement("iframe"); 78 content.document.documentElement.appendChild(iframe); 79 let ifrPrincipal = SpecialPowers.wrap(iframe.contentWindow).document 80 .nodePrincipal; 81 Assert.ok( 82 !ifrPrincipal.isSystemPrincipal, 83 "about:blank iframe has no system principal" 84 ); 85 }); 86 87 BrowserTestUtils.removeTab(tab); 88 }); 89 90 add_task(async function test_open_about_blank_link_from_chrome_doc() { 91 // open a tab with system principal due to chrome URI 92 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, CHROME_URI); 93 let browser = tab.linkedBrowser; 94 95 const linkOpened = BrowserTestUtils.waitForNewTab(gBrowser, "about:blank"); 96 97 await SpecialPowers.spawn(browser, [], async () => { 98 let bc = content.browsingContext; 99 let contentPrincipal = content.document.nodePrincipal; 100 Assert.ok(contentPrincipal.isSystemPrincipal, "tab has system principal"); 101 Assert.ok(bc.isContent, "tab BC is content"); 102 103 // within a system context, add an about:blank link 104 let link = content.document.createElement("a"); 105 link.href = "about:blank"; 106 link.target = "_blank"; 107 content.document.documentElement.appendChild(link); 108 link.click(); 109 }); 110 111 const blanktab = await linkOpened; 112 113 // Check the opened tab from the link is privileged 114 await SpecialPowers.spawn(blanktab.linkedBrowser, [], async () => { 115 let bc = content.browsingContext; 116 let contentPrincipal = content.document.nodePrincipal; 117 Assert.ok( 118 contentPrincipal.isSystemPrincipal, 119 "about:blank has system principal" 120 ); 121 Assert.ok(bc.isContent, "about:blank BC is content"); 122 }); 123 124 BrowserTestUtils.removeTab(blanktab); 125 BrowserTestUtils.removeTab(tab); 126 });