mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
Divider polish: the `or` separator and the footer rule are brand gradients
rather than flat hairlines. The `or` fades up to the logo blue from each side
toward the word, so the pair reads as one mark centred on it; the footer's is
the same blue, strongest at the centre and gone by both edges, so the card
closes instead of being cut off by a full-width line. Drawn as a pseudo-element
because a border cannot hold a gradient.
The register transition no longer claims "Account created". /api/auth/register
returns the same neutral response whether or not the email already existed --
that is the anti-enumeration guarantee -- so the screen cannot know which
happened, and claiming creation tells an existing owner something untrue. It now
says "Signing you in...", which is what both branches actually do.
Captcha on the support form. The route's own comment admitted the gap
("Turnstile will gate this too once wired"), leaving an unauthenticated endpoint
that sends TWO emails per accepted request: the relay to the private inbox, and
an acknowledgement to a caller-supplied, UNVERIFIED address. The only control
was a per-IP cap of 5/hour, which rotating IPs walk straight past -- and the
cost lands on the sending domain's reputation, which is exactly what puts
recipients on Resend's suppression list. Content injection was already handled:
the ack never echoes the submitted message back.
Now gated by verifyTurnstileDetailed + captchaRejection, the same pair the auth
routes use, checked before any work so a forged token never reaches the mail
provider. The client renders the widget interaction-only and awaits the token on
submit rather than disabling the button, so nothing is visible unless Cloudflare
wants a click. The per-IP and per-recipient limits stay as defence in depth.
worker/test/support-captcha.test.ts was verified against a build with the gate
removed: three of its four cases fail there. The fourth was vacuous at first --
without RESEND_API_KEY in the test env sendMail short-circuits before calling
out, so "sends no mail" passed with or without the gate. The env override sets
one.
102 lines
3.7 KiB
TypeScript
102 lines
3.7 KiB
TypeScript
/**
|
|
* The support form is unauthenticated and sends TWO emails per accepted request: the relay to the
|
|
* private inbox, and an acknowledgement to a caller-supplied, UNVERIFIED address. A per-IP cap
|
|
* does not bound that — rotating IPs walk past it — and the cost lands on the sending domain's
|
|
* reputation, which is exactly what gets recipients onto Resend's suppression list.
|
|
*
|
|
* So the captcha is what makes each attempt cost something, and it must be enforced server-side:
|
|
* the client is the attacker's to modify.
|
|
*/
|
|
import { env } from 'cloudflare:test';
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import app from '../src/index';
|
|
|
|
const ORIGIN = 'https://dev.tokencircles.com';
|
|
|
|
function post(body: unknown, ip = '203.0.113.10') {
|
|
return app.fetch(
|
|
new Request(`${ORIGIN}/api/support/contact`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'CF-Connecting-IP': ip,
|
|
Origin: ORIGIN,
|
|
},
|
|
body: JSON.stringify(body),
|
|
}),
|
|
// RESEND_API_KEY must be set or sendMail short-circuits before ever calling out, which would
|
|
// make the "sends no mail" assertion below pass whether or not the captcha is enforced.
|
|
{
|
|
...env,
|
|
TURNSTILE_SECRET: 'test-secret',
|
|
APP_ENV: 'production',
|
|
SUPPORT_EMAIL: 'x@y.tld',
|
|
RESEND_API_KEY: 'test-key',
|
|
}
|
|
);
|
|
}
|
|
|
|
/** Cloudflare's siteverify, plus Resend so no test ever tries to send real mail. */
|
|
function stubFetch(captchaOk: boolean) {
|
|
return vi.spyOn(globalThis, 'fetch').mockImplementation(async (input: RequestInfo | URL) => {
|
|
const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
|
|
if (url.includes('challenges.cloudflare.com')) {
|
|
return new Response(
|
|
JSON.stringify({ success: captchaOk, 'error-codes': captchaOk ? [] : ['invalid-input'] }),
|
|
{ status: 200 }
|
|
);
|
|
}
|
|
if (url.includes('api.resend.com')) {
|
|
return new Response(JSON.stringify({ id: 'resend-id-1' }), { status: 200 });
|
|
}
|
|
throw new Error(`unexpected fetch: ${url}`);
|
|
});
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe('the support form is captcha-gated', () => {
|
|
it('refuses a request that carries no captcha token', async () => {
|
|
stubFetch(true);
|
|
// A unique IP per test keeps the per-IP limiter from being the thing that rejects.
|
|
const res = await post({ email: 'a@b.tld', message: 'hello there' }, '203.0.113.21');
|
|
expect(res.status).not.toBe(200);
|
|
});
|
|
|
|
it('refuses a request whose token Cloudflare rejects', async () => {
|
|
stubFetch(false);
|
|
const res = await post(
|
|
{ email: 'a@b.tld', message: 'hello there', turnstileToken: 'forged' },
|
|
'203.0.113.22'
|
|
);
|
|
expect(res.status).not.toBe(200);
|
|
});
|
|
|
|
it('accepts a request with a token Cloudflare accepts', async () => {
|
|
stubFetch(true);
|
|
const res = await post(
|
|
{ email: 'a@b.tld', message: 'hello there', turnstileToken: 'good' },
|
|
'203.0.113.23'
|
|
);
|
|
expect(res.status).toBe(200);
|
|
const body = (await res.json()) as { ok?: boolean; ticketId?: string };
|
|
expect(body.ok).toBe(true);
|
|
expect(body.ticketId).toMatch(/^TC-/);
|
|
});
|
|
|
|
it('checks the captcha before doing any work, so a forged token sends no mail', async () => {
|
|
const spy = stubFetch(false);
|
|
await post(
|
|
{ email: 'a@b.tld', message: 'hello there', turnstileToken: 'forged' },
|
|
'203.0.113.24'
|
|
);
|
|
const resendCalls = spy.mock.calls.filter(([input]) => {
|
|
const url = typeof input === 'string' ? input : (input as Request).url;
|
|
return url.includes('api.resend.com');
|
|
});
|
|
expect(resendCalls, 'a rejected captcha must not reach the mail provider').toHaveLength(0);
|
|
});
|
|
});
|