Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions tests/worker/dsar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,43 @@ describe("dsar", () => {
assert.deepEqual(emailPayload?.to, ["contact@oyme.site"]);
assert.match(String(emailPayload?.subject), /DSAR delete request/);
});

it("escapes user-controlled HTML in the staff email body", async (t) => {
const { env, db } = createTestEnv();
const user = seedUser(db, { username: "DsarUser", email: "u@example.com" });
seedSession(db, user.id, "dsar-token");

let emailPayload: Record<string, unknown> | null = null;
const originalFetch = globalThis.fetch;
globalThis.fetch = async (_input, init) => {
if (init?.body && typeof init.body === "string") {
emailPayload = JSON.parse(init.body) as Record<string, unknown>;
}
return { ok: true, json: async () => ({ id: "email_123" }) } as Response;
};
t.after(() => {
globalThis.fetch = originalFetch;
});

const { res, json } = await jsonRequest(env, "/api/dsar", {
method: "POST",
headers: { "x-session-token": "dsar-token" },
body: {
requestType: "access",
jurisdiction: "<img src=x onerror=alert(1)>",
details: "<script>alert(1)</script>",
},
});

assert.equal(res.status, 200);
assert.equal(json.success, true);
assert.ok(emailPayload);
const html = String(emailPayload?.html);
// Escaped forms are present.
assert.ok(html.includes("&lt;script&gt;alert(1)&lt;/script&gt;"));
assert.ok(html.includes("&lt;img src=x onerror=alert(1)&gt;"));
// Raw markup is not.
assert.ok(!html.includes("<script>"));
assert.ok(!html.includes("<img "));
});
});
21 changes: 15 additions & 6 deletions worker/routes/dsar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ const allowedTypes: ReadonlySet<DsarRequestType> = new Set([
"restrict",
]);

function escapeHtml(value: string): string {
return value
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#39;");
}

function buildDsarEmailHtml(input: {
user: User;
requestType: DsarRequestType;
Expand All @@ -33,13 +42,13 @@ function buildDsarEmailHtml(input: {
<body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f5f5f5; padding: 24px; margin: 0;">
<div style="max-width: 640px; margin: 0 auto; background: #ffffff; border-radius: 12px; padding: 24px;">
<h1 style="margin: 0 0 16px; font-size: 22px; color: #111;">DSAR Request</h1>
<p style="margin: 0 0 8px; color: #333;"><strong>User ID:</strong> ${user.id}</p>
<p style="margin: 0 0 8px; color: #333;"><strong>Username:</strong> ${user.username}</p>
<p style="margin: 0 0 8px; color: #333;"><strong>Email:</strong> ${user.email ?? "Not set"}</p>
<p style="margin: 0 0 8px; color: #333;"><strong>Request Type:</strong> ${requestType}</p>
<p style="margin: 0 0 8px; color: #333;"><strong>Jurisdiction:</strong> ${jurisdiction}</p>
<p style="margin: 0 0 8px; color: #333;"><strong>User ID:</strong> ${escapeHtml(String(user.id))}</p>
<p style="margin: 0 0 8px; color: #333;"><strong>Username:</strong> ${escapeHtml(user.username)}</p>
<p style="margin: 0 0 8px; color: #333;"><strong>Email:</strong> ${escapeHtml(user.email ?? "Not set")}</p>
<p style="margin: 0 0 8px; color: #333;"><strong>Request Type:</strong> ${escapeHtml(requestType)}</p>
<p style="margin: 0 0 8px; color: #333;"><strong>Jurisdiction:</strong> ${escapeHtml(jurisdiction)}</p>
<p style="margin: 0 0 8px; color: #333;"><strong>Details:</strong></p>
<pre style="white-space: pre-wrap; background: #f8fafc; padding: 12px; border-radius: 8px; margin: 0;">${details}</pre>
<pre style="white-space: pre-wrap; background: #f8fafc; padding: 12px; border-radius: 8px; margin: 0;">${escapeHtml(details)}</pre>
</div>
</body>
</html>`;
Expand Down
Loading