diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a3fcf7e..f81e466 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -74,18 +74,21 @@ jobs: permissions: contents: read packages: write + # The actions are pinned to commits, not to tags like @v3: this job may write to the registry + # every instance deploys from, and a tag can be moved to other code. To update an action, take + # the commit of its newer release (shown on its releases page) and adjust the comment. steps: - - uses: actions/checkout@v5 - - uses: docker/setup-buildx-action@v3 + - uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5.1.0 + - uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0 # The run's own GitHub token may push to the repository's packages: no secret to set up. - - uses: docker/login-action@v3 + - uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} # Same build as the "Build Docker image" job; its cached layers make this quick. Tagged # "latest" (what fly.toml deploys) and with the commit (to deploy or go back to a fixed version). - - uses: docker/build-push-action@v6 + - uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2 with: context: . push: true diff --git a/CLAUDE.md b/CLAUDE.md index 53649d6..fbe2116 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -135,6 +135,9 @@ it unless `.dockerignore` excludes them). (`Failure` by its `ApiError`, `NetworkError`); the client never throws except on cancellation. - Keep business logic (code allocation, validation) out of Ktor routes and Compose UI, in plain classes that can be unit tested (server: package `server.domain`; routes only map HTTP to it). +- Server database access goes through `db.query { }` (`server/.../db/Query.kt`): a transaction on `Dispatchers.IO`, + so blocking JDBC never runs on a request thread. Repository functions are therefore `suspend`; tests call them in + `runBlocking`. - Inject `kotlin.time.Clock` wherever "now" matters, so tests can control time. - Never install Ktor's `CallLogging` or log request paths: they contain invite tokens. The `Exposed` logger is off because it logs statement values (codes). Don't log exception messages of @@ -144,6 +147,8 @@ it unless `.dockerignore` excludes them). including a concurrency test for rule 1. - Use `kotlinx-datetime` / `kotlin.time.Instant` for timestamps and store them in UTC. - Add dependencies through `gradle/libs.versions.toml`, not as inline version strings. +- In CI jobs that can publish or deploy (write permissions, secrets), pin actions to a commit with the version in a + comment (`uses: owner/action@ # v1.2.3`), not to a tag or branch. - Follow the official Kotlin code style (`kotlin.code.style=official`). - Keep commits small and focused, with descriptive messages. diff --git a/README.md b/README.md index 3eb650d..2ee81f3 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,8 @@ AVAILABLE ──(user requests code)──────▶ CONSUMED | Build JDK | Java 21, any vendor (Gradle daemon criteria). Not pinned to Azul so cloud AI sessions can use the preinstalled OpenJDK. | | API models | Separate KMP module `api` (jvm, js, wasmJs), only kotlinx.serialization. The server doesn't pull in Compose. | | Admin login | `POST /api/admin/login` with username and password. On success the server creates a session (kept in memory, 12 hours) and sets a random session id as an `HttpOnly`, `SameSite=Strict` cookie (also `Secure` unless `COOKIE_SECURE=false`). Logout ends the session. A server restart logs the admin out. | -| Database access | [Exposed](https://www.jetbrains.com/exposed/) DSL on the SQLite JDBC driver. Migrations are plain SQL versioned with `PRAGMA user_version` (no extra migration library). Timestamps are stored as UTC epoch milliseconds. | +| Database access | [Exposed](https://www.jetbrains.com/exposed/) DSL on the SQLite JDBC driver. Migrations are plain SQL versioned with `PRAGMA user_version` (no extra migration library). Timestamps are stored as UTC epoch milliseconds. The repositories' functions are `suspend` and run their transaction on `Dispatchers.IO` (`db.query { }`): JDBC blocks, and the server has only one or two request threads on a small machine, which also answer the health check. | +| Request bodies | Capped at 64 KB, 2 MB for a batch upload (the largest valid upload is about 1.3 MB), with Ktor's `RequestBodyLimit`. Bodies are read into memory, and login is open to anyone, so without a cap one huge request could exhaust the heap. | | Atomic hand-out | One SQL statement: `UPDATE promo_code SET consumed_at = ? WHERE consumed_at IS NULL AND id = (SELECT … LIMIT 1) RETURNING code`. Transactions use `BEGIN IMMEDIATE` and a busy timeout, so concurrent writers queue up instead of failing. | | Expiry | A batch's `expiresAt` is an exact instant (UTC). From that instant on, its remaining codes count as expired. In the admin UI the admin picks a day; the codes are valid until the end of that day (23:59:59) in the admin's browser time zone. | | API client | `ApiClient` in `shared` (package `client`), on the Ktor client with the browser's `fetch` for JS and Wasm. Every call returns an `ApiResult` (`Success`, `Failure` with status and `ApiError`, `NetworkError`) instead of throwing, so the UI has to handle each case. JSON is parsed leniently (`ignoreUnknownKeys`, `coerceInputValues`), so an older web app keeps working against a newer server. Path segments are encoded including `/`, so an invite token can't change the request path. `webApp/main.kt` creates one client for the page's lifetime and passes it to `App`; it is never closed. On the `jvm` target (previews, tests) there is no default engine, so one must be passed. | @@ -153,6 +154,9 @@ proxy), so the web app treats it as a network error: "The server can't be reache whole truth: a `504` can come after the server handed out the code. The invite page then says the server didn't answer and that a code may have been handed out without reaching the user (it is lost; the user can request another one). +A request body that can't be read gets `invalid_request` with `400` (malformed JSON), `415` (missing, or not +`application/json`) or `413` (larger than 64 KB, 2 MB for a batch upload). + User endpoints (scoped by the invite token; an unknown token, an unknown batch and a batch of another app all give the same `404`): @@ -501,6 +505,10 @@ every pull request and every push to `main`: `...:`. The run's own GitHub token may do that, so there is no secret to set up. Forks don't publish. CI never deploys: Fly.io instances fetch the image with `fly deploy` (see [Deploying to Fly.io](#deploying-to-flyio)). +The publish job's actions are pinned to commits (`docker/login-action@ # v3.7.0`), not to tags like `@v3`: +it may write to the registry every instance deploys from, and a tag can be moved to other code. To update one, take +the commit of a newer release from the action's releases page. The other jobs can't publish anything and use tags. + The workflow also has a *Run workflow* button (Actions tab, `workflow_dispatch`): it runs the same jobs for a branch without a new commit, and publishes when run on `main`. @@ -537,6 +545,14 @@ Each step is one GitHub issue, one AI conversation and one pull request (see [CL `RUN find /app/web -name '*.map' -delete` in the runtime stage would drop them, e.g. if the repository becomes private. - **Suggestion**: rate-limit `POST /api/admin/login` (Ktor `RateLimit` plugin) against password guessing, keyed by Fly.io's `Fly-Client-IP` (`X-Forwarded-For` can be faked). The invite endpoints could be rate-limited too. +- **Open from the code review after step #7** (fixed there: unreadable bodies answered `500`, blocking database + calls on request threads, no body size limit, unpinned actions in the publish job): + - Logging out while a page request answers `401` keeps the page's state (e.g. pasted codes) for the next login, + because the session then counts as expired instead of logged out. + - Code parsing trims whitespace only. A byte order mark or zero-width space from a CSV or spreadsheet stays in the + code, which then doesn't redeem and doesn't match the same code without it as a duplicate. + - The app page computes its statistics twice per load (`GET /apps/{id}` and `GET /apps/{id}/batches`). + - Every transaction opens a new SQLite connection; a small connection pool would reuse them. - **Accepted for now** (decided after step #5, no action planned): - Received codes are lost when the user reloads or closes the invite page (the page says so). Keeping them in the browser's `localStorage` per invite token would make that safer. diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 4f606a4..b053823 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -35,6 +35,7 @@ ktor-server-auth = { module = "io.ktor:ktor-server-auth", version.ref = "ktor" } ktor-server-compression = { module = "io.ktor:ktor-server-compression", version.ref = "ktor" } ktor-server-conditionalHeaders = { module = "io.ktor:ktor-server-conditional-headers", version.ref = "ktor" } ktor-server-forwardedHeader = { module = "io.ktor:ktor-server-forwarded-header", version.ref = "ktor" } +ktor-server-bodyLimit = { module = "io.ktor:ktor-server-body-limit", version.ref = "ktor" } ktor-server-testHost = { module = "io.ktor:ktor-server-test-host", version.ref = "ktor" } ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" } ktor-client-js = { module = "io.ktor:ktor-client-js", version.ref = "ktor" } diff --git a/server/build.gradle.kts b/server/build.gradle.kts index 825b748..37b6753 100644 --- a/server/build.gradle.kts +++ b/server/build.gradle.kts @@ -23,6 +23,7 @@ dependencies { implementation(libs.ktor.server.compression) implementation(libs.ktor.server.conditionalHeaders) implementation(libs.ktor.server.forwardedHeader) + implementation(libs.ktor.server.bodyLimit) implementation(libs.ktor.serialization.kotlinxJson) implementation(libs.exposed.core) implementation(libs.exposed.jdbc) diff --git a/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/Application.kt b/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/Application.kt index 028fdea..66e6646 100644 --- a/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/Application.kt +++ b/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/Application.kt @@ -6,6 +6,7 @@ import at.techbee.gplaypromocodeprovider.server.domain.AppRepository import at.techbee.gplaypromocodeprovider.server.domain.BatchRepository import at.techbee.gplaypromocodeprovider.server.domain.CodeAllocator import at.techbee.gplaypromocodeprovider.server.domain.ValidationException +import at.techbee.gplaypromocodeprovider.server.routes.MAX_UPLOAD_BODY_BYTES import at.techbee.gplaypromocodeprovider.server.routes.adminRoutes import at.techbee.gplaypromocodeprovider.server.routes.installAdminAuth import at.techbee.gplaypromocodeprovider.server.routes.respondNotFound @@ -16,6 +17,11 @@ import io.ktor.serialization.kotlinx.json.json import io.ktor.server.application.Application import io.ktor.server.application.install import io.ktor.server.plugins.BadRequestException +import io.ktor.server.plugins.CannotTransformContentToTypeException +import io.ktor.server.plugins.ContentTransformationException +import io.ktor.server.plugins.PayloadTooLargeException +import io.ktor.server.plugins.UnsupportedMediaTypeException +import io.ktor.server.plugins.bodylimit.RequestBodyLimit import io.ktor.server.plugins.compression.Compression import io.ktor.server.plugins.compression.condition import io.ktor.server.plugins.compression.gzip @@ -35,6 +41,9 @@ import java.io.File import java.sql.SQLException import kotlin.time.Clock +/** Largest request body accepted, except for batch uploads ([MAX_UPLOAD_BODY_BYTES]). */ +const val MAX_REQUEST_BODY_BYTES = 64L * 1024 + private val log = LoggerFactory.getLogger("at.techbee.gplaypromocodeprovider.server") /** @@ -69,6 +78,20 @@ fun Application.module(config: ServerConfig, db: Database, clock: Clock = Clock. exception { call, _ -> call.respond(HttpStatusCode.BadRequest, ErrorResponse(ApiError.INVALID_REQUEST, "Malformed request")) } + // The body couldn't be read: missing, not JSON, or too large. These aren't BadRequestExceptions; + // without their own handlers they'd end up below as 500 with an error log. + exception { call, _ -> + call.respond(HttpStatusCode.UnsupportedMediaType, ErrorResponse(ApiError.INVALID_REQUEST, "Expected a JSON body")) + } + exception { call, _ -> + call.respond(HttpStatusCode.UnsupportedMediaType, ErrorResponse(ApiError.INVALID_REQUEST, "Expected a JSON body")) + } + exception { call, _ -> + call.respond(HttpStatusCode.PayloadTooLarge, ErrorResponse(ApiError.INVALID_REQUEST, "The request is too large")) + } + exception { call, _ -> + call.respond(HttpStatusCode.BadRequest, ErrorResponse(ApiError.INVALID_REQUEST, "Malformed request")) + } exception { call, cause -> if (cause is SQLException) { // No message: it can contain the SQL statement including promo codes. @@ -82,6 +105,10 @@ fun Application.module(config: ServerConfig, db: Database, clock: Clock = Clock. installAdminAuth(config, clock) routing { + // Request bodies are read into memory, so their size is capped, also before anyone has logged + // in, and a huge body can't exhaust the heap. The batch upload allows more (see adminRoutes). + // Installed here, not on the application: Ktor only lets a route override a routing-level install. + install(RequestBodyLimit) { bodyLimit { MAX_REQUEST_BODY_BYTES } } route("/api") { // Used by Fly.io's health check: the server is up and answers requests. get("/health") { call.respondText("ok") } diff --git a/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/db/Query.kt b/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/db/Query.kt new file mode 100644 index 0000000..bde8b0b --- /dev/null +++ b/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/db/Query.kt @@ -0,0 +1,16 @@ +package at.techbee.gplaypromocodeprovider.server.db + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import org.jetbrains.exposed.v1.jdbc.Database +import org.jetbrains.exposed.v1.jdbc.JdbcTransaction +import org.jetbrains.exposed.v1.jdbc.transactions.transaction + +/** + * Runs [statement] in a transaction on [Dispatchers.IO]. JDBC blocks, possibly for seconds (a large + * upload, or waiting for SQLite's write lock), and must not hold up the server's request threads: + * a small machine has only one or two, which also answer the health check and serve the web app. + * Use this for every database access from request handling. + */ +suspend fun Database.query(statement: JdbcTransaction.() -> T): T = + withContext(Dispatchers.IO) { transaction(db = this@query, statement = statement) } diff --git a/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/domain/AppRepository.kt b/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/domain/AppRepository.kt index ef58008..c546bc2 100644 --- a/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/domain/AppRepository.kt +++ b/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/domain/AppRepository.kt @@ -1,18 +1,18 @@ package at.techbee.gplaypromocodeprovider.server.domain import at.techbee.gplaypromocodeprovider.server.db.Apps +import at.techbee.gplaypromocodeprovider.server.db.query import org.jetbrains.exposed.v1.core.ResultRow import org.jetbrains.exposed.v1.core.eq import org.jetbrains.exposed.v1.jdbc.Database import org.jetbrains.exposed.v1.jdbc.insert import org.jetbrains.exposed.v1.jdbc.selectAll -import org.jetbrains.exposed.v1.jdbc.transactions.transaction import org.jetbrains.exposed.v1.jdbc.update import kotlin.time.Clock class AppRepository(private val db: Database, private val clock: Clock = Clock.System) { - fun create(input: AppInput): App = transaction(db) { + suspend fun create(input: AppInput): App = db.query { val token = InviteTokens.generate() val id = Apps.insert { it[name] = input.name @@ -25,33 +25,34 @@ class AppRepository(private val db: Database, private val clock: Clock = Clock.S } /** Returns the updated app, or null if there is no app with this id. */ - fun update(id: Long, input: AppInput): App? = transaction(db) { + suspend fun update(id: Long, input: AppInput): App? = db.query { val updated = Apps.update({ Apps.id eq id }) { it[name] = input.name it[packageName] = input.packageName it[description] = input.description } - if (updated == 0) null else find(id) + if (updated == 0) null else findById(id) } /** Replaces the invite token, which revokes the old invite link. Null if there is no such app. */ - fun regenerateInviteToken(id: Long): App? = transaction(db) { + suspend fun regenerateInviteToken(id: Long): App? = db.query { val updated = Apps.update({ Apps.id eq id }) { it[inviteToken] = InviteTokens.generate() } - if (updated == 0) null else find(id) + if (updated == 0) null else findById(id) } - fun find(id: Long): App? = transaction(db) { - Apps.selectAll().where { Apps.id eq id }.singleOrNull()?.toApp() - } + suspend fun find(id: Long): App? = db.query { findById(id) } - fun findByInviteToken(token: String): App? = transaction(db) { + suspend fun findByInviteToken(token: String): App? = db.query { Apps.selectAll().where { Apps.inviteToken eq token }.singleOrNull()?.toApp() } - fun list(): List = transaction(db) { + suspend fun list(): List = db.query { Apps.selectAll().orderBy(Apps.name).map { it.toApp() } } + /** Within a transaction. */ + private fun findById(id: Long): App? = Apps.selectAll().where { Apps.id eq id }.singleOrNull()?.toApp() + private fun ResultRow.toApp() = App( id = this[Apps.id], name = this[Apps.name], diff --git a/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/domain/BatchRepository.kt b/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/domain/BatchRepository.kt index bfc8efd..2b42728 100644 --- a/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/domain/BatchRepository.kt +++ b/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/domain/BatchRepository.kt @@ -3,6 +3,7 @@ package at.techbee.gplaypromocodeprovider.server.domain import at.techbee.gplaypromocodeprovider.server.db.Apps import at.techbee.gplaypromocodeprovider.server.db.CodeBatches import at.techbee.gplaypromocodeprovider.server.db.PromoCodes +import at.techbee.gplaypromocodeprovider.server.db.query import org.jetbrains.exposed.v1.core.ResultRow import org.jetbrains.exposed.v1.core.SortOrder import org.jetbrains.exposed.v1.core.and @@ -18,7 +19,6 @@ import org.jetbrains.exposed.v1.jdbc.batchInsert import org.jetbrains.exposed.v1.jdbc.insert import org.jetbrains.exposed.v1.jdbc.select import org.jetbrains.exposed.v1.jdbc.selectAll -import org.jetbrains.exposed.v1.jdbc.transactions.transaction import org.jetbrains.exposed.v1.jdbc.update import kotlin.time.Clock import kotlin.time.Instant @@ -35,7 +35,7 @@ class BatchRepository(private val db: Database, private val clock: Clock = Clock * the app, or repeat within the upload, are skipped and reported in [UploadResult.Created.duplicates]. * Throws [ValidationException] for invalid input, including an upload without any new code. */ - fun upload(appId: Long, description: String?, expiresAt: Instant, codesText: String): UploadResult { + suspend fun upload(appId: Long, description: String?, expiresAt: Instant, codesText: String): UploadResult { val now = clock.now() if (expiresAt <= now) throw ValidationException("The expiration date must be in the future") val batchDescription = description.trimToNull()?.checkLength("Description", MAX_DESCRIPTION_LENGTH) @@ -48,8 +48,8 @@ class BatchRepository(private val db: Database, private val clock: Clock = Clock // The transaction holds SQLite's write lock from the start (BEGIN IMMEDIATE), so no // other upload can insert the same code between the check and the insert. The // UNIQUE (app_id, code) constraint is the last line of defence. - return transaction(db) { - if (Apps.select(Apps.id).where { Apps.id eq appId }.empty()) return@transaction UploadResult.AppNotFound + return db.query { + if (Apps.select(Apps.id).where { Apps.id eq appId }.empty()) return@query UploadResult.AppNotFound val existing = parsed.unique.chunked(500).flatMapTo(HashSet()) { chunk -> PromoCodes.select(PromoCodes.code) @@ -79,7 +79,7 @@ class BatchRepository(private val db: Database, private val clock: Clock = Clock } /** Batches of the app that are not expired or invalidated and still have available codes, soonest expiry first. */ - fun listAvailable(appId: Long): List = transaction(db) { + suspend fun listAvailable(appId: Long): List = db.query { val now = clock.now().toEpochMilliseconds() val availableCode = PromoCodes.select(PromoCodes.id).where { (PromoCodes.batchId eq CodeBatches.id) and PromoCodes.consumedAt.isNull() @@ -94,7 +94,7 @@ class BatchRepository(private val db: Database, private val clock: Clock = Clock } /** All batches of the app (or of all apps if [appId] is null) with their code statistics, newest first. */ - fun listWithStats(appId: Long? = null): List = transaction(db) { + suspend fun listWithStats(appId: Long? = null): List = db.query { val now = clock.now() val batches = CodeBatches.selectAll() .apply { if (appId != null) where { CodeBatches.appId eq appId } } @@ -123,7 +123,7 @@ class BatchRepository(private val db: Database, private val clock: Clock = Clock * nothing (the first time is kept). Returns the batch with its statistics, or `null` if the app * has no such batch. */ - fun invalidate(appId: Long, batchId: Long): BatchWithStats? = transaction(db) { + suspend fun invalidate(appId: Long, batchId: Long): BatchWithStats? = db.query { val now = clock.now() val ofApp = (CodeBatches.id eq batchId) and (CodeBatches.appId eq appId) // Like a hand-out, this is a write transaction (BEGIN IMMEDIATE), so the two never interleave: @@ -131,7 +131,7 @@ class BatchRepository(private val db: Database, private val clock: Clock = Clock CodeBatches.update({ ofApp and CodeBatches.invalidatedAt.isNull() }) { it[invalidatedAt] = now.toEpochMilliseconds() } - val batch = CodeBatches.selectAll().where(ofApp).singleOrNull()?.toBatch() ?: return@transaction null + val batch = CodeBatches.selectAll().where(ofApp).singleOrNull()?.toBatch() ?: return@query null val consumed = PromoCodes.select(PromoCodes.id) .where { (PromoCodes.batchId eq batchId) and PromoCodes.consumedAt.isNotNull() }.count().toInt() val remaining = PromoCodes.select(PromoCodes.id) diff --git a/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/domain/CodeAllocator.kt b/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/domain/CodeAllocator.kt index 8e1941f..8622def 100644 --- a/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/domain/CodeAllocator.kt +++ b/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/domain/CodeAllocator.kt @@ -1,13 +1,13 @@ package at.techbee.gplaypromocodeprovider.server.domain import at.techbee.gplaypromocodeprovider.server.db.CodeBatches +import at.techbee.gplaypromocodeprovider.server.db.query import org.jetbrains.exposed.v1.core.LongColumnType import org.jetbrains.exposed.v1.core.and import org.jetbrains.exposed.v1.core.eq import org.jetbrains.exposed.v1.core.statements.StatementType import org.jetbrains.exposed.v1.jdbc.Database import org.jetbrains.exposed.v1.jdbc.select -import org.jetbrains.exposed.v1.jdbc.transactions.transaction import kotlin.time.Clock sealed interface HandOutResult { @@ -21,7 +21,7 @@ sealed interface HandOutResult { /** Hands out promo codes. Each code is handed out at most once (CLAUDE.md, domain rule 1). */ class CodeAllocator(private val db: Database, private val clock: Clock = Clock.System) { - fun handOut(appId: Long, batchId: Long): HandOutResult = transaction(db) { + suspend fun handOut(appId: Long, batchId: Long): HandOutResult = db.query { val now = clock.now().toEpochMilliseconds() val long = LongColumnType() // Picks an available code of a valid, non-expired batch and marks it consumed in a single diff --git a/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/routes/AdminRoutes.kt b/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/routes/AdminRoutes.kt index 35b0fe9..f8165a4 100644 --- a/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/routes/AdminRoutes.kt +++ b/server/src/main/kotlin/at/techbee/gplaypromocodeprovider/server/routes/AdminRoutes.kt @@ -17,11 +17,14 @@ import at.techbee.gplaypromocodeprovider.server.domain.AppInput import at.techbee.gplaypromocodeprovider.server.domain.AppRepository import at.techbee.gplaypromocodeprovider.server.domain.BatchRepository import at.techbee.gplaypromocodeprovider.server.domain.BatchWithStats +import at.techbee.gplaypromocodeprovider.server.domain.CodeParser +import at.techbee.gplaypromocodeprovider.server.domain.MAX_CODES_PER_UPLOAD import at.techbee.gplaypromocodeprovider.server.domain.Stats import at.techbee.gplaypromocodeprovider.server.domain.UploadResult import io.ktor.http.HttpStatusCode import io.ktor.server.auth.authenticate import io.ktor.server.auth.principal +import io.ktor.server.plugins.bodylimit.RequestBodyLimit import io.ktor.server.request.receive import io.ktor.server.response.respond import io.ktor.server.routing.Route @@ -34,6 +37,12 @@ import io.ktor.server.sessions.sessions import io.ktor.server.sessions.set import kotlin.time.Clock +/** + * Largest batch upload body. The largest valid upload ([MAX_CODES_PER_UPLOAD] codes of + * [CodeParser.MAX_CODE_LENGTH] characters) is about 1.3 MB of JSON. + */ +const val MAX_UPLOAD_BODY_BYTES = 2L * 1024 * 1024 + fun Route.adminRoutes(config: ServerConfig, apps: AppRepository, batches: BatchRepository, clock: Clock) { val credentials = AdminCredentials(config.adminUsername, config.adminPassword) @@ -77,7 +86,7 @@ fun Route.adminRoutes(config: ServerConfig, apps: AppRepository, batches: BatchR route("/{appId}") { get { - val app = call.parameters["appId"]?.toLongOrNull()?.let(apps::find) + val app = call.parameters["appId"]?.toLongOrNull()?.let { apps.find(it) } ?: return@get call.respondNotFound() call.respond(app.toAdminApp(batches.statsOf(app.id))) } @@ -91,30 +100,34 @@ fun Route.adminRoutes(config: ServerConfig, apps: AppRepository, batches: BatchR } post("/invite-token") { - val app = call.parameters["appId"]?.toLongOrNull()?.let(apps::regenerateInviteToken) + val app = call.parameters["appId"]?.toLongOrNull()?.let { apps.regenerateInviteToken(it) } ?: return@post call.respondNotFound() call.respond(app.toAdminApp(batches.statsOf(app.id))) } get("/batches") { - val app = call.parameters["appId"]?.toLongOrNull()?.let(apps::find) + val app = call.parameters["appId"]?.toLongOrNull()?.let { apps.find(it) } ?: return@get call.respondNotFound() call.respond(batches.listWithStats(app.id).map { it.toAdminBatch() }) } - post("/batches") { - val appId = call.parameters["appId"]?.toLongOrNull() ?: return@post call.respondNotFound() - val request = call.receive() - when (val result = batches.upload(appId, request.description, request.expiresAt, request.codes)) { - is UploadResult.Created -> call.respond( - HttpStatusCode.Created, - BatchUploadResponse( - batch = BatchWithStats(result.batch, Stats(result.addedCount, 0, 0)).toAdminBatch(), - addedCount = result.addedCount, - duplicates = result.duplicates, - ), - ) - UploadResult.AppNotFound -> call.respondNotFound() + route("/batches") { + // Only the upload may send more than MAX_REQUEST_BODY_BYTES. + install(RequestBodyLimit) { bodyLimit { MAX_UPLOAD_BODY_BYTES } } + post { + val appId = call.parameters["appId"]?.toLongOrNull() ?: return@post call.respondNotFound() + val request = call.receive() + when (val result = batches.upload(appId, request.description, request.expiresAt, request.codes)) { + is UploadResult.Created -> call.respond( + HttpStatusCode.Created, + BatchUploadResponse( + batch = BatchWithStats(result.batch, Stats(result.addedCount, 0, 0)).toAdminBatch(), + addedCount = result.addedCount, + duplicates = result.duplicates, + ), + ) + UploadResult.AppNotFound -> call.respondNotFound() + } } } @@ -130,7 +143,7 @@ fun Route.adminRoutes(config: ServerConfig, apps: AppRepository, batches: BatchR } } -private fun BatchRepository.statsOf(appId: Long) = listWithStats(appId).fold(Stats.ZERO) { sum, b -> sum + b.stats } +private suspend fun BatchRepository.statsOf(appId: Long) = listWithStats(appId).fold(Stats.ZERO) { sum, b -> sum + b.stats } private fun Stats.toCodeStats() = CodeStats(available, consumed, expired, invalidated) diff --git a/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/domain/BatchRepositoryTest.kt b/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/domain/BatchRepositoryTest.kt index 6489b8c..d5781f0 100644 --- a/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/domain/BatchRepositoryTest.kt +++ b/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/domain/BatchRepositoryTest.kt @@ -2,6 +2,7 @@ package at.techbee.gplaypromocodeprovider.server.domain import at.techbee.gplaypromocodeprovider.server.TestClock import at.techbee.gplaypromocodeprovider.server.createTestDatabase +import kotlinx.coroutines.runBlocking import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -17,11 +18,11 @@ class BatchRepositoryTest { private val batches = BatchRepository(db, clock) private val allocator = CodeAllocator(db, clock) - private val app = apps.create(AppInput.of("Test app", null, null)) + private val app = runBlocking { apps.create(AppInput.of("Test app", null, null)) } private val inAMonth = clock.now() + 30.days @Test - fun uploadStoresTrimmedCodes() { + fun uploadStoresTrimmedCodes(): Unit = runBlocking { val result = batches.upload(app.id, " Beta wave 1 ", inAMonth, " A \n\nB\n \n C") val created = assertIs(result) @@ -34,7 +35,7 @@ class BatchRepositoryTest { } @Test - fun uploadSkipsAndReportsDuplicates() { + fun uploadSkipsAndReportsDuplicates(): Unit = runBlocking { batches.upload(app.id, null, inAMonth, "A\nB") val result = assertIs(batches.upload(app.id, null, inAMonth, "B\nC\nC\nD")) @@ -45,7 +46,7 @@ class BatchRepositoryTest { } @Test - fun consumedCodesAlsoCountAsDuplicates() { + fun consumedCodesAlsoCountAsDuplicates(): Unit = runBlocking { val first = assertIs(batches.upload(app.id, null, inAMonth, "A")) allocator.handOut(app.id, first.batch.id) @@ -54,7 +55,7 @@ class BatchRepositoryTest { } @Test - fun sameCodeIsAllowedForDifferentApps() { + fun sameCodeIsAllowedForDifferentApps(): Unit = runBlocking { val other = apps.create(AppInput.of("Other app", null, null)) batches.upload(app.id, null, inAMonth, "A") @@ -63,7 +64,7 @@ class BatchRepositoryTest { } @Test - fun uploadRejectsInvalidInput() { + fun uploadRejectsInvalidInput(): Unit = runBlocking { assertFailsWith { batches.upload(app.id, null, inAMonth, " \n ") } assertFailsWith { batches.upload(app.id, null, clock.now(), "A") } assertFailsWith { batches.upload(app.id, "x".repeat(MAX_DESCRIPTION_LENGTH + 1), inAMonth, "A") } @@ -71,12 +72,12 @@ class BatchRepositoryTest { } @Test - fun uploadForUnknownAppIsRejected() { + fun uploadForUnknownAppIsRejected(): Unit = runBlocking { assertEquals(UploadResult.AppNotFound, batches.upload(4711, null, inAMonth, "A")) } @Test - fun listAvailableHidesEmptyAndExpiredBatches() { + fun listAvailableHidesEmptyAndExpiredBatches(): Unit = runBlocking { val soon = assertIs(batches.upload(app.id, "soon", clock.now() + 1.days, "S")).batch val later = assertIs(batches.upload(app.id, "later", inAMonth, "L")).batch val empty = assertIs(batches.upload(app.id, "empty", inAMonth, "E")).batch @@ -88,7 +89,7 @@ class BatchRepositoryTest { } @Test - fun invalidatedBatchIsNoLongerOfferedAndCountsItsCodesAsInvalidated() { + fun invalidatedBatchIsNoLongerOfferedAndCountsItsCodesAsInvalidated(): Unit = runBlocking { val batch = assertIs(batches.upload(app.id, null, inAMonth, "A\nB\nC")).batch val other = assertIs(batches.upload(app.id, null, inAMonth, "D")).batch allocator.handOut(app.id, batch.id) @@ -105,7 +106,7 @@ class BatchRepositoryTest { } @Test - fun invalidatingAgainKeepsTheFirstTime() { + fun invalidatingAgainKeepsTheFirstTime(): Unit = runBlocking { val batch = assertIs(batches.upload(app.id, null, inAMonth, "A")).batch val first = assertNotNull(batches.invalidate(app.id, batch.id)).batch.invalidatedAt clock.advance(1.days) @@ -114,7 +115,7 @@ class BatchRepositoryTest { } @Test - fun invalidatingAnUnknownBatchOrOneOfAnotherAppFindsNothing() { + fun invalidatingAnUnknownBatchOrOneOfAnotherAppFindsNothing(): Unit = runBlocking { val other = apps.create(AppInput.of("Other app", null, null)) val otherBatch = assertIs(batches.upload(other.id, null, inAMonth, "A")).batch @@ -124,7 +125,7 @@ class BatchRepositoryTest { } @Test - fun statsSplitCodesIntoAvailableConsumedAndExpired() { + fun statsSplitCodesIntoAvailableConsumedAndExpired(): Unit = runBlocking { val expiring = assertIs(batches.upload(app.id, null, clock.now() + 1.days, "A\nB\nC")).batch val running = assertIs(batches.upload(app.id, null, inAMonth, "D\nE")).batch allocator.handOut(app.id, expiring.id) diff --git a/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/domain/CodeAllocatorTest.kt b/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/domain/CodeAllocatorTest.kt index eefb0b8..bc8d0cf 100644 --- a/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/domain/CodeAllocatorTest.kt +++ b/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/domain/CodeAllocatorTest.kt @@ -19,17 +19,17 @@ class CodeAllocatorTest { private val batches = BatchRepository(db, clock) private val allocator = CodeAllocator(db, clock) - private val app = apps.create(AppInput.of("Test app", null, null)) + private val app = runBlocking { apps.create(AppInput.of("Test app", null, null)) } - private fun upload(codes: List, appId: Long = app.id, expiresIn: kotlin.time.Duration = 30.days): Batch { + private suspend fun upload(codes: List, appId: Long = app.id, expiresIn: kotlin.time.Duration = 30.days): Batch { val result = batches.upload(appId, null, clock.now() + expiresIn, codes.joinToString("\n")) return assertIs(result).batch } - private fun statsOf(batch: Batch) = batches.listWithStats(batch.appId).single { it.batch.id == batch.id }.stats + private suspend fun statsOf(batch: Batch) = batches.listWithStats(batch.appId).single { it.batch.id == batch.id }.stats @Test - fun handOutReturnsACodeAndMarksItConsumed() { + fun handOutReturnsACodeAndMarksItConsumed(): Unit = runBlocking { val batch = upload(listOf("CODE-1", "CODE-2")) val result = allocator.handOut(app.id, batch.id) @@ -40,7 +40,7 @@ class CodeAllocatorTest { } @Test - fun handsOutEveryCodeOnceThenNoCodeAvailable() { + fun handsOutEveryCodeOnceThenNoCodeAvailable(): Unit = runBlocking { val batch = upload(listOf("A", "B", "C")) val codes = List(3) { assertIs(allocator.handOut(app.id, batch.id)).code } @@ -51,7 +51,7 @@ class CodeAllocatorTest { } @Test - fun expiredBatchHandsOutNothing() { + fun expiredBatchHandsOutNothing(): Unit = runBlocking { val batch = upload(listOf("A", "B"), expiresIn = 1.days) clock.advance(1.days) @@ -60,7 +60,7 @@ class CodeAllocatorTest { } @Test - fun batchIsUsableUntilJustBeforeItsExpiration() { + fun batchIsUsableUntilJustBeforeItsExpiration(): Unit = runBlocking { val batch = upload(listOf("A", "B"), expiresIn = 1.days) clock.advance(1.days - 1.milliseconds) @@ -71,12 +71,12 @@ class CodeAllocatorTest { } @Test - fun unknownBatchIsNotFound() { + fun unknownBatchIsNotFound(): Unit = runBlocking { assertEquals(HandOutResult.BatchNotFound, allocator.handOut(app.id, 4711)) } @Test - fun batchOfAnotherAppIsNotFound() { + fun batchOfAnotherAppIsNotFound(): Unit = runBlocking { val otherApp = apps.create(AppInput.of("Other app", null, null)) val otherBatch = upload(listOf("OTHER"), appId = otherApp.id) @@ -85,7 +85,7 @@ class CodeAllocatorTest { } @Test - fun invalidatedBatchHandsOutNothing() { + fun invalidatedBatchHandsOutNothing(): Unit = runBlocking { val batch = upload(listOf("A", "B", "C")) allocator.handOut(app.id, batch.id) @@ -96,7 +96,7 @@ class CodeAllocatorTest { } @Test - fun invalidatingDuringParallelRequestsNeverHandsOutAfterwards() = runBlocking { + fun invalidatingDuringParallelRequestsNeverHandsOutAfterwards(): Unit = runBlocking { val batch = upload(List(200) { "CODE-$it" }) val requests = List(100) { async(Dispatchers.IO) { allocator.handOut(app.id, batch.id) } } @@ -111,7 +111,7 @@ class CodeAllocatorTest { } @Test - fun parallelRequestsNeverGetTheSameCode() = runBlocking { + fun parallelRequestsNeverGetTheSameCode(): Unit = runBlocking { val batch = upload(List(50) { "CODE-$it" }) val results = List(100) { diff --git a/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/routes/RequestBodyTest.kt b/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/routes/RequestBodyTest.kt new file mode 100644 index 0000000..5fb577c --- /dev/null +++ b/server/src/test/kotlin/at/techbee/gplaypromocodeprovider/server/routes/RequestBodyTest.kt @@ -0,0 +1,111 @@ +package at.techbee.gplaypromocodeprovider.server.routes + +import at.techbee.gplaypromocodeprovider.api.ApiError +import at.techbee.gplaypromocodeprovider.api.BatchUploadRequest +import at.techbee.gplaypromocodeprovider.api.BatchUploadResponse +import at.techbee.gplaypromocodeprovider.api.ErrorResponse +import at.techbee.gplaypromocodeprovider.server.MAX_REQUEST_BODY_BYTES +import at.techbee.gplaypromocodeprovider.server.domain.CodeParser +import at.techbee.gplaypromocodeprovider.server.domain.MAX_CODES_PER_UPLOAD +import io.ktor.client.call.body +import io.ktor.client.request.post +import io.ktor.client.request.setBody +import io.ktor.client.statement.HttpResponse +import io.ktor.http.ContentType +import io.ktor.http.HttpStatusCode +import io.ktor.http.content.OutgoingContent +import io.ktor.http.contentType +import io.ktor.utils.io.ByteWriteChannel +import io.ktor.utils.io.writeFully +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue +import kotlin.time.Duration.Companion.days + +/** Requests whose body can't be read get a 4xx with an [ErrorResponse], never a 500. */ +class RequestBodyTest { + + private suspend fun HttpResponse.assertInvalidRequest(status: HttpStatusCode) { + assertEquals(status, this.status) + assertEquals(ApiError.INVALID_REQUEST, body().error) + } + + /** A JSON login body of about [size] bytes. */ + private fun loginJson(size: Int) = """{"username":"admin","password":"${"x".repeat(size)}"}""" + + @Test + fun bodyOfAnotherContentTypeIsUnsupportedMediaType() = apiTest { + client.post("/api/admin/login") { + contentType(ContentType.Application.Xml) + setBody(loginJson(10)) + }.assertInvalidRequest(HttpStatusCode.UnsupportedMediaType) + } + + @Test + fun bodyWithoutContentTypeIsUnsupportedMediaType() = apiTest { + client.post("/api/admin/login") { setBody(loginJson(10).encodeToByteArray()) } + .assertInvalidRequest(HttpStatusCode.UnsupportedMediaType) + } + + @Test + fun missingBodyIsUnsupportedMediaType() = apiTest { + client.post("/api/admin/login").assertInvalidRequest(HttpStatusCode.UnsupportedMediaType) + } + + @Test + fun malformedJsonIsBadRequest() = apiTest { + client.post("/api/admin/login") { + contentType(ContentType.Application.Json) + setBody("{\"username\":") + }.assertInvalidRequest(HttpStatusCode.BadRequest) + } + + @Test + fun tooLargeBodyIsRejectedBeforeLogin() = apiTest { + client.post("/api/admin/login") { + contentType(ContentType.Application.Json) + setBody(loginJson(MAX_REQUEST_BODY_BYTES.toInt())) + }.assertInvalidRequest(HttpStatusCode.PayloadTooLarge) + } + + /** Without a Content-Length the limit applies while reading. */ + @Test + fun tooLargeStreamedBodyIsRejected() = apiTest { + val bytes = loginJson(MAX_REQUEST_BODY_BYTES.toInt()).encodeToByteArray() + client.post("/api/admin/login") { + setBody(object : OutgoingContent.WriteChannelContent() { + override val contentType = ContentType.Application.Json + override suspend fun writeTo(channel: ByteWriteChannel) = channel.writeFully(bytes) + }) + }.assertInvalidRequest(HttpStatusCode.PayloadTooLarge) + } + + /** The upload has a higher limit, enough for the largest upload the server accepts. */ + @Test + fun largestValidUploadFitsItsLimit() = apiTest { + login() + val app = createApp() + val codes = List(MAX_CODES_PER_UPLOAD) { it.toString().padStart(CodeParser.MAX_CODE_LENGTH, 'C') } + + val response = client.post("/api/admin/apps/${app.id}/batches") { + contentType(ContentType.Application.Json) + setBody(BatchUploadRequest("x".repeat(1000), clock.now() + 30.days, codes.joinToString("\n"))) + } + + assertEquals(HttpStatusCode.Created, response.status) + assertEquals(MAX_CODES_PER_UPLOAD, response.body().addedCount) + } + + @Test + fun uploadAboveItsLimitIsRejected() = apiTest { + login() + val app = createApp() + val codes = "C".repeat(MAX_UPLOAD_BODY_BYTES.toInt()) + assertTrue(codes.length > MAX_REQUEST_BODY_BYTES) + + client.post("/api/admin/apps/${app.id}/batches") { + contentType(ContentType.Application.Json) + setBody(BatchUploadRequest(null, clock.now() + 30.days, codes)) + }.assertInvalidRequest(HttpStatusCode.PayloadTooLarge) + } +}