Skip to content

Truly independent web browser

4.7 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<script>
    // A violation that the process fetching for an isolated iframe finds, of its container document's frame-src, names
    // the URL the navigation started with: not a later redirect's, and upgraded to https when the upgrade-insecure-requests
    // of the sibling that started the navigation upgrades it.
    promiseTest(async () => {
        const httpServer = httpTestServer();
        const runId = uniqueID();
        const targetHostname = uniqueLocalhostHostname("named-violation-target");

        const echo = async (path, options, hostname) => {
            const url = new URL(await httpServer.createEcho("GET", path, options));
            url.hostname = hostname;
            return url;
        };
        const html = (body, headers = {}) => ({ status: 200, headers: { "Content-Type": "text/html", ...headers }, body });

        const targetUrl = await echo(`/named-violation-target-${runId}`, html("<!DOCTYPE html>target"), targetHostname);
        const blockedUrl = await echo(
            `/named-violation-blocked-${runId}`,
            html(`<!DOCTYPE html><script>top.postMessage("blocked navigation went through", "*");<\/script>`),
            uniqueLocalhostHostname("named-violation-blocked")
        );
        const redirectUrl = await echo(
            `/named-violation-redirect-${runId}`,
            { status: 302, headers: { Location: blockedUrl.href }, body: "" },
            targetHostname
        );

        const messages = [];
        addEventListener("message", event => messages.push(event.data));

        // The source navigates its sibling once, and the container posts the blocked URL of each violation it sees.
        const blockedURIOfNavigation = async (name, sourcePolicy, href) => {
            const sourceHeaders = sourcePolicy ? { "Content-Security-Policy": sourcePolicy } : {};
            const sourceUrl = await echo(
                `/named-violation-source-${name}-${runId}`,
                html(
                    `<!DOCTYPE html>
                    <a id="navigate" target="target" href="${href}">navigate</a>
                    <script>document.getElementById("navigate").click();<\/script>`,
                    sourceHeaders
                ),
                uniqueLocalhostHostname(`named-violation-source-${name}`)
            );
            const containerUrl = await echo(
                `/named-violation-container-${name}-${runId}`,
                html(
                    `<!DOCTYPE html>
                    <iframe name="target" src="${targetUrl.href}"></iframe>
                    <script>
                        document.addEventListener("securitypolicyviolation", event => {
                            top.postMessage({ name: "${name}", blockedURI: event.blockedURI }, "*");
                        });
                        const target = document.querySelector("iframe");
                        target.onload = () => {
                            target.onload = null;
                            const source = document.createElement("iframe");
                            source.src = "${sourceUrl.href}";
                            document.body.appendChild(source);
                        };
                    <\/script>`,
                    { "Content-Security-Policy": `frame-src ${targetUrl.origin} ${sourceUrl.origin}` }
                ),
                uniqueLocalhostHostname(`named-violation-container-${name}`)
            );

            const reported = new Promise(resolve => {
                addEventListener("message", function receive(event) {
                    if (event.data?.name !== name)
                        return;
                    removeEventListener("message", receive);
                    resolve(event.data.blockedURI);
                });
            });
            const container = document.createElement("iframe");
            container.src = containerUrl;
            document.body.appendChild(container);
            const blockedURI = await reported;
            container.remove();
            return blockedURI;
        };

        const afterRedirect = await blockedURIOfNavigation("redirect", null, redirectUrl.href);
        println(`After a redirect, the violation names the navigation's first URL: ${afterRedirect === redirectUrl.href}`);

        const upgradedUrl = new URL(blockedUrl);
        upgradedUrl.protocol = "https:";
        const upgraded = await blockedURIOfNavigation("upgraded", "upgrade-insecure-requests", blockedUrl.href);
        println(`Under upgrade-insecure-requests, the violation names the https URL: ${upgraded === upgradedUrl.href}`);

        for (const message of messages) {
            if (typeof message === "string")
                println(message);
        }
    });
</script>