forge
Explore
Log in

Don't you dare go Hollow

🔒 security headers and sanitized editor input

Browse files at this commit

a3ac38ffcea8d6cb8a57da204920da9da7bcb1fbBurak BoduroğluParent: b84d860
  • fix
    • 🔒 security headers and sanitized editor input
Show full message
fix: 🔒 security headers and sanitized editor input
- server.ts sends CSP frame-ancestors/object-src/base-uri, X-Frame-Options,
  nosniff, Referrer-Policy and HSTS on every file it serves
- RichTextEditor sanitizes HTML it loads from props (saved answers)
server.tsmodified+16 -4
@@ -60,13 +60,25 @@ type Bridge = {
60 queue: (string | Uint8Array)[];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 async function serveStatic(pathname: string): Promise<Response> {75 async function serveStatic(pathname: string): Promise<Response> {
64 // No path traversal out of dist.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 const rel = pathname === "/" ? "/index.html" : pathname;78 const rel = pathname === "/" ? "/index.html" : pathname;
67 const file = Bun.file(DIST + rel);79 const file = Bun.file(DIST + rel);
68 if (await file.exists()) {80 if (await file.exists()) {
69- const headers: Record<string, string> = {};81+ const headers: Record<string, string> = { ...SECURITY_HEADERS };
70 // Vite emits content-hashed files under /assets — safe to cache forever.82 // Vite emits content-hashed files under /assets — safe to cache forever.
71 if (rel.startsWith("/assets/")) headers["Cache-Control"] = "public, max-age=31536000, immutable";83 if (rel.startsWith("/assets/")) headers["Cache-Control"] = "public, max-age=31536000, immutable";
72 else if (rel === "/index.html") headers["Cache-Control"] = "no-store";84 else if (rel === "/index.html") headers["Cache-Control"] = "no-store";
@@ -75,9 +87,9 @@ async function serveStatic(pathname: string): Promise<Response> {
75 // A missing hashed asset must stay a 404. Returning index.html here makes87 // A missing hashed asset must stay a 404. Returning index.html here makes
76 // browsers report a misleading "failed to fetch dynamically imported88 // browsers report a misleading "failed to fetch dynamically imported
77 // module" because they receive HTML where JavaScript was requested.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 // SPA fallback: unknown non-asset paths are client routes, serve the shell.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 async function proxyHttp(req: Request, url: URL): Promise<Response> {95 async function proxyHttp(req: Request, url: URL): Promise<Response> {
src/components/ui/rich-text-editor.tsxmodified+4 -1
@@ -1,5 +1,6 @@
1 import { createSignal, createEffect } from "solid-js";1 import { createSignal, createEffect } from "solid-js";
2 import { cn } from "@/lib/cn";2 import { cn } from "@/lib/cn";
3+import { sanitizeRichText } from "@/lib/rich-text";
3 import { useT } from "@/stores/preferences-context";4 import { useT } from "@/stores/preferences-context";
4 5
5 interface RichTextEditorProps {6 interface RichTextEditorProps {
@@ -22,7 +23,9 @@ export function RichTextEditor(props: RichTextEditorProps) {
22 if (!props.value) {23 if (!props.value) {
23 editorRef.innerHTML = "";24 editorRef.innerHTML = "";
24 } else if (editorRef.innerHTML !== props.value) {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 });