From 33b03f181138ae420a4e9c6793b765fec728f182 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 19:51:18 +0000 Subject: [PATCH] Answer missing files with 404 instead of index.html Only page paths (no file extension, like /invite/) fall back to index.html now. A missing file such as /webApp.js.map got index.html, which Safari reported as a source map parse error; scripts or Wasm files missing after a release would fail the same confusing way. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MJfCxi13ynQCqKcyND1muB --- README.md | 5 ++-- .../server/routes/WebAppRoutes.kt | 29 +++++++++++++++---- .../server/routes/WebAppRoutesTest.kt | 21 ++++++++++++++ 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 51234cc..3eb650d 100644 --- a/README.md +++ b/README.md @@ -307,8 +307,9 @@ volume is storage that lives outside the container and is mounted into it at `/d How the server serves the web app With `WEB_DIR` set, the server answers every path outside `/api` from that directory. Paths without a file -(`/invite/`, `/admin/apps/1`) get `index.html`, so links and page reloads work. Unknown `/api` paths get the -API's JSON `404`. `index.html` has ``: the production build's loader and webpack chunks load further +(`/invite/`, `/admin/apps/1`) get `index.html`, so links and page reloads work. A missing *file*, i.e. a path whose +last segment has an extension (`/webApp.js.map`), gets a plain `404` instead of `index.html`, so a browser never +tries to read HTML as a script or Wasm file. Unknown `/api` paths get the API's JSON `404`. `index.html` has ``: the production build's loader and webpack chunks load further files with relative URLs, which on `/invite/` would otherwise point to `/invite/originWasmWebApp.js` and get `index.html` instead of the script. The files are sent gzip-compressed (API responses are not: they contain secrets, and compressing secrets together with attacker-controlled data can leak them, see BREACH) with `Cache-Control: no-cache`, so a diff --git a/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/routes/WebAppRoutes.kt b/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/routes/WebAppRoutes.kt index 83a5af2..bfd281d 100644 --- a/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/routes/WebAppRoutes.kt +++ b/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/routes/WebAppRoutes.kt @@ -1,25 +1,44 @@ package at.techbee.gplaypromocodeprovider.server.routes import io.ktor.http.CacheControl +import io.ktor.http.HttpHeaders +import io.ktor.http.HttpStatusCode import io.ktor.server.http.content.staticFiles +import io.ktor.server.response.header +import io.ktor.server.response.respond +import io.ktor.server.response.respondFile import io.ktor.server.routing.Route import java.io.File /** * Serves the compiled web app (`index.html`, scripts, Wasm files) from [dir]. * - * Every path without a file (e.g. `/invite/`, `/admin/apps/1`) gets `index.html`, so deep - * links and page reloads work: the web app picks its screen from the path. Unknown `/api` paths don't - * get here, they have their own 404 (see `Application.module`). + * A page path without a file (e.g. `/invite/`, `/admin/apps/1`) gets `index.html`, so deep + * links and page reloads work: the web app picks its screen from the path. A missing *file* (a path + * whose last segment has an extension, like `/webApp.js.map`) gets a plain `404` instead: answering it + * with `index.html` makes the browser try to parse HTML as a script, source map or Wasm file, which + * hides the real problem. Unknown `/api` paths don't get here, they have their own 404 (see + * `Application.module`). * * The file names of a build don't change between releases (`webApp.js`, ...), so the browser must * not use a cached copy without asking: `Cache-Control: no-cache` makes it revalidate each time, and * the `ConditionalHeaders` plugin answers `304 Not Modified` when the file is unchanged. */ fun Route.webAppRoutes(dir: File) { - require(dir.resolve("index.html").isFile) { "WEB_DIR ${dir.absolutePath} contains no index.html" } + val index = dir.resolve("index.html") + require(index.isFile) { "WEB_DIR ${dir.absolutePath} contains no index.html" } staticFiles("/", dir) { - default("index.html") cacheControl { listOf(CacheControl.NoCache(null)) } + fallback { requestedPath, call -> + if (looksLikeFile(requestedPath)) { + call.respond(HttpStatusCode.NotFound) + } else { + call.response.header(HttpHeaders.CacheControl, CacheControl.NoCache(null).toString()) + call.respondFile(index) + } + } } } + +/** Whether the last path segment has a file extension (`app.wasm`), unlike page paths (`/invite/`). */ +internal fun looksLikeFile(path: String): Boolean = '.' in path.substringAfterLast('/') diff --git a/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/routes/WebAppRoutesTest.kt b/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/routes/WebAppRoutesTest.kt index 126f021..0337ed3 100644 --- a/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/routes/WebAppRoutesTest.kt +++ b/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/routes/WebAppRoutesTest.kt @@ -17,6 +17,7 @@ import java.io.File import java.nio.file.Files import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertFalse import kotlin.test.assertNotNull import kotlin.test.assertNull import kotlin.test.assertTrue @@ -48,9 +49,29 @@ class WebAppRoutesTest { val response = client.get(path) assertEquals(HttpStatusCode.OK, response.status, path) assertEquals("index", response.bodyAsText(), path) + assertEquals("no-cache", response.headers[HttpHeaders.CacheControl], path) } } + @Test + fun missingFilesGetNotFoundInsteadOfIndexHtml() = webTest { + for (path in listOf("/webApp.js.map", "/missing.wasm", "/invite/x/old-chunk.js")) { + val response = client.get(path) + assertEquals(HttpStatusCode.NotFound, response.status, path) + assertTrue("index" !in response.bodyAsText(), path) + } + } + + @Test + fun invitePathsAreNeverTakenForFiles() { + // Invite tokens are URL-safe Base64 (letters, digits, '-' and '_'): no dot, so never a "file". + assertFalse(looksLikeFile("/invite/Ab-3_xYz09")) + assertFalse(looksLikeFile("/admin/apps/12")) + assertFalse(looksLikeFile("/")) + assertTrue(looksLikeFile("/webApp.js.map")) + assertTrue(looksLikeFile("/composeResources/font.ttf")) + } + @Test fun servesFilesWithTheirContentType() = webTest { val script = client.get("/webApp.js")