Skip to content

Commit 39f2f3a

Browse files
committed
test: add escapeHtml tests covering XSS vectors
Verify that HTML special characters and malicious tag payloads like attribute-breaking injections are properly escaped.
1 parent 0d98741 commit 39f2f3a

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

routes/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ async function getRelease(tag: string | undefined): Promise<CachedRelease | null
9191
return kv.get<CachedRelease>(LATEST_STALE_KEY);
9292
}
9393

94-
function escapeHtml(s: string): string {
94+
export function escapeHtml(s: string): string {
9595
return s
9696
.replace(/&/g, "&amp;")
9797
.replace(/"/g, "&quot;")

tests/index.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vite-plus/test";
2-
import { buildReleaseFromTag, detectArch } from "../routes/index";
2+
import { buildReleaseFromTag, detectArch, escapeHtml } from "../routes/index";
33

44
describe("detectArch", () => {
55
it("defaults to x64 when no query param or user-agent", () => {
@@ -64,6 +64,26 @@ describe("detectArch", () => {
6464
});
6565
});
6666

67+
describe("escapeHtml", () => {
68+
it("escapes HTML special characters", () => {
69+
expect(escapeHtml('<script>alert("xss")</script>')).toBe(
70+
"&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;",
71+
);
72+
});
73+
74+
it("escapes ampersands", () => {
75+
expect(escapeHtml("a&b")).toBe("a&amp;b");
76+
});
77+
78+
it("escapes a malicious tag used in attribute context", () => {
79+
const malicious = 'x"><img src=x onerror=alert(1)>';
80+
const escaped = escapeHtml(malicious);
81+
expect(escaped).not.toContain("<");
82+
expect(escaped).not.toContain(">");
83+
expect(escaped).not.toContain('"');
84+
});
85+
});
86+
6787
describe("buildReleaseFromTag", () => {
6888
it("constructs download URLs from a tag", () => {
6989
const result = buildReleaseFromTag("v0.1.17-alpha.0");

0 commit comments

Comments
 (0)