Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(cdp): improve email link redirect validation (#120817)

Tom Piccirello6a4af37cf989
diff --git a/nodejs/src/cdp/services/messaging/email-tracking.service.test.ts b/nodejs/src/cdp/services/messaging/email-tracking.service.test.ts
index efda6aea12a3..c76983c6f596 100644
--- a/nodejs/src/cdp/services/messaging/email-tracking.service.test.ts
+++ b/nodejs/src/cdp/services/messaging/email-tracking.service.test.ts
@@ -25,6 +25,7 @@ import { Hub, Team } from '../../../types'
import {
METRIC_NAME_TO_EVENT_NAME,
PIXEL_GIF,
+ UNSIGNED_REDIRECT_SUNSET,
addTrackingToEmail,
decodeHtmlEntitiesInHref,
resolveEmailEngagementDistinctId,
@@ -259,34 +260,130 @@ describe('EmailTrackingService', () => {
// there is no SES, so the pixel/redirect handlers themselves emit the metric — these
// tests run in test env and exercise that path.
describe('handleEmailTrackingRedirect', () => {
- it('should redirect to the target url and record an email_link_clicked metric', async () => {
- const phId = signer.generate({
- functionId: hogFunction.id,
- id: invocationId,
- teamId: team.id,
- })
- const res = await supertest(app).get(`/public/m/redirect?ph_id=${phId}&target=https://example.com`)
- expect(res.status).toBe(302)
- expect(res.headers.location).toBe('https://example.com')
+ const trackingCode = (): string =>
+ signer.generate({ functionId: hogFunction.id, id: invocationId, teamId: team.id })
+
+ const mintedLinkPath = (target: string): string => {
+ const link = new URL(
+ signer.redirectUrl({ functionId: hogFunction.id, id: invocationId, teamId: team.id }, target)
+ )
+ return `${link.pathname}${link.search}`
+ }
+
+ const realNow = Date.now
+ let clock: jest.SpyInstance
+ // Shifts the clock instead of freezing it, because waitForExpect reads Date.now for its timeout.
+ const setClock = (at: number): void => {
+ const offset = at - realNow()
+ clock.mockImplementation(() => realNow() + offset)
+ }
+ beforeEach(() => {
+ clock = jest.spyOn(Date, 'now')
+ setClock(UNSIGNED_REDIRECT_SUNSET - 60_000)
+ })
+
+ afterEach(() => {
+ clock.mockRestore()
+ })
+
+ const expectLinkClickedMetrics = async (count: number): Promise<void> => {
await waitForExpect(() => {
const messages = mockProducerObserver.getProducedKafkaMessagesForTopic(KAFKA_APP_METRICS_2)
- expect(messages).toHaveLength(1)
- expect(messages[0].value).toMatchObject({
- team_id: team.id,
- metric_name: 'email_link_clicked',
- metric_kind: 'email',
- })
+ expect(messages).toHaveLength(count)
+ for (const message of messages) {
+ expect(message.value).toMatchObject({
+ team_id: team.id,
+ metric_name: 'email_link_clicked',
+ metric_kind: 'email',
+ })
+ }
})
+ }
+
+ it('should redirect a signed link after the unsigned link sunset and record an email_link_clicked metric', async () => {
+ setClock(UNSIGNED_REDIRECT_SUNSET)
+ const res = await supertest(app).get(mintedLinkPath('https://example.com/?a=1&b=2'))
+ expect(res.status).toBe(302)
+ expect(res.headers.location).toBe('https://example.com/?a=1&b=2')
+
+ await expectLinkClickedMetrics(1)
})
- it('should return 404 if the target is not provided', async () => {
- const phId = signer.generate({
- functionId: hogFunction.id,
- id: invocationId,
- teamId: team.id,
- })
- const res = await supertest(app).get(`/public/m/redirect?ph_id=${phId}`)
+ it.each([
+ [
+ 'has no tracking code',
+ () => '/public/m/redirect?target=https://other.example/',
+ 'https://other.example/',
+ 0,
+ ],
+ [
+ 'has no target signature',
+ () => `/public/m/redirect?ph_id=${trackingCode()}&target=https://other.example/`,
+ 'https://other.example/',
+ 1,
+ ],
+ [
+ 'has its target swapped after signing',
+ () =>
+ mintedLinkPath('https://example.com/').replace(/target=[^&]+/, 'target=https://other.example/'),
+ 'https://other.example/',
+ 1,
+ ],
+ [
+ 'carries markup in its target',
+ // The URL parser percent-encodes markup in a query or a hierarchical path, but keeps it raw in
+ // the opaque path of a mailto: URL, so only this target depends on the page escaping it.
+ () =>
+ `/public/m/redirect?target=${encodeURIComponent('mailto:"><script>alert(1)</script>@example.com')}`,
+ 'mailto:&quot;&gt;&lt;script&gt;alert(1)&lt;/script&gt;@example.com',
+ 0,
+ ],
+ [
+ 'points at an email address',
+ () => '/public/m/redirect?target=mailto:[email protected]',
+ 'mailto:[email protected]',
+ 0,
+ ],
+ ])(
+ 'should show a confirmation page instead of redirecting when the link %s',
+ async (_name, path, expectedHref, expectedMetrics) => {
+ const res = await supertest(app).get(path())
+ expect(res.status).toBe(200)
+ expect(res.headers.location).toBeUndefined()
+ expect(res.headers).toMatchObject({
+ 'content-type': 'text/html; charset=utf-8',
+ 'x-content-type-options': 'nosniff',
+ 'cache-control': 'no-store',
+ })
+ expect(res.headers['content-security-policy']).toContain("default-src 'none'")
+ expect(res.text).toContain(`href="${expectedHref}"`)
+ expect(res.text).not.toContain('<script')
+
+ await expectLinkClickedMetrics(expectedMetrics)
+ }
+ )
+
+ it('should return 404 for an unsigned link after the unsigned link sunset', async () => {
+ setClock(UNSIGNED_REDIRECT_SUNSET)
+ const res = await supertest(app).get('/public/m/redirect?target=https://other.example/')
+ expect(res.status).toBe(404)
+ })
+
+ it.each([
+ ['the target is not provided', () => `/public/m/redirect?ph_id=${trackingCode()}`],
+ ['an unsigned target has a script scheme', () => '/public/m/redirect?target=javascript:alert(1)'],
+ ['an unsigned target opens an app', () => '/public/m/redirect?target=sms:5555550100'],
+ [
+ 'an unsigned target puts a username before its host',
+ () => '/public/m/redirect?target=https://[email protected]/',
+ ],
+ [
+ 'the target is repeated',
+ () => '/public/m/redirect?target=https://a.example/&target=https://b.example/',
+ ],
+ ])('should return 404 if %s', async (_name, path) => {
+ const res = await supertest(app).get(path())
expect(res.status).toBe(404)
const messages = mockProducerObserver.getProducedKafkaMessagesForTopic(KAFKA_APP_METRICS_2)
diff --git a/nodejs/src/cdp/services/messaging/email-tracking.service.ts b/nodejs/src/cdp/services/messaging/email-tracking.service.ts
index edad30cc77a5..645866c70885 100644
--- a/nodejs/src/cdp/services/messaging/email-tracking.service.ts
+++ b/nodejs/src/cdp/services/messaging/email-tracking.service.ts
@@ -21,6 +21,7 @@ import { HogFunctionManagerService } from '../managers/hog-function-manager.serv
import { TeamWorkflowsConfigService } from '../managers/team-workflows-config.service'
import { HogFunctionMonitoringService } from '../monitoring/hog-function-monitoring.service'
import { EmailSuppressionService } from './email-suppression.service'
+import { escapeHtml } from './helpers/escape-html'
import { SES_LINK_INDEX_TAG, SesWebhookHandler } from './helpers/ses'
import { EmailTrackingCodeSigner, trackingCodeFormatCounter } from './helpers/tracking-code'
@@ -210,6 +211,65 @@ export const addTrackingToEmail = (
return html
}
+const REDIRECT_CONFIRMATION_PAGE_HEADERS = {
+ 'Content-Type': 'text/html; charset=utf-8',
+ 'X-Content-Type-Options': 'nosniff',
+ 'Cache-Control': 'no-store',
+ // The page runs no script, and blocking frames stops another site from overlaying the link.
+ 'Content-Security-Policy':
+ "default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'",
+ // The page URL carries the tracking code, so the click must not send it to the destination.
+ 'Referrer-Policy': 'no-referrer',
+}
+
+// The redirect mode wrapped every href except `javascript:`, so a legacy link can carry any other
+// scheme or a relative href. Only web, email and phone links get the page. Every other target returns
+// 404, so the page never renders a link that runs script in this origin or opens an arbitrary app.
+const CONFIRMABLE_PROTOCOLS = new Set(['http:', 'https:', 'mailto:', 'tel:'])
+
+// Only the local maildev path still wraps links, and it signs them, so every unsigned link in a real
+// inbox predates the signature. After this instant an unsigned link returns 404, which closes the
+// window in which anyone can make this endpoint show a destination of their choice.
+export const UNSIGNED_REDIRECT_SUNSET = Date.parse('2027-02-11T00:00:00Z')
+
+// Emails sent before redirect targets were signed still sit in inboxes, so an unsigned link gets a
+// page that names the destination instead of a 404. The recipient then decides whether to continue,
+// which keeps this endpoint from silently forwarding anyone to a URL that no email ever contained.
+const renderRedirectConfirmationPage = (target: string): string | null => {
+ let url: URL
+ try {
+ url = new URL(target)
+ } catch {
+ return null
+ }
+ if (!CONFIRMABLE_PROTOCOLS.has(url.protocol)) {
+ return null
+ }
+ // `https://[email protected]/` goes to evil.example, but a reader trusts the prefix.
+ if (url.username || url.password) {
+ return null
+ }
+ const href = escapeHtml(url.href)
+ // A mailto: or tel: URL has no host, so the page names the whole target instead.
+ const destination = escapeHtml(url.host || url.href)
+ return `<!doctype html>
+<html lang="en">
+<head>
+<meta charset="utf-8">
+<meta name="viewport" content="width=device-width, initial-scale=1">
+<title>Confirm the link</title>
+<style>body{font-family:ui-sans-serif,system-ui,-apple-system,sans-serif;max-width:560px;margin:4rem auto;padding:0 1rem;line-height:1.5;color:#111}.url{word-break:break-all;color:#555}</style>
+</head>
+<body>
+<h1>Confirm the link</h1>
+<p>This link goes to <strong>${destination}</strong>. Continue only if you trust it.</p>
+<p class="url">${href}</p>
+<p><a href="${href}" rel="noreferrer">Continue to ${destination}</a></p>
+</body>
+</html>
+`
+}
+
export class EmailTrackingService {
private sesWebhookHandler: SesWebhookHandler
@@ -572,14 +632,28 @@ export class EmailTrackingService {
// Same rationale as handleEmailTrackingPixel: skip in production (SES webhooks own
// click tracking), but emit in dev/test where maildev never produces webhooks.
public handleEmailTrackingRedirect(req: ModifiedRequest, res: express.Response): void {
- const { target } = req.query
+ const { target, ph_id, ph_sig } = req.query
- if (!target) {
+ if (typeof target !== 'string' || !target) {
res.status(404).send('Not found')
return
}
- res.redirect(target as string)
+ const isSignedTarget =
+ typeof ph_id === 'string' &&
+ typeof ph_sig === 'string' &&
+ this.trackingCodeSigner.verifyRedirectTarget(ph_id, target, ph_sig)
+
+ if (isSignedTarget) {
+ res.redirect(target)
+ } else {
+ const page = Date.now() < UNSIGNED_REDIRECT_SUNSET ? renderRedirectConfirmationPage(target) : null
+ if (!page) {
+ res.status(404).send('Not found')
+ return
+ }
+ res.status(200).set(REDIRECT_CONFIRMATION_PAGE_HEADERS).send(page)
+ }
if (isDevEnv() || isTestEnv()) {
const params = this.parseTrackingParams(req.query as Record<string, any>)
diff --git a/nodejs/src/cdp/services/messaging/email.service.test.ts b/nodejs/src/cdp/services/messaging/email.service.test.ts
index ddfddec81416..d958223b0637 100644
--- a/nodejs/src/cdp/services/messaging/email.service.test.ts
+++ b/nodejs/src/cdp/services/messaging/email.service.test.ts
@@ -1000,7 +1000,7 @@ describe('EmailService', () => {
// ph_id may be unsigned (base64url only) or signed (base64url + `.` + signature) depending on
// ENCRYPTION_SALT_KEYS. Match the structure, not the exact value.
expect(emails[0].html).toMatch(
- /^<body>Hi! <a href="http:\/\/localhost:8010\/public\/m\/redirect\?ph_id=[A-Za-z0-9._-]+&target=https%3A%2F%2Fexample\.com">Click me<\/a><img src="http:\/\/localhost:8010\/public\/m\/pixel\?ph_id=[A-Za-z0-9._-]+" style="display: none;" \/><\/body>$/
+ /^<body>Hi! <a href="http:\/\/localhost:8010\/public\/m\/redirect\?ph_id=[A-Za-z0-9._-]+&ph_sig=[A-Za-z0-9_-]+&target=https%3A%2F%2Fexample\.com">Click me<\/a><img src="http:\/\/localhost:8010\/public\/m\/pixel\?ph_id=[A-Za-z0-9._-]+" style="display: none;" \/><\/body>$/
)
})
})
diff --git a/nodejs/src/cdp/services/messaging/helpers/escape-html.ts b/nodejs/src/cdp/services/messaging/helpers/escape-html.ts
new file mode 100644
index 000000000000..869e51418ed5
--- /dev/null
+++ b/nodejs/src/cdp/services/messaging/helpers/escape-html.ts
@@ -0,0 +1,10 @@
+const HTML_ESCAPE: Record<string, string> = {
+ '&': '&amp;',
+ '<': '&lt;',
+ '>': '&gt;',
+ '"': '&quot;',
+ "'": '&#39;',
+}
+
+// Covers `& < > " '`, which is enough for a text node and for a quoted attribute value.
+export const escapeHtml = (text: string): string => text.replace(/[&<>"']/g, (c) => HTML_ESCAPE[c])
diff --git a/nodejs/src/cdp/services/messaging/helpers/tracking-code.ts b/nodejs/src/cdp/services/messaging/helpers/tracking-code.ts
index 2d211d6fa40e..b313506a8b76 100644
--- a/nodejs/src/cdp/services/messaging/helpers/tracking-code.ts
+++ b/nodejs/src/cdp/services/messaging/helpers/tracking-code.ts
@@ -96,6 +96,12 @@ const VERSIONED_PAYLOAD_MARKER = 'v2'
const EMIT_VERSIONED_PAYLOAD = true
+// The redirect signature binds the destination to the tracking code, so a link cannot be edited to
+// point somewhere else. A JSON array keeps the boundary between the two values unambiguous, and the
+// leading `[` keeps this payload apart from tracking-code payloads, which are bare base64url.
+const redirectSignaturePayload = (trackingCode: string, targetUrl: string): string =>
+ JSON.stringify(['redirect', trackingCode, targetUrl])
+
// Generates, signs, verifies and renders email tracking codes. Signing keys and the public
// tracking URL are read once in the constructor so callers can be injected with a configured
// instance (see cdp-services.ts) instead of reaching into global config — and tests can pass
@@ -246,6 +252,13 @@ export class EmailTrackingCodeSigner {
}
redirectUrl(invocation: TrackingInvocation, targetUrl: string, isTest = false): string {
- return `${this.trackingUrl}/public/m/redirect?ph_id=${this.generate(invocation, isTest)}&target=${encodeURIComponent(targetUrl)}`
+ const trackingCode = this.generate(invocation, isTest)
+ const signature = this.signPayload(redirectSignaturePayload(trackingCode, targetUrl), this.signingKeys[0])
+ return `${this.trackingUrl}/public/m/redirect?ph_id=${trackingCode}&ph_sig=${signature}&target=${encodeURIComponent(targetUrl)}`
+ }
+
+ // Links minted before the target was signed carry no `ph_sig`, so they fail this check.
+ verifyRedirectTarget(trackingCode: string, targetUrl: string, signature: string): boolean {
+ return this.verifySignature(redirectSignaturePayload(trackingCode, targetUrl), signature)
}
}
diff --git a/nodejs/src/cdp/services/messaging/message-assets.service.ts b/nodejs/src/cdp/services/messaging/message-assets.service.ts
index b3d2a0a1ea4d..e3bbd4354beb 100644
--- a/nodejs/src/cdp/services/messaging/message-assets.service.ts
+++ b/nodejs/src/cdp/services/messaging/message-assets.service.ts
@@ -13,6 +13,7 @@ import { captureException } from '~/common/utils/posthog'
import { CyclotronJobInvocationHogFunction, CyclotronJobInvocationResult, MessageAssetRow } from '../../types'
import { resolveEmailEngagementDistinctId } from './email-tracking.service'
+import { escapeHtml } from './helpers/escape-html'
export type { MessageAssetRow } from '../../types'
@@ -48,18 +49,10 @@ const isoMicroseconds = (date: Date): string => {
return date.toISOString().replace('T', ' ').replace('Z', '000')
}
-const HTML_ESCAPE: Record<string, string> = {
- '&': '&amp;',
- '<': '&lt;',
- '>': '&gt;',
- '"': '&quot;',
- "'": '&#39;',
-}
-
// Escape user-typed `<`/`>`/`&` so a `<pre>`-wrapped plain-text body can't break
// surrounding markup or inject scripts.
const wrapPlainTextAsHtml = (text: string): string => {
- const escaped = text.replace(/[&<>"']/g, (c) => HTML_ESCAPE[c])
+ const escaped = escapeHtml(text)
return `<!doctype html><meta charset="utf-8"><pre style="white-space:pre-wrap;font-family:ui-monospace,Menlo,monospace;margin:0;padding:1rem">${escaped}</pre>`
}
@@ -71,8 +64,6 @@ const wrapPlainTextAsHtml = (text: string): string => {
// the safe budget — the row still lands, the chip works, and the batch is unaffected.
const MAX_HTML_BYTES = 4 * 1024 * 1024
-const escapeHtml = (text: string): string => text.replace(/[&<>"']/g, (c) => HTML_ESCAPE[c])
-
// A push has no stored body to replay the way an email does, so render the delivered content into the
// same `html` column. That keeps one asset pipeline and one viewer for both channels: the person view
// renders this snapshot instead of needing a push-specific renderer.