From a3ac38ffcea8d6cb8a57da204920da9da7bcb1fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Sat, 19 Sep 2026 21:14:56 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=94=92=20security=20headers=20and?= =?UTF-8?q?=20sanitized=20editor=20input=20-=20server.ts=20sends=20CSP=20f?= =?UTF-8?q?rame-ancestors/object-src/base-uri,=20X-Frame-Options,=20=20=20?= =?UTF-8?q?nosniff,=20Referrer-Policy=20and=20HSTS=20on=20every=20file=20i?= =?UTF-8?q?t=20serves=20-=20RichTextEditor=20sanitizes=20HTML=20it=20loads?= =?UTF-8?q?=20from=20props=20(saved=20answers)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.ts | 20 ++++++++++++++++---- src/components/ui/rich-text-editor.tsx | 5 ++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/server.ts b/server.ts index b73a328..2351fbc 100644 --- a/server.ts +++ b/server.ts @@ -60,13 +60,25 @@ type Bridge = { queue: (string | Uint8Array)[]; }; +// Sent on every file this server answers itself (the SPA shell and its +// assets). Framing is limited to our own origin: the app embeds same-origin +// file previews, but no other site may frame it (clickjacking). HSTS is only +// honoured over HTTPS, i.e. behind the TLS-terminating proxy in production. +const SECURITY_HEADERS: Record = { + "Content-Security-Policy": "frame-ancestors 'self'; object-src 'none'; base-uri 'self'", + "X-Frame-Options": "SAMEORIGIN", + "X-Content-Type-Options": "nosniff", + "Referrer-Policy": "strict-origin-when-cross-origin", + "Strict-Transport-Security": "max-age=31536000", +}; + async function serveStatic(pathname: string): Promise { // No path traversal out of dist. - if (pathname.includes("..")) return new Response("bad request", { status: 400 }); + if (pathname.includes("..")) return new Response("bad request", { status: 400, headers: SECURITY_HEADERS }); const rel = pathname === "/" ? "/index.html" : pathname; const file = Bun.file(DIST + rel); if (await file.exists()) { - const headers: Record = {}; + const headers: Record = { ...SECURITY_HEADERS }; // Vite emits content-hashed files under /assets — safe to cache forever. if (rel.startsWith("/assets/")) headers["Cache-Control"] = "public, max-age=31536000, immutable"; else if (rel === "/index.html") headers["Cache-Control"] = "no-store"; @@ -75,9 +87,9 @@ async function serveStatic(pathname: string): Promise { // A missing hashed asset must stay a 404. Returning index.html here makes // browsers report a misleading "failed to fetch dynamically imported // module" because they receive HTML where JavaScript was requested. - if (rel.startsWith("/assets/")) return new Response("asset not found", { status: 404 }); + if (rel.startsWith("/assets/")) return new Response("asset not found", { status: 404, headers: SECURITY_HEADERS }); // SPA fallback: unknown non-asset paths are client routes, serve the shell. - return new Response(Bun.file(`${DIST}/index.html`), { headers: { "Cache-Control": "no-store" } }); + return new Response(Bun.file(`${DIST}/index.html`), { headers: { ...SECURITY_HEADERS, "Cache-Control": "no-store" } }); } async function proxyHttp(req: Request, url: URL): Promise { diff --git a/src/components/ui/rich-text-editor.tsx b/src/components/ui/rich-text-editor.tsx index 376ce82..32c6fad 100644 --- a/src/components/ui/rich-text-editor.tsx +++ b/src/components/ui/rich-text-editor.tsx @@ -1,5 +1,6 @@ import { createSignal, createEffect } from "solid-js"; import { cn } from "@/lib/cn"; +import { sanitizeRichText } from "@/lib/rich-text"; import { useT } from "@/stores/preferences-context"; interface RichTextEditorProps { @@ -22,7 +23,9 @@ export function RichTextEditor(props: RichTextEditorProps) { if (!props.value) { editorRef.innerHTML = ""; } else if (editorRef.innerHTML !== props.value) { - editorRef.innerHTML = props.value; + // The value can come back from the server (a saved homework answer), + // so it goes through the same sanitizer as every other stored HTML. + editorRef.innerHTML = sanitizeRichText(props.value); } } });