| 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'sha256-/luUDdba82j70NvkxS1nxDvTfjxLjRfnpKE6sqiin3U=' blob: 'wasm-unsafe-eval'; worker-src blob:; child-src blob:; frame-src 'self'; connect-src blob: data:; img-src blob: data:; font-src blob: data:; style-src 'unsafe-inline'; media-src 'none'; object-src 'none'; manifest-src 'none'; base-uri 'none'; form-action 'none'"> |
| 6 | <meta name="referrer" content="no-referrer"> |
| 7 | <title>Elffuss Code extension sandbox</title> |
| 8 | </head> |
| 9 | <body> |
| 10 | <script> |
| 11 | // Extension sandbox of Elffuss Code. This document lives on its own site, |
| 12 | // with no network host in any CSP directive. Code's page frames it, hands it |
| 13 | // the verified vendor bundle and its own relay script over a MessagePort, and |
| 14 | // renders nothing that comes out of it except as plain data. |
| 15 | (() => { |
| 16 | 'use strict'; |
| 17 | const CODE_ORIGINS = ['https://code.elffuss.com']; |
| 18 | const here = window.location; |
| 19 | // Development: Code on http://127.0.0.1:<port>, sandbox on http://localhost:<port>. |
| 20 | const devParent = o => here.hostname === 'localhost' && o === 'http://127.0.0.1:' + here.port; |
| 21 | const allowedParent = o => CODE_ORIGINS.includes(o) || devParent(o); |
| 22 | |
| 23 | // 1. Run only when framed directly by Code's page, and never on Code's own origin. |
| 24 | if (window.parent === window || CODE_ORIGINS.includes(here.origin)) return; |
| 25 | const ancestors = here.ancestorOrigins; |
| 26 | if (!ancestors || ancestors.length !== 1 || !allowedParent(ancestors[0]) || ancestors[0] === here.origin) return; |
| 27 | const parentOrigin = ancestors[0]; |
| 28 | const tell = msg => window.parent.postMessage(msg, parentOrigin); |
| 29 | |
| 30 | // 3. No popups and no peer connections from this realm. |
| 31 | const GLOBALS = ['open', 'RTCPeerConnection', 'webkitRTCPeerConnection', 'RTCDataChannel']; |
| 32 | const removeGlobals = w => { |
| 33 | for (const k of GLOBALS) { |
| 34 | try { Object.defineProperty(w, k, { value: undefined, writable: false, configurable: false }); } catch { /* already gone */ } |
| 35 | } |
| 36 | }; |
| 37 | removeGlobals(window); |
| 38 | |
| 39 | // 4. This document never navigates. |
| 40 | let port = null, navBlocked = 0; |
| 41 | const lockNavigation = (w, where) => { |
| 42 | if (!w.navigation) return; |
| 43 | w.navigation.addEventListener('navigate', e => { |
| 44 | navBlocked++; |
| 45 | if (e.cancelable) e.preventDefault(); |
| 46 | try { port && port.postMessage({ t: 'log', level: 'warn', text: where + ' navigation blocked' }); } catch { /* port closed */ } |
| 47 | }); |
| 48 | }; |
| 49 | lockNavigation(window, 'sandbox'); |
| 50 | |
| 51 | // 4b. The extension-host frame the library creates here (same site) gets the |
| 52 | // same treatment once it loads. It is never reloaded on purpose, so a |
| 53 | // second load means it navigated: everything stops. |
| 54 | const watchFrame = f => { |
| 55 | if (watched.has(f)) return; |
| 56 | watched.add(f); |
| 57 | let loads = 0; |
| 58 | f.addEventListener('load', () => { |
| 59 | if (++loads > 1) { |
| 60 | try { port && port.postMessage({ t: 'fatal', reason: 'navigated', text: 'the extension host frame navigated' }); } catch { /* port closed */ } |
| 61 | return; |
| 62 | } |
| 63 | try { removeGlobals(f.contentWindow); lockNavigation(f.contentWindow, 'extension host'); } catch { /* not ours */ } |
| 64 | }); |
| 65 | }; |
| 66 | const watched = new WeakSet(); |
| 67 | new MutationObserver(records => { |
| 68 | for (const r of records) for (const n of r.addedNodes) { |
| 69 | if (n.nodeName === 'IFRAME') watchFrame(n); |
| 70 | else if (n.querySelectorAll) n.querySelectorAll('iframe').forEach(watchFrame); |
| 71 | } |
| 72 | }).observe(document.documentElement, { childList: true, subtree: true }); |
| 73 | |
| 74 | // 2. CSP self-test: a request to a name that never resolves must be refused |
| 75 | // by the policy itself. Without that refusal, nothing starts. |
| 76 | const selfTest = () => new Promise(resolve => { |
| 77 | let seen = false, settled = false; |
| 78 | const done = () => { if (!settled) { settled = true; document.removeEventListener('securitypolicyviolation', onViolation); resolve(seen); } }; |
| 79 | const onViolation = e => { if (String(e.blockedURI || '').startsWith('https://csp-selftest.invalid')) { seen = true; done(); } }; |
| 80 | document.addEventListener('securitypolicyviolation', onViolation); |
| 81 | fetch('https://csp-selftest.invalid/').catch(() => {}).finally(() => setTimeout(done, 500)); |
| 82 | }); |
| 83 | |
| 84 | let booted = false; |
| 85 | async function boot(d) { |
| 86 | const urls = {}; |
| 87 | for (const f of d.vendor) urls[f.role] = URL.createObjectURL(new Blob([f.bytes], { type: 'text/javascript' })); |
| 88 | if (!urls.main || !urls.extensionHostWorkerMain || !urls.editorWorkerService) throw new Error('incomplete vendor bundle'); |
| 89 | self.MonacoEnvironment = { |
| 90 | getWorkerUrl: (_, label) => urls[label], |
| 91 | getWorkerOptions: () => ({ type: 'module' }), |
| 92 | }; |
| 93 | const m = await import(urls.main); |
| 94 | m.registerAssets({ |
| 95 | 'vs/workbench/services/extensions/worker/webWorkerExtensionHostIframe.html': () => here.origin + '/x2/v1/exthost-iframe.html', |
| 96 | }); |
| 97 | const relay = await import(URL.createObjectURL(new Blob([d.relay], { type: 'text/javascript' }))); |
| 98 | await relay.start({ m, port, config: d.config, sandbox: { navBlocked: () => navBlocked } }); |
| 99 | } |
| 100 | |
| 101 | function onInit(e) { |
| 102 | if (port || e.source !== window.parent || e.origin !== parentOrigin) return; |
| 103 | const d = e.data; |
| 104 | if (!d || d.x2 !== 'init' || !e.ports || e.ports.length !== 1) return; |
| 105 | window.removeEventListener('message', onInit); |
| 106 | port = e.ports[0]; |
| 107 | port.onmessage = ev => { |
| 108 | const m = ev.data; |
| 109 | if (booted || !m || m.t !== 'boot') return; |
| 110 | booted = true; |
| 111 | port.onmessage = null; |
| 112 | boot(m).catch(err => port.postMessage({ t: 'fatal', reason: 'boot', text: String((err && err.message) || err).slice(0, 500) })); |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | selfTest().then(ok => { |
| 117 | if (!ok) { tell({ x2: 'fatal', reason: 'csp' }); return; } |
| 118 | window.addEventListener('message', onInit); |
| 119 | tell({ x2: 'hello', v: 1 }); |
| 120 | }); |
| 121 | })(); |
| 122 | </script> |
| 123 | </body> |
| 124 | </html> |