diff --git a/bskylink/src/redirectCache.test.ts b/bskylink/src/redirectCache.test.ts new file mode 100644 index 000000000..51b77ebf3 --- /dev/null +++ b/bskylink/src/redirectCache.test.ts @@ -0,0 +1,91 @@ +import assert from 'node:assert' +import {type Server} from 'node:http' +import {type AddressInfo} from 'node:net' +import {after, before, describe, it} from 'node:test' + +import express from 'express' + +import {type AppContext} from './context.js' +import {type SafelinkRule} from './db/schema.js' +import i18n from './i18n.js' +import redirect from './routes/redirect.js' + +const rules: Record = { + 'https://allowed.example.com/': 'whitelist', + 'https://blocked.example.com/': 'block', + 'https://warned.example.com/': 'warn', +} + +const ctx = { + cfg: { + service: { + appHostname: 'bsky.app', + hostnames: ['go.bsky.app'], + safelinkEnabled: true, + }, + }, + metrics: {track: () => {}}, + safelinkClient: { + tryFindRule: (link: string) => { + const action = rules[link] + return Promise.resolve(action ? {action} : 'ok') + }, + }, +} as unknown as AppContext + +void describe('redirect Cache-Control', () => { + let server: Server + let baseUrl: string + + before(async () => { + const app = express() + app.use(i18n.init) + redirect(ctx, app) + server = app.listen(0) + await new Promise(resolve => server.once('listening', resolve)) + baseUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}` + }) + + after(() => { + server.close() + }) + + const cacheControlFor = async (link: string) => { + const res = await fetch( + `${baseUrl}/redirect?u=${encodeURIComponent(link)}`, + {redirect: 'manual'}, + ) + await res.text() + assert.ok(res.status < 500, `unexpected status ${res.status}`) + return res.headers.get('cache-control') + } + + void it('does not cache links without a rule', async () => { + assert.strictEqual( + await cacheControlFor('https://unknown.example.com/'), + 'no-store', + ) + }) + + void it('briefly caches whitelisted links', async () => { + assert.strictEqual( + await cacheControlFor('https://allowed.example.com/'), + 'max-age=3600', + ) + }) + + void it('does not cache blocked or warned links', async () => { + assert.strictEqual( + await cacheControlFor('https://blocked.example.com/'), + 'no-store', + ) + assert.strictEqual( + await cacheControlFor('https://warned.example.com/'), + 'no-store', + ) + }) + + void it('does not cache invalid redirects', async () => { + assert.strictEqual(await cacheControlFor('not a url'), 'no-store') + }) +}) diff --git a/bskylink/src/routes/redirect.ts b/bskylink/src/routes/redirect.ts index ed1e32f9b..95c5afb96 100644 --- a/bskylink/src/routes/redirect.ts +++ b/bskylink/src/routes/redirect.ts @@ -1,6 +1,6 @@ import assert from 'node:assert' -import {DAY, SECOND} from '@atproto/common' +import {HOUR, SECOND} from '@atproto/common' import {type Express} from 'express' import {type AppContext} from '../context.js' @@ -15,6 +15,13 @@ const INTERNAL_IP_REGEX = new RegExp( 'i', ) +/** + * Only plain string query params are tracked, since repeated or nested params + * parse to arrays or objects. + */ +const queryString = (value: unknown) => + typeof value === 'string' ? value : undefined + export default function (ctx: AppContext, app: Express) { return app.get( '/redirect', @@ -43,8 +50,11 @@ export default function (ctx: AppContext, app: Express) { return res.status(302).end() } - // Default to a max age header - res.setHeader('Cache-Control', `max-age=${(7 * DAY) / SECOND}`) + /* + * A cached response bypasses rules added after it was cached, so only + * explicitly whitelisted links get a (short) max age. + */ + res.setHeader('Cache-Control', 'no-store') res.status(200) res.type('html') @@ -59,6 +69,7 @@ export default function (ctx: AppContext, app: Express) { switch (rule.action) { case 'whitelist': redirectLogger.info({rule}, 'Whitelist rule matched') + res.setHeader('Cache-Control', `max-age=${HOUR / SECOND}`) whitelisted = 'yes' break case 'block': @@ -101,11 +112,11 @@ export default function (ctx: AppContext, app: Express) { whitelisted, blocked, warned, - utm_source: req.query.utm_source?.toString(), - utm_medium: req.query.utm_medium?.toString(), - utm_campaign: req.query.utm_campaign?.toString(), - utm_content: req.query.utm_content?.toString(), - utm_term: req.query.utm_term?.toString(), + utm_source: queryString(req.query.utm_source), + utm_medium: queryString(req.query.utm_medium), + utm_campaign: queryString(req.query.utm_campaign), + utm_content: queryString(req.query.utm_content), + utm_term: queryString(req.query.utm_term), }) return res.end(html) diff --git a/oxlint-suppressions.json b/oxlint-suppressions.json index ee3baf327..329436251 100644 --- a/oxlint-suppressions.json +++ b/oxlint-suppressions.json @@ -1,4 +1,9 @@ { + "bskylink/src/routes/redirect.ts": { + "typescript/no-misused-promises": { + "count": 1 + } + }, "modules/bottom-sheet/src/BottomSheetNativeComponent.tsx": { "typescript/no-explicit-any": { "count": 2