tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

initial-about-blank-baseURI.window.js (2960B)


      1 // META: script=/common/get-host-info.sub.js
      2 // Test that the initial about:blank gets the about base URL
      3 // from the entry global object.
      4 
      5 async function withIframe(src, cb) {
      6    const ifr = document.createElement("iframe");
      7    ifr.src = src;
      8    document.body.append(ifr);
      9    cb(ifr);
     10    ifr.remove();
     11 }
     12 
     13 async function withWindow(src, cb) {
     14    const w = window.open(src);
     15    cb(w);
     16    w.close();
     17 }
     18 
     19 // Need a trailing '/' for equality checks
     20 const REMOTE_ORIGIN = new URL("/", get_host_info().REMOTE_ORIGIN).href;
     21 
     22 async function withWindowOpenerNotInitiator(src, cb) {
     23    window.deferredIframeWindow = Promise.withResolvers();
     24 
     25    // Create an iframe with a different base URL.
     26    // If it opens a window with window.top being the opener,
     27    // the base URL should come from the initiator, i.e. this iframe.
     28    const ifr = document.createElement("iframe");
     29    ifr.srcdoc = `
     30    <head>
     31    <base href='${REMOTE_ORIGIN}'>
     32    <script>
     33        const w = window.top.open('${src}');
     34        window.top.deferredIframeWindow.resolve(w);
     35    </scr` + `ipt>
     36    </head>
     37    <body></body>
     38    `;
     39    document.body.append(ifr);
     40 
     41    const w = await window.deferredIframeWindow.promise;
     42 
     43    cb(w);
     44 
     45    w.close();
     46    ifr.remove();
     47 }
     48 
     49 promise_test(async t => {
     50    await withIframe("", ifr => {
     51        assert_equals(ifr.contentDocument.baseURI, document.baseURI, "about:blank has creator's base URI");
     52    })
     53 }, "Initial iframe about:blank gets base url from creator");
     54 
     55 promise_test(async t => {
     56    await withIframe("/arbitrary-sameorigin-src", ifr => {
     57        assert_equals(ifr.contentDocument.baseURI, document.baseURI, "about:blank has creator's base URI");
     58    })
     59 }, "Transient iframe about:blank gets base url from creator");
     60 
     61 promise_test(async t => {
     62    await withWindow("", w => {
     63        assert_equals(w.document.baseURI, document.baseURI, "about:blank has creator's base URI");
     64    })
     65 }, "Initial top-level about:blank gets base url from creator = opener");
     66 
     67 promise_test(async t => {
     68    await withWindow("/arbitrary-sameorigin-src", w => {
     69        assert_equals(w.document.baseURI, document.baseURI, "about:blank has creator's base URI");
     70    })
     71 }, "Transient top-level about:blank gets base url from creator = opener");
     72 
     73 promise_test(async t => {
     74    await withWindowOpenerNotInitiator("", w => {
     75        assert_not_equals(REMOTE_ORIGIN, document.baseURI, "These need to be different");
     76        assert_equals(w.document.baseURI, REMOTE_ORIGIN, "about:blank has creator's base URI");
     77    })
     78 }, "Initial top-level about:blank gets base url from creator != opener");
     79 
     80 promise_test(async t => {
     81    await withWindowOpenerNotInitiator("/arbitrary-sameorigin-src", w => {
     82        assert_not_equals(REMOTE_ORIGIN, document.baseURI, "These need to be different");
     83        assert_equals(w.document.baseURI, REMOTE_ORIGIN, "about:blank has creator's base URI");
     84    })
     85 }, "Transient top-level about:blank gets base url from creator != opener");