Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# PostgreSQL connection string (serverless/pooled). Supabase Vercel
# integration sets POSTGRES_URL automatically (aws-<region>.pooler.supabase.com:6543).
POSTGRES_URL=

# Direct (non-pooled) connection used by `drizzle-kit migrate`. Supabase
# users: db.<project-ref>.supabase.co:5432.
POSTGRES_URL_NON_POOLING=

# Secret used to encrypt per-paste keys at rest. Generate a strong random value,
# e.g. `openssl rand -hex 32`. Keep this stable across deploys or pastes break.
PASTE_MASTER_KEY=replace-with-a-long-random-string

# Public base URL of the app (used to build shareable links).
APP_URL=https://your-app.vercel.app

# Upstash Redis (Vercel KV) credentials for rate limiting (optional for local dev).
# Vercel's "KV" integration (Upstash) provides KV_REST_API_URL / KV_REST_API_TOKEN.
KV_REST_API_URL=
KV_REST_API_TOKEN=

# Self-hosting only (not needed on Vercel): the request header your reverse
# proxy sets to the real client IP, e.g. cf-connecting-ip or x-real-ip. Rate
# limits are per IP; without a trusted header all clients share one limit.
TRUSTED_IP_HEADER=

# Secret that the /api/cron/cleanup endpoint expects in the Authorization header.
CRON_SECRET=replace-with-a-random-string

# Vercel Blob store (private access). Create a Blob store in the Vercel
# Storage tab and paste the BLOB_READ_WRITE_TOKEN value here.
BLOB_READ_WRITE_TOKEN=replace-with-vercel-blob-token
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ concurrency:
jobs:
check:
runs-on: ubuntu-latest
# Redis behind Upstash's REST protocol, for the rate-limit script tests
# (src/lib/rate-limit.redis.test.ts). SRH reaches Redis by service name
# on the services' shared network; the job reaches SRH on localhost.
services:
redis:
image: redis:6.2
srh:
image: hiett/serverless-redis-http:latest
ports:
- 8079:80
env:
SRH_MODE: env
SRH_TOKEN: ci-test-token
SRH_CONNECTION_STRING: redis://redis:6379
steps:
- uses: actions/checkout@v4
# Version comes from "packageManager" in package.json.
Expand All @@ -27,4 +41,7 @@ jobs:
- run: pnpm typecheck
- run: pnpm lint
- run: pnpm test
env:
TEST_KV_REST_API_URL: http://localhost:8079
TEST_KV_REST_API_TOKEN: ci-test-token
- run: pnpm build
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ yarn-error.log*

# env files (can opt-in for committing if needed)
.env*
!.env.example

# vercel
.vercel
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ A short-URL pastebin built with **Next.js 16 (App Router) + TypeScript + Drizzle
- **Encrypted file attachments** — up to 20 files per paste, 50 MB each, 100 MB total; files-only pastes (no text) are allowed. Add files with the picker, drag-and-drop anywhere on the page, or paste from the clipboard. Files are compressed (when smaller; a 256 KB sample is tried first so already-compressed media skips it) and encrypted client-side with the paste's content key, then uploaded straight to Vercel Blob, up to 3 at a time within a 64 MB in-flight budget so large files go one by one; filenames and MIME types are stored encrypted too. Viewers download the encrypted blobs straight from the store and decrypt them in the browser.
- **Resilient uploads** — files over 8 MB use Vercel Blob multipart uploads (8 MB parts, retried individually), so a dropped connection on a slow or mobile network retries one part instead of restarting the file. Overall progress is shown and uploads can be cancelled.
- **Delete your own paste** — the creating browser saves a delete token in `localStorage`, so the creator sees a **Delete** button on the paste page until it expires, even after closing the tab. A "Copy delete link" (`/<code>#delete=<token>`) works from other devices. The token lives in the URL fragment, which browsers never send to the server, and only its SHA-256 hash is stored.
- **Viewer tools** — wrap long lines, line numbers (pastes up to 5,000 lines), download text as `.txt`, copy, per-file download progress, and "Download all (.zip)" for multi-file pastes. The share button uses the native share sheet where available and falls back to copying the link.
- **Viewer tools** — wrap long lines, line numbers (pastes up to 5,000 lines), download text as `.txt`, copy, per-file download progress, and "Download all (.zip)" for multi-file pastes. The share button uses the native share sheet where available and falls back to copying the link, and a **QR code** opens the new paste's link on another device.
- **Syntax highlighting** — opt-in from the viewer's language picker (32 languages, or "Detect language"), for pastes up to 200K characters, with or without line numbers. Text stays plain by default because detection can't reliably tell code from prose. The highlighter loads only when a language is picked, and renders through React elements (no HTML injection).
- **UI** — light / dark / system theme toggle, slim theme-aware scrollbars, and a no-scroll responsive layout that compacts itself on short screens and landscape phones.
- **Rate limiting** — create + read limits via Vercel KV / Upstash Redis (per-IP, sliding window).
- **Hardened security headers** — strict CSP (`nonce` + `strict-dynamic`, `script-src-attr 'none'`), Referrer-Policy, nosniff, `X-Frame-Options: DENY`, Permissions-Policy, Cross-Origin-Opener-Policy, Cross-Origin-Resource-Policy. File downloads are always served as `application/octet-stream` with a sandboxing CSP.
Expand All @@ -21,8 +22,9 @@ A short-URL pastebin built with **Next.js 16 (App Router) + TypeScript + Drizzle

| Layer | Choice |
|---|---|
| Framework | Next.js 16.3.8 (App Router), TypeScript |
| Framework | Next.js 16.4.6 (App Router), TypeScript |
| UI | Tailwind CSS v4, Geist fonts (`next/font`), lucide-react icons |
| Highlighting / QR | lowlight (highlight.js grammars) + hast-util-to-jsx-runtime; uqr |
| ORM | Drizzle (+ postgres.js driver, drizzle-kit migrations) |
| Database | PostgreSQL on Supabase (RLS enabled, Data API grants revoked) |
| Crypto | Node `crypto` / WebCrypto — AES-256-GCM, PBKDF2-SHA256, master-key wrapping |
Expand Down Expand Up @@ -56,6 +58,7 @@ KV_REST_API_URL= # optional — rate limiting (Vercel KV / Upstash)
KV_REST_API_TOKEN=
BLOB_READ_WRITE_TOKEN= # optional — file attachments (Vercel Blob store)
CRON_SECRET= # guards the cleanup endpoint
TRUSTED_IP_HEADER= # self-hosting only — header your proxy sets to the client IP
```

> **PASTE_MASTER_KEY must stay constant** across deploys, or stored pastes can no longer be decrypted. Keep it secret.
Expand Down Expand Up @@ -142,7 +145,7 @@ Both tables have row-level security enabled, and the Supabase `anon` / `authenti
- Upload quota: 300 MB of uploads per IP and 300 MB across all clients per UTC day (pastes live up to 3 days, so the Blob store stays under the 1 GB Hobby limit). The client declares each encrypted file's size when requesting its upload token; the server counts it against the quota in Redis and issues a token capped at exactly that size, so Blob rejects anything larger. Each token stores one blob at exactly `files/<uuid>` (no random suffix, no overwrite), so a token can't be reused to upload again on a single quota charge.
- Per-paste file size check: the declared size is also recorded in Redis per upload path (3 h, one token per path), so `POST /api/pastes` checks the real 100 MB total with one Redis lookup. Files without a record (no Redis, or expired) fall back to a Blob `head()` per file.

Limits are keyed by an HMAC of the client IP (keyed from `PASTE_MASTER_KEY`), so raw IPs are never sent to or stored in Redis. When KV env vars are absent, rate limiting and the upload quota are disabled (fine for local dev).
Limits are keyed by an HMAC of the client IP (keyed from `PASTE_MASTER_KEY`), so raw IPs are never sent to or stored in Redis. The IP comes only from a header a proxy in front of the app overwrites, never from one a client could forge: `x-real-ip` on Vercel, or the header named in `TRUSTED_IP_HEADER` when self-hosting (e.g. `cf-connecting-ip`). Without either, all clients share one bucket, so limits get stricter rather than bypassable. When KV env vars are absent, rate limiting and the upload quota are disabled (fine for local dev).

### Cleanup

Expand Down
3 changes: 0 additions & 3 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import nextTs from "eslint-config-next/typescript";
const eslintConfig = defineConfig([
...nextVitals,
...nextTs,
// eslint-plugin-react's "detect" calls context.getFilename(), which ESLint
// 10 removed; a fixed version skips detection.
{ settings: { react: { version: "19.2" } } },
// Override default ignores of eslint-config-next.
globalIgnores([
// Default ignores of eslint-config-next:
Expand Down
41 changes: 23 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,35 @@
"test": "vitest run"
},
"dependencies": {
"@upstash/ratelimit": "^2.0.8",
"@upstash/redis": "^1.38.3",
"@upstash/ratelimit": "^2.2.1",
"@upstash/redis": "^1.41.0",
"@vercel/analytics": "^2.0.1",
"@vercel/blob": "^2.8.0",
"@vercel/blob": "^2.9.0",
"@vercel/speed-insights": "^2.1.0",
"drizzle-orm": "^0.45.2",
"lucide-react": "^1.41.0",
"nanoid": "^6.0.1",
"next": "16.3.8",
"drizzle-orm": "^0.45.4",
"hast-util-to-jsx-runtime": "^2.3.7",
"lowlight": "^3.3.0",
"lucide-react": "^1.76.0",
"nanoid": "^6.0.2",
"next": "16.4.6",
"postgres": "^3.4.9",
"react": "19.2.8",
"react-dom": "19.2.8",
"zod": "^4.4.3"
"react": "19.3.1",
"react-dom": "19.3.1",
"uqr": "^0.1.3",
"zod": "^4.6.7"
},
"devDependencies": {
"@tailwindcss/postcss": "^4.3.3",
"@types/node": "^26.4.0",
"@types/react": "^19.2.18",
"@types/react-dom": "^19.2.5",
"@tailwindcss/postcss": "^4.3.5",
"@types/hast": "^3.0.5",
"@types/node": "^26.6.6",
"@types/react": "^19.3.0",
"@types/react-dom": "^19.3.0",
"@typescript/native": "npm:typescript@^7.0.2",
"drizzle-kit": "^0.31.10",
"eslint": "^10.9.1",
"eslint-config-next": "16.3.8",
"tailwindcss": "^4.3.3",
"drizzle-kit": "^0.31.11",
"eslint": "^10.15.0",
"eslint-config-next": "16.4.6",
"jsqr": "^1.4.0",
"tailwindcss": "^4.3.5",
"typescript": "npm:@typescript/typescript6@^6.0.2",
"vitest": "^5.1.1"
},
Expand Down
Loading
Loading