document-base-url-changes-after-nav-about-srcdoc.https.window.js (3350B)
1 // META: script=/common/get-host-info.sub.js 2 // 3 // A test to verify that, when a srcdoc frame is created when it's parent 4 // has a non-default base URI, and is later restored from history, it has 5 // the same baseURI it started with, even if the parent has changed its own 6 // baseURI in the meantime. 7 // The parent always communicates with the child via postMessage since some 8 // of the test cases include the child being sandboxed. 9 10 async function sendMessage(frame, msg) { 11 const result = new Promise(r => onmessage = e => r(e.data)); 12 frame.postMessage(msg, "*"); 13 return await result; 14 } 15 16 const runTest = (description, sandbox_flags) => { 17 promise_test(async test => { 18 const original_parent_baseURI = document.baseURI; 19 // Create a URL for the child frame to navigate to. 20 const nav_target = 21 (new URL('./resources/send-back-base-url.html', location.href)).href; 22 23 // Change parent to a non-default baseURI. 24 const base_element = document.createElement("base"); 25 base_element.href = get_host_info().REMOTE_ORIGIN; 26 document.head.appendChild(base_element); 27 assert_not_equals(document.baseURI, original_parent_baseURI, 28 "parent baseURI failed to change."); 29 const non_default_parent_baseURI = document.baseURI; 30 31 // Create child and load a srcdoc frame. 32 const iframe = document.createElement("iframe"); 33 if (sandbox_flags !== null) 34 iframe.sandbox = sandbox_flags; 35 iframe.srcdoc = ` 36 <head> 37 <script> 38 addEventListener('message', (event) => { 39 if (event.data == 'report baseURI') 40 event.source.postMessage(document.baseURI, event.origin); 41 if (event.data == 'click link') 42 document.getElementById('link').click(); 43 }); 44 parent.postMessage('loaded', '*'); 45 </scr`+`ipt> 46 </head> 47 <body> 48 <a id='link' href='` + nav_target+ `'>link</a> 49 </body> 50 `; 51 52 const child_loaded = new Promise(r => onmessage = e => r(e.data)); 53 document.body.appendChild(iframe); 54 assert_equals(await child_loaded, "loaded"); 55 56 // Verify child's baseURI matches parent. 57 const child_base_uri = await sendMessage(frames[0], "report baseURI"); 58 assert_equals(child_base_uri, non_default_parent_baseURI); 59 60 // Navigate child frame to non-srcdoc. 61 const child_loaded2 = await sendMessage(frames[0], "click link"); 62 assert_equals(child_loaded2, "loaded"); 63 const child_base_uri2 = await sendMessage(frames[0], "report baseURI"); 64 assert_not_equals(child_base_uri2, non_default_parent_baseURI); 65 assert_not_equals(child_base_uri2, original_parent_baseURI); 66 assert_equals(child_base_uri2, nav_target); 67 68 // Parent resets its baseURI. 69 base_element.remove(); 70 assert_equals(document.baseURI, original_parent_baseURI, 71 "parent baseURI failed to reset."); 72 73 // Navigate child back and verify its baseURI didn't change. 74 const child_loaded3 = new Promise(r => onmessage = e => r(e.data)); 75 window.history.back(); 76 assert_equals(await child_loaded3, "loaded"); 77 const child_base_uri3 = await sendMessage(frames[0], "report baseURI"); 78 assert_equals(child_base_uri3, non_default_parent_baseURI); 79 }, description); 80 } 81 82 runTest("non-sandboxed srcdoc - parent changes baseURI",null); 83 runTest("sandboxed srcdoc - parent changes baseURI", "allow-scripts");