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
19 changes: 16 additions & 3 deletions packages/void/src/router/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(".")) {`);
Expand All @@ -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("");
}
Expand Down
70 changes: 70 additions & 0 deletions packages/void/test/integration/kitchen-sink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions packages/void/test/unit/pages-compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Loading