-
Notifications
You must be signed in to change notification settings - Fork 274
fix: handle invalid session replay config JSON #3561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'posthog-js': patch | ||
| --- | ||
|
|
||
| Handle invalid persisted session replay config JSON gracefully |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -273,7 +273,15 @@ export class SessionRecording implements Extension { | |
| if (!persistedConfig) { | ||
| return false | ||
| } | ||
| const config = typeof persistedConfig === 'object' ? persistedConfig : JSON.parse(persistedConfig) | ||
| let config: SessionRecordingPersistedConfig | ||
| try { | ||
| config = typeof persistedConfig === 'object' ? persistedConfig : JSON.parse(persistedConfig) | ||
| } catch (e) { | ||
| // Do not unregister here: the SDK only registers structured configs, and this read path should | ||
| // ignore corrupt legacy/external values without mutating persistence. | ||
| logger.warn('persisted remote config for session recording is invalid and will be ignored', e) | ||
| return false | ||
| } | ||
|
Comment on lines
+276
to
+284
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The try/catch block here — including its body, the identical comment, and the identical Prompt To Fix With AIThis is a comment left during a code review.
Path: packages/browser/src/extensions/replay/session-recording.ts
Line: 276-284
Comment:
**Duplicated safe-parse logic across two files**
The try/catch block here — including its body, the identical comment, and the identical `logger.warn` message — is copy-pasted verbatim into `lazy-loaded-session-recorder.ts` (`_remoteConfig` getter, same shape). This violates OnceAndOnlyOnce. A small shared helper such as `parsePersistedReplayConfig(raw): SessionRecordingPersistedConfig | null` that returns `null` on parse failure would let both callers handle the error uniformly and remove the duplicated comment explaining why mutation is skipped.
How can I resolve this? If you propose a fix, please make it concise. |
||
| // default to now so that configs persisted by older SDK versions | ||
| // (which never set cache_timestamp) are treated as fresh | ||
| const cacheTimestamp = config.cache_timestamp ?? Date.now() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typeof persistedConfig === 'object', while the equivalent block inlazy-loaded-session-recorder.tsusesisObject(persistedConfig). These two checks differ in behaviour fornull(which passestypeof === 'object'but notisObject). While both sites guard againstnullvia the earlier!persistedConfigcheck, the inconsistency makes the intent harder to follow and could silently diverge if the guard is copied elsewhere. Both sites should use the same helper.Prompt To Fix With AI