diff --git a/packages/void/src/router/compile.ts b/packages/void/src/router/compile.ts index 16a9eec5b..9e7926be2 100644 --- a/packages/void/src/router/compile.ts +++ b/packages/void/src/router/compile.ts @@ -1216,7 +1216,11 @@ function compilePageRoutes(lines: string[], scan: ScanResult, pages: PagesCompil // Try static assets for any path (not just dot-containing last segments) lines.push(` const assetRes = await fetchPagesAsset(c);`); lines.push(` if (assetRes.status !== 404) return assetRes;`); - // If the last path segment contains a dot (e.g. style.css, .env, .htaccess), + // Dot-prefixed path segments (/.well-known/, /.env, /.git/) are never pages — 404 + lines.push(` if (path.includes("/.")) {`); + lines.push(` return serveCustomErrorPage(c, 404, "Not Found");`); + lines.push(` }`); + // If the last path segment contains a dot (e.g. style.css, favicon.ico), // treat it as a file request — serve 404 so the browser doesn't parse HTML as a file lines.push(` const lastSeg = path.substring(path.lastIndexOf("/") + 1);`); lines.push(` if (lastSeg.includes(".")) {`); @@ -1229,10 +1233,19 @@ function compilePageRoutes(lines: string[], scan: ScanResult, pages: PagesCompil lines.push(` url.pathname = path.slice(0, -1);`); lines.push(` return Response.redirect(url.href, 301);`); lines.push(` }`); - // SPA fallback — render the index page; errors propagate to app.onError (500) + // All page routes are registered as explicit Hono handlers above. + // Any path reaching here is genuinely unknown — try rendering the index + // page (for a nice client-side 404 UI) but always return 404 status. + // The try/catch hides server errors from handlePageGet (e.g. index + // component crashing on missing loader props). + lines.push(` try {`); lines.push( - ` return handlePageGet(undefined, undefined, c, "index", renderPage, getPagesAssetTags, __voidHeadConfig);`, + ` const res = await handlePageGet(undefined, undefined, c, "index", renderPage, getPagesAssetTags, __voidHeadConfig);`, ); + lines.push(` return new Response(res.body, { status: 404, headers: res.headers });`); + lines.push(` } catch {`); + lines.push(` return serveCustomErrorPage(c, 404, "Not Found");`); + lines.push(` }`); lines.push(`});`); lines.push(""); } diff --git a/packages/void/test/integration/kitchen-sink.test.ts b/packages/void/test/integration/kitchen-sink.test.ts index 83a7cdc59..3f5ad6db4 100644 --- a/packages/void/test/integration/kitchen-sink.test.ts +++ b/packages/void/test/integration/kitchen-sink.test.ts @@ -435,6 +435,76 @@ describe("playground/kitchen-sink", () => { expect(res.status).toBe(404); }); + it("returns 404 for dot-prefixed directory segments like /.well-known/traffic-advice", async () => { + const res = await fetch(`${BASE}/.well-known/traffic-advice`); + expect(res.status).toBe(404); + }); + + it("returns 404 for /.git/HEAD", async () => { + const res = await fetch(`${BASE}/.git/HEAD`); + expect(res.status).toBe(404); + }); + + it("returns 404 for /.htaccess", async () => { + const res = await fetch(`${BASE}/.htaccess`); + expect(res.status).toBe(404); + }); + + // File extension paths caught by last-segment dot check + it("returns 404 for /wp-login.php", async () => { + const res = await fetch(`${BASE}/wp-login.php`); + expect(res.status).toBe(404); + }); + + it("returns 404 for /xmlrpc.php", async () => { + const res = await fetch(`${BASE}/xmlrpc.php`); + expect(res.status).toBe(404); + }); + + it("returns 404 for /robots.txt", async () => { + const res = await fetch(`${BASE}/robots.txt`); + expect(res.status).toBe(404); + }); + + it("returns 404 for /favicon.ico", async () => { + const res = await fetch(`${BASE}/favicon.ico`); + expect(res.status).toBe(404); + }); + + // Bot probes — single-segment paths without dots, caught by notFound fallback + it("returns 404 for /wp-admin", async () => { + const res = await fetch(`${BASE}/wp-admin`); + expect(res.status).toBe(404); + }); + + it("returns 404 for /phpmyadmin", async () => { + const res = await fetch(`${BASE}/phpmyadmin`); + expect(res.status).toBe(404); + }); + + // Multi-segment unknown paths + it("returns 404 for /foo/bar/baz", async () => { + const res = await fetch(`${BASE}/foo/bar/baz`); + expect(res.status).toBe(404); + }); + + it("returns 404 for /wp-content/plugins/exploit", async () => { + const res = await fetch(`${BASE}/wp-content/plugins/exploit`); + expect(res.status).toBe(404); + }); + + // API prefix returns 404 text (not HTML) + it("returns 404 for unknown API routes", async () => { + const res = await fetch(`${BASE}/api/nonexistent`); + expect(res.status).toBe(404); + }); + + // Non-GET methods to unknown paths + it("returns 404 for POST to unknown paths", async () => { + const res = await fetch(`${BASE}/nonexistent`, { method: "POST" }); + expect(res.status).toBe(404); + }); + it("redirects trailing slash to non-trailing-slash path", async () => { const res = await fetch(`${BASE}/wp-content/`, { redirect: "manual" }); expect(res.status).toBe(301); diff --git a/packages/void/test/unit/pages-compile.test.ts b/packages/void/test/unit/pages-compile.test.ts index 580ace257..7513f0271 100644 --- a/packages/void/test/unit/pages-compile.test.ts +++ b/packages/void/test/unit/pages-compile.test.ts @@ -201,6 +201,17 @@ describe("compileRouteTable with pages", () => { expect(code).toContain("app.notFound("); // Should skip API routes expect(code).toContain('path.startsWith("/api/")'); + // Dot-prefixed segments (/.well-known, /.env, /.git) → 404 + expect(code).toContain('path.includes("/.")'); + // Last segment with dot (style.css, wp-login.php) → 404 + expect(code).toContain('lastSeg.includes(".")'); + // Trailing slash redirect + expect(code).toContain('path.endsWith("/")'); + // notFound fallback: try render index as 404, catch errors + expect(code).toContain("try {"); + expect(code).toContain("{ status: 404, headers: res.headers }"); + expect(code).toContain("} catch {"); + expect(code).toContain('serveCustomErrorPage(c, 404, "Not Found")'); }); it("embeds the default self-contained error page helper", () => {