diff --git a/apps/events/backend/src/app.js b/apps/events/backend/src/app.js index c18be5c..724f4be 100644 --- a/apps/events/backend/src/app.js +++ b/apps/events/backend/src/app.js @@ -99,8 +99,10 @@ const globalLimiter = rateLimit({ standardHeaders: true, legacyHeaders: false, message: { message: 'Too many requests, please try again later.' }, - skip: (req) => req.path.startsWith('/api/webhooks'), + skip: (req) => RAW_BODY_PREFIXES.some(p => req.path.startsWith(p)), }); +// Incoming webhooks and links in sent messages on v2 (1.51.16): see src/v2/links.ts. +const { RAW_BODY_PREFIXES, webhooksRouter, linksRouter, legacyLinkRedirects } = require('./v2/links'); // /api/v2 answers every error as { error: { code, message } }, including the shared // middleware below (rate limits, read-only guards, auth) that writes v1's { message }. app.use('/api/v2', require('./v2/errors').legacyErrorsToV2); @@ -115,7 +117,7 @@ app.use(require('./utils/impersonation').readOnlyGuard); // Raw body parsing middleware for webhooks app.use((req, res, next) => { - if (req.path.startsWith('/api/webhooks')) { + if (RAW_BODY_PREFIXES.some(p => req.path.startsWith(p))) { getRawBody(req, { length: req.headers['content-length'], encoding: 'utf-8' @@ -161,6 +163,7 @@ const privacyRoutes = require('./routes/privacyRoutes'); // Mount webhook routes BEFORE JSON body parser to avoid double-reading the stream app.use('/api/webhooks', webhookRoutes); +app.use('/api/v2/webhooks', webhooksRouter); // Apply JSON body parser for all non-webhook routes app.use(express.json()); @@ -196,7 +199,10 @@ app.get('/health', async (req, res) => { // Use routes // /api/v2 (1.51.0, TypeScript): consistent errors and paging, described by OpenAPI at /docs/v2. +app.use('/api/v2', linksRouter); app.use('/api/v2', require('./v2').v2Router()); +// Links in messages already sent: v1 addresses answer 308 with their v2 one. +app.use(legacyLinkRedirects); app.use('/docs/v2', require('./v2').v2DocsRouter()); app.use('/api/hub', require('./routes/hubRoutes')); app.use('/api/campuses', require('./routes/campusRoutes')); diff --git a/apps/events/backend/src/controllers/webhookController.js b/apps/events/backend/src/controllers/webhookController.js index 136fa68..2904c6d 100644 --- a/apps/events/backend/src/controllers/webhookController.js +++ b/apps/events/backend/src/controllers/webhookController.js @@ -5,16 +5,6 @@ const { handleProviderWebhook } = require('../payments/webhook'); // @access Public const handleYocoWebhook = handleProviderWebhook('yoco'); -const handleWhatsappWebhook = async (req, res) => { - try { - const { body } = req; - const { message } = body; - } catch (error) { - res.status(500).json({ error: 'Internal Server Error' }); - } -} - module.exports = { handleYocoWebhook, - handleWhatsappWebhook, }; diff --git a/apps/events/backend/src/routes/webhookRoutes.js b/apps/events/backend/src/routes/webhookRoutes.js index f5fbebd..298252c 100644 --- a/apps/events/backend/src/routes/webhookRoutes.js +++ b/apps/events/backend/src/routes/webhookRoutes.js @@ -1,11 +1,12 @@ const express = require('express'); const router = express.Router(); -const { handleYocoWebhook, handleWhatsappWebhook } = require('../controllers/webhookController'); +const { handleYocoWebhook } = require('../controllers/webhookController'); const { handleProviderWebhook } = require('../payments/webhook'); // Payment provider webhooks (src/payments) router.post('/yoco', handleYocoWebhook); router.post('/payfast', handleProviderWebhook('payfast')); -router.post('/whatsapp', handleWhatsappWebhook); +// The WAWP handler (WAWP is registered at /api/whatsapp/webhook, now /api/v2/webhooks/whatsapp). +router.post('/whatsapp', ...require('../v2/links').whatsappWebhook); module.exports = router; \ No newline at end of file diff --git a/apps/events/backend/src/utils/notifications.js b/apps/events/backend/src/utils/notifications.js index fba6d68..a0e65f6 100644 --- a/apps/events/backend/src/utils/notifications.js +++ b/apps/events/backend/src/utils/notifications.js @@ -34,7 +34,7 @@ function getBackendUrl() { function calendarLinkRow(eventId) { const backendUrl = getBackendUrl(); if (!backendUrl || !eventId) return ''; - return secondaryLink('Add to calendar', `${backendUrl}/api/events/${eventId}/ics`); + return secondaryLink('Add to calendar', `${backendUrl}/api/v2/events/${eventId}/calendar.ics`); } function getOrg() { diff --git a/apps/events/backend/src/utils/payfast.js b/apps/events/backend/src/utils/payfast.js index f0c2db4..fcdd57a 100644 --- a/apps/events/backend/src/utils/payfast.js +++ b/apps/events/backend/src/utils/payfast.js @@ -38,7 +38,7 @@ const payfastHost = (mode) => HOSTS[mode === 'live' ? 'live' : 'sandbox']; /** Where PayFast should post ITNs: BACKEND_URL (or the request's origin) + /api/webhooks/payfast. */ function payfastNotifyUrl(req) { const base = (process.env.BACKEND_URL || (req ? `${req.protocol}://${req.get('host')}` : '')).replace(/\/$/, ''); - return base ? `${base}/api/webhooks/payfast` : ''; + return base ? `${base}/api/v2/webhooks/payfast` : ''; } /** Why PayFast can't reach the notify URL (null when it can). */ diff --git a/apps/events/backend/src/utils/privacy.js b/apps/events/backend/src/utils/privacy.js index 431302e..fe19a3b 100644 --- a/apps/events/backend/src/utils/privacy.js +++ b/apps/events/backend/src/utils/privacy.js @@ -120,7 +120,7 @@ function unsubscribePageUrl(target) { /** RFC 8058 one-click endpoint, for the List-Unsubscribe header (mail apps POST to it). */ function oneClickUrl(target) { - return `${backendUrl()}/api/privacy/unsubscribe/${encodeURIComponent(unsubscribeToken(target))}/one-click`; + return `${backendUrl()}/api/v2/unsubscribe/${encodeURIComponent(unsubscribeToken(target))}/one-click`; } /** What an unsubscribe link refers to, for the landing page. */ diff --git a/apps/events/backend/src/utils/whatsapp.js b/apps/events/backend/src/utils/whatsapp.js index b4a7a29..d81fc6a 100644 --- a/apps/events/backend/src/utils/whatsapp.js +++ b/apps/events/backend/src/utils/whatsapp.js @@ -314,7 +314,7 @@ async function getWebhookSecret() { /** The URL to paste into WAWP's webhook settings (events: message, session.status). */ async function webhookUrl() { const base = String(process.env.BACKEND_URL || process.env.API_URL || 'http://localhost:5000').replace(/\/$/, ''); - return `${base}/api/whatsapp/webhook?secret=${encodeURIComponent(await getWebhookSecret())}`; + return `${base}/api/v2/webhooks/whatsapp?secret=${encodeURIComponent(await getWebhookSecret())}`; } /** The sender's phone (27…) from a WAWP chat id, or null for groups, channels and LIDs. */ diff --git a/apps/events/backend/src/utils/yoco.js b/apps/events/backend/src/utils/yoco.js index fa490c8..ce66ae8 100644 --- a/apps/events/backend/src/utils/yoco.js +++ b/apps/events/backend/src/utils/yoco.js @@ -114,7 +114,7 @@ const deleteWebhook = (id) => yocoRequest('delete', `/webhooks/${encodeURIComp */ function defaultWebhookUrl(req) { const base = (process.env.BACKEND_URL || (req ? `${req.protocol}://${req.get('host')}` : '')).replace(/\/$/, ''); - return base ? `${base}/api/webhooks/yoco` : ''; + return base ? `${base}/api/v2/webhooks/yoco` : ''; } /** Returns an error message if Yoco won't be able to reach the URL, otherwise null. */ diff --git a/apps/events/backend/src/v2/deprecation.ts b/apps/events/backend/src/v2/deprecation.ts index a4fbee8..45c1664 100644 --- a/apps/events/backend/src/v2/deprecation.ts +++ b/apps/events/backend/src/v2/deprecation.ts @@ -46,6 +46,10 @@ const SUCCESSORS: Successor[] = [ { method: 'POST', v1: /^\/api\/events\/([^/]+)\/copy-from$/, v2: m => `/api/v2/events/${m[1]}/copy-from` }, { method: 'GET', v1: /^\/api\/events\/([^/]+)\/notify-recipients$/, v2: m => `/api/v2/events/${m[1]}/notify-recipients` }, { method: 'PUT', v1: /^\/api\/events\/([^/]+)\/notify-recipients$/, v2: m => `/api/v2/events/${m[1]}/notify-recipients` }, + // 1.51.16: incoming webhooks and links in sent messages (v1 still answered by the same code) + { method: 'POST', v1: /^\/api\/webhooks\/(yoco|payfast|whatsapp)$/, v2: m => `/api/v2/webhooks/${m[1]}` }, + { method: 'POST', v1: /^\/api\/whatsapp\/webhook$/, v2: () => '/api/v2/webhooks/whatsapp' }, + { method: 'POST', v1: /^\/api\/privacy\/unsubscribe\/([^/]+)\/one-click$/, v2: m => `/api/v2/unsubscribe/${m[1]}/one-click` }, // 1.51.15: privacy and admin { method: 'GET', v1: /^\/api\/privacy\/me$/, v2: () => '/api/v2/me/privacy' }, { method: 'PUT', v1: /^\/api\/privacy\/me\/news$/, v2: () => '/api/v2/me/privacy/news' }, diff --git a/apps/events/backend/src/v2/links.ts b/apps/events/backend/src/v2/links.ts new file mode 100644 index 0000000..aa972bd --- /dev/null +++ b/apps/events/backend/src/v2/links.ts @@ -0,0 +1,52 @@ +/** + * Incoming webhooks and the links in sent messages on v2 (1.51.16). These aren't JSON API calls, + * so they're plain Express routes, mounted by app.js beside the v2 router: + * + * - `webhooksRouter` at /api/v2/webhooks: Yoco, PayFast and WAWP (WhatsApp) post here. New + * provider registrations use these URLs; the v1 paths (/api/webhooks/…, /api/whatsapp/webhook) + * are still answered by the same handlers, because providers may not follow a redirect on POST. + * - `linksRouter` at /api/v2: the one-click unsubscribe link (RFC 8058) from email headers. + * - `legacyLinkRedirects` at /api: links in messages already sent (calendar files, opening a + * one-click unsubscribe link) answer 308 with their v2 address. A mail app's one-click POST to + * the v1 address is still handled there, like a webhook. + */ +import express, { Router, type RequestHandler } from 'express'; + +/* eslint-disable @typescript-eslint/no-explicit-any -- the handlers are JavaScript */ +const { handleYocoWebhook } = require('../controllers/webhookController') as { handleYocoWebhook: RequestHandler }; +const { handleProviderWebhook } = require('../payments/webhook') as { handleProviderWebhook(id: string): RequestHandler }; +const { handleWebhook: handleWhatsappWebhook } = require('../controllers/whatsappController') as { handleWebhook: RequestHandler }; +const privacy = require('../controllers/privacyController') as { oneClickRedirect: RequestHandler; oneClickUnsubscribe: RequestHandler }; + +/** Paths whose request body is read raw (signatures are checked over the exact bytes). */ +export const RAW_BODY_PREFIXES = ['/api/webhooks', '/api/v2/webhooks']; + +/** WAWP sends JSON (and ?secret=…); on the webhook paths its body arrives raw. */ +export const whatsappWebhook: RequestHandler[] = [(req: any, _res, next) => { + try { req.body = req.rawBody ? JSON.parse(req.rawBody) : {}; } catch { req.body = {}; } + next(); +}, handleWhatsappWebhook]; + +export const webhooksRouter = Router(); +webhooksRouter.post('/yoco', handleYocoWebhook); +webhooksRouter.post('/payfast', handleProviderWebhook('payfast')); +webhooksRouter.post('/whatsapp', ...whatsappWebhook); + +export const linksRouter = Router(); +linksRouter.get('/unsubscribe/:token/one-click', privacy.oneClickRedirect); +linksRouter.post('/unsubscribe/:token/one-click', express.urlencoded({ extended: false }), privacy.oneClickUnsubscribe); + +/** v1 links in messages already sent → their v2 address (308 keeps the method). */ +const REDIRECTS: { method: string; v1: RegExp; v2: (m: RegExpMatchArray) => string }[] = [ + { method: 'GET', v1: /^\/api\/events\/([^/]+)\/ics$/, v2: m => `/api/v2/events/${m[1]}/calendar.ics` }, + { method: 'GET', v1: /^\/api\/privacy\/unsubscribe\/([^/]+)\/one-click$/, v2: m => `/api/v2/unsubscribe/${m[1]}/one-click` }, +]; + +export const legacyLinkRedirects: RequestHandler = (req, res, next) => { + const [path, query] = req.originalUrl.split('?'); + for (const r of REDIRECTS) { + const m = req.method === r.method ? path.match(r.v1) : null; + if (m) return res.redirect(308, r.v2(m) + (query ? `?${query}` : '')); + } + next(); +}; diff --git a/apps/events/backend/tests/links.test.ts b/apps/events/backend/tests/links.test.ts new file mode 100644 index 0000000..53cc829 --- /dev/null +++ b/apps/events/backend/tests/links.test.ts @@ -0,0 +1,77 @@ +import express from 'express'; +import request from 'supertest'; + +const mockCalls: { name: string; body?: unknown; query?: unknown }[] = []; +jest.mock('../src/controllers/webhookController', () => ({ handleYocoWebhook: (req: { rawBody: string }, res: { json(b: unknown): void }) => { mockCalls.push({ name: 'yoco', body: req.rawBody }); res.json({ ok: true }); } })); +jest.mock('../src/payments/webhook', () => ({ handleProviderWebhook: (id: string) => (_req: unknown, res: { json(b: unknown): void }) => { mockCalls.push({ name: id }); res.json({ ok: true }); } })); +jest.mock('../src/controllers/whatsappController', () => ({ handleWebhook: (req: { body: unknown; query: unknown }, res: { json(b: unknown): void }) => { mockCalls.push({ name: 'whatsapp', body: req.body, query: req.query }); res.json({ ok: true }); } })); +jest.mock('../src/controllers/privacyController', () => ({ + oneClickRedirect: (req: { params: { token: string } }, res: { redirect(c: number, u: string): void }) => res.redirect(302, `https://site.example/unsubscribe?t=${req.params.token}`), + oneClickUnsubscribe: (req: { params: { token: string }; body: unknown }, res: { json(b: unknown): void }) => { mockCalls.push({ name: 'one-click', body: req.body }); res.json({ ok: true }); }, +})); + +import { webhooksRouter, linksRouter, legacyLinkRedirects } from '../src/v2/links'; + +// The same order and raw-body handling as app.js. +function app() { + const a = express(); + a.use((req, _res, next) => { + if (req.path.startsWith('/api/v2/webhooks')) { + let raw = ''; + req.setEncoding('utf8'); + req.on('data', (c: string) => { raw += c; }); + req.on('end', () => { (req as unknown as { rawBody: string }).rawBody = raw; next(); }); + } else next(); + }); + a.use('/api/v2/webhooks', webhooksRouter); + a.use(express.json()); + a.use('/api/v2', linksRouter); + a.use(legacyLinkRedirects); + a.post('/api/privacy/unsubscribe/:token/one-click', (_req, res) => { mockCalls.push({ name: 'v1 one-click' }); res.json({ ok: true }); }); + return a; +} + +beforeEach(() => { mockCalls.length = 0; }); + +describe('incoming webhooks on v2', () => { + test('Yoco gets the exact bytes it signed', async () => { + const body = '{"type":"payment.succeeded","payload":{"id":"p1"}}'; + await request(app()).post('/api/v2/webhooks/yoco').set('Content-Type', 'application/json').send(body); + expect(mockCalls).toEqual([{ name: 'yoco', body }]); + }); + + test('PayFast reaches the PayFast handler', async () => { + await request(app()).post('/api/v2/webhooks/payfast').type('form').send('m_payment_id=c1'); + expect(mockCalls[0].name).toBe('payfast'); + }); + + test('WhatsApp gets its JSON parsed and keeps the secret in the query', async () => { + await request(app()).post('/api/v2/webhooks/whatsapp?secret=s3').send({ event: 'message', payload: { body: 'hi' } }); + expect(mockCalls[0]).toEqual({ name: 'whatsapp', body: { event: 'message', payload: { body: 'hi' } }, query: { secret: 's3' } }); + }); +}); + +describe('links in sent messages', () => { + test('an old calendar link answers 308 with its v2 address', async () => { + const res = await request(app()).get('/api/events/e1/ics?x=1'); + expect(res.status).toBe(308); + expect(res.headers.location).toBe('/api/v2/events/e1/calendar.ics?x=1'); + }); + + test('opening an old one-click link goes to v2, which sends the person to the page', async () => { + const old = await request(app()).get('/api/privacy/unsubscribe/tok/one-click'); + expect(old.status).toBe(308); + expect(old.headers.location).toBe('/api/v2/unsubscribe/tok/one-click'); + const v2 = await request(app()).get('/api/v2/unsubscribe/tok/one-click'); + expect(v2.status).toBe(302); + expect(v2.headers.location).toBe('https://site.example/unsubscribe?t=tok'); + }); + + test("a mail app's one-click POST works on v2, and on the old address without a redirect", async () => { + await request(app()).post('/api/v2/unsubscribe/tok/one-click').type('form').send('List-Unsubscribe=One-Click'); + expect(mockCalls[0]).toEqual({ name: 'one-click', body: { 'List-Unsubscribe': 'One-Click' } }); + const old = await request(app()).post('/api/privacy/unsubscribe/tok/one-click').type('form').send('List-Unsubscribe=One-Click'); + expect(old.status).toBe(200); + expect(mockCalls[1].name).toBe('v1 one-click'); + }); +}); diff --git a/apps/events/backend/tests/payments.test.js b/apps/events/backend/tests/payments.test.js index b73dedd..b988bb3 100644 --- a/apps/events/backend/tests/payments.test.js +++ b/apps/events/backend/tests/payments.test.js @@ -193,7 +193,7 @@ describe('PayFast provider', () => { expect(form.action).toBe('https://sandbox.payfast.co.za/eng/process'); expect(Object.keys(form.fields)).toEqual(['merchant_id', 'merchant_key', 'return_url', 'cancel_url', 'notify_url', 'name_first', 'name_last', 'email_address', 'm_payment_id', 'amount', 'item_name', 'item_description', 'signature']); expect(form.fields).toMatchObject({ - notify_url: 'https://api.example.org/api/webhooks/payfast', name_first: 'Thandi', name_last: "O'Neil Mokoena", + notify_url: 'https://api.example.org/api/v2/webhooks/payfast', name_first: 'Thandi', name_last: "O'Neil Mokoena", m_payment_id: 'chk_9', amount: '125.50', item_name: "Men's Camp (Youth)", item_description: 'Ticket x2', }); const { signature, ...rest } = form.fields; diff --git a/apps/events/backend/tests/privacy.test.js b/apps/events/backend/tests/privacy.test.js index e575567..cfb11e4 100644 --- a/apps/events/backend/tests/privacy.test.js +++ b/apps/events/backend/tests/privacy.test.js @@ -97,7 +97,7 @@ describe('footers', () => { test('news emails carry an unsubscribe link and one-click headers', () => { const f = privacy.bulkEmailFooter({ kind: 'news', recipient: { email: 'a@b.co', userId: 'u1' } }); expect(f.html).toContain('https://events.example.org/unsubscribe?t='); - expect(f.headers['List-Unsubscribe']).toMatch(/^$/); + expect(f.headers['List-Unsubscribe']).toMatch(/^$/); expect(f.headers['List-Unsubscribe-Post']).toBe('List-Unsubscribe=One-Click'); }); diff --git a/apps/events/backend/tests/yoco.test.js b/apps/events/backend/tests/yoco.test.js index fc379b8..d57a08f 100644 --- a/apps/events/backend/tests/yoco.test.js +++ b/apps/events/backend/tests/yoco.test.js @@ -82,12 +82,12 @@ describe('webhook URL', () => { test('built from BACKEND_URL', () => { process.env.BACKEND_URL = 'https://api.example.com/'; - expect(defaultWebhookUrl(req)).toBe('https://api.example.com/api/webhooks/yoco'); + expect(defaultWebhookUrl(req)).toBe('https://api.example.com/api/v2/webhooks/yoco'); }); test('falls back to the request origin', () => { delete process.env.BACKEND_URL; - expect(defaultWebhookUrl(req)).toBe('https://events-demo-api.crosscode.co.za/api/webhooks/yoco'); + expect(defaultWebhookUrl(req)).toBe('https://events-demo-api.crosscode.co.za/api/v2/webhooks/yoco'); }); test('must be public https', () => { diff --git a/apps/events/e2e/api-v2.spec.ts b/apps/events/e2e/api-v2.spec.ts index 44d471f..66d58f4 100644 --- a/apps/events/e2e/api-v2.spec.ts +++ b/apps/events/e2e/api-v2.spec.ts @@ -91,8 +91,17 @@ test.describe("API v2", () => { const all = await request.get(`${API}/api/events/all`, { headers: { Authorization: `Bearer ${seed().users.staff.token}` } }); expect(all.headers()["link"]).toBe(`; rel="successor-version"`); expect((await request.get(`${API}/api/settings/needs-setup`)).headers()["link"]).toBe(`; rel="successor-version"`); - // Endpoints with no v2 version yet (incoming webhooks, until 1.51.16) say nothing. - expect((await request.post(`${API}/api/webhooks/whatsapp`, { data: {} })).headers()["deprecation"]).toBeUndefined(); + // Incoming webhooks: the v1 address still works (same handler) and names its v2 one. + const hook = await request.post(`${API}/api/webhooks/whatsapp`, { data: {} }); + expect(hook.status()).toBe(200); + expect(hook.headers()["link"]).toBe(`; rel="successor-version"`); + expect((await request.post(`${API}/api/v2/webhooks/whatsapp`, { data: { event: "noop" } })).status()).toBe(200); + // A calendar link in an email already sent goes to its v2 address. + const ics = await request.get(`${API}/api/events/${seed().events.conference}/ics`, { maxRedirects: 0 }); + expect(ics.status()).toBe(308); + expect(ics.headers()["location"]).toBe(`/api/v2/events/${seed().events.conference}/calendar.ics`); + const followed = await request.get(`${API}/api/events/${seed().events.conference}/ics`); + expect(followed.headers()["content-type"]).toContain("text/calendar"); }); test("staff list every event; supervisors create, change and admins deactivate", async ({ request }) => { diff --git a/apps/events/frontend/src/components/admin/YocoTab.tsx b/apps/events/frontend/src/components/admin/YocoTab.tsx index 0f7fe2e..19402bf 100644 --- a/apps/events/frontend/src/components/admin/YocoTab.tsx +++ b/apps/events/frontend/src/components/admin/YocoTab.tsx @@ -284,7 +284,7 @@ export function YocoTab({ active, onChanged }: { active: boolean; onChanged?: ()

Registers a webhook with Yoco and saves the secret it returns. Yoco only shows the secret once, so it is stored straight away.

setHookName(e.target.value)} placeholder="Name" /> - setHookUrl(e.target.value)} placeholder="https://…/api/webhooks/yoco" /> + setHookUrl(e.target.value)} placeholder="https://…/api/v2/webhooks/yoco" />