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(workflows): keep unsubscribe links valid for 60 days (#128462)

posthog[bot]e88d6302eddb
diff --git a/nodejs/src/cdp/services/messaging/recipient-tokens.service.test.ts b/nodejs/src/cdp/services/messaging/recipient-tokens.service.test.ts
index f69af91a9f2b..1dc4fcef81ac 100644
--- a/nodejs/src/cdp/services/messaging/recipient-tokens.service.test.ts
+++ b/nodejs/src/cdp/services/messaging/recipient-tokens.service.test.ts
@@ -1,5 +1,6 @@
import { DateTime } from 'luxon'
+import { PosthogJwtAudience } from '../../utils/jwt-utils'
import { RecipientTokensService } from './recipient-tokens.service'
describe('RecipientTokensService', () => {
@@ -14,7 +15,7 @@ describe('RecipientTokensService', () => {
const token = service.generatePreferencesToken({ team_id: 1, identifier: '[email protected]' })
expect(token).toBeDefined()
expect(token).toMatchInlineSnapshot(
- `"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZWFtX2lkIjoxLCJpZGVudGlmaWVyIjoidGVzdEB0ZXN0LmNvbSIsImlhdCI6MTczNTY4OTYwMCwiZXhwIjoxNzM2Mjk0NDAwLCJhdWQiOiJwb3N0aG9nOm1lc3NhZ2luZzpzdWJzY3JpcHRpb25fcHJlZmVyZW5jZXMifQ.qpYA4Yx5lYA2ABEd_lgjn-rSGPgl-gg4PIbH3QXIZ7g"`
+ `"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZWFtX2lkIjoxLCJpZGVudGlmaWVyIjoidGVzdEB0ZXN0LmNvbSIsImlhdCI6MTczNTY4OTYwMCwiZXhwIjoxNzQwODczNjAwLCJhdWQiOiJwb3N0aG9nOm1lc3NhZ2luZzpzdWJzY3JpcHRpb25fcHJlZmVyZW5jZXMifQ.GJOdyHjjiySuycHf3ux_rixHAmFyxwcW8JvYqW1pprw"`
)
})
it.each([
@@ -37,13 +38,30 @@ describe('RecipientTokensService', () => {
const result = service.validatePreferencesToken(token)
expect(result).toEqual({ valid: false })
})
- it('should not accept an expired token', () => {
+ it.each([
+ { days: 8, valid: true },
+ { days: 30, valid: true },
+ { days: 59, valid: true },
+ { days: 61, valid: false },
+ ])('should treat a token as valid=$valid after $days days', ({ days, valid }) => {
const token = service.generatePreferencesToken({ team_id: 1, identifier: '[email protected]' })
- jest.spyOn(Date, 'now').mockReturnValue(fixedTime.plus({ days: 6 }).toMillis())
+ jest.spyOn(Date, 'now').mockReturnValue(fixedTime.plus({ days }).toMillis())
const result = service.validatePreferencesToken(token)
- expect(result).toEqual({ valid: true, team_id: 1, identifier: '[email protected]' })
- jest.spyOn(Date, 'now').mockReturnValue(fixedTime.plus({ days: 8 }).toMillis())
- const result2 = service.validatePreferencesToken(token)
- expect(result2).toEqual({ valid: false })
+ expect(result).toEqual(valid ? { valid: true, team_id: 1, identifier: '[email protected]' } : { valid: false })
+ })
+ it('should accept a token minted with the old 7 day expiry for up to 60 days', () => {
+ const token = service['jwt'].sign(
+ { team_id: 1, identifier: '[email protected]' },
+ PosthogJwtAudience.SUBSCRIPTION_PREFERENCES,
+ { expiresIn: '7d' }
+ )
+ jest.spyOn(Date, 'now').mockReturnValue(fixedTime.plus({ days: 30 }).toMillis())
+ expect(service.validatePreferencesToken(token)).toEqual({
+ valid: true,
+ team_id: 1,
+ identifier: '[email protected]',
+ })
+ jest.spyOn(Date, 'now').mockReturnValue(fixedTime.plus({ days: 61 }).toMillis())
+ expect(service.validatePreferencesToken(token)).toEqual({ valid: false })
})
})
diff --git a/nodejs/src/cdp/services/messaging/recipient-tokens.service.ts b/nodejs/src/cdp/services/messaging/recipient-tokens.service.ts
index 0f7f88eb0746..b658b5035656 100644
--- a/nodejs/src/cdp/services/messaging/recipient-tokens.service.ts
+++ b/nodejs/src/cdp/services/messaging/recipient-tokens.service.ts
@@ -18,6 +18,9 @@ export type ValidatedPreferencesToken =
| { valid: false }
| { valid: true; team_id: number; identifier: string; app_source_id?: string; instance_id?: string }
+// CAN-SPAM requires an opt-out link to work for at least 30 days after the email is sent.
+const PREFERENCES_TOKEN_MAX_AGE = '60d'
+
export function preferencesTokenSourceForInvocation(
invocation: Pick<CyclotronJobInvocationHogFunction, 'functionId' | 'parentRunId' | 'state'>
): PreferencesTokenSource | undefined {
@@ -45,7 +48,9 @@ export class RecipientTokensService {
try {
const decoded = this.jwt.verify(token, PosthogJwtAudience.SUBSCRIPTION_PREFERENCES, {
ignoreVerificationErrors: true,
- maxAge: '7d',
+ // Older tokens carry a 7 day `exp` claim, so the age check uses `iat` only.
+ ignoreExpiration: true,
+ maxAge: PREFERENCES_TOKEN_MAX_AGE,
})
if (!decoded) {
return { valid: false }
@@ -79,7 +84,7 @@ export class RecipientTokensService {
...(source?.instanceId ? { instance_id: source.instanceId } : {}),
},
PosthogJwtAudience.SUBSCRIPTION_PREFERENCES,
- { expiresIn: '7d' }
+ { expiresIn: PREFERENCES_TOKEN_MAX_AGE }
)
}