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(replay): let the recording-api encrypt route accept the legacy secret (#120399)

Tue Haulund2acda94d09df
diff --git a/nodejs/src/session-replay/recording-api/auth.test.ts b/nodejs/src/session-replay/recording-api/auth.test.ts
index 311b69ef3572..db105dca2b83 100644
--- a/nodejs/src/session-replay/recording-api/auth.test.ts
+++ b/nodejs/src/session-replay/recording-api/auth.test.ts
@@ -201,33 +201,6 @@ describe('createRecordingApiAuthMiddleware', () => {
})
})
- describe('routes that require a JWT', () => {
- it.each([
- ['the legacy secret', { jwtSecret: JWT_SECRET }, { 'x-internal-api-secret': LEGACY_SECRET }],
- [
- 'any request when no JWT key is configured',
- { jwtSecret: '' },
- { 'x-internal-api-secret': LEGACY_SECRET },
- ],
- ])('reject %s', (_, opts, headers) => {
- const { res, next } = run(
- { ...opts, op: 'write', allowLegacySecret: true, requireJwt: true },
- mockRequest('123', headers)
- )
- expect(next).not.toHaveBeenCalled()
- expect(res.status).toHaveBeenCalledWith(401)
- })
-
- it('accept a scoped write token', () => {
- const { res, next } = run(
- { op: 'write', allowLegacySecret: true, requireJwt: true },
- mockRequest('123', bearer(mintToken({ team_id: 123, op: 'write' })))
- )
- expect(next).toHaveBeenCalled()
- expect(res.status).not.toHaveBeenCalled()
- })
- })
-
describe('progressive rollout (legacy secret before JWT is enabled)', () => {
it('requires the legacy secret when no JWT secret is configured', () => {
const req = mockRequest('123', { 'x-internal-api-secret': LEGACY_SECRET })
diff --git a/nodejs/src/session-replay/recording-api/auth.ts b/nodejs/src/session-replay/recording-api/auth.ts
index c8f1e1f007e9..f80103f88358 100644
--- a/nodejs/src/session-replay/recording-api/auth.ts
+++ b/nodejs/src/session-replay/recording-api/auth.ts
@@ -25,8 +25,6 @@ export interface RecordingApiAuthOptions {
legacySecret: string
allowLegacySecret: boolean
op: RecordingApiOp
- // Accept only a scoped JWT, and refuse every request when no JWT key is configured.
- requireJwt?: boolean
}
function getHeader(req: Request, name: string): string | undefined {
@@ -82,19 +80,14 @@ export function assertRecordingApiAuthConfigured(opts: {
}
export function createRecordingApiAuthMiddleware(options: RecordingApiAuthOptions) {
- const { jwtSecret, legacySecret, allowLegacySecret, op, requireJwt = false } = options
+ const { jwtSecret, legacySecret, allowLegacySecret, op } = options
// Build the verifier once. Comma-separated keys give zero-downtime rotation (verify against all);
// a malformed/empty value yields no keys and disables JWT (null) rather than throwing. `|| ''`
// guards against a partial runtime config where the field is absent.
const jwt = makeOptionalJwt(jwtSecret || '')
- const legacyEnabled = allowLegacySecret && !!legacySecret && !requireJwt
+ const legacyEnabled = allowLegacySecret && !!legacySecret
return (req: Request, res: Response, next: NextFunction): void => {
- if (requireJwt && !jwt) {
- res.status(401).json({ error: 'Unauthorized' })
- return
- }
-
// Nothing configured at all (local dev): allow. In every other case some auth is required —
// before the JWT scheme is enabled in an environment, the legacy secret still protects it.
if (!jwt && !legacyEnabled) {
diff --git a/nodejs/src/session-replay/recording-api/recording-api.test.ts b/nodejs/src/session-replay/recording-api/recording-api.test.ts
index 7a8c21505785..7aae40bcd646 100644
--- a/nodejs/src/session-replay/recording-api/recording-api.test.ts
+++ b/nodejs/src/session-replay/recording-api/recording-api.test.ts
@@ -668,6 +668,34 @@ describe('RecordingApi', () => {
expect(res.status).toBe(expectedStatus)
})
+ it('accepts the legacy secret while JWT auth is not configured', async () => {
+ const legacyApi = new RecordingApi(
+ {
+ RECORDING_API_JWT_SECRET: '',
+ RECORDING_API_ALLOW_LEGACY_SECRET: true,
+ INTERNAL_API_SECRET: 'legacy-secret',
+ } as RecordingApiConfig,
+ mockPostgres,
+ mockOutputs
+ )
+ await legacyApi.start(mockService)
+ const legacyApp = express()
+ legacyApp.use(express.json())
+ legacyApp.use('/', legacyApi.router())
+ const legacyServer = legacyApp.listen(0, () => {})
+
+ try {
+ const res = await supertest(legacyApp)
+ .post('/api/projects/1/recordings/session-1/encrypt')
+ .set('X-Internal-Api-Secret', 'legacy-secret')
+ .send(body)
+
+ expect(res.status).toBe(200)
+ } finally {
+ legacyServer.close()
+ }
+ })
+
it.each([
[
{ ok: true, blocks: [Buffer.from('sealed')] },
diff --git a/nodejs/src/session-replay/recording-api/recording-api.ts b/nodejs/src/session-replay/recording-api/recording-api.ts
index af0548204774..688ae4149a04 100644
--- a/nodejs/src/session-replay/recording-api/recording-api.ts
+++ b/nodejs/src/session-replay/recording-api/recording-api.ts
@@ -210,8 +210,6 @@ export class RecordingApi {
legacySecret: this.config.INTERNAL_API_SECRET,
allowLegacySecret: this.config.RECORDING_API_ALLOW_LEGACY_SECRET,
op,
- // Key minting is new, so it never inherits the fleet-wide legacy secret.
- requireJwt: op === 'write',
})
const readAuth = auth('read')
const deleteAuth = auth('delete')