|
const uid = require('./uid'); |
|
|
|
|
|
const generateQuadUid = () => uid() + uid() + uid() + uid(); |
|
|
|
|
|
const none = "'none'"; |
|
const featurePolicy = { |
|
'accelerometer': none, |
|
'ambient-light-sensor': none, |
|
'battery': none, |
|
'camera': none, |
|
'display-capture': none, |
|
'document-domain': none, |
|
'encrypted-media': none, |
|
'fullscreen': none, |
|
'geolocation': none, |
|
'gyroscope': none, |
|
'magnetometer': none, |
|
'microphone': none, |
|
'midi': none, |
|
'payment': none, |
|
'picture-in-picture': none, |
|
'publickey-credentials-get': none, |
|
'speaker-selection': none, |
|
'usb': none, |
|
'vibrate': none, |
|
'vr': none, |
|
'screen-wake-lock': none, |
|
'web-share': none, |
|
'interest-cohort': none |
|
}; |
|
|
|
|
|
const generateAllow = () => Object.entries(featurePolicy) |
|
.map(([name, permission]) => `${name} ${permission}`) |
|
.join('; '); |
|
|
|
const createFrame = () => { |
|
const element = document.createElement("iframe"); |
|
const frameId = generateQuadUid(); |
|
|
|
|
|
element.dataset.id = frameId; |
|
element.style.display = "none"; |
|
element.setAttribute('aria-hidden', 'true'); |
|
|
|
element.sandbox = 'allow-scripts allow-modals'; |
|
element.allow = generateAllow(); |
|
document.body.append(element); |
|
return element; |
|
}; |
|
|
|
const origin = window.origin; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const messageHandler = (event, iframe, removeHandler) => new Promise(resolve => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!event.data.payload) return; |
|
|
|
|
|
if (event.data.payload.id !== iframe.dataset.id) return; |
|
const data = event.data.payload; |
|
|
|
window.removeEventListener('message', removeHandler); |
|
try { |
|
const url = iframe.src; |
|
|
|
URL.revokeObjectURL(url); |
|
} catch { |
|
|
|
|
|
console.warn('failed to revoke url of iframe sandboxed eval'); |
|
} |
|
iframe.remove(); |
|
|
|
|
|
resolve(data); |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
const prepareCodeForEval = (code) => { |
|
const escaped = JSON.stringify(code); |
|
|
|
|
|
const scriptEscaped = escaped.replaceAll('<\/script>', '<\\/script>'); |
|
return scriptEscaped; |
|
} |
|
|
|
const generateEvaluateSrc = (code, frame) => { |
|
|
|
|
|
const runnerCode = `(async () => { |
|
let result = null; |
|
let success = true; |
|
try { |
|
// techincally eval can also postMessage |
|
// and also modify success & result probably |
|
// but theres no real reason to prevent it |
|
// nor does the user have any reason to do it |
|
result = await eval(${prepareCodeForEval(code)}); |
|
} catch (err) { |
|
success = false; |
|
result = err; |
|
} |
|
|
|
const parent = window.parent; |
|
const origin = '*'; |
|
// console.log(result,success); |
|
console.log(origin); |
|
|
|
try { |
|
parent.postMessage({ |
|
payload: { |
|
success: success, |
|
value: result, |
|
id: ${JSON.stringify(frame.dataset.id)} |
|
}, |
|
}, origin); |
|
} catch (topLevelError) { |
|
// couldnt clone likely |
|
try { |
|
parent.postMessage({ |
|
payload: { |
|
success: success, |
|
value: JSON.stringify(result), |
|
id: ${JSON.stringify(frame.dataset.id)} |
|
}, |
|
}, origin); |
|
} catch (err) { |
|
// ok we cant stringify it just error |
|
parent.postMessage({ |
|
payload: { |
|
success: false, |
|
value: [String(topLevelError), String(err)].join("; "), |
|
id: ${JSON.stringify(frame.dataset.id)} |
|
}, |
|
}, origin); |
|
} |
|
} |
|
})();`; |
|
|
|
const html = [ |
|
'<!DOCTYPE html>', |
|
'<html lang="en-US">', |
|
|
|
'<head>', |
|
'<title>the an one of an iframe</title>', |
|
'</head>', |
|
|
|
'<body>', |
|
'<h1><p>epic computing in progress...</p></h1>', |
|
|
|
|
|
'<script>', |
|
runnerCode, |
|
'</script>', |
|
'</body>', |
|
'</html>' |
|
].join("\n"); |
|
|
|
const blob = new Blob([html], { type: 'text/html;charset=UTF-8' }); |
|
const url = URL.createObjectURL(blob); |
|
|
|
return url; |
|
}; |
|
|
|
class SandboxRunner { |
|
static execute(code) { |
|
return new Promise(resolve => { |
|
const frame = createFrame(); |
|
|
|
|
|
|
|
|
|
const trueHandler = e => { |
|
|
|
|
|
|
|
messageHandler(e, frame, trueHandler).then(payload => { |
|
|
|
resolve({ |
|
success: payload.success, |
|
value: payload.value |
|
}); |
|
}); |
|
}; |
|
window.addEventListener('message', trueHandler); |
|
frame.src = generateEvaluateSrc(code, frame); |
|
}); |
|
} |
|
} |
|
|
|
module.exports = SandboxRunner; |
|
|