| 1 | // El iframe del recinto y el apretón de manos. La página solo acepta del |
| 2 | // iframe dos mensajes por window.postMessage («hello» y «fatal»), con origen |
| 3 | // y fuente exactos; a partir de ahí todo va por un MessagePort que crea la |
| 4 | // página. Un segundo «load» del iframe (el recinto navegó) lo desmonta. |
| 5 | import { SANDBOX_DOC } from './config.js'; |
| 6 | |
| 7 | const fallo = (code, msg) => Object.assign(new Error(msg), { code }); |
| 8 | |
| 9 | export function openSandbox(origin, { timeout = 20000, onLost = () => {} } = {}) { |
| 10 | const iframe = document.createElement('iframe'); |
| 11 | iframe.id = 'x2-sandbox'; |
| 12 | iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin'); |
| 13 | iframe.setAttribute('allow', ''); |
| 14 | iframe.setAttribute('referrerpolicy', 'no-referrer'); |
| 15 | iframe.setAttribute('title', 'Extension sandbox'); |
| 16 | iframe.setAttribute('aria-hidden', 'true'); |
| 17 | iframe.setAttribute('tabindex', '-1'); |
| 18 | iframe.inert = true; |
| 19 | // dentro de la ventana (un iframe de otro origen fuera de ella o de tamaño |
| 20 | // cero se ralentiza), pero invisible y sin recibir nada |
| 21 | iframe.style.cssText = 'position:fixed;right:0;bottom:0;width:1px;height:1px;opacity:0;border:0;pointer-events:none;z-index:-1'; |
| 22 | iframe.src = origin + SANDBOX_DOC; |
| 23 | |
| 24 | let cargas = 0, listo = false, cerrado = false, onMsg = null; |
| 25 | const quitar = () => { |
| 26 | cerrado = true; |
| 27 | if (onMsg) window.removeEventListener('message', onMsg); |
| 28 | iframe.remove(); |
| 29 | }; |
| 30 | const promesa = new Promise((resolve, reject) => { |
| 31 | const reloj = setTimeout(() => { quitar(); reject(fallo('timeout', 'el recinto de extensiones no contestó')); }, timeout); |
| 32 | iframe.addEventListener('load', () => { |
| 33 | if (++cargas > 1 && !cerrado) { |
| 34 | quitar(); |
| 35 | if (listo) onLost(fallo('navigated', 'el recinto de extensiones se recargó solo')); |
| 36 | else { clearTimeout(reloj); reject(fallo('navigated', 'el recinto de extensiones se recargó solo')); } |
| 37 | } |
| 38 | }); |
| 39 | onMsg = e => { |
| 40 | if (e.source !== iframe.contentWindow || e.origin !== origin) return; |
| 41 | const d = e.data; |
| 42 | if (!d || typeof d !== 'object' || listo) return; |
| 43 | if (d.x2 === 'fatal') { |
| 44 | clearTimeout(reloj); quitar(); |
| 45 | reject(fallo(d.reason === 'csp' ? 'csp' : 'fatal', d.reason === 'csp' |
| 46 | ? 'el recinto se negó a arrancar: su política de seguridad no bloquea la red' |
| 47 | : 'el recinto de extensiones falló al arrancar')); |
| 48 | return; |
| 49 | } |
| 50 | if (d.x2 !== 'hello' || d.v !== 1) return; |
| 51 | listo = true; |
| 52 | clearTimeout(reloj); |
| 53 | window.removeEventListener('message', onMsg); |
| 54 | onMsg = null; |
| 55 | const canal = new MessageChannel(); |
| 56 | iframe.contentWindow.postMessage({ x2: 'init' }, origin, [canal.port2]); |
| 57 | resolve({ port: canal.port1, iframe, close: () => { canal.port1.close(); quitar(); } }); |
| 58 | }; |
| 59 | window.addEventListener('message', onMsg); |
| 60 | }); |
| 61 | document.body.appendChild(iframe); |
| 62 | return promesa; |
| 63 | } |