From 44226405f653b9157514fa54cf359c6afbe56cbb Mon Sep 17 00:00:00 2001 From: jonastmb <20173713+kopachlager@users.noreply.github.com> Date: Sun, 18 Oct 2026 13:05:25 +0200 Subject: [PATCH 1/3] Align standalone browser dependencies and refresh container builds --- .github/workflows/ci.yml | 8 ++-- .github/workflows/codeql.yml | 4 +- .github/workflows/release.yml | 8 ++-- Dockerfile | 7 ++- package-lock.json | 80 +++++++++++++++++++++-------------- package.json | 5 ++- test/distribution.test.js | 11 +++++ 7 files changed, 78 insertions(+), 45 deletions(-) create mode 100644 test/distribution.test.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 480b470..4aad8f2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,7 @@ jobs: node-version: [20.x, 22.x, 24.x] steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 - - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7 + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7 with: node-version: ${{ matrix.node-version }} cache: npm @@ -28,9 +28,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 - - uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4 + - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4 - name: Build browser integration image - uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7 + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7 with: context: . target: test @@ -39,7 +39,7 @@ jobs: tags: prerenderbuddy/engine:test - name: Run real Chromium fixture matrix run: docker run --rm --shm-size=1g prerenderbuddy/engine:test - - uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7 + - uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7 with: context: . load: true diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 04d3bee..2f5d0ce 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -19,9 +19,9 @@ jobs: steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 - name: Initialize CodeQL - uses: github/codeql-action/init@5595ccaf912efad79be6eef63a5619ff05969be3 # v4.37.6 + uses: github/codeql-action/init@b96794f015dfd88f77b49b1c93e0fa7110f94c63 # v4.38.0 with: languages: javascript-typescript build-mode: none - name: Analyze - uses: github/codeql-action/analyze@5595ccaf912efad79be6eef63a5619ff05969be3 # v4.37.6 + uses: github/codeql-action/analyze@b96794f015dfd88f77b49b1c93e0fa7110f94c63 # v4.38.0 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0d9d96a..728eaab 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -19,7 +19,7 @@ jobs: with: fetch-depth: 0 - - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7 + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7 with: node-version: 22.x cache: npm @@ -66,7 +66,7 @@ jobs: npm test npm audit --audit-level=moderate - - uses: docker/setup-buildx-action@bb05f3f5519dd87d3ba754cc423b652a5edd6d2c # v4 + - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4 - name: Build and run real Chromium fixtures working-directory: ${{ steps.archive.outputs.source_dir }} @@ -95,7 +95,7 @@ jobs: docker logs engine-release-smoke exit 1 - - uses: docker/setup-qemu-action@96fe6ef7f33517b61c61be40b68a1882f3264fb8 # v4.2.0 + - uses: docker/setup-qemu-action@99012661954931238ded8c8b007157a8430204e1 # v4.4.0 - name: Sign in to GitHub Container Registry uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0 @@ -105,7 +105,7 @@ jobs: password: ${{ secrets.GITHUB_TOKEN }} - name: Publish versioned runtime image - uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7.3.0 + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 with: context: ${{ steps.archive.outputs.source_dir }} target: runtime diff --git a/Dockerfile b/Dockerfile index 5b1b7cf..afd3658 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,9 @@ -FROM mcr.microsoft.com/playwright:v1.62.1-noble@sha256:dcc5531e97840b9b5e794f2814476b21571c5124a3fca2267d73041f56e7580e AS base +FROM mcr.microsoft.com/playwright:v1.64.0-noble@sha256:06a9939e57531807f8d5fd76ce44b53165ffb7d7501d87ab10e285c20b1e971f AS base + +# Refresh Ubuntu security fixes, including OpenSSL, in both build stages. +RUN apt-get update \ + && apt-get upgrade --yes --no-install-recommends \ + && rm -rf /var/lib/apt/lists/* WORKDIR /app ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright diff --git a/package-lock.json b/package-lock.json index c81f546..4dc3146 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,11 +10,12 @@ "license": "Apache-2.0", "dependencies": { "express": "^5.2.1", + "express-rate-limit": "8.7.1", "ipaddr.js": "2.5.0", - "playwright": "1.62.1" + "playwright": "1.64.0" }, "devDependencies": { - "supertest": "^7.1.4" + "supertest": "^7.3.0" }, "engines": { "node": ">=20" @@ -395,6 +396,25 @@ "url": "https://opencollective.com/express" } }, + "node_modules/express-rate-limit": { + "version": "8.7.1", + "resolved": "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-8.7.1.tgz", + "integrity": "sha512-2FXbqkygil3mZiXzEtM0IjE/UBmBNot0J1GEVrJT4JbrvwOSoEtqtO+wHbiMQELWe1eNvHoYRg2dkjOid+6JkA==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.3", + "ip-address": "^10.2.0" + }, + "engines": { + "node": ">= 16" + }, + "funding": { + "url": "https://github.com/sponsors/express-rate-limit" + }, + "peerDependencies": { + "express": ">= 4.11" + } + }, "node_modules/fast-safe-stringify": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz", @@ -499,20 +519,6 @@ "node": ">= 0.8" } }, - "node_modules/fsevents": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", - "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", - "hasInstallScript": true, - "license": "MIT", - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^8.16.0 || ^10.6.0 || >=11.0.0" - } - }, "node_modules/function-bind": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", @@ -653,6 +659,15 @@ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", "license": "ISC" }, + "node_modules/ip-address": { + "version": "10.7.4", + "resolved": "https://registry.npmjs.org/ip-address/-/ip-address-10.7.4.tgz", + "integrity": "sha512-DWBMKyeM25D70nrIiTkgQth59/G+naKOTep6huF9j+GmeK3DMD60Zvxg5d5qQnz+38AHgou6ean2LECX+lrKDg==", + "license": "MIT", + "engines": { + "node": ">= 12" + } + }, "node_modules/ipaddr.js": { "version": "2.5.0", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.5.0.tgz", @@ -818,27 +833,24 @@ } }, "node_modules/playwright": { - "version": "1.62.1", - "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.62.1.tgz", - "integrity": "sha512-0M+L3LAD8/nm554LOla9Ayx0j0tmFZ0FBcoQ7F1VuVHpM/XpiC8RcDzBQB8W5+hA8L22THxELzeF+2WcUzvcLg==", + "version": "1.64.0", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.64.0.tgz", + "integrity": "sha512-kVzTnYFYEyQ9RGzBjNCd2+yEqWmVbxl3AHPynv3qQ6JTjv4a+kwRcnqQwIpuP3NnrnIrE/Q6wWAIAvMfIE2/Yg==", "license": "Apache-2.0", "dependencies": { - "playwright-core": "1.62.1" + "playwright-core": "1.64.0" }, "bin": { "playwright": "cli.js" }, "engines": { "node": ">=20" - }, - "optionalDependencies": { - "fsevents": "2.3.2" } }, "node_modules/playwright-core": { - "version": "1.62.1", - "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.62.1.tgz", - "integrity": "sha512-wPYSwEBJY9GHraISXqyqtx0na0LpO3XEX7jNDhntbex7tzUS7kLnZsOlFruFJB4Hi/rhDMjXGqHewDZ68nYZVw==", + "version": "1.64.0", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.64.0.tgz", + "integrity": "sha512-T9r+MZkTECl2+oUcZ26YgZzTrYidTsIhYYkJix+6iDOymOb1FB2RLLSOxRGnZGv5DStt+aduIVbt+E8WygXYpg==", "license": "Apache-2.0", "bin": { "playwright-core": "cli.js" @@ -848,9 +860,9 @@ } }, "node_modules/proxy-addr": { - "version": "2.0.7", - "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", - "integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==", + "version": "2.0.8", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.8.tgz", + "integrity": "sha512-5nnx0yGyVUcY6t9RnWcARWtwT9F1D8O9rt08htPvnd49W1IgZtmLkhu9WfMzQj1cFxjHIO6connUNVW5k7AVyQ==", "license": "MIT", "dependencies": { "forwarded": "0.2.0", @@ -858,6 +870,10 @@ }, "engines": { "node": ">= 0.10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" } }, "node_modules/proxy-addr/node_modules/ipaddr.js": { @@ -1089,9 +1105,9 @@ } }, "node_modules/supertest": { - "version": "7.2.2", - "resolved": "https://registry.npmjs.org/supertest/-/supertest-7.2.2.tgz", - "integrity": "sha512-oK8WG9diS3DlhdUkcFn4tkNIiIbBx9lI2ClF8K+b2/m8Eyv47LSawxUzZQSNKUrVb2KsqeTDCcjAAVPYaSLVTA==", + "version": "7.3.0", + "resolved": "https://registry.npmjs.org/supertest/-/supertest-7.3.0.tgz", + "integrity": "sha512-UwwmWq3xLhyU96c521wYNaBg7IqX78Wpj4FKs6Katp6vdklX42zU5ESvX3KeZRLFyO44RwFp4yvxYMNZzCcX9g==", "dev": true, "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index 7437e11..ce811a1 100644 --- a/package.json +++ b/package.json @@ -42,10 +42,11 @@ }, "dependencies": { "express": "^5.2.1", + "express-rate-limit": "8.7.1", "ipaddr.js": "2.5.0", - "playwright": "1.62.1" + "playwright": "1.64.0" }, "devDependencies": { - "supertest": "^7.1.4" + "supertest": "^7.3.0" } } diff --git a/test/distribution.test.js b/test/distribution.test.js new file mode 100644 index 0000000..abc337a --- /dev/null +++ b/test/distribution.test.js @@ -0,0 +1,11 @@ +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import test from 'node:test'; + +test('the pinned browser image matches the installed Playwright package', () => { + const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8')); + const dockerfile = fs.readFileSync(new URL('../Dockerfile', import.meta.url), 'utf8'); + const image = dockerfile.match(/^FROM mcr\.microsoft\.com\/playwright:v(\d+\.\d+\.\d+)-noble@sha256:([a-f0-9]{64}) AS base$/m); + assert.ok(image, 'the browser image must be versioned and digest-pinned'); + assert.equal(image[1], pkg.dependencies.playwright, 'package and image need one matching browser version'); +}); From 5018c956d0ef40603148a87903c661c2b6fb145e Mon Sep 17 00:00:00 2001 From: jonastmb <20173713+kopachlager@users.noreply.github.com> Date: Sun, 18 Oct 2026 13:05:25 +0200 Subject: [PATCH 2/3] Bound protected API request rates before authorization --- .env.example | 2 + CHANGELOG.md | 8 ++++ docs/api.md | 7 +++- docs/security-review.md | 8 ++++ docs/self-hosting.md | 7 ++++ src/config.js | 15 +++++++ src/routes.js | 24 ++++++++--- test/config.test.js | 12 ++++++ test/requestRateLimit.test.js | 76 +++++++++++++++++++++++++++++++++++ 9 files changed, 153 insertions(+), 6 deletions(-) create mode 100644 test/requestRateLimit.test.js diff --git a/.env.example b/.env.example index e6dcd3a..76e4371 100644 --- a/.env.example +++ b/.env.example @@ -12,6 +12,8 @@ ALLOW_ANY_PUBLIC_DOMAIN=false ENGINE_PORT=3000 PORT=3000 HEADLESS=true +# Per-process shared budget for protected API routes, including failed auth. +REQUESTS_PER_MINUTE=600 RENDER_TIMEOUT_MS=20000 RENDER_MAX_REDIRECTS=10 RENDER_MAX_REQUESTS=250 diff --git a/CHANGELOG.md b/CHANGELOG.md index 84d9291..e5964d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes are documented here. The project follows Semantic Versioning after the initial pre-1.0 stabilization period. +## Unreleased + +- Align the Playwright npm package and digest-pinned browser container at 1.64.0 so Chromium fixtures use the matching executable. +- Update proxy-addr to 2.0.8, refresh Ubuntu packages during image builds, and update the supertest test dependency. +- Refresh pinned Node setup, Docker build/push, Buildx, QEMU and CodeQL actions. +- Limit protected API requests before authentication with a configurable per-process budget, preserving public health/readiness probes and the required external proxy controls. +- Check package/container Playwright alignment in the unit suite to prevent independent updates from breaking browser startup. + ## 0.1.3 - 2026-08-03 - Verify public GHCR access with an anonymous registry token instead of attempting to change user-package visibility with a repository-scoped workflow token. diff --git a/docs/api.md b/docs/api.md index 83309c0..febdc6a 100644 --- a/docs/api.md +++ b/docs/api.md @@ -12,6 +12,10 @@ The engine exposes an HTTP API on port `3000` by default. Except for liveness an `GET /ready` returns `200 {"ready":true}` when Chromium is connected, or `503 {"ready":false}`. These endpoints intentionally require no token so container orchestrators can call them. +## Request-rate protection (unreleased) + +All five authenticated route variants share a per-process budget configured by `REQUESTS_PER_MINUTE` (default 600 per minute). Authentication failures also consume it. A rejected request returns `429 {"error":"Request rate limit exceeded"}` and a `Retry-After` header. `RateLimit` and `RateLimit-Policy` describe the shared budget. Liveness/readiness are exempt. This control is in the maintenance branch and is not included in published `v0.1.3`. + ## Render Use either `GET /render?url=` or: @@ -54,8 +58,9 @@ JSON errors use `{"error":"message"}`. Stable statuses are: | `404` | Unknown endpoint or cache miss; a render may also preserve an origin `404` as HTML | | `413` | Rendered HTML exceeds the configured limit | | `422` | Per-render browser request budget exhausted | +| `429` | Protected API request budget exhausted (unreleased) | | `500` | Browser or unexpected render failure | | `503` | Chromium not ready, render capacity reached, or coalescing wait exceeded | | `504` | Render timed out | -Capacity responses may include `Retry-After`. Clients should retry `503` and `504` only with bounded exponential backoff and jitter. Do not retry policy `403`, size `413`, or request-budget `422` responses without changing the request or configuration. +Honor `Retry-After` on `429` before retrying. Capacity responses may include `Retry-After`. Clients should retry `503` and `504` only with bounded exponential backoff and jitter. Do not retry policy `403`, size `413`, or request-budget `422` responses without changing the request or configuration. diff --git a/docs/security-review.md b/docs/security-review.md index e4307f8..fbbd6eb 100644 --- a/docs/security-review.md +++ b/docs/security-review.md @@ -71,3 +71,11 @@ npm pack --dry-run ``` The Chromium matrix covers static HTML, server-rendered metadata/content, hydrated app shells, same-site redirects, cross-site and excessive redirect blocks, 404/503 status preservation, blocked subresources, popup/WebSocket/service-worker egress attempts, request storms, timeouts, oversized output, malformed responses, and SIGTERM shutdown. + +## Maintenance review — 2026-10-18 (unreleased) + +The maintenance branch aligns the Playwright package and digest-pinned browser image at 1.64.0, retains the patched proxy-addr 2.0.8 dependency, and refreshes Ubuntu packages while building both container stages. The dependency, Node, real-Chromium, runtime vulnerability and readiness gates remain required. Published v0.1.3 and its immutable tag are unchanged. + +Authenticated endpoints now share an explicit process-local request-rate limiter, before authorization, URL validation or browser allocation. It includes unsuccessful token attempts and returns 429 with Retry-After. One constant budget key avoids trusting forwarded IP headers or allocating attacker-controlled per-client buckets. The default is 600 protected requests per minute, configurable with startup validation. Health/readiness do not consume that budget. This addresses the common missing-rate-limiting finding on the five authenticated route variants without dismissing the alerts or treating render concurrency as a request-rate control. + +The limiter is an additional local protection. It does not replace private ingress, TLS, reverse-proxy limits, host resource limits or egress controls. Its counter is local to each process and resets on restart; multiple engine replicas do not share a distributed budget. No managed-service routing or infrastructure is part of this change. diff --git a/docs/self-hosting.md b/docs/self-hosting.md index f2fb02d..bb7a82a 100644 --- a/docs/self-hosting.md +++ b/docs/self-hosting.md @@ -29,6 +29,7 @@ The container reads these environment variables: | `PORT` | `3000` | Container HTTP port | | `ENGINE_PORT` | `3000` | Host-only Compose port; used by `docker-compose.yml`, not the Node process | | `HEADLESS` | `true` | Keep enabled in normal operation | +| `REQUESTS_PER_MINUTE` | `600` | Unreleased: shared protected-API budget per process; integer from 1 to 1,000,000 | | `RENDER_TIMEOUT_MS` | `20000` | Bounded between 1 and 120 seconds | | `RENDER_MAX_REDIRECTS` | `10` | Bounded between 0 and 20 hops | | `RENDER_MAX_REQUESTS` | `250` | Bounded between 10 and 2,000 HTTP(S) requests; exhaustion terminates the render | @@ -43,6 +44,12 @@ The container reads these environment variables: An entry in `ALLOWED_DOMAINS` permits only that exact hostname. List both apex and `www` hostnames if both should be rendered. +## Request-rate protection (unreleased) + +The maintenance branch adds a shared one-minute budget for `/render`, `/cache/read`, `/cache/clear`, and `/internal/metrics`. It counts both successful and rejected authentication attempts before protected work runs. The default is 600 requests per minute across all callers and those endpoints together; tune `REQUESTS_PER_MINUTE` to your workload. `/health` and `/ready` remain outside the budget so orchestrator probes keep working. + +Exhausted requests return `429` JSON with `Retry-After`, `RateLimit` and `RateLimit-Policy` headers. Follow the retry delay with bounded backoff. Each process has its own in-memory budget, which resets on restart. The engine does not trust client-supplied forwarding headers to create new budgets, retain client IPs, or maintain per-IP buckets. Keep external proxy limits and private ingress in place, especially when running multiple replicas. Published `v0.1.3` does not contain this new control. + ## Resource baseline Start a single engine with 2 vCPU, 2 GB RAM, and the configured 1 GB shared-memory allocation. Page complexity matters more than URL count, so treat this as a starting point and measure representative workloads. Version 0.1 requires no Redis or external queue: its cache and duplicate-render coordination are deliberately process-local. diff --git a/src/config.js b/src/config.js index 785a154..9a35597 100644 --- a/src/config.js +++ b/src/config.js @@ -21,6 +21,15 @@ export function getAllowedOrigins() { return parseCsv(process.env.ALLOWED_ORIGINS); } +export function getRequestsPerMinute(env = process.env) { + const value = env.REQUESTS_PER_MINUTE; + const limit = value === undefined || value === '' ? 600 : Number(value); + if (!Number.isInteger(limit) || limit < 1 || limit > 1_000_000) { + throw new Error('REQUESTS_PER_MINUTE must be an integer between 1 and 1000000'); + } + return limit; +} + export function validateConfiguration(env = process.env) { const errors = []; const token = String(env.PRERENDER_TOKEN || ''); @@ -36,6 +45,12 @@ export function validateConfiguration(env = process.env) { errors.push('ALLOWED_DOMAINS must contain exact hostnames without wildcards, protocols, paths, or spaces'); } + try { + getRequestsPerMinute(env); + } catch (error) { + errors.push(error.message); + } + return { valid: errors.length === 0, errors }; } diff --git a/src/routes.js b/src/routes.js index e930d8c..51802c8 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1,4 +1,6 @@ import express from 'express'; +import { rateLimit } from 'express-rate-limit'; +import { getRequestsPerMinute } from './config.js'; import { requireAuthorization } from './auth.js'; import { isBrowserReady } from './browser.js'; import * as cache from './cache.js'; @@ -55,7 +57,19 @@ export function createRoutes(options = {}) { res.status(ready ? 200 : 503).json({ ready }); }); - router.get('/internal/metrics', requireAuthorization, (req, res) => { + // One process/token owns one shared budget across all protected endpoints. + // Do not trust forwarded IP headers or create unbounded per-client buckets. + const requestLimiter = rateLimit({ + windowMs: 60_000, + limit: getRequestsPerMinute(), + keyGenerator: () => 'engine', + standardHeaders: 'draft-8', + legacyHeaders: false, + identifier: 'engine-requests', + message: { error: 'Request rate limit exceeded' }, + }); + + router.get('/internal/metrics', requestLimiter, requireAuthorization, (req, res) => { res.setHeader('Cache-Control', 'private, no-store'); res.json({ observedAt: new Date().toISOString(), @@ -136,10 +150,10 @@ export function createRoutes(options = {}) { } } - router.get('/render', requireAuthorization, handleRender); - router.post('/render', requireAuthorization, handleRender); + router.get('/render', requestLimiter, requireAuthorization, handleRender); + router.post('/render', requestLimiter, requireAuthorization, handleRender); - router.get('/cache/read', requireAuthorization, async (req, res) => { + router.get('/cache/read', requestLimiter, requireAuthorization, async (req, res) => { const url = req.query.url; if (!url || typeof url !== 'string') return res.status(400).json({ error: 'Missing url' }); const validation = await urlValidator(url); @@ -151,7 +165,7 @@ export function createRoutes(options = {}) { return res.status(cached.statusCode).send(cached.html); }); - router.post('/cache/clear', requireAuthorization, async (req, res) => { + router.post('/cache/clear', requestLimiter, requireAuthorization, async (req, res) => { const url = req.body?.url; if (!url || typeof url !== 'string') return res.status(400).json({ error: 'Missing url' }); const validation = await urlValidator(url); diff --git a/test/config.test.js b/test/config.test.js index 784b9cc..7ec5ffc 100644 --- a/test/config.test.js +++ b/test/config.test.js @@ -30,3 +30,15 @@ test('configuration rejects wildcard and URL-shaped domain entries', () => { assert.equal(result.valid, false, domain); } }); + +test('request rate limit defaults and explicit values are validated at startup', () => { + const base = { PRERENDER_TOKEN: 'a'.repeat(32), ALLOWED_DOMAINS: 'example.com' }; + for (const REQUESTS_PER_MINUTE of ['1', '600', '1000000']) { + assert.equal(validateConfiguration({ ...base, REQUESTS_PER_MINUTE }).valid, true); + } + for (const REQUESTS_PER_MINUTE of ['0', '-1', '1.5', 'invalid', '1000001']) { + const result = validateConfiguration({ ...base, REQUESTS_PER_MINUTE }); + assert.equal(result.valid, false, REQUESTS_PER_MINUTE); + assert.match(result.errors[0], /REQUESTS_PER_MINUTE/); + } +}); diff --git a/test/requestRateLimit.test.js b/test/requestRateLimit.test.js new file mode 100644 index 0000000..6729b11 --- /dev/null +++ b/test/requestRateLimit.test.js @@ -0,0 +1,76 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import request from 'supertest'; +import { createApp } from '../src/app.js'; + +const token = 'test-token-that-is-longer-than-32-characters'; + +function limitedApp(limit, options = {}) { + const previous = process.env.REQUESTS_PER_MINUTE; + process.env.REQUESTS_PER_MINUTE = String(limit); + try { + return createApp({ isReady: () => true, ...options }); + } finally { + if (previous === undefined) delete process.env.REQUESTS_PER_MINUTE; + else process.env.REQUESTS_PER_MINUTE = previous; + } +} + +test('all five protected routes limit repeated unauthenticated requests', async () => { + for (const [method, path] of [ + ['get', '/render'], ['post', '/render'], ['get', '/cache/read'], + ['post', '/cache/clear'], ['get', '/internal/metrics'], + ]) { + const app = limitedApp(1); + await request(app)[method](path).expect(401); + const response = await request(app)[method](path).expect(429, { error: 'Request rate limit exceeded' }); + assert.ok(Number(response.headers['retry-after']) > 0); + assert.match(response.headers['ratelimit-policy'], /engine-requests/); + } +}); + +test('authorized and unauthorized requests share a budget across protected routes', async (t) => { + const previous = process.env.PRERENDER_TOKEN; + process.env.PRERENDER_TOKEN = token; + t.after(() => { + if (previous === undefined) delete process.env.PRERENDER_TOKEN; + else process.env.PRERENDER_TOKEN = previous; + }); + let validations = 0; + let renders = 0; + const app = limitedApp(2, { + validateUrl: async () => { validations += 1; return { valid: true }; }, + renderer: async () => { renders += 1; }, + }); + await request(app).get('/internal/metrics').set('Authorization', `Bearer ${token}`).expect(200); + await request(app).get('/render?url=https://example.com/').expect(401); + await request(app).get('/cache/read?url=https://example.com/').set('X-Prerender-Token', token).expect(429); + assert.equal(validations, 0, 'rate-limited requests cannot reach URL validation'); + assert.equal(renders, 0, 'rate-limited requests cannot allocate a browser render'); +}); + +test('liveness and readiness neither consume nor depend on the protected budget', async () => { + const app = limitedApp(1); + await request(app).get('/health').expect(200); + await request(app).get('/ready').expect(200); + await request(app).get('/render').expect(401); + await request(app).get('/render').expect(429); + await request(app).get('/health').expect(200); + await request(app).get('/ready').expect(200); + await request(limitedApp(1, { isReady: () => false })).get('/ready').expect(503); +}); + +test('changing forwarded client addresses cannot bypass the engine-wide budget', async () => { + const app = limitedApp(1); + await request(app).get('/render').set('X-Forwarded-For', '198.51.100.1').expect(401); + await request(app).get('/render').set('X-Forwarded-For', '203.0.113.2').expect(429); + assert.equal(app.get('trust proxy'), false); +}); + +test('separate engine instances do not share their in-memory request budgets', async () => { + const first = limitedApp(1); + const second = limitedApp(1); + await request(first).get('/render').expect(401); + await request(first).get('/render').expect(429); + await request(second).get('/render').expect(401); +}); From 8a8779da942317fb90aed92d4a2ab062c07ad0ff Mon Sep 17 00:00:00 2001 From: jonastmb <20173713+kopachlager@users.noreply.github.com> Date: Sun, 18 Oct 2026 13:12:28 +0200 Subject: [PATCH 3/3] Apply request limits before JSON parsing and expose retry headers --- docs/api.md | 4 ++-- docs/security-review.md | 4 +++- docs/self-hosting.md | 2 +- src/app.js | 20 +++++++++++++++++--- src/routes.js | 24 +++++------------------- test/requestRateLimit.test.js | 28 ++++++++++++++++++++++++++++ 6 files changed, 56 insertions(+), 26 deletions(-) diff --git a/docs/api.md b/docs/api.md index febdc6a..c11823b 100644 --- a/docs/api.md +++ b/docs/api.md @@ -14,7 +14,7 @@ The engine exposes an HTTP API on port `3000` by default. Except for liveness an ## Request-rate protection (unreleased) -All five authenticated route variants share a per-process budget configured by `REQUESTS_PER_MINUTE` (default 600 per minute). Authentication failures also consume it. A rejected request returns `429 {"error":"Request rate limit exceeded"}` and a `Retry-After` header. `RateLimit` and `RateLimit-Policy` describe the shared budget. Liveness/readiness are exempt. This control is in the maintenance branch and is not included in published `v0.1.3`. +All non-probe API requests share a per-process budget configured by `REQUESTS_PER_MINUTE` (default 600 per minute). Authentication failures also consume it. A rejected request returns `429 {"error":"Request rate limit exceeded"}` and a `Retry-After` header. `RateLimit` and `RateLimit-Policy` describe the shared budget. Liveness/readiness are exempt. This control is in the maintenance branch and is not included in published `v0.1.3`. ## Render @@ -56,7 +56,7 @@ JSON errors use `{"error":"message"}`. Stable statuses are: | `401` | Missing or incorrect token | | `403` | Destination, redirect, CORS preflight, or navigation rejected by policy | | `404` | Unknown endpoint or cache miss; a render may also preserve an origin `404` as HTML | -| `413` | Rendered HTML exceeds the configured limit | +| `413` | Request body exceeds 16 KB, or rendered HTML exceeds the configured limit | | `422` | Per-render browser request budget exhausted | | `429` | Protected API request budget exhausted (unreleased) | | `500` | Browser or unexpected render failure | diff --git a/docs/security-review.md b/docs/security-review.md index fbbd6eb..90ca428 100644 --- a/docs/security-review.md +++ b/docs/security-review.md @@ -76,6 +76,8 @@ The Chromium matrix covers static HTML, server-rendered metadata/content, hydrat The maintenance branch aligns the Playwright package and digest-pinned browser image at 1.64.0, retains the patched proxy-addr 2.0.8 dependency, and refreshes Ubuntu packages while building both container stages. The dependency, Node, real-Chromium, runtime vulnerability and readiness gates remain required. Published v0.1.3 and its immutable tag are unchanged. -Authenticated endpoints now share an explicit process-local request-rate limiter, before authorization, URL validation or browser allocation. It includes unsuccessful token attempts and returns 429 with Retry-After. One constant budget key avoids trusting forwarded IP headers or allocating attacker-controlled per-client buckets. The default is 600 protected requests per minute, configurable with startup validation. Health/readiness do not consume that budget. This addresses the common missing-rate-limiting finding on the five authenticated route variants without dismissing the alerts or treating render concurrency as a request-rate control. +Authenticated endpoints now share an explicit process-local request-rate limiter, before JSON parsing, authorization, URL validation or browser allocation. It includes unsuccessful token attempts and returns 429 with Retry-After. One constant budget key avoids trusting forwarded IP headers or allocating attacker-controlled per-client buckets. The default is 600 protected requests per minute, configurable with startup validation. Health/readiness do not consume that budget. This addresses the common missing-rate-limiting finding on the five authenticated route variants without dismissing the alerts or treating render concurrency as a request-rate control. + +Malformed JSON and oversized bodies consume the same budget; oversized request bodies return a stable 413 response. Allowed browser origins can read Retry-After, RateLimit and RateLimit-Policy through the existing CORS boundary. The limiter is an additional local protection. It does not replace private ingress, TLS, reverse-proxy limits, host resource limits or egress controls. Its counter is local to each process and resets on restart; multiple engine replicas do not share a distributed budget. No managed-service routing or infrastructure is part of this change. diff --git a/docs/self-hosting.md b/docs/self-hosting.md index bb7a82a..b0b53a0 100644 --- a/docs/self-hosting.md +++ b/docs/self-hosting.md @@ -46,7 +46,7 @@ An entry in `ALLOWED_DOMAINS` permits only that exact hostname. List both apex a ## Request-rate protection (unreleased) -The maintenance branch adds a shared one-minute budget for `/render`, `/cache/read`, `/cache/clear`, and `/internal/metrics`. It counts both successful and rejected authentication attempts before protected work runs. The default is 600 requests per minute across all callers and those endpoints together; tune `REQUESTS_PER_MINUTE` to your workload. `/health` and `/ready` remain outside the budget so orchestrator probes keep working. +The maintenance branch adds a shared one-minute budget before JSON parsing and authorization. It covers `/render`, `/cache/read`, `/cache/clear`, `/internal/metrics`, and unknown non-probe requests. It counts both successful and rejected authentication attempts before protected work runs. The default is 600 requests per minute across all callers and those endpoints together; tune `REQUESTS_PER_MINUTE` to your workload. `/health` and `/ready` remain outside the budget so orchestrator probes keep working. Exhausted requests return `429` JSON with `Retry-After`, `RateLimit` and `RateLimit-Policy` headers. Follow the retry delay with bounded backoff. Each process has its own in-memory budget, which resets on restart. The engine does not trust client-supplied forwarding headers to create new budgets, retain client IPs, or maintain per-IP buckets. Keep external proxy limits and private ingress in place, especially when running multiple replicas. Published `v0.1.3` does not contain this new control. diff --git a/src/app.js b/src/app.js index e0a85ad..515d33d 100644 --- a/src/app.js +++ b/src/app.js @@ -1,11 +1,11 @@ import express from 'express'; -import { getAllowedOrigins } from './config.js'; +import { rateLimit } from 'express-rate-limit'; +import { getAllowedOrigins, getRequestsPerMinute } from './config.js'; import { createRoutes } from './routes.js'; export function createApp(options = {}) { const app = express(); app.disable('x-powered-by'); - app.use(express.json({ limit: '16kb', strict: true })); app.use((req, res, next) => { res.setHeader('X-Content-Type-Options', 'nosniff'); res.setHeader('X-Frame-Options', 'DENY'); @@ -19,17 +19,31 @@ export function createApp(options = {}) { res.setHeader('Vary', 'Origin'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Authorization, Content-Type, X-Prerender-Token, X-Prerender-Cache-Ttl-Seconds'); - res.setHeader('Access-Control-Expose-Headers', 'X-Prerender-Cache, X-Prerender-Cache-Ttl, X-Prerender-Time, X-Prerender-Final-Url, X-Prerender-Coordination'); + res.setHeader('Access-Control-Expose-Headers', 'X-Prerender-Cache, X-Prerender-Cache-Ttl, X-Prerender-Time, X-Prerender-Final-Url, X-Prerender-Coordination, Retry-After, RateLimit, RateLimit-Policy'); } if (req.method === 'OPTIONS') { return origin && allowedOrigins.includes(origin) ? res.sendStatus(204) : res.sendStatus(403); } return next(); }); + // Bound requests before JSON parsing, token checks or browser allocation. + // One constant key gives the process a shared, bounded-memory budget. + app.use(rateLimit({ + windowMs: 60_000, + limit: getRequestsPerMinute(), + keyGenerator: () => 'engine', + skip: (req) => ['GET', 'HEAD'].includes(req.method) && /^\/(health|ready)\/?$/i.test(req.path), + standardHeaders: 'draft-8', + legacyHeaders: false, + identifier: 'engine-requests', + message: { error: 'Request rate limit exceeded' }, + })); + app.use(express.json({ limit: '16kb', strict: true })); app.use(createRoutes(options)); app.use((req, res) => res.status(404).json({ error: 'Not found' })); app.use((error, req, res, next) => { if (res.headersSent) return next(error); + if (error?.type === 'entity.too.large') return res.status(413).json({ error: 'Request body exceeds the 16 KB limit' }); if (error instanceof SyntaxError) return res.status(400).json({ error: 'Invalid JSON body' }); console.error('Unhandled request error:', error?.message || 'unknown'); return res.status(500).json({ error: 'Internal server error' }); diff --git a/src/routes.js b/src/routes.js index 51802c8..e930d8c 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1,6 +1,4 @@ import express from 'express'; -import { rateLimit } from 'express-rate-limit'; -import { getRequestsPerMinute } from './config.js'; import { requireAuthorization } from './auth.js'; import { isBrowserReady } from './browser.js'; import * as cache from './cache.js'; @@ -57,19 +55,7 @@ export function createRoutes(options = {}) { res.status(ready ? 200 : 503).json({ ready }); }); - // One process/token owns one shared budget across all protected endpoints. - // Do not trust forwarded IP headers or create unbounded per-client buckets. - const requestLimiter = rateLimit({ - windowMs: 60_000, - limit: getRequestsPerMinute(), - keyGenerator: () => 'engine', - standardHeaders: 'draft-8', - legacyHeaders: false, - identifier: 'engine-requests', - message: { error: 'Request rate limit exceeded' }, - }); - - router.get('/internal/metrics', requestLimiter, requireAuthorization, (req, res) => { + router.get('/internal/metrics', requireAuthorization, (req, res) => { res.setHeader('Cache-Control', 'private, no-store'); res.json({ observedAt: new Date().toISOString(), @@ -150,10 +136,10 @@ export function createRoutes(options = {}) { } } - router.get('/render', requestLimiter, requireAuthorization, handleRender); - router.post('/render', requestLimiter, requireAuthorization, handleRender); + router.get('/render', requireAuthorization, handleRender); + router.post('/render', requireAuthorization, handleRender); - router.get('/cache/read', requestLimiter, requireAuthorization, async (req, res) => { + router.get('/cache/read', requireAuthorization, async (req, res) => { const url = req.query.url; if (!url || typeof url !== 'string') return res.status(400).json({ error: 'Missing url' }); const validation = await urlValidator(url); @@ -165,7 +151,7 @@ export function createRoutes(options = {}) { return res.status(cached.statusCode).send(cached.html); }); - router.post('/cache/clear', requestLimiter, requireAuthorization, async (req, res) => { + router.post('/cache/clear', requireAuthorization, async (req, res) => { const url = req.body?.url; if (!url || typeof url !== 'string') return res.status(400).json({ error: 'Missing url' }); const validation = await urlValidator(url); diff --git a/test/requestRateLimit.test.js b/test/requestRateLimit.test.js index 6729b11..d0e12f8 100644 --- a/test/requestRateLimit.test.js +++ b/test/requestRateLimit.test.js @@ -74,3 +74,31 @@ test('separate engine instances do not share their in-memory request budgets', a await request(first).get('/render').expect(429); await request(second).get('/render').expect(401); }); + +test('malformed JSON consumes the budget before body parsing can reject it', async () => { + const app = limitedApp(1); + await request(app).post('/render').set('Content-Type', 'application/json').send('{').expect(400); + await request(app).post('/render').set('Content-Type', 'application/json').send('{').expect(429); +}); + +test('oversized bodies consume the budget and return a stable client error', async () => { + const app = limitedApp(1); + await request(app).post('/render').send({ url: 'x'.repeat(20_000) }).expect(413, { error: 'Request body exceeds the 16 KB limit' }); + await request(app).post('/render').send({ url: 'x'.repeat(20_000) }).expect(429); +}); + +test('allowed browser origins can read rate-limit and retry headers on 429', async (t) => { + const previous = process.env.ALLOWED_ORIGINS; + process.env.ALLOWED_ORIGINS = 'https://ui.example.com'; + t.after(() => { + if (previous === undefined) delete process.env.ALLOWED_ORIGINS; + else process.env.ALLOWED_ORIGINS = previous; + }); + const app = limitedApp(1); + await request(app).get('/render').set('Origin', 'https://ui.example.com').expect(401); + const response = await request(app).get('/render').set('Origin', 'https://ui.example.com').expect(429); + assert.equal(response.headers['access-control-allow-origin'], 'https://ui.example.com'); + const exposed = response.headers['access-control-expose-headers'].split(',').map((name) => name.trim()); + for (const name of ['Retry-After', 'RateLimit', 'RateLimit-Policy']) assert.ok(exposed.includes(name)); + assert.ok(Number(response.headers['retry-after']) > 0); +});