diff --git a/.env.example b/.env.example index 24ec5f3..812f3d7 100644 --- a/.env.example +++ b/.env.example @@ -20,6 +20,21 @@ IP_HASH_SECRET=change_me_with_openssl_rand_hex_32 # Rate-Limit: Max. Link-Erstellungen pro IP / Stunde RATE_LIMIT_CREATE_PER_HOUR=20 +# ─── Spamschutz ─────────────────────────────────────────────────────── +# Cloudflare Turnstile für das Formular. In Produktion PFLICHT: ohne beide +# Schlüssel kürzt das Formular dort nichts. Widget anlegen unter +# dash.cloudflare.com → Turnstile für s.msk-scripts.de. Für lokale Tests +# bestehen Cloudflares Testschlüssel immer: +# Site-Key 1x00000000000000000000AA, Secret 1x0000000000000000000000000000000AA +CAPTCHA_SITE_KEY= +CAPTCHA_SECRET_KEY= + +# Links, die eine IP über das Formular in 24 Stunden kürzen darf +DAILY_LINKS_PER_IP=20 + +# Links, die ein API-Schlüssel pro Stunde kürzen darf (Schlüssel: scripts/api-key.sh) +API_KEY_CREATE_PER_HOUR=120 + # ─── Short-Code Settings ────────────────────────────────────────────── # Gültig sind 3 bis 16. Ungültige Werte fallen auf 7 zurück. SHORTCODE_LENGTH=7 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cecee1c..1e5a3c5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,8 +1,8 @@ # ============================================================================= # Build # ----------------------------------------------------------------------------- -# 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 +# 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 @@ -44,6 +44,12 @@ jobs: - 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:3001 diff --git a/README.md b/README.md index 4a2c558..0b4350b 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Part of the **MSK ecosystem** – open source, privacy-friendly, no tracking, no | 🔌 **REST API** | Fully documented endpoints for automation | | 🌍 **Bilingual UI** | German and English, switchable per user (cookie-based) | | 🛡️ **Privacy-friendly** | IP hashing, no GeoIP, no third-party trackers, no analytics cookies | -| ⚡ **Rate limiting** | 20 links/hour per IP to prevent abuse | +| ⚡ **Spam protection** | Cloudflare Turnstile, daily limits per IP, phishing and shortener-chain checks, API keys for scripts | | 🗑️ **Delete tokens** | Every short link comes with a one-time delete token | --- @@ -111,7 +111,8 @@ This walks you through domain setup, MariaDB, Apache, Let's Encrypt SSL, systemd ## 📡 REST API -Every feature in the web UI is available through the REST API. +Reading is open. Shortening a link from a script needs an API key, issued by the operator on the +server (see [API keys](#-api-keys)); without one, `POST /api/links` only accepts the site's own form. Full documentation: **[docu.msk-scripts.de/shortener](https://docu.msk-scripts.de/shortener)** @@ -119,6 +120,7 @@ Full documentation: **[docu.msk-scripts.de/shortener](https://docu.msk-scripts.d ```bash curl -X POST https://s.msk-scripts.de/api/links \ + -H "Authorization: Bearer msks_…" \ -H "Content-Type: application/json" \ -d '{ "url": "https://msk-scripts.de", @@ -143,7 +145,7 @@ curl -X POST https://s.msk-scripts.de/api/links \ | Method | Path | Description | |---|---|---| -| `POST` | `/api/links` | Create a new short link | +| `POST` | `/api/links` | Create a new short link (API key required) | | `GET` | `/api/links/:code` | Look up a link | | `DELETE` | `/api/links/:code` | Delete a link (requires `deleteToken`) | | `GET` | `/api/links/:code/stats` | Get click statistics for one link | @@ -153,6 +155,21 @@ curl -X POST https://s.msk-scripts.de/api/links \ --- +## 🔑 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 a scrypt hash of it. + +```bash +/opt/msk-shortener/scripts/api-key.sh help # all commands +/opt/msk-shortener/scripts/api-key.sh list # id, prefix, label, last use, state +/opt/msk-shortener/scripts/api-key.sh create "