|
| 1 | +/** |
| 2 | + * Regression test for XSS via mDNS server name injection. |
| 3 | + * |
| 4 | + * Verifies that the launcher's server list rendering does not |
| 5 | + * interpolate user-controlled strings into inline event handlers. |
| 6 | + * |
| 7 | + * Run: node src-tauri/resources/index.test.mjs |
| 8 | + */ |
| 9 | + |
| 10 | +import { readFileSync } from "fs"; |
| 11 | +import { strict as assert } from "assert"; |
| 12 | + |
| 13 | +const html = readFileSync(new URL("./index.html", import.meta.url), "utf8"); |
| 14 | + |
| 15 | +// ---- Test 1: No inline onclick handlers with interpolated values ---- |
| 16 | +// The server list template should NOT contain onclick with escapeHtml |
| 17 | +// interpolation, which is vulnerable to quote-breakout injection. |
| 18 | +const onclickPattern = /onclick="connectToServer\('\$\{escapeHtml/; |
| 19 | +assert.ok( |
| 20 | + !onclickPattern.test(html), |
| 21 | + "FAIL: Found inline onclick with escapeHtml interpolation — vulnerable to XSS via quote breakout" |
| 22 | +); |
| 23 | + |
| 24 | +// ---- Test 2: Server items use data attributes + addEventListener ---- |
| 25 | +assert.ok( |
| 26 | + html.includes("data-server-index"), |
| 27 | + "FAIL: Server items should use data-server-index attributes" |
| 28 | +); |
| 29 | +assert.ok( |
| 30 | + html.includes("addEventListener"), |
| 31 | + "FAIL: Click handlers should be bound via addEventListener, not inline onclick" |
| 32 | +); |
| 33 | + |
| 34 | +// ---- Test 3: Simulate the escapeHtml function and verify XSS payloads are inert ---- |
| 35 | +// Replicate the browser's textContent/innerHTML escaping behavior |
| 36 | +function escapeHtml(text) { |
| 37 | + return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); |
| 38 | + // Note: textContent/innerHTML does NOT escape ' " or ` |
| 39 | +} |
| 40 | + |
| 41 | +const xssPayloads = [ |
| 42 | + "z');alert(1);('", |
| 43 | + "z',alert(1),'", |
| 44 | + 'z");alert(1);("', |
| 45 | + "z`);alert(1);(`", |
| 46 | + "z');new Image().src=`http://evil.com`;('", |
| 47 | + "<script>alert(1)</script>", |
| 48 | + "test' onclick='alert(1)", |
| 49 | +]; |
| 50 | + |
| 51 | +for (const payload of xssPayloads) { |
| 52 | + const escaped = escapeHtml(payload); |
| 53 | + |
| 54 | + // escapeHtml must neutralize HTML tag injection in element content |
| 55 | + assert.ok( |
| 56 | + !escaped.includes("<script>"), |
| 57 | + `FAIL: escapeHtml did not neutralize script tag in: ${payload}` |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +// ---- Test 4: The template in index.html uses safe integer index, not user content ---- |
| 62 | +// Extract the server-item template from the source and verify it only |
| 63 | +// interpolates a numeric index into attributes, never escapeHtml(server.name/url). |
| 64 | +const templateSection = html.slice(html.indexOf(".map("), html.indexOf(".join(")); |
| 65 | + |
| 66 | +// The data attribute must use a safe integer index |
| 67 | +assert.ok( |
| 68 | + templateSection.includes("data-server-index"), |
| 69 | + "FAIL: Template should use data-server-index for click binding" |
| 70 | +); |
| 71 | + |
| 72 | +// User-controlled values must NOT appear in any HTML attribute context |
| 73 | +// (they should only appear inside element content via escapeHtml) |
| 74 | +const attrPattern = /=["'][^"']*\$\{escapeHtml\(server\.(name|url)\)/; |
| 75 | +assert.ok( |
| 76 | + !attrPattern.test(templateSection), |
| 77 | + "FAIL: User-controlled escapeHtml values must not appear in HTML attributes" |
| 78 | +); |
| 79 | + |
| 80 | +console.log("All tests passed."); |
0 commit comments