: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(replay): clamp rating-bar star counts to avoid RangeError (#65170)
posthog[bot]5ff4bcb2bb21
diff --git a/common/replay-shared/src/mobile/transformer/transformers.ts b/common/replay-shared/src/mobile/transformer/transformers.tsindex bd94ab2e854f..872bc9b842c6 100644--- a/common/replay-shared/src/mobile/transformer/transformers.ts+++ b/common/replay-shared/src/mobile/transformer/transformers.ts@@ -92,6 +92,9 @@ const NAVIGATION_BAR_PARENT_ID = 7const KEYBOARD_PARENT_ID = 9export const STATUS_BAR_PARENT_ID = 11+// upper bound on rendered rating stars, guards against absurd `max` values in wireframe data+const MAX_RATING_STARS = 100+function isKeyboardEvent(x: unknown): x is keyboardEvent {return isObject(x) && 'data' in x && isObject(x.data) && 'tag' in x.data && x.data.tag === 'keyboard'}@@ -536,9 +539,16 @@ function makeRatingBar(return makePlaceholderElement(wireframe, children, context)}- const numberOfFilledStars = Math.floor(wireframe.value)- const numberOfHalfStars = wireframe.value - numberOfFilledStars > 0 ? 1 : 0- const numberOfEmptyStars = wireframe.max - numberOfFilledStars - numberOfHalfStars+ // wireframe.value/max are untrusted SDK data: negative, fractional, or out-of-range+ // values would otherwise feed a negative length into Array() and throw RangeError.+ // Clamp every star count to a non-negative integer (and cap the total against max).+ const safeStarCount = (n: number): number => Math.max(0, Math.min(MAX_RATING_STARS, Math.floor(n)))++ const flooredValue = Math.floor(wireframe.value)+ const numberOfFilledStars = safeStarCount(flooredValue)+ // only a positive fractional value yields a half star; negative values clamp to zero filled stars+ const numberOfHalfStars = wireframe.value > 0 && wireframe.value > flooredValue ? 1 : 0+ const numberOfEmptyStars = safeStarCount(wireframe.max - numberOfFilledStars - numberOfHalfStars)const filledStars = Array(numberOfFilledStars).fill(undefined)diff --git a/frontend/src/scenes/session-recordings/mobile-replay/transform.test.ts b/frontend/src/scenes/session-recordings/mobile-replay/transform.test.tsindex dd3fbbe1051b..c476f89e7216 100644--- a/frontend/src/scenes/session-recordings/mobile-replay/transform.test.ts+++ b/frontend/src/scenes/session-recordings/mobile-replay/transform.test.ts@@ -798,6 +798,49 @@ describe('replay/transform', () => {).toMatchSnapshot()})+ // Keep these malformed-data cases before the 'progress rating' full-snapshot test below.+ // Each transformEventToWeb full snapshot resets the shared global id sequence, so the+ // reset inside 'progress rating' absorbs the ids these cases consume and keeps the+ // downstream incremental-snapshot tests (keyboard/add/update) stable.+ test.each([+ ['negative value', -1, 5],+ ['value larger than max', 10, 3],+ ['negative max', 2, -5],+ ['fractional out-of-range value', 7.5, 2],+ ['zero max with positive value', 3, 0],+ ['negative fractional value', -0.5, 5],+ ])('progress rating transforms malformed data without crashing: %s', (_label, value, max) => {+ // transformEventToWeb swallows transformer exceptions into telemetry, so asserting+ // .not.toThrow() is not enough — spy on captureException to prove makeRatingBar did+ // not hit the RangeError this patch prevents.+ const telemetry = { capture: jest.fn(), captureException: jest.fn() }+ const result = transformEventToWeb(+ {+ type: 2,+ data: {+ wireframes: [+ {+ id: 12365,+ width: 100,+ height: 30,+ type: 'input',+ inputType: 'progress',+ style: { bar: 'rating' },+ max,+ value,+ },+ ],+ },+ timestamp: 1,+ },+ telemetry+ )++ expect(telemetry.captureException).not.toHaveBeenCalled()+ // a successful transform replaces the mobile node with a web document+ expect(result).toMatchObject({ type: 2, data: { node: expect.any(Object) } })+ })+test('progress rating', () => {expect(transformEventToWeb({