🔒 security headers and sanitized editor input
Browse files at this commita3ac38ffcea8d6cb8a57da204920da9da7bcb1fbParent: b84d860- fix
- 🔒 security headers and sanitized editor input
2 changed files +20 -5
- server.ts +16 -4
- src/components/ui/rich-text-editor.tsx +4 -1
server.tsmodified+16 -4
| @@ -60,13 +60,25 @@ type Bridge = { | ||
| 60 | 60 | queue: (string | Uint8Array)[]; |
| 61 | 61 | }; |
| 62 | 62 | |
| 63 | +// Sent on every file this server answers itself (the SPA shell and its | |
| 64 | +// assets). Framing is limited to our own origin: the app embeds same-origin | |
| 65 | +// file previews, but no other site may frame it (clickjacking). HSTS is only | |
| 66 | +// honoured over HTTPS, i.e. behind the TLS-terminating proxy in production. | |
| 67 | +const SECURITY_HEADERS: Record<string, string> = { | |
| 68 | + "Content-Security-Policy": "frame-ancestors 'self'; object-src 'none'; base-uri 'self'", | |
| 69 | + "X-Frame-Options": "SAMEORIGIN", | |
| 70 | + "X-Content-Type-Options": "nosniff", | |
| 71 | + "Referrer-Policy": "strict-origin-when-cross-origin", | |
| 72 | + "Strict-Transport-Security": "max-age=31536000", | |
| 73 | +}; | |
| 74 | + | |
| 63 | 75 | async function serveStatic(pathname: string): Promise<Response> { |
| 64 | 76 | // No path traversal out of dist. |
| 65 | - if (pathname.includes("..")) return new Response("bad request", { status: 400 }); | |
| 77 | + if (pathname.includes("..")) return new Response("bad request", { status: 400, headers: SECURITY_HEADERS }); | |
| 66 | 78 | const rel = pathname === "/" ? "/index.html" : pathname; |
| 67 | 79 | const file = Bun.file(DIST + rel); |
| 68 | 80 | if (await file.exists()) { |
| 69 | - const headers: Record<string, string> = {}; | |
| 81 | + const headers: Record<string, string> = { ...SECURITY_HEADERS }; | |
| 70 | 82 | // Vite emits content-hashed files under /assets — safe to cache forever. |
| 71 | 83 | if (rel.startsWith("/assets/")) headers["Cache-Control"] = "public, max-age=31536000, immutable"; |
| 72 | 84 | else if (rel === "/index.html") headers["Cache-Control"] = "no-store"; |
| @@ -75,9 +87,9 @@ async function serveStatic(pathname: string): Promise<Response> { | ||
| 75 | 87 | // A missing hashed asset must stay a 404. Returning index.html here makes |
| 76 | 88 | // browsers report a misleading "failed to fetch dynamically imported |
| 77 | 89 | // module" because they receive HTML where JavaScript was requested. |
| 78 | - if (rel.startsWith("/assets/")) return new Response("asset not found", { status: 404 }); | |
| 90 | + if (rel.startsWith("/assets/")) return new Response("asset not found", { status: 404, headers: SECURITY_HEADERS }); | |
| 79 | 91 | // SPA fallback: unknown non-asset paths are client routes, serve the shell. |
| 80 | - return new Response(Bun.file(`${DIST}/index.html`), { headers: { "Cache-Control": "no-store" } }); | |
| 92 | + return new Response(Bun.file(`${DIST}/index.html`), { headers: { ...SECURITY_HEADERS, "Cache-Control": "no-store" } }); | |
| 81 | 93 | } |
| 82 | 94 | |
| 83 | 95 | async function proxyHttp(req: Request, url: URL): Promise<Response> { |
src/components/ui/rich-text-editor.tsxmodified+4 -1
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | import { createSignal, createEffect } from "solid-js"; |
| 2 | 2 | import { cn } from "@/lib/cn"; |
| 3 | +import { sanitizeRichText } from "@/lib/rich-text"; | |
| 3 | 4 | import { useT } from "@/stores/preferences-context"; |
| 4 | 5 | |
| 5 | 6 | interface RichTextEditorProps { |
| @@ -22,7 +23,9 @@ export function RichTextEditor(props: RichTextEditorProps) { | ||
| 22 | 23 | if (!props.value) { |
| 23 | 24 | editorRef.innerHTML = ""; |
| 24 | 25 | } else if (editorRef.innerHTML !== props.value) { |
| 25 | - editorRef.innerHTML = props.value; | |
| 26 | + // The value can come back from the server (a saved homework answer), | |
| 27 | + // so it goes through the same sanitizer as every other stored HTML. | |
| 28 | + editorRef.innerHTML = sanitizeRichText(props.value); | |
| 26 | 29 | } |
| 27 | 30 | } |
| 28 | 31 | }); |