File size: 3,305 Bytes
9c890d3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | """Generate a browser regression fixture with an opaque origin and no network."""
from __future__ import annotations
import argparse
import json
from pathlib import Path
def make_harness(viewer: str, *, label: str, verify: str = "") -> str:
policy = (
"default-src 'none'; connect-src 'none'; media-src data:; "
"img-src data:; style-src 'unsafe-inline'; script-src 'unsafe-inline'"
)
viewer = viewer.replace(
"<head>",
f'<head><meta http-equiv="Content-Security-Policy" content="{policy}">',
1,
)
observe = r"""
window.__SANDBOX_FETCH_CALLS__ = 0;
const reportFailure = setInterval(() => {
const overlay = document.getElementById("error-overlay");
if (overlay && !overlay.hidden) {
clearInterval(reportFailure);
parent.postMessage({sandboxResult: true, passed: false,
error: document.getElementById("error-message").textContent,
frameUrl: location.href, origin: window.origin}, "*");
}
}, 100);
"""
if verify:
observe += "\nwindow.fetch = () => { window.__SANDBOX_FETCH_CALLS__++; throw new TypeError('Network disabled by regression test'); };\n"
viewer = viewer.replace("<head>", "<head><script>" + observe + "</script>", 1)
if verify:
verification = (
"Promise.resolve("
+ verify.strip()
+ ").then(result => { clearInterval(reportFailure); "
+ "parent.postMessage({sandboxResult:true, ...result, "
+ "fetchCalls:window.__SANDBOX_FETCH_CALLS__, frameUrl:location.href, "
+ "origin:window.origin, videoSource:document.getElementById('source-video').src.slice(0,30)}, '*'); "
+ "}).catch(error => parent.postMessage({sandboxResult:true,passed:false,error:String(error)}, '*'));"
)
viewer = viewer.replace(
"</body>", "<script>" + verification + "</script></body>", 1
)
source = json.dumps(viewer).replace("<", "\\u003c")
return (
'<!doctype html><html><head><meta charset="utf-8"></head><body '
'style="margin:0"><iframe id="viewer" sandbox="allow-scripts" '
'allow="autoplay" style="width:100%;height:100vh;border:0"></iframe><script>'
f"window.__SANDBOX_LABEL__={json.dumps(label)};"
"window.__SANDBOX_RESULT__=null;"
"addEventListener('message', event => {"
"if(event.source===document.getElementById('viewer').contentWindow "
"&& event.data?.sandboxResult) window.__SANDBOX_RESULT__=event.data;});"
f"document.getElementById('viewer').srcdoc={source};"
"</script></body></html>"
)
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--viewer", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
parser.add_argument("--label", required=True)
parser.add_argument("--verify", type=Path)
args = parser.parse_args()
args.output.parent.mkdir(parents=True, exist_ok=True)
with args.output.open("x") as handle:
handle.write(
make_harness(
args.viewer.read_text(),
label=args.label,
verify=args.verify.read_text() if args.verify else "",
)
)
if __name__ == "__main__":
main()
|