diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0705d49..9e52d77 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,7 +37,7 @@ jobs: - run: npm run lint - run: npm run typecheck - run: npm test - - name: Verify migrations, owner isolation and account deletion + - name: Verify migrations, owner isolation, account deletion and rate limits run: | npm install --prefix "$RUNNER_TEMP/buffer-pglite" --no-package-lock --no-audit --no-fund @electric-sql/pglite@0.5.8 export BUFFER_PGLITE_MODULE="$RUNNER_TEMP/buffer-pglite/node_modules/@electric-sql/pglite/dist/index.js" @@ -45,6 +45,7 @@ jobs: node scripts/verify-hosted-monitoring.mjs node scripts/verify-monitoring-scheduler.mjs node scripts/verify-account-deletion.mjs + node scripts/verify-rate-limit.mjs - run: npm run build # Deterministic browser regressions block the release. External credentials diff --git a/docs/DATABASE-VERIFICATION.md b/docs/DATABASE-VERIFICATION.md index 12a6c85..75f9a63 100644 --- a/docs/DATABASE-VERIFICATION.md +++ b/docs/DATABASE-VERIFICATION.md @@ -221,3 +221,7 @@ The initial scheduler preparation applied as `20260919183534`, but hosted postfl The synchronous trigger migration applied as `20260919185033` (`use_synchronous_monitoring_trigger`). Hosted checks confirm http 1.6, HEAD-only helper with no net calls, non-debug logging, private-helper execution denied to anon/authenticated/service_role, and Vault reads denied to browser roles. The managed base HTTP function retains browser EXECUTE grants; those do not grant access to the private helper or its Vault credential and no persistent request queue is used. Scheduler activation/actual completed runs are recorded in the release evidence. + +## Rate-limit retention and keyed hash (not yet applied) + +`20261008120000_rate_limit_retention_and_keyed_hash.sql` fixes two read-only findings on the hosted limiter: `private.prune_rate_limits()` existed but nothing called it, so counters from 17 September onward were never deleted, and each counter key was the server's unkeyed SHA-256 of the client IP, which brute force reverses for IPv4. `public.consume_rate_limit` keeps its signature, result and secret, but stores HMAC-SHA-256 of the sent key under a random 32-byte key in `private.rate_limit_hash_key` (row-level security on, no grants to anon, authenticated or service_role), refuses windows longer than one minute, deletes expired counters on every check, and `buffer-prune-rate-limits` (pg_cron, every three minutes) removes idle ones, so no counter outlives the last request by more than four minutes. The migration deletes every counter stored under the old hash, records the limiter's tables for fresh projects, and leaves only anon (the server's publishable key, gated by the secret) able to execute the limiter. It can be applied before or after the app deploys. `scripts/verify-rate-limit.mjs` checks it locally against the hosted pre-migration definitions and against an empty project, applying it as a non-superuser owner as hosted Supabase does. diff --git a/docs/DEPLOYMENT.md b/docs/DEPLOYMENT.md index 3c0df97..7d4d700 100644 --- a/docs/DEPLOYMENT.md +++ b/docs/DEPLOYMENT.md @@ -13,7 +13,7 @@ Buffer runs on Node 24 with Next.js App Router. `/` is the public website, `/app | `NEXT_PUBLIC_SUPABASE_URL` | Public | Dedicated Buffer project's API URL. | | `NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY` | Public | Supabase publishable key; never a service-role or secret key. | | `NEXT_PUBLIC_AUTH_EMAIL_READY` | Public | Set `true` only after email signup, confirmation, recovery, redirects, and password policy are verified. It is currently `false`, so public signup and recovery remain visibly unavailable. | -| `RATE_LIMIT_SECRET` | Server only | Optional secret for the protected Supabase `consume_rate_limit` RPC; absent/unreachable shared storage falls back to process limits. Never expose this value to the browser. | +| `RATE_LIMIT_SECRET` | Server only | Optional secret for the protected Supabase `consume_rate_limit` RPC; absent/unreachable shared storage falls back to process limits. It must equal `private.rate_limit_config.secret`; on a fresh project, `20261008120000_rate_limit_retention_and_keyed_hash.sql` generates that value. Never expose this value to the browser. | | `BUFFER_TEST_AUTHORITY` | Local verification only | Optional public authority for manual checks; the application does not load it automatically. | | `BUFFER_ALERT_STORE` | Local worker only | Optional path for the durable local SQLite worker; defaults to `.local/alerts.sqlite` and is ignored by Git. | diff --git a/e2e/legal.spec.ts b/e2e/legal.spec.ts index f4c3580..6a22a00 100644 --- a/e2e/legal.spec.ts +++ b/e2e/legal.spec.ts @@ -10,9 +10,13 @@ test('the privacy policy is public, specific to Buffer and states what deletion await expect(page).toHaveTitle('Privacy policy — Buffer'); await expect(page.getByRole('heading', { level: 1, name: 'Privacy policy' })).toBeVisible(); const main = page.getByRole('main'); - await expect(main).toContainText('Last updated: 7 October 2026'); + await expect(main).toContainText('Last updated: 8 October 2026'); await expect(main).toContainText('Operator Uplift'); await expect(main).toContainText('IPs are hashed for rate limiting and no address is stored.'); + await expect(page.locator('#rate-limiting')).toContainText('HMAC-SHA-256 under a random key that never leaves our database'); + await expect(page.locator('#rate-limiting')).toContainText('No counter is kept more than five minutes after your last request.'); + await expect(page.locator('#retention')).toContainText('no later than five minutes after your last request'); + await expect(main).not.toContainText('one-way SHA-256'); for (const processor of ['Vercel', 'Supabase', 'Solana RPC provider', 'Velocity', 'Pacifica', 'Drift', 'Jupiter', 'Discord']) await expect(main).toContainText(processor); for (const record of ['Saved reports', 'Monitored wallet addresses and alert rules', 'Alert destinations', 'Alert events and delivery records']) await expect(page.locator('#delete-account')).toContainText(record); await expect(page.locator('#delete-account')).toContainText('BUFFER_WEBHOOK_DESTINATIONS_JSON'); @@ -29,7 +33,7 @@ test('the terms carry the Solana dApp Store clause and the read-only limits', as await expect(page).toHaveTitle('Terms of use — Buffer'); await expect(page.getByRole('heading', { level: 1, name: 'Terms of use' })).toBeVisible(); const main = page.getByRole('main'); - await expect(main).toContainText('Last updated: 7 October 2026'); + await expect(main).toContainText('Last updated: 8 October 2026'); await expect(main).toContainText('not trading advice'); await expect(main).toContainText('cross-margin-hold-others-v1'); await expect(main).toContainText('never asks for your seed phrase or private key'); diff --git a/scripts/lib/pglite.mjs b/scripts/lib/pglite.mjs new file mode 100644 index 0000000..ee5faae --- /dev/null +++ b/scripts/lib/pglite.mjs @@ -0,0 +1,12 @@ +/** + * Disposable local PostgreSQL (PGlite) with pgcrypto loaded, because hosted + * Supabase installs pgcrypto in the `extensions` schema and migrations use it. + * BUFFER_PGLITE_MODULE may point at PGlite's dist/index.js, as CI does. + */ +const MODULE = process.env.BUFFER_PGLITE_MODULE || '@electric-sql/pglite'; +const PGCRYPTO = MODULE.endsWith('/index.js') ? MODULE.replace(/index\.js$/, 'contrib/pgcrypto.js') : `${MODULE}/contrib/pgcrypto`; +const [{ PGlite }, { pgcrypto }] = await Promise.all([import(MODULE), import(PGCRYPTO)]); + +export function createDatabase() { + return new PGlite({ extensions: { pgcrypto } }); +} diff --git a/scripts/verify-account-deletion.mjs b/scripts/verify-account-deletion.mjs index b627ed4..f330a51 100644 --- a/scripts/verify-account-deletion.mjs +++ b/scripts/verify-account-deletion.mjs @@ -7,11 +7,11 @@ */ import { readdir, readFile } from 'node:fs/promises'; import assert from 'node:assert/strict'; -const { PGlite } = await import(process.env.BUFFER_PGLITE_MODULE || '@electric-sql/pglite'); +import { createDatabase } from './lib/pglite.mjs'; const DELETION_MIGRATION = '20261007120000_delete_own_account.sql'; const migrations = new URL('../supabase/migrations/', import.meta.url); -const db = new PGlite(); +const db = createDatabase(); let checks = 0; const ownerA = '00000000-0000-4000-8000-00000000000a'; const ownerB = '00000000-0000-4000-8000-00000000000b'; @@ -44,6 +44,8 @@ await db.exec(` alter default privileges in schema public grant execute on functions to anon, authenticated, service_role; alter default privileges for role buffer_migrator in schema public grant execute on functions to anon, authenticated, service_role; create schema private; + -- Hosted Supabase installs pgcrypto here; the rate-limit migration uses it. + create schema extensions; `); const files = (await readdir(migrations)).filter(name => name.endsWith('.sql')).sort(); // It must run after every migration that created an owner-keyed table. diff --git a/scripts/verify-rate-limit.mjs b/scripts/verify-rate-limit.mjs new file mode 100644 index 0000000..ee8dc5f --- /dev/null +++ b/scripts/verify-rate-limit.mjs @@ -0,0 +1,250 @@ +/** + * Local PostgreSQL/WASM checks for the rate-limit retention and keyed-hash + * migration. No hosted connection and no external writes. + * + * `hosted` reproduces the production limiter as it was before this migration + * (definitions and grants read, read-only, from the Buffer project), holding + * counters stored under the old unkeyed SHA-256, with pg_cron present. + * `fresh` starts empty, as a new project does, without pg_cron. + * As on hosted Supabase, the migration runs as a non-superuser role that owns + * the limiter objects (there it is `postgres`; PGlite's superuser has that name). + */ +import { createHash, createHmac } from 'node:crypto'; +import { readFile } from 'node:fs/promises'; +import assert from 'node:assert/strict'; +import { createDatabase } from './lib/pglite.mjs'; + +const MIGRATION = '20261008120000_rate_limit_retention_and_keyed_hash.sql'; +const migration = await readFile(new URL(`../supabase/migrations/${MIGRATION}`, import.meta.url), 'utf8'); +const SECRET = 'f'.repeat(64); +const WINDOW = 60; +let checks = 0; + +const sha256 = value => createHash('sha256').update(value).digest('hex'); +/** The key the deployed server sends (src/server/rate-limit.ts clientKey). */ +const deployedKey = (scope, ip) => `${scope}:${sha256(ip).slice(0, 32)}`; +const rows = async (db, query, params = []) => (await db.query(query, params)).rows; +async function check(db, query, expected, params = []) { assert.deepEqual(await rows(db, query, params), expected); checks++; } +async function rejects(db, role, query, code) { + await db.exec(`reset role; set role ${role}`); + try { await assert.rejects(db.query(query), error => error.code === code, `${role}: ${query}`); checks++; } + finally { await db.exec('reset role'); } +} +async function consume(db, key, { role = 'anon', secret = SECRET, limit = 3, window = WINDOW } = {}) { + await db.exec(`reset role; set role ${role}`); + try { return (await db.query('select * from public.consume_rate_limit($1, $2, $3, $4)', [secret, key, limit, window])).rows[0]; } + finally { await db.exec('reset role'); } +} +const hashKey = async db => Buffer.from((await rows(db, "select encode(key, 'hex') as k from private.rate_limit_hash_key"))[0].k, 'hex'); +/** allowed and remaining are exact; retry_after is the ceiling of the time left, so it may tick down. */ +function expectResult(result, allowed, remaining) { + assert.equal(result.allowed, allowed); assert.equal(result.remaining, remaining); + assert.ok(Number.isInteger(result.retry_after) && result.retry_after >= 1 && result.retry_after <= WINDOW, `retry_after ${result.retry_after}`); + checks++; +} +const storedKeys = async db => (await rows(db, 'select key from private.rate_limits order by key')).map(row => row.key); + +async function supabaseRoles(db) { + await db.exec(` + create role anon; create role authenticated; create role service_role bypassrls; + grant usage on schema public to anon, authenticated, service_role; + -- Supabase grants browser roles EXECUTE on new public functions directly, + -- so revoking PUBLIC alone would leave them able to call a new function. + create role buffer_owner; + alter default privileges in schema public grant execute on functions to anon, authenticated, service_role; + alter default privileges for role buffer_owner in schema public grant execute on functions to anon, authenticated, service_role; + create schema extensions; create extension pgcrypto with schema extensions; + grant usage on schema extensions to buffer_owner; + -- Hosted postgres owns the database; CREATE SCHEMA IF NOT EXISTS checks this first. + do $$ begin execute format('grant create on database %I to buffer_owner', current_database()); end $$; + grant usage, create on schema public to buffer_owner; + create schema private authorization buffer_owner; + `); +} +const asOwner = sql => `set role buffer_owner; ${sql}; reset role;`; + +// ---------------------------------------------------------------- hosted +const hosted = createDatabase(); +await supabaseRoles(hosted); +await hosted.exec(` + set role buffer_owner; + create table private.rate_limit_config(id boolean primary key default true check (id), secret text not null default encode(extensions.gen_random_bytes(32), 'hex')); + create table private.rate_limits(key text primary key, count integer not null, window_start timestamptz not null); + create function private.prune_rate_limits() returns void language sql security definer set search_path = '' + as $$ delete from private.rate_limits where window_start < now() - interval '1 hour'; $$; + create function public.consume_rate_limit(p_secret text, p_key text, p_limit integer, p_window_seconds integer) + returns table(allowed boolean, remaining integer, retry_after integer) + language plpgsql security definer set search_path to 'private', 'pg_temp' + as $function$ + declare + v_now timestamptz := now(); + v_count integer; + v_start timestamptz; + begin + if p_secret is null or p_secret <> (select secret from private.rate_limit_config where id) then + return query select false, 0, p_window_seconds; + return; + end if; + if p_key is null or length(p_key) > 200 or p_limit < 1 or p_window_seconds < 1 then + return query select false, 0, p_window_seconds; + return; + end if; + insert into private.rate_limits (key, count, window_start) + values (p_key, 1, v_now) + on conflict (key) do update + set count = case when private.rate_limits.window_start + make_interval(secs => p_window_seconds) <= v_now then 1 else private.rate_limits.count + 1 end, + window_start = case when private.rate_limits.window_start + make_interval(secs => p_window_seconds) <= v_now then v_now else private.rate_limits.window_start end + returning count, window_start into v_count, v_start; + return query select + v_count <= p_limit, + greatest(p_limit - v_count, 0), + greatest(ceil(extract(epoch from (v_start + make_interval(secs => p_window_seconds) - v_now)))::integer, 1); + end; + $function$; + insert into private.rate_limit_config(secret) values ('${SECRET}'); + reset role; + -- pg_cron as hosted: extension metadata plus a cron.schedule stub that records jobs by name. + insert into pg_catalog.pg_extension(oid, extname, extowner, extnamespace, extrelocatable, extversion) + values (777778, 'pg_cron', current_user::regrole, 'pg_catalog'::regnamespace, false, '1.6.4'); + create schema cron; + create table cron.job(jobid bigserial primary key, jobname text unique, schedule text not null, command text not null, username text not null default current_user); + create function cron.schedule(job_name text, schedule text, command text) returns bigint language sql as $$ + insert into cron.job(jobname, schedule, command) values (job_name, schedule, command) + on conflict (jobname) do update set schedule = excluded.schedule, command = excluded.command returning jobid $$; + grant usage on schema cron to buffer_owner; + grant select, insert, update on cron.job to buffer_owner; + grant usage on sequence cron.job_jobid_seq to buffer_owner; +`); + +// Before: the deployed key is stored as sent, an unkeyed SHA-256 that anyone can recompute from an IP. +const IPS = ['203.0.113.7', '198.51.100.23', '2001:db8::1']; +await hosted.exec(`insert into private.rate_limits values + ('${deployedKey('live-read', IPS[0])}', 4, '2026-09-17 17:42:03+00'), + ('${deployedKey('monitoring', IPS[1])}', 1, '2026-10-07 09:33:43+00')`); +await consume(hosted, deployedKey('live-read', IPS[2])); +const before = await storedKeys(hosted); +assert.ok(before.includes(deployedKey('live-read', IPS[2])), 'fixture reproduces the unkeyed finding'); checks++; +await check(hosted, "select count(*)::int as n from private.rate_limits where window_start < '2026-09-18 00:00+00'", [{ n: 1 }]); + +await hosted.exec(asOwner(migration)); + +// Retention: every row stored under the unkeyed hash is gone, including the 17 September one. +await check(hosted, 'select count(*)::int as n from private.rate_limits', [{ n: 0 }]); +// The configured secret survives, so the deployed RATE_LIMIT_SECRET keeps working. +await check(hosted, 'select secret from private.rate_limit_config', [{ secret: SECRET }]); + +// Keyed hash: the deployed request is stored as HMAC-SHA-256(database key, p_key). +const key = await hashKey(hosted); +assert.equal(key.length, 32); checks++; +const sent = deployedKey('live-read', IPS[0]); +expectResult(await consume(hosted, sent), true, 2); +const [stored] = await storedKeys(hosted); +assert.equal(stored, createHmac('sha256', key).update(sent).digest('hex')); checks++; +for (const unkeyed of [sent, sha256(sent), sha256(IPS[0]), sha256(IPS[0]).slice(0, 32)]) { assert.notEqual(stored, unkeyed); checks++; } +assert.ok(!stored.includes(sha256(IPS[0]).slice(0, 32)) && !stored.includes('live-read'), 'no unkeyed fragment or scope is stored'); checks++; + +// Limits are still enforced per key, and scopes stay separate. +expectResult(await consume(hosted, sent), true, 1); +expectResult(await consume(hosted, sent), true, 0); +expectResult(await consume(hosted, sent), false, 0); +assert.equal((await consume(hosted, deployedKey('monitoring', IPS[0]))).allowed, true); checks++; +await check(hosted, 'select count::int as n from private.rate_limits order by count desc limit 1', [{ n: 4 }]); +await check(hosted, `select count(*)::int as n from private.rate_limits where expires_at = window_start + interval '${WINDOW} seconds'`, [{ n: 2 }]); +// Rollover: a counter whose window has passed, before any prune removes it, starts a new window and a new expiry. +await hosted.query("update private.rate_limits set window_start = now() - interval '2 minutes' where key = $1", [stored]); +expectResult(await consume(hosted, sent), true, 2); +await check(hosted, `select count::int as n, expires_at = window_start + interval '${WINDOW} seconds' and window_start > now() - interval '1 minute' as renewed from private.rate_limits where key = $1`, [{ n: 1, renewed: true }], [stored]); + +// Rejected calls write nothing: wrong, missing or browser-guessed secrets, bad keys and bad limits. +for (const [label, options, call] of [ + ['wrong secret', { secret: 'guess' }, sent], ['null secret', { secret: null }, sent], + ['oversized key', {}, 'k'.repeat(201)], ['zero limit', { limit: 0 }, sent], ['zero window', { window: 0 }, sent], + ['null limit', { limit: null }, sent], ['null window', { window: null }, sent], + // The privacy policy's retention period assumes one-minute windows. + ['window over a minute', { window: WINDOW + 1 }, sent], +]) { + const before = await rows(hosted, 'select key, count from private.rate_limits order by key'); + const result = await consume(hosted, call, options); + assert.equal(result.allowed, false, label); assert.equal(result.remaining, 0, label); + assert.deepEqual(await rows(hosted, 'select key, count from private.rate_limits order by key'), before, label); checks++; +} + +// The check prunes expired counters, its own included, before counting. +await hosted.exec("update private.rate_limits set window_start = now() - interval '2 minutes', expires_at = now() - interval '1 minute'"); +expectResult(await consume(hosted, sent), true, 2); +await check(hosted, 'select count(*)::int as n, min(count)::int as c from private.rate_limits', [{ n: 1, c: 1 }]); +// The scheduled pruner deletes only expired counters. +await hosted.exec(`insert into private.rate_limits values ('expired', 1, now() - interval '3 minutes', now() - interval '2 minutes')`); +await hosted.query('select private.prune_rate_limits()'); +await check(hosted, "select count(*)::int as n from private.rate_limits where key = 'expired'", [{ n: 0 }]); +await check(hosted, 'select count(*)::int as n from private.rate_limits', [{ n: 1 }]); +const jobs = await rows(hosted, 'select jobname, schedule, command, username from cron.job order by jobid'); +assert.deepEqual(jobs, [{ jobname: 'buffer-prune-rate-limits', schedule: '*/3 * * * *', command: 'select private.prune_rate_limits()', username: 'buffer_owner' }]); checks++; +// pg_cron runs the command as the job's user, the owner, despite the revokes. +await hosted.exec(`insert into private.rate_limits values ('idle', 1, now() - interval '4 minutes', now() - interval '3 minutes')`); +await hosted.exec(`set role ${jobs[0].username}; ${jobs[0].command}; reset role;`); +await check(hosted, "select count(*)::int as n from private.rate_limits where key = 'idle'", [{ n: 0 }]); + +// A missing key fails loudly: the RPC errors, so the server falls back to its per-process limit. +await hosted.exec('create table private.saved_key as select * from private.rate_limit_hash_key; delete from private.rate_limit_hash_key;'); +await rejects(hosted, 'anon', `select * from public.consume_rate_limit('${SECRET}', '${sent}', 3, ${WINDOW})`, '55000'); +await hosted.exec('insert into private.rate_limit_hash_key select * from private.saved_key; drop table private.saved_key;'); + +// Privileges. Only anon (the server's publishable key) may call the limiter, and only with the secret. +const limiter = 'public.consume_rate_limit(text,text,integer,integer)'; +for (const [role, permitted] of [['anon', true], ['authenticated', false], ['service_role', false]]) { + await check(hosted, `select has_function_privilege('${role}', '${limiter}', 'EXECUTE') as p`, [{ p: permitted }]); + await check(hosted, `select has_function_privilege('${role}', 'private.prune_rate_limits()', 'EXECUTE') as p`, [{ p: false }]); + for (const table of ['rate_limit_hash_key', 'rate_limits', 'rate_limit_config']) { + await check(hosted, `select has_table_privilege('${role}', 'private.${table}', 'SELECT, INSERT, UPDATE, DELETE') as p`, [{ p: false }]); + await rejects(hosted, role, `select * from private.${table}`, '42501'); + } + await rejects(hosted, role, 'select private.prune_rate_limits()', '42501'); +} +await rejects(hosted, 'authenticated', `select * from public.consume_rate_limit('${SECRET}', 'x', 3, 60)`, '42501'); +await check(hosted, `select relname, relrowsecurity as rls from pg_class where relnamespace = 'private'::regnamespace and relkind = 'r' and relname like 'rate_limit%' order by 1`, + [{ relname: 'rate_limit_config', rls: true }, { relname: 'rate_limit_hash_key', rls: true }, { relname: 'rate_limits', rls: true }]); +await check(hosted, `select proname, prosecdef as definer, proconfig as config, pg_get_userbyid(proowner) as owner from pg_proc where oid in ('${limiter}'::regprocedure, 'private.prune_rate_limits()'::regprocedure) order by 1`, + [{ proname: 'consume_rate_limit', definer: true, config: ['search_path=""'], owner: 'buffer_owner' }, { proname: 'prune_rate_limits', definer: true, config: ['search_path=""'], owner: 'buffer_owner' }]); +await check(hosted, "select pg_get_userbyid(relowner) as owner from pg_class where oid = 'private.rate_limit_hash_key'::regclass", [{ owner: 'buffer_owner' }]); +// Row-level security is a second barrier: even a mistaken grant reveals no key. +await hosted.exec('grant usage on schema private to anon; grant select on private.rate_limit_hash_key to anon; set role anon;'); +await check(hosted, 'select count(*)::int as n from private.rate_limit_hash_key', [{ n: 0 }]); +await hosted.exec('reset role; revoke select on private.rate_limit_hash_key from anon; revoke usage on schema private from anon;'); + +// Re-running the migration is harmless: same key, one job, counters cleared. +await hosted.exec(asOwner(migration)); +assert.deepEqual(await hashKey(hosted), key); checks++; +await check(hosted, 'select count(*)::int as n from cron.job', [{ n: 1 }]); +await check(hosted, 'select count(*)::int as n from private.rate_limits', [{ n: 0 }]); + +// ----------------------------------------------------------------- fresh +const fresh = createDatabase(); +await supabaseRoles(fresh); +await fresh.exec(asOwner(migration)); +const freshSecret = (await rows(fresh, 'select secret from private.rate_limit_config'))[0].secret; +assert.match(freshSecret, /^[0-9a-f]{64}$/); checks++; +assert.equal((await consume(fresh, sent, { secret: SECRET })).allowed, false, 'an unknown secret is refused'); checks++; +assert.equal((await consume(fresh, sent, { secret: freshSecret })).allowed, true); checks++; +const [freshStored] = await storedKeys(fresh); +assert.equal(freshStored, createHmac('sha256', await hashKey(fresh)).update(sent).digest('hex')); checks++; +assert.notEqual(freshStored, stored, 'each database has its own key'); checks++; +for (const [role, permitted] of [['anon', true], ['authenticated', false], ['service_role', false]]) { + await check(fresh, `select has_function_privilege('${role}', '${limiter}', 'EXECUTE') as p`, [{ p: permitted }]); +} +// The empty search_path and qualified names mean a caller's temp tables or a public function cannot stand in. +await fresh.exec(`create function public.hmac(bytea, bytea, text) returns bytea language sql as $$ select '\\x00'::bytea $$; + set role anon; + create temp table rate_limit_hash_key(id boolean, key bytea); insert into rate_limit_hash_key values (true, '\\x${'00'.repeat(32)}'); + create temp table rate_limits(key text, count integer, window_start timestamptz, expires_at timestamptz); + reset role;`); +assert.equal((await consume(fresh, 'shadow-test', { secret: freshSecret })).allowed, true); checks++; +await check(fresh, 'select count(*)::int as n from private.rate_limits where key = $1', [{ n: 1 }], [createHmac('sha256', await hashKey(fresh)).update('shadow-test').digest('hex')]); +await check(fresh, 'select count(*)::int as n from pg_temp.rate_limits', [{ n: 0 }]); +// With no configured secret at all, nothing passes (the hosted version accepted any secret then). +await fresh.exec('delete from private.rate_limit_config'); +assert.equal((await consume(fresh, sent, { secret: 'anything' })).allowed, false); checks++; + +await hosted.close(); +await fresh.close(); +console.log(JSON.stringify({ result: 'PASS', assertions: checks, migration: MIGRATION, database: 'local PostgreSQL (PGlite)', externalWrites: false }, null, 2)); diff --git a/src/app/privacy/page.tsx b/src/app/privacy/page.tsx index b710d7e..41d0025 100644 --- a/src/app/privacy/page.tsx +++ b/src/app/privacy/page.tsx @@ -57,7 +57,8 @@ const sections: LegalSection[] = [
Destinations are Discord channels or signed webhooks that the operator configures on the server for specific accounts. You cannot add one yourself, and their URLs and secrets are never stored in the database. When an alert fires, Buffer sends the alert message, which includes the monitored wallet address, subaccount, metric and values, to that destination.
> }, { id: 'rate-limiting', title: 'Rate limiting and service logs', body: <> -To keep live reads available, Buffer limits how often one connection can read. IPs are hashed for rate limiting and no address is stored. The counter keeps a one-way SHA-256 hash of your IP address, never the address itself, with a request count for the current minute. It holds no account or wallet data.
+To keep live reads available, Buffer limits how often one connection can read. IPs are hashed for rate limiting and no address is stored. The counter keeps a keyed hash derived from your IP address (HMAC-SHA-256 under a random key that never leaves our database), never the address itself, with a request count for the current minute. Without that key, the hash cannot be matched to an address by trying every possible one. It holds no account or wallet data.
+Each counter is deleted when its one-minute window ends: by the next rate-limit check, or by a cleanup that runs every three minutes. No counter is kept more than five minutes after your last request.
Our hosting provider, Vercel, processes every request to serve Buffer and keeps standard request logs under its own policies. These can include your IP address, browser details and the address of the page or API called. For a live read, that includes the public wallet address you looked up.
> }, { id: 'processors', title: 'Service providers', body: <> @@ -79,7 +80,7 @@ const sections: LegalSection[] = [ 'Alert events and delivery records: events that end delivered or suppressed are deleted 30 days after they were observed. Other events stay until you delete your account. Deleting a rule keeps its history until then.', 'Manual check records: 7 days.', 'Reports on your device: until you delete them or clear your browser data.', - 'Rate-limit counters: a hashed key and a count, with no IP address, account or wallet data.', + 'Rate-limit counters: a keyed hash and a count, with no IP address, account or wallet data. Each is deleted when its one-minute window ends, and no later than five minutes after your last request.', 'Vercel and Supabase logs follow their own retention. Deleted data can remain in Supabase backups until those backups expire.', ]} /> }, { id: 'delete-account', title: 'Deleting your account', body: <> diff --git a/src/lib/legal.ts b/src/lib/legal.ts index 30f236e..b955087 100644 --- a/src/lib/legal.ts +++ b/src/lib/legal.ts @@ -1,7 +1,7 @@ /** Facts shared by the privacy policy and the terms. Keep both pages on one revision date. */ export const LEGAL_OPERATOR = 'Operator Uplift'; -export const LEGAL_UPDATED = '7 October 2026'; -export const LEGAL_UPDATED_ISO = '2026-10-07'; +export const LEGAL_UPDATED = '8 October 2026'; +export const LEGAL_UPDATED_ISO = '2026-10-08'; export const SUPPORT_ISSUES_URL = 'https://github.com/operatoruplift/buffer/issues'; export type SupportContact = { kind: 'email' | 'issues'; href: string; label: string }; diff --git a/src/server/rate-limit.ts b/src/server/rate-limit.ts index bd38891..6cd1d68 100644 --- a/src/server/rate-limit.ts +++ b/src/server/rate-limit.ts @@ -8,11 +8,16 @@ import { createHash } from 'node:crypto'; * and therefore not a limit under concurrency: forty parallel requests each * landed on a fresh instance and every one reached mainnet. When Supabase is * configured the count lives in Postgres behind a SECURITY DEFINER function - * guarded by a server-held secret. Keys hash the client IP; no address is - * stored. If the store is unreachable this returns null and the local window - * still applies. + * guarded by a server-held secret. The key sent is a digest of the client IP, + * never the address. The database does not store it as sent: it keeps an + * HMAC-SHA-256 of it under a random key that never leaves the database, and + * deletes each counter once its window ends, within minutes even when idle + * (see supabase/migrations/ + * 20261008120000_rate_limit_retention_and_keyed_hash.sql). If the store is + * unreachable or errors, this returns null and the local window still applies. */ +/** The database refuses longer windows: the privacy policy's retention period depends on it. */ const WINDOW_SECONDS = 60; const TIMEOUT_MS = 1500; diff --git a/supabase/migrations/20261008120000_rate_limit_retention_and_keyed_hash.sql b/supabase/migrations/20261008120000_rate_limit_retention_and_keyed_hash.sql new file mode 100644 index 0000000..d402f2c --- /dev/null +++ b/supabase/migrations/20261008120000_rate_limit_retention_and_keyed_hash.sql @@ -0,0 +1,154 @@ +-- Rate-limit counters: keyed hashing and automatic deletion. +-- +-- Before this migration the limiter stored each key exactly as the server sent +-- it, an unkeyed SHA-256 of the client IP that anyone can recompute for every +-- IPv4 address, and nothing called private.prune_rate_limits(), so rows were +-- never deleted. +-- +-- Now public.consume_rate_limit stores HMAC-SHA-256(key, p_key) under a random +-- 32-byte key that never leaves the database, and deletes expired counters on +-- every check. Where pg_cron is installed (it is on the hosted project) a job +-- also prunes every three minutes, so idle counters go too. Windows are capped +-- at one minute, so a counter is deleted within four minutes of the last +-- request; the privacy policy promises five, leaving a minute for scheduling. +-- +-- Deploy order does not matter. The RPC keeps its signature, arguments, result +-- and secret, and the server keeps sending an opaque digest (never an IP), so +-- the currently deployed app and the next one both work before and after this +-- runs. Nothing new must be configured: the key is generated here. +-- +-- The hosted limiter predates this migration history. Its tables are recorded +-- with their production shape so a fresh project gets the same limiter; where +-- they exist, the IF NOT EXISTS statements do nothing. + +create extension if not exists pgcrypto with schema extensions; +create schema if not exists private; + +create table if not exists private.rate_limit_config ( + id boolean primary key default true check (id), + secret text not null default encode(extensions.gen_random_bytes(32), 'hex') +); +-- A fresh project gets a random secret to copy into RATE_LIMIT_SECRET; the hosted row is kept. +insert into private.rate_limit_config (id) values (true) on conflict (id) do nothing; + +create table if not exists private.rate_limits ( + key text primary key, + count integer not null, + window_start timestamptz not null, + expires_at timestamptz +); +alter table private.rate_limits add column if not exists expires_at timestamptz; + +-- The HMAC key. Only the table owner can read it; the SECURITY DEFINER limiter +-- below is its only user. Row-level security with no policy is a second barrier. +create table if not exists private.rate_limit_hash_key ( + id boolean primary key default true check (id), + key bytea not null default extensions.gen_random_bytes(32) check (octet_length(key) = 32) +); +insert into private.rate_limit_hash_key (id) values (true) on conflict (id) do nothing; + +create or replace function private.prune_rate_limits() +returns void +language plpgsql +security definer +set search_path = '' +as $$ +begin + -- The scheduled pruner waits for a request that is pruning (see + -- consume_rate_limit) instead of skipping its tick. Pruners never run + -- together, so two multi-row deletes cannot deadlock. + perform pg_catalog.pg_advisory_xact_lock(2610080001); + delete from private.rate_limits r where r.expires_at <= pg_catalog.now(); +end; +$$; + +create or replace function public.consume_rate_limit(p_secret text, p_key text, p_limit integer, p_window_seconds integer) +returns table (allowed boolean, remaining integer, retry_after integer) +language plpgsql +security definer +set search_path = '' +as $$ +declare + v_now timestamptz := pg_catalog.now(); + v_hash_key bytea; + v_key text; + v_window interval; + v_count integer; + v_start timestamptz; +begin + -- Refuse unless the server's secret matches. With no configured secret, nothing matches. + if p_secret is null or not exists (select 1 from private.rate_limit_config c where c.id and c.secret = p_secret) then + return query select false, 0, p_window_seconds; + return; + end if; + -- Windows longer than a minute are refused: the privacy policy's retention + -- period depends on it. The server uses 60 seconds. + if p_key is null or length(p_key) > 200 or p_limit is null or p_limit < 1 + or p_window_seconds is null or p_window_seconds < 1 or p_window_seconds > 60 then + return query select false, 0, p_window_seconds; + return; + end if; + + select k.key into v_hash_key from private.rate_limit_hash_key k where k.id; + if v_hash_key is null then + -- The request fails, so the server falls back to its per-process limit. + raise exception 'Rate-limit hash key is missing' using errcode = '55000'; + end if; + v_key := encode(extensions.hmac(convert_to(p_key, 'UTF8'), v_hash_key, 'sha256'), 'hex'); + v_window := make_interval(secs => p_window_seconds); + + -- Delete expired counters first. An expired counter holds no state: the next + -- request after expiry starts a new window either way. If another call is + -- already pruning, skip rather than wait; it removes the same rows. + if pg_catalog.pg_try_advisory_xact_lock(2610080001) then + delete from private.rate_limits x where x.expires_at <= v_now; + end if; + + insert into private.rate_limits as r (key, count, window_start, expires_at) + values (v_key, 1, v_now, v_now + v_window) + on conflict (key) do update + set count = case when r.window_start + v_window <= v_now then 1 else r.count + 1 end, + window_start = case when r.window_start + v_window <= v_now then v_now else r.window_start end, + expires_at = case when r.window_start + v_window <= v_now then v_now + v_window else r.window_start + v_window end + returning r.count, r.window_start into v_count, v_start; + + return query select + v_count <= p_limit, + greatest(p_limit - v_count, 0), + greatest(ceil(extract(epoch from (v_start + v_window - v_now)))::integer, 1); +end; +$$; + +-- Rows stored under the old unkeyed hash are deleted. They are one-minute +-- counters, so the only effect is that current windows start again. +delete from private.rate_limits; +alter table private.rate_limits alter column expires_at set not null; +create index if not exists rate_limits_expires_at_idx on private.rate_limits (expires_at); + +alter table private.rate_limit_config enable row level security; +alter table private.rate_limits enable row level security; +alter table private.rate_limit_hash_key enable row level security; +revoke all on table private.rate_limit_config, private.rate_limits, private.rate_limit_hash_key + from public, anon, authenticated, service_role; + +-- Supabase grants browser roles EXECUTE on public functions directly, so +-- revoking PUBLIC alone is not enough. The server calls the limiter with the +-- publishable key, which is the anon role; the secret gates every call. +revoke all on function private.prune_rate_limits() from public, anon, authenticated, service_role; +revoke all on function public.consume_rate_limit(text, text, integer, integer) from public, anon, authenticated, service_role; +grant execute on function public.consume_rate_limit(text, text, integer, integer) to anon; + +do $$ +begin + if exists (select 1 from pg_catalog.pg_extension where extname = 'pg_cron') then + -- Upserts by name, so applying this again keeps one job. + perform cron.schedule('buffer-prune-rate-limits', '*/3 * * * *', 'select private.prune_rate_limits()'); + else + raise notice 'pg_cron is not installed; expired rate-limit counters are pruned by each rate-limit check only.'; + end if; +end; +$$; + +comment on table private.rate_limits is 'Per-client request counters. key is HMAC-SHA-256 under private.rate_limit_hash_key, never an IP or an unkeyed hash. Rows are deleted once expires_at passes.'; +comment on table private.rate_limit_hash_key is 'Random HMAC key for rate-limit counter keys. Owner-only; never returned by any function.'; +comment on function public.consume_rate_limit(text, text, integer, integer) is 'Shared rate limit gated by private.rate_limit_config.secret. Stores HMAC-SHA-256 of p_key and prunes expired counters.'; diff --git a/tests/legal.test.ts b/tests/legal.test.ts index 4512e5f..96432fc 100644 --- a/tests/legal.test.ts +++ b/tests/legal.test.ts @@ -4,8 +4,8 @@ import { LEGAL_OPERATOR, LEGAL_UPDATED, LEGAL_UPDATED_ISO, SUPPORT_ISSUES_URL, s describe('legal page facts', () => { it('names the operator and the shared revision date', () => { expect(LEGAL_OPERATOR).toBe('Operator Uplift'); - expect(LEGAL_UPDATED).toBe('7 October 2026'); - expect(LEGAL_UPDATED_ISO).toBe('2026-10-07'); + expect(LEGAL_UPDATED).toBe('8 October 2026'); + expect(LEGAL_UPDATED_ISO).toBe('2026-10-08'); }); it('uses the configured support email when it is a plain address', () => { diff --git a/tests/rate-limit.test.ts b/tests/rate-limit.test.ts new file mode 100644 index 0000000..8fc996f --- /dev/null +++ b/tests/rate-limit.test.ts @@ -0,0 +1,101 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +vi.mock('server-only', () => ({})); +import { clientKey, consumeSharedLimit } from '../src/server/rate-limit'; + +const URL_BASE = 'https://project.supabase.example'; +const PUBLISHABLE = 'sb_publishable_test'; +const SECRET = 'server-only-rate-limit-secret'; +const IP = '203.0.113.7'; + +const request = (headers: Record