Truly independent web browser
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
// Fetch Metadata headers go only to a potentially trustworthy URL, and after a redirect it's the request's current
// URL that has to be one. An IPv4-mapped loopback address reaches the test server without being potentially
// trustworthy, as localhost is. The requests come from a popup on that address, since this page on localhost is a
// secure context, which mixed content checks would stop from requesting it at all.
asyncTest(async done => {
const server = httpTestServer();
function onHost(url, hostname) {
const result = new URL(url);
result.hostname = hostname;
return result.href;
}
async function redirectStart(name, fromHost, toHost) {
const target = await server.createEcho("GET", `/fetch-metadata-${name}-target`, {
status: 200,
headers: { "Content-Type": "text/plain" },
body: "target",
});
const start = await server.createEcho("GET", `/fetch-metadata-${name}-start`, {
status: 302,
headers: { "Location": onHost(target, toHost) },
});
return onHost(start, fromHost);
}
const starts = [
await redirectStart("trustworthy-to-not", "localhost", "[::ffff:127.0.0.1]"),
await redirectStart("not-to-trustworthy", "[::ffff:127.0.0.1]", "localhost"),
];
// The responses are cross-origin and opaque, and may well be blocked; only the requests matter here.
const popupURL = await server.createEcho("GET", "/fetch-metadata-popup", {
status: 200,
headers: { "Content-Type": "text/html" },
body: `<!DOCTYPE html><script>
(async () => {
for (const url of ${JSON.stringify(starts)})
await fetch(url, { mode: "no-cors" }).catch(() => {});
opener.postMessage("done", "*");
})();
<\/script>`,
});
window.addEventListener("message", async () => {
async function recorded(path) {
const response = await fetch(server.recordedRequestHeadersURL(server.echoURL(path)));
return response.ok ? await response.json() : null;
}
function describe(headers) {
if (!headers)
return "not fetched";
const names = Object.keys(headers).filter(name => name.toLowerCase().startsWith("sec-fetch-")).sort();
return names.length ? names.join(", ") : "no Sec-Fetch- headers";
}
for (const name of ["trustworthy-to-not", "not-to-trustworthy"]) {
const first = describe(await recorded(`/fetch-metadata-${name}-start`));
const second = describe(await recorded(`/fetch-metadata-${name}-target`));
println(`${name}: first hop ${first}; second hop ${second}`);
}
popup.close();
done();
}, { once: true });
const popup = window.open(onHost(popupURL, "[::ffff:127.0.0.1]"));
});
</script>