From 6cfa2762cc49324f381eb166b99c800cb5dd70b2 Mon Sep 17 00:00:00 2001 From: Musiker15 Date: Fri, 30 Oct 2026 17:05:58 +0100 Subject: [PATCH 1/3] Protect paste creation against spam and lock the write API behind keys 95 % of all pastes came from one address that used the service as file storage (blocks of about 0.5 MB of base64, one year each), the rest was link and comment spam. The create route had only an in-memory limit per address, and that address came from CF-Connecting-IP or the first X-Forwarded-For entry, both set by the client. - The client address is X-Real-IP (set by Apache) or the last X-Forwarded-For entry, never a header the client chooses. - The form needs a solved Cloudflare Turnstile (required in production, fails closed), the same origin, an untouched honeypot and a signed form stamp at least two seconds old. Per address it may create 20 pastes and 5 MB a day, counted in the database. - Scripts need an API key. scripts/api-key.sh (help, list, create, revoke, delete) manages them on the server; only the SHA-256 is stored. - Both ways pass content checks: encoded files, link spam and the same content again within a day are turned down. - The privacy policy names Turnstile, the README the key requirement. - Vitest for the rules, and a build workflow that type-checks, tests and builds every pull request. --- .env.example | 16 + .github/workflows/build.yml | 61 ++ README.md | 24 +- app/api/pastes/route.ts | 28 +- app/page.tsx | 21 +- app/privacy/page.tsx | 2 +- messages/de.json | 24 +- messages/en.json | 24 +- middleware.ts | 12 +- migrations/003_api_keys.sql | 15 + package-lock.json | 996 ++++++++++++++++++++++++++++- package.json | 7 +- scripts/api-key.sh | 15 + scripts/api-key.ts | 159 +++++ src/components/CreatePasteForm.tsx | 84 ++- src/components/TurnstileWidget.tsx | 73 +++ src/lib/apiKeys.ts | 60 ++ src/lib/formStamp.test.ts | 30 + src/lib/formStamp.ts | 49 ++ src/lib/paste.ts | 13 + src/lib/rateLimit.test.ts | 33 + src/lib/rateLimit.ts | 23 +- src/lib/spamGuard.test.ts | 158 +++++ src/lib/spamGuard.ts | 146 +++++ src/lib/spamHeuristics.test.ts | 98 +++ src/lib/spamHeuristics.ts | 133 ++++ src/lib/turnstile.test.ts | 79 +++ src/lib/turnstile.ts | 83 +++ src/types/index.ts | 2 + vitest.config.ts | 17 + 30 files changed, 2439 insertions(+), 46 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 migrations/003_api_keys.sql create mode 100755 scripts/api-key.sh create mode 100644 scripts/api-key.ts create mode 100644 src/components/TurnstileWidget.tsx create mode 100644 src/lib/apiKeys.ts create mode 100644 src/lib/formStamp.test.ts create mode 100644 src/lib/formStamp.ts create mode 100644 src/lib/rateLimit.test.ts create mode 100644 src/lib/spamGuard.test.ts create mode 100644 src/lib/spamGuard.ts create mode 100644 src/lib/spamHeuristics.test.ts create mode 100644 src/lib/spamHeuristics.ts create mode 100644 src/lib/turnstile.test.ts create mode 100644 src/lib/turnstile.ts create mode 100644 vitest.config.ts diff --git a/.env.example b/.env.example index db8bf44..330d6e8 100644 --- a/.env.example +++ b/.env.example @@ -17,6 +17,22 @@ DB_NAME=msk_paste # Generate with: openssl rand -hex 32 IP_HASH_SECRET=change_me_with_openssl_rand_hex_32 +# ─── Spam protection ──────────────────────────────────────────────── +# Cloudflare Turnstile for the create form. REQUIRED in production: without +# both keys the form cannot create pastes there. Create a widget at +# dash.cloudflare.com → Turnstile for paste.msk-scripts.de. For local tests +# Cloudflare's test keys always pass: +# site key 1x00000000000000000000AA, secret 1x0000000000000000000000000000000AA +CAPTCHA_SITE_KEY= +CAPTCHA_SECRET_KEY= + +# Pastes and bytes one address may create through the form per 24 hours +DAILY_PASTES_PER_IP=20 +DAILY_BYTES_PER_IP=5242880 + +# Pastes one API key may create per hour (keys: scripts/api-key.sh) +API_KEY_CREATE_PER_HOUR=120 + # ─── Limits ────────────────────────────────────────────────────────── # IMPORTANT: never put inline `# comments` next to a value — systemd's # EnvironmentFile keeps them as part of the variable, which breaks parsing. diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..5b8cbab --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,61 @@ +# ============================================================================= +# Build +# ----------------------------------------------------------------------------- +# Type-checks, tests and compiles the app the same way the deploy workflow +# does, but on every pull request. Without it a change that breaks the build +# only shows up after the merge, when deploy.yml runs on main. +# +# No repository secrets are used on purpose. Pull requests from bots run in a +# context where secrets are empty, and a job that needs them fails there every +# time. The dummy values below are enough to compile: every route is rendered +# on demand, so nothing touches the database at build time. +# ============================================================================= + +name: Build + +on: + push: + branches: [main] + pull_request: + branches: [main] + +permissions: + contents: read + +concurrency: + group: build-${{ github.ref }} + cancel-in-progress: true + +jobs: + build: + name: Build + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v6 + + - name: Setup Node.js + uses: actions/setup-node@v6 + with: + node-version: '24' + cache: npm + + - name: Install dependencies + run: npm ci + + - name: Type-check + run: npm run type-check + + - name: Test + run: npm test + + - name: Build + env: + NEXT_PUBLIC_BASE_URL: http://localhost:3012 + DB_HOST: build-dummy + DB_USER: build-dummy + DB_PASSWORD: build-dummy + DB_NAME: build-dummy + IP_HASH_SECRET: build-dummy-secret-do-not-use + run: npm run build diff --git a/README.md b/README.md index 6578acd..5d6c9b1 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,9 @@ ## 🛡️ Privacy & Security -- **No tracking, no analytics, no third-party scripts** other than Google Fonts. +- **No tracking, no analytics.** The only third-party script is Cloudflare Turnstile on the create page. +- **Spam protection:** Turnstile, a honeypot, a signed form timestamp, daily limits per address and + content checks (encoded files, link spam, repeats). Scripts need an API key. - **IPs are never stored.** Rate limiting uses HMAC-SHA-256(ip, secret). - **Passwords** are stored as bcrypt hashes (cost 12) only. - **Expired pastes are deleted**, not soft-deleted. @@ -82,10 +84,14 @@ Details: [`deployment/README.md`](deployment/README.md). ## 📡 REST API +Reading is open. Creating a paste from a script needs an API key, issued by the operator on the +server (see [API keys](#-api-keys)); without one, `POST /api/pastes` only accepts the site's own form. + ### `POST /api/pastes` ```bash curl -X POST https://paste.msk-scripts.de/api/pastes \ + -H 'Authorization: Bearer mskp_…' \ -H 'Content-Type: application/json' \ -d '{ "content": "console.log(\"hello world\")", @@ -112,7 +118,7 @@ Response: | Method | Path | Purpose | |----------|-------------------------------|--------------------------------------| -| `POST` | `/api/pastes` | Create a paste | +| `POST` | `/api/pastes` | Create a paste (API key required) | | `GET` | `/api/pastes/:id` | Fetch paste metadata + content | | `POST` | `/api/pastes/:id/verify` | Verify password, returns content | | `DELETE` | `/api/pastes/:id?token=…` | Delete a paste with its delete token | @@ -129,6 +135,20 @@ npm run lint # ESLint npm run type-check # TypeScript --noEmit npm run migrate # Apply DB migrations npm run cleanup # Delete expired pastes (run via cron) +npm run api-key -- help # Manage API keys (see below) +``` + +## 🔑 API keys + +Only someone with a shell on the server and read access to the app's `.env` can manage keys. A key is +shown once when it is created; the database keeps only its SHA-256. + +```bash +/opt/msk-paste/scripts/api-key.sh help # all commands +/opt/msk-paste/scripts/api-key.sh list # id, prefix, label, last use, state +/opt/msk-paste/scripts/api-key.sh create "