This commit is contained in:
Peng Zhang 2026-01-26 18:39:57 -08:00 committed by GitHub
commit 25d5578e10
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 5 deletions

View File

@ -229,4 +229,37 @@ describe("canvas host", () => {
await fs.rm(dir, { recursive: true, force: true }); await fs.rm(dir, { recursive: true, force: true });
} }
}); });
it("default index.html uses safe DOM manipulation to prevent XSS", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-canvas-"));
const server = await startCanvasHost({
runtime: defaultRuntime,
rootDir: dir,
port: 0,
listenHost: "127.0.0.1",
allowInTests: true,
liveReload: false,
});
try {
const res = await fetch(`http://127.0.0.1:${server.port}${CANVAS_HOST_PATH}/`);
const html = await res.text();
expect(res.status).toBe(200);
// Verify the HTML uses safe DOM manipulation methods
expect(html).toContain("document.createElement");
expect(html).toContain("textContent");
// Verify dangerous innerHTML assignment for status is not present
expect(html).not.toContain("statusEl.innerHTML");
// Verify the status element construction is present
expect(html).toContain("bridgeSpan.className");
expect(html).toContain("bridgeSpan.textContent");
} finally {
await server.close();
await fs.rm(dir, { recursive: true, force: true });
}
});
}); });

View File

@ -100,11 +100,18 @@ function defaultIndexHTML() {
const hasIOS = () => !!(window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.clawdbotCanvasA2UIAction); const hasIOS = () => !!(window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.clawdbotCanvasA2UIAction);
const hasAndroid = () => !!(window.clawdbotCanvasA2UIAction && typeof window.clawdbotCanvasA2UIAction.postMessage === "function"); const hasAndroid = () => !!(window.clawdbotCanvasA2UIAction && typeof window.clawdbotCanvasA2UIAction.postMessage === "function");
const hasHelper = () => typeof window.clawdbotSendUserAction === "function"; const hasHelper = () => typeof window.clawdbotSendUserAction === "function";
statusEl.innerHTML =
"Bridge: " + // Build status message safely using DOM manipulation to prevent XSS
(hasHelper() ? "<span class='ok'>ready</span>" : "<span class='bad'>missing</span>") + statusEl.textContent = ""; // Clear existing content
" · iOS=" + (hasIOS() ? "yes" : "no") + statusEl.appendChild(document.createTextNode("Bridge: "));
" · Android=" + (hasAndroid() ? "yes" : "no");
const bridgeSpan = document.createElement("span");
bridgeSpan.className = hasHelper() ? "ok" : "bad";
bridgeSpan.textContent = hasHelper() ? "ready" : "missing";
statusEl.appendChild(bridgeSpan);
statusEl.appendChild(document.createTextNode(" · iOS=" + (hasIOS() ? "yes" : "no")));
statusEl.appendChild(document.createTextNode(" · Android=" + (hasAndroid() ? "yes" : "no")));
window.addEventListener("clawdbot:a2ui-action-status", (ev) => { window.addEventListener("clawdbot:a2ui-action-status", (ev) => {
const d = ev && ev.detail || {}; const d = ev && ev.detail || {};