Skip to content

Commit 6f4c294

Browse files
Potential fix for code scanning alert no. 38: Client-side cross-site scripting
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent b4000df commit 6f4c294

1 file changed

Lines changed: 39 additions & 3 deletions

File tree

client/public/sandbox_proxy.html

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,35 @@
2424
const SANDBOX_RESOURCE_READY_METHOD =
2525
"ui/notifications/sandbox-resource-ready";
2626

27+
// Optionally restrict which origins are allowed to send sandbox HTML.
28+
// Fill this array with explicit origins (for example, ["https://your-app.com"]).
29+
// Leaving it empty will accept messages from any origin, but HTML is still sanitized.
30+
const ALLOWED_ORIGINS = [];
31+
32+
function sanitizeHtml(html) {
33+
if (window.DOMPurify && typeof window.DOMPurify.sanitize === "function") {
34+
return window.DOMPurify.sanitize(html, { RETURN_TRUSTED_TYPE: false });
35+
}
36+
37+
// Very conservative fallback: strip <script> tags and common inline event handlers.
38+
const parser = new DOMParser();
39+
const doc = parser.parseFromString(html, "text/html");
40+
41+
// Remove all <script> elements.
42+
doc.querySelectorAll("script").forEach((el) => el.remove());
43+
44+
// Remove inline event handlers such as onclick, onload, etc.
45+
doc.querySelectorAll("*").forEach((el) => {
46+
[...el.attributes].forEach((attr) => {
47+
if (/^on/i.test(attr.name)) {
48+
el.removeAttribute(attr.name);
49+
}
50+
});
51+
});
52+
53+
return doc.documentElement.innerHTML;
54+
}
55+
2756
// Notify host that we are ready to receive the app resource
2857
window.parent.postMessage(
2958
{
@@ -36,6 +65,15 @@
3665

3766
// Listen for the app resource (HTML) from the host
3867
window.addEventListener("message", (event) => {
68+
// If ALLOWED_ORIGINS is non-empty, enforce origin restriction.
69+
if (
70+
Array.isArray(ALLOWED_ORIGINS) &&
71+
ALLOWED_ORIGINS.length > 0 &&
72+
!ALLOWED_ORIGINS.includes(event.origin)
73+
) {
74+
return;
75+
}
76+
3977
const message = event.data;
4078
if (
4179
message &&
@@ -47,9 +85,7 @@
4785
const { html } = message.params;
4886
if (html) {
4987
// Sanitize the HTML before writing it to the document
50-
const safeHtml = window.DOMPurify
51-
? window.DOMPurify.sanitize(html)
52-
: html;
88+
const safeHtml = sanitizeHtml(html);
5389
document.open();
5490
document.write(safeHtml);
5591
document.close();

0 commit comments

Comments
 (0)