fix: sanitize <script> in __FRSH_STATE state (#739)

This commit escapes `<` `>` and related characters in the serialized state that is encoded into the `<script id=__FRSH_STATE>` tag. This prevents XSS attack occuring through injection of a `</script>` string in island props.
This commit is contained in:
OkadaGenya
2022-09-12 17:33:02 +02:00
committed by GitHub
parent b75509526d
commit 9d13f6ff26
7 changed files with 142 additions and 37 deletions
+59
View File
@@ -82,3 +82,62 @@ Deno.test({
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "island tests with </script>",
async fn(t) {
// Preparation
const serverProcess = Deno.run({
cmd: ["deno", "run", "-A", "./tests/fixture/main.ts"],
stdout: "piped",
stderr: "inherit",
});
const decoder = new TextDecoderStream();
const lines = serverProcess.stdout.readable
.pipeThrough(decoder)
.pipeThrough(new TextLineStream());
let started = false;
for await (const line of lines) {
if (line.includes("Listening on http://")) {
started = true;
break;
}
}
if (!started) {
throw new Error("Server didn't start up");
}
await delay(100);
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
const page = await browser.newPage();
page.on("dialog", () => {
assert(false, "There is XSS");
});
await page.goto("http://localhost:8000/evil", {
waitUntil: "networkidle2",
});
await t.step("prevent XSS on Island", async () => {
const bodyElem = await page.waitForSelector(`body`);
const value = await bodyElem?.evaluate((el) => el.getInnerHTML());
assertStringIncludes(
value,
`{"message":"\\u003c/script\\u003e\\u003cscript\\u003ealert('test')\\u003c/script\\u003e"}`,
`XSS is not escaped`,
);
});
await browser.close();
await lines.cancel();
serverProcess.kill("SIGTERM");
serverProcess.close();
},
sanitizeOps: false,
sanitizeResources: false,
});