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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,9 @@ volume is storage that lives outside the container and is mounted into it at `/d
<summary>How the server serves the web app</summary>

With `WEB_DIR` set, the server answers every path outside `/api` from that directory. Paths without a file
(`/invite/<token>`, `/admin/apps/1`) get `index.html`, so links and page reloads work. Unknown `/api` paths get the
API's JSON `404`. `index.html` has `<base href="/">`: the production build's loader and webpack chunks load further
(`/invite/<token>`, `/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 `<base href="/">`: the production build's loader and webpack chunks load further
files with relative URLs, which on `/invite/<token>` 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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/<token>`, `/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/<token>`, `/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/<token>`). */
internal fun looksLikeFile(path: String): Boolean = '.' in path.substringAfterLast('/')
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,9 +49,29 @@ class WebAppRoutesTest {
val response = client.get(path)
assertEquals(HttpStatusCode.OK, response.status, path)
assertEquals("<html>index</html>", 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")
Expand Down
Loading