Skip to content

Truly independent web browser

1.9 KB
Raw Blame
<!DOCTYPE html>
<script src="include.js"></script>
<script>
    asyncTest(async done => {
        const server = httpTestServer();

        // A child document that loads holds an iframe of its own, which this page can count across origins.
        const childSource = "<!DOCTYPE html><iframe></iframe>";

        const report = async (name, iframe) => {
            await new Promise(resolve => {
                iframe.onload = resolve;
                document.body.appendChild(iframe);
            });
            println(`${name}: ${iframe.contentWindow.length === 1 ? "loaded" : "blocked"}`);
            iframe.remove();
        };

        const frame = async (name, { crossOrigin, headers = {} }) => {
            const url = new URL(await server.createEcho("GET", `/${name}`, {
                status: 200,
                headers: { "Content-Type": "text/html", ...headers },
                body: childSource,
            }));
            if (crossOrigin)
                url.hostname = uniqueLocalhostHostname(name);
            const iframe = document.createElement("iframe");
            iframe.src = url;
            await report(name, iframe);
        };

        const coep = { "Cross-Origin-Embedder-Policy": "require-corp" };
        const corp = { "Cross-Origin-Resource-Policy": "cross-origin" };
        await frame("same-origin-without-coep", { crossOrigin: false });
        await frame("same-origin-with-coep", { crossOrigin: false, headers: coep });
        await frame("cross-origin-with-coep-and-corp", { crossOrigin: true, headers: { ...coep, ...corp } });
        await frame("cross-origin-with-coep-without-corp", { crossOrigin: true, headers: coep });
        await frame("cross-origin-with-corp-without-coep", { crossOrigin: true, headers: corp });

        const srcdoc = document.createElement("iframe");
        srcdoc.srcdoc = childSource;
        await report("srcdoc", srcdoc);

        done();
    });
</script>