: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
feat(replay): cap snapshot blob fetches per logged-in user (#127876)
Tue Haulunde069c5c99ca6
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex a9d08481fe30..e3bd6da77cd6 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -8822,6 +8822,10 @@ snapshots:hash: v1.k794b7964.437d3c8dfdd121bda6fad40b2205bf3f7945b2494e6b6e374a238a758300fc43.X0GoW05bsLsPC3ERjwbp7GGcL4I2GTm7LLf1gt91bJ4replay-tabs-home-failure--not-found--light:hash: v1.k794b7964.49ea7dd5dead8a4545c4b7499408647959fec345ee4d40b8152ba7849733e377.QON8b8jJ00Ur3R0fNZ7_qNXkMPthG1pn8Gb8ycVtepk+ replay-tabs-home-failure--snapshots-rate-limited--dark:+ hash: v1.k794b7964.1bb13db9e4a3822c186cf6547bf9ea09e1f9250dfd99161a9c24bdd28b642116.LJvFOUBRb2Tgy-p4j3KnDNgGUuD7xd2B39IwFyGHSj8+ replay-tabs-home-failure--snapshots-rate-limited--light:+ hash: v1.k794b7964.390203868a25f09ec244b27988f515a750416add06e40a0e606ff21cee3f17ee.iSVGDW2tnBchlhAo2Lc7f9AF3UmRm-6XqisPAPZ-Fsgreplay-tabs-home-filters-banner--event-match-scope-only-during-recording--dark:hash: v1.k794b7964.9beb61ea56525082087e0f04472a36de2503d4bfd8cbf4baebb5e287dca8e232.z9jJjPgtJGDDS0sPeddm6jNB4hm1jZZP3yno9EnWMOoreplay-tabs-home-filters-banner--event-match-scope-only-during-recording--light:diff --git a/frontend/src/scenes/session-recordings/SessionsRecordings-player-failure.stories.tsx b/frontend/src/scenes/session-recordings/SessionsRecordings-player-failure.stories.tsxindex 5bf8ab7eefc0..4bb7bc1f807a 100644--- a/frontend/src/scenes/session-recordings/SessionsRecordings-player-failure.stories.tsx+++ b/frontend/src/scenes/session-recordings/SessionsRecordings-player-failure.stories.tsx@@ -3,14 +3,126 @@ import { HttpResponse } from 'msw'import { App } from 'scenes/App'import recordingEventsJson from 'scenes/session-recordings/__mocks__/recording_events_query'+import { recordingMetaJson } from 'scenes/session-recordings/__mocks__/recording_meta'import { snapshotsAsJSONLines } from 'scenes/session-recordings/__mocks__/recording_snapshots'import { urls } from 'scenes/urls'import { mswDecorator } from '~/mocks/browser'+import { Mocks } from '~/mocks/utils'import { recordingPlaylists } from './__mocks__/recording_playlists'import { recordings } from './__mocks__/recordings'+// API is set up so that everything except the call to load session recording metadata succeeds+const failureMocks: Mocks = {+ get: {+ '/api/environments/:team_id/session_recordings': ({ request }) => {+ const version = new URL(request.url).searchParams.get('version')+ return [+ 200,+ {+ has_next: false,+ results: recordings,+ version,+ },+ ]+ },+ '/api/projects/:team_id/session_recording_playlists': recordingPlaylists,+ '/api/projects/:team_id/session_recording_playlists/:playlist_id': ({ params }) => {+ const playlistId = params.playlist_id++ return [+ 200,+ {+ id: 29,+ short_id: playlistId,+ name: 'I am a playlist',+ derived_name: '(Untitled)',+ description: '',+ pinned: false,+ created_at: '2023-07-31T16:24:38.956943Z',+ created_by: {+ id: 1,+ uuid: '01896512-b4e6-0000-3add-7143ff5174c5',+ distinct_id: 'qs3Sp9pxE3nC827IbjDB6qNW6pD22X4tmGWwonM20p7',+ first_name: 'paul',+ email: '[email protected]',+ is_email_verified: true,+ },+ deleted: false,+ filters: {+ events: [],+ actions: [],+ date_to: null,+ date_from: '-7d',+ properties: [],+ console_logs: [],+ session_recording_duration: {+ key: 'duration',+ type: 'recording',+ value: 60,+ operator: 'gt',+ },+ },+ last_modified_at: '2023-07-31T16:34:15.297322Z',+ last_modified_by: {+ id: 1,+ uuid: '01896512-b4e6-0000-3add-7143ff5174c5',+ distinct_id: 'qs3Sp9pxE3nC827IbjDB6qNW6pD22X4tmGWwonM20p7',+ first_name: 'paul',+ email: '[email protected]',+ is_email_verified: true,+ },+ },+ ]+ },+ '/api/projects/:team_id/session_recording_playlists/:playlist_id/recordings': ({ params }) => {+ const playlistId = params.playlist_id+ const response = playlistId === '1234567' ? recordings : []+ return [200, { has_next: false, results: response, version: 1 }]+ },+ '/api/environments/:team_id/session_recordings/:id/snapshots': ({ request }) => {+ // with no sources, returns sources...+ if (new URL(request.url).searchParams.get('source') === 'blob_v2') {+ return new HttpResponse(snapshotsAsJSONLines())+ }+ // with no source requested should return sources+ return [+ 200,+ {+ sources: [+ {+ source: 'blob_v2',+ start_timestamp: '2023-08-11T12:03:36.097000Z',+ end_timestamp: '2023-08-11T12:04:52.268000Z',+ blob_key: '0',+ },+ ],+ },+ ]+ },+ '/api/environments/:team_id/session_recordings/:id': () => {+ return [404, {}]+ },+ 'api/projects/:team/notebooks': {+ count: 0,+ next: null,+ previous: null,+ results: [],+ },+ },+ post: {+ '/api/environments/:team_id/query/:kind': async ({ request }) => {+ const body = (await request.json()) as Record<string, any>+ if (body.query.kind === 'HogQLQuery' && body.query.query.includes('raw_session_replay_events')) {+ return [200, { results: [[7]] }]+ }+ return [200, recordingEventsJson]+ },+ '/api/environments/:team_id/session_recordings/:id/capture_diagnostics': { properties: null },+ },+}+const meta: Meta = {component: App,title: 'Replay/Tabs/Home/Failure',@@ -20,81 +132,35 @@ const meta: Meta = {mockDate: '2023-02-01',pageUrl: urls.replay(),},+}+export default meta++type Story = StoryObj<{}>+export const NotFound: Story = {+ decorators: [mswDecorator(failureMocks)],+ parameters: {+ testOptions: { waitForLoadersToDisappear: false, waitForSelector: '[data-attr="not-found-recording"]' },+ },+}++export const SnapshotsRateLimited: Story = {decorators: [- // API is set up so that everything except the call to load session recording metadata succeeds+ // The failed loads raise toasts at varying times, which would make the snapshot differ between runs.+ (Story) => (+ <>+ <style>{'.Toastify { display: none; }'}</style>+ <Story />+ </>+ ),mswDecorator({+ ...failureMocks,get: {- '/api/environments/:team_id/session_recordings': ({ request }) => {- const version = new URL(request.url).searchParams.get('version')- return [- 200,- {- has_next: false,- results: recordings,- version,- },- ]- },- '/api/projects/:team_id/session_recording_playlists': recordingPlaylists,- '/api/projects/:team_id/session_recording_playlists/:playlist_id': ({ params }) => {- const playlistId = params.playlist_id-- return [- 200,- {- id: 29,- short_id: playlistId,- name: 'I am a playlist',- derived_name: '(Untitled)',- description: '',- pinned: false,- created_at: '2023-07-31T16:24:38.956943Z',- created_by: {- id: 1,- uuid: '01896512-b4e6-0000-3add-7143ff5174c5',- distinct_id: 'qs3Sp9pxE3nC827IbjDB6qNW6pD22X4tmGWwonM20p7',- first_name: 'paul',- email: '[email protected]',- is_email_verified: true,- },- deleted: false,- filters: {- events: [],- actions: [],- date_to: null,- date_from: '-7d',- properties: [],- console_logs: [],- session_recording_duration: {- key: 'duration',- type: 'recording',- value: 60,- operator: 'gt',- },- },- last_modified_at: '2023-07-31T16:34:15.297322Z',- last_modified_by: {- id: 1,- uuid: '01896512-b4e6-0000-3add-7143ff5174c5',- distinct_id: 'qs3Sp9pxE3nC827IbjDB6qNW6pD22X4tmGWwonM20p7',- first_name: 'paul',- email: '[email protected]',- is_email_verified: true,- },- },- ]- },- '/api/projects/:team_id/session_recording_playlists/:playlist_id/recordings': ({ params }) => {- const playlistId = params.playlist_id- const response = playlistId === '1234567' ? recordings : []- return [200, { has_next: false, results: response, version: 1 }]- },+ ...failureMocks.get,+ '/api/environments/:team_id/session_recordings/:id': recordingMetaJson,'/api/environments/:team_id/session_recordings/:id/snapshots': ({ request }) => {- // with no sources, returns sources...if (new URL(request.url).searchParams.get('source') === 'blob_v2') {- return new HttpResponse(snapshotsAsJSONLines())+ return [429, { detail: 'Request was throttled.' }]}- // with no source requested should return sourcesreturn [200,{@@ -109,34 +175,11 @@ const meta: Meta = {},]},- '/api/environments/:team_id/session_recordings/:id': () => {- return [404, {}]- },- 'api/projects/:team/notebooks': {- count: 0,- next: null,- previous: null,- results: [],- },- },- post: {- '/api/environments/:team_id/query/:kind': async ({ request }) => {- const body = (await request.json()) as Record<string, any>- if (body.query.kind === 'HogQLQuery' && body.query.query.includes('raw_session_replay_events')) {- return [200, { results: [[7]] }]- }- return [200, recordingEventsJson]- },- '/api/environments/:team_id/session_recordings/:id/capture_diagnostics': { properties: null },},}),],-}-export default meta--type Story = StoryObj<{}>-export const NotFound: Story = {parameters: {- testOptions: { waitForLoadersToDisappear: false, waitForSelector: '[data-attr="not-found-recording"]' },+ // The player keeps its buffering class in the error state, so the loader wait never finishes.+ testOptions: { waitForLoadersToDisappear: false, waitForSelector: '[data-attr="replay-overlay-retry"]' },},}diff --git a/frontend/src/scenes/session-recordings/player/PlayerFrameOverlay.tsx b/frontend/src/scenes/session-recordings/player/PlayerFrameOverlay.tsxindex 2a7e1e033afa..6ce281a9a09d 100644--- a/frontend/src/scenes/session-recordings/player/PlayerFrameOverlay.tsx+++ b/frontend/src/scenes/session-recordings/player/PlayerFrameOverlay.tsx@@ -78,6 +78,7 @@ const RECOVERABLE_SNAPSHOT_ERRORS = ['loadSnapshotsForSourceFailure','loadSnapshotSourcesFailure','snapshotSourceLoadExhausted',+ 'snapshotRateLimited',]// The frame failure reports show an online browser and a frame that never started to load, and a retry@@ -168,9 +169,11 @@ const PlayerFrameOverlayContent = (): JSX.Element | null => {? 'This part of the recording is missing the snapshot data needed to render it. The data never reached PostHog, usually because the browser was closed or went offline before the recording finished uploading.': isUnauthorized? 'Your session has expired. Sign in again to keep watching this recording.'- : isRecoverable- ? "We couldn't fetch the recording data. This is usually a temporary network problem. Retry, and if it keeps failing contact support."- : 'An error occurred that is preventing this recording from being played. You can refresh the page to reload the recording.'}+ : playerError === 'snapshotRateLimited'+ ? "You've loaded a lot of recording data in a short time. Wait a few minutes, then retry."+ : isRecoverable+ ? "We couldn't fetch the recording data. This is usually a temporary network problem. Retry, and if it keeps failing contact support."+ : 'An error occurred that is preventing this recording from being played. You can refresh the page to reload the recording.'}</div>{isUnauthorized && (<LemonButton data-attr="replay-overlay-sign-in" to={urls.login()} type="primary" fullWidth center>diff --git a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.tsindex 65b82cdafcea..322d7ef96777 100644--- a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts+++ b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts@@ -99,6 +99,7 @@ import { clipWindowSeconds } from './controller/clipRange'import { getPlayerFrameLoadDiagnostics } from './playerFrameLoadDiagnostics'import { playerSettingsLogic } from './playerSettingsLogic'import { snapshotDataLogic } from './snapshotDataLogic'+import type { SnapshotLoadErrorKind } from './snapshotDataLogic'import {addAssetError,DoctorDiagnostics,@@ -770,8 +771,8 @@ export interface sessionRecordingPlayerLogicValues {}[] // sessionRecordingDataCoordinatorLogicallSourcesLoaded: boolean // snapshotDataLogicisLoadingSnapshots: boolean // snapshotDataLogic- isSnapshotUnauthorized: boolean // snapshotDataLogicreplayProxyToken: string | null // snapshotDataLogic+ snapshotLoadErrorKind: SnapshotLoadErrorKind | null // snapshotDataLogicsnapshotSources: SessionRecordingSnapshotSource[] | null // snapshotDataLogicsnapshotStore: SnapshotStore // snapshotDataLogicsnapshotsLoaded: boolean // snapshotDataLogic@@ -1520,7 +1521,7 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>('snapshotStore','allSourcesLoaded','storeVersion',- 'isSnapshotUnauthorized',+ 'snapshotLoadErrorKind','isLoadingSnapshots',],sessionRecordingDataCoordinatorLogic(props),@@ -3277,26 +3278,20 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(loadSnapshotsForSourceFailure: () => {if (Object.keys(values.sessionPlayerData.snapshotsByWindowId).length === 0) {console.error('PostHog Recording Playback Error: No snapshots loaded')- actions.setPlayerError(- values.isSnapshotUnauthorized ? 'snapshotUnauthorized' : 'loadSnapshotsForSourceFailure'- )+ actions.setPlayerError(values.snapshotLoadErrorKind ?? 'loadSnapshotsForSourceFailure')}},loadSnapshotSourcesFailure: () => {if (Object.keys(values.sessionPlayerData.snapshotsByWindowId).length === 0) {console.error('PostHog Recording Playback Error: No snapshots loaded')- actions.setPlayerError(- values.isSnapshotUnauthorized ? 'snapshotUnauthorized' : 'loadSnapshotSourcesFailure'- )+ actions.setPlayerError(values.snapshotLoadErrorKind ?? 'loadSnapshotSourcesFailure')}},// Both are terminal give-ups: unlike the per-attempt failures above they fire even when other// data already loaded, because the missing range would otherwise buffer forever with no error.snapshotSourceLoadExhausted: () => {console.error('PostHog Recording Playback Error: A snapshot source repeatedly failed to load')- actions.setPlayerError(- values.isSnapshotUnauthorized ? 'snapshotUnauthorized' : 'snapshotSourceLoadExhausted'- )+ actions.setPlayerError(values.snapshotLoadErrorKind ?? 'snapshotSourceLoadExhausted')},snapshotProcessingFailed: () => {console.error('PostHog Recording Playback Error: Snapshot processing repeatedly failed')diff --git a/frontend/src/scenes/session-recordings/player/snapshotDataLogic.test.ts b/frontend/src/scenes/session-recordings/player/snapshotDataLogic.test.tsindex 2b73fc971a29..2a6c230816b6 100644--- a/frontend/src/scenes/session-recordings/player/snapshotDataLogic.test.ts+++ b/frontend/src/scenes/session-recordings/player/snapshotDataLogic.test.ts@@ -85,6 +85,16 @@ describe('snapshotDataLogic', () => {consoleError.mockRestore()})+ it('gives up on the first 429, because retrying within seconds cannot outlast the per-user quota', async () => {+ const consoleError = jest.spyOn(console, 'error').mockImplementation(() => {})+ await expectLogic(logic, () => {+ logic.actions.loadSnapshotsForSourceFailure('Too many requests', new ApiError('Throttled', 429))+ })+ .toDispatchActions(['snapshotSourceLoadExhausted'])+ .toMatchValues({ snapshotLoadErrorKind: 'snapshotRateLimited' })+ consoleError.mockRestore()+ })+it('re-arms the retry budget after retrySnapshotLoading, so a later failure is not instantly exhausted', async () => {const consoleError = jest.spyOn(console, 'error').mockImplementation(() => {})// Exhaust the budget; consume the terminal action so it is out of the recording window.diff --git a/frontend/src/scenes/session-recordings/player/snapshotDataLogic.tsx b/frontend/src/scenes/session-recordings/player/snapshotDataLogic.tsxindex 4036edddb0b7..54693bc0a6f5 100644--- a/frontend/src/scenes/session-recordings/player/snapshotDataLogic.tsx+++ b/frontend/src/scenes/session-recordings/player/snapshotDataLogic.tsx@@ -46,6 +46,8 @@ export interface SnapshotLogicProps {accessToken?: string}+export type SnapshotLoadErrorKind = 'snapshotUnauthorized' | 'snapshotRateLimited'+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface snapshotDataLogicValues {getWindowId: (uuid: string | undefined) => number | undefined // windowIdRegistryLogic@@ -63,6 +65,7 @@ export interface snapshotDataLogicValues {replayProxyToken: string | nullseekTarget: SeekTarget | nullsnapshotLoadError: Error | null+ snapshotLoadErrorKind: SnapshotLoadErrorKind | nullsnapshotSources: SessionRecordingSnapshotSource[] | nullsnapshotSourcesLoading: booleansnapshotStore: SnapshotStore@@ -228,6 +231,7 @@ export interface snapshotDataLogicMeta {recordingDeletedAt: (snapshotLoadError: Error | null) => number | nullrecordingDeletedBy: (snapshotLoadError: Error | null) => string | nullisSnapshotUnauthorized: (snapshotLoadError: Error | null) => boolean+ snapshotLoadErrorKind: (snapshotLoadError: Error | null) => SnapshotLoadErrorKind | null}}@@ -605,7 +609,8 @@ export const snapshotDataLogic = kea<snapshotDataLogicType>([loadSnapshotsForSourceFailure: async (_, breakpoint) => {cache.loadFailureCount = (cache.loadFailureCount ?? 0) + 1- if (cache.loadFailureCount > 3) {+ // The per-user quota refills over minutes, so retrying within seconds only fails again.+ if (values.snapshotLoadErrorKind === 'snapshotRateLimited' || cache.loadFailureCount > 3) {// Give up loudly: nothing else retries this source, so without a terminal action the// player would buffer forever whenever playback reaches the missing range.actions.snapshotSourceLoadExhausted()@@ -765,6 +770,20 @@ export const snapshotDataLogic = kea<snapshotDataLogicType>([return snapshotLoadError instanceof ApiError && snapshotLoadError.status === 401},],++ // The player error for a load failure whose cause the overlay explains with its own message.+ snapshotLoadErrorKind: [+ (s) => [s.snapshotLoadError],+ (snapshotLoadError: Error | null): SnapshotLoadErrorKind | null => {+ if (!(snapshotLoadError instanceof ApiError)) {+ return null+ }+ if (snapshotLoadError.status === 401) {+ return 'snapshotUnauthorized'+ }+ return snapshotLoadError.status === 429 ? 'snapshotRateLimited' : null+ },+ ],})),afterMount(({ actions, cache }) => {cache.playerActive = truediff --git a/posthog/session_recordings/session_recording_api.py b/posthog/session_recordings/session_recording_api.pyindex a33fb4d1bf4b..96f77aae8af0 100644--- a/posthog/session_recordings/session_recording_api.py+++ b/posthog/session_recordings/session_recording_api.py@@ -901,6 +901,68 @@ class SharingTokenReplayThrottle(SimpleRateThrottle):return False+class _SnapshotUserRateThrottle(SimpleRateThrottle):+ """Per-user cap on blob fetches from a browser session or an OAuth token.++ The view checks it only for blob fetches, not through throttle_classes, so listing sources never uses quota.+ It does not reuse the shared user-bucket throttle, because that one counts every refusal in the global+ rate_limit_exceeded metrics, which log-only mode would fill with requests it never blocked.+ """++ scope: str+ rate_setting: str++ def __init__(self) -> None:+ # Read at instantiation so override_settings takes effect.+ self.rate = getattr(settings, self.rate_setting)+ super().__init__()++ def get_cache_key(self, request, view) -> str | None:+ if not request.user or not request.user.is_authenticated:+ return None+ return self.cache_format % {"scope": self.scope, "ident": request.user.pk}++ def allow_request(self, request, view) -> bool:+ try:+ return self._allow_request(request, view)+ except Exception as e:+ # The quota protects storage, not access, so a cache outage must not stop playback.+ capture_exception(e, {"location": "snapshot_user_rate_throttle", "scope": self.scope})+ return True++ def _allow_request(self, request, view) -> bool:+ if not is_rate_limit_enabled(round(time.time() / 60)):+ return True+ team_id = PersonalApiKeyRateThrottle.safely_get_team_id_from_view(view)+ if team_id is not None and team_is_allowed_to_bypass_throttle(team_id):+ return True+ if super().allow_request(request, view):+ return True+ enforced = settings.SNAPSHOT_USER_RATE_LIMIT_ENFORCED+ location = self.scope if enforced else f"{self.scope}_log_only"+ _count_session_recording_throttled(location=location, auth_type=_request_auth_type(request))+ # A client over the limit stays over it for every later request, so log once a minute per user, not per request.+ if cache.add(f"{self.scope}_logged:{request.user.pk}", 1, timeout=60):+ logger.info(+ "snapshot_user_rate_limit_exceeded",+ scope=self.scope,+ enforced=enforced,+ user_id=request.user.pk,+ team_id=team_id,+ )+ return not enforced+++class SnapshotsUserBurstRateThrottle(_SnapshotUserRateThrottle):+ scope = "snapshots_user_burst"+ rate_setting = "SNAPSHOT_USER_RATE_BURST"+++class SnapshotsUserSustainedRateThrottle(_SnapshotUserRateThrottle):+ scope = "snapshots_user_sustained"+ rate_setting = "SNAPSHOT_USER_RATE_SUSTAINED"++def _length_prefix_blocks(blocks: list[bytes]) -> bytes:chunks = []for block in blocks:@@ -1569,6 +1631,12 @@ class SessionRecordingViewSet(try:response: Response | HttpResponseif source == "blob_v2" and "min_blob_key" in validated_data:+ # The tier limits cover only personal API keys, so cap browser sessions and OAuth agents here. Exports and+ # ID-JAG tokens also resolve to a real user, but they must not share that user's quota.+ if isinstance(request.successful_authenticator, SessionAuthentication | OAuthAccessTokenAuthentication):+ for throttle in (SnapshotsUserBurstRateThrottle(), SnapshotsUserSustainedRateThrottle()):+ if not throttle.allow_request(request, self):+ raise Throttled(wait=throttle.wait())response = self._stream_blob_v2_to_client(recording,timer,@@ -1581,7 +1649,7 @@ class SessionRecordingViewSet(response.headers["Server-Timing"] = timer.to_header_string()return response- except NotFound:+ except (NotFound, Throttled):raiseexcept RecordingDeletedError as e:logger.info(diff --git a/posthog/session_recordings/test/test_session_recording_snapshots_api_blob_v2.py b/posthog/session_recordings/test/test_session_recording_snapshots_api_blob_v2.pyindex edf04e5fd5b0..cdb8e691baf1 100644--- a/posthog/session_recordings/test/test_session_recording_snapshots_api_blob_v2.py+++ b/posthog/session_recordings/test/test_session_recording_snapshots_api_blob_v2.py@@ -1,6 +1,7 @@from posthog.test.base import APIBaseTest, ClickhouseTestMixin, QueryMatchingTestfrom unittest.mock import AsyncMock, MagicMock, patch+from django.core.cache import cachefrom django.test import override_settingsfrom parameterized import parameterized@@ -931,3 +932,48 @@ class TestSessionRecordingSnapshotsAPI(APIBaseTest, ClickhouseTestMixin, QueryMaassert response.status_code == status.HTTP_410_GONEassert response.json()["error"] == "recording_deleted"assert response.json()["deleted_at"] == 1700000000++ @parameterized.expand(+ [("enforced", True, status.HTTP_429_TOO_MANY_REQUESTS), ("log_only", False, status.HTTP_200_OK)]+ )+ @patch("posthog.session_recordings.session_recording_api.is_rate_limit_enabled", return_value=True)+ @patch("posthog.session_recordings.session_recording_api.recording_api_client")+ @patch("posthog.session_recordings.session_recording_api.list_blocks_async", new_callable=AsyncMock)+ @patch(+ "posthog.session_recordings.queries.session_replay_events.SessionReplayEvents.exists",+ return_value=True,+ )+ @patch("posthog.session_recordings.session_recording_api.SessionRecording.get_or_build")+ def test_blob_v2_user_quota(+ self,+ _name,+ enforced,+ expected_status_over_quota,+ mock_get_session_recording,+ _mock_exists,+ mock_list_blocks,+ mock_recording_api_client,+ _mock_rate_limit_enabled,+ ):+ cache.clear()+ session_id = str(uuid7())+ mock_get_session_recording.return_value = SessionRecording(session_id=session_id, team=self.team, deleted=False)+ mock_list_blocks.return_value = [+ RecordingBlock(+ key="key0",+ start_byte=0,+ end_byte=100,+ start_timestamp="2024-01-01T00:00:00Z",+ end_timestamp="2024-01-01T00:01:00Z",+ )+ ]+ mock_storage = MagicMock()+ mock_storage.fetch_block = AsyncMock(return_value=b'{"timestamp": 1}')+ mock_recording_api_client.return_value.__aenter__.return_value = mock_storage+ url = f"/api/projects/{self.team.pk}/session_recordings/{session_id}/snapshots/?source=blob_v2&start_blob_key=0&end_blob_key=0"++ with override_settings(SNAPSHOT_USER_RATE_BURST="2/minute", SNAPSHOT_USER_RATE_LIMIT_ENFORCED=enforced):+ assert self.client.get(url).status_code == status.HTTP_200_OK+ assert self.client.get(url).status_code == status.HTTP_200_OK+ assert self.client.get(url).status_code == expected_status_over_quota+ cache.clear()diff --git a/posthog/session_recordings/test/test_session_recording_throttle.py b/posthog/session_recordings/test/test_session_recording_throttle.pyindex e54bbcdf3fc9..2d234c9eee2c 100644--- a/posthog/session_recordings/test/test_session_recording_throttle.py+++ b/posthog/session_recordings/test/test_session_recording_throttle.py@@ -1,5 +1,5 @@from posthog.test.base import BaseTest-from unittest.mock import patch+from unittest.mock import MagicMock, patchfrom django.core.cache import cachefrom django.test import SimpleTestCase, override_settings@@ -25,6 +25,7 @@ from posthog.session_recordings.session_recording_api import (SharingTokenReplayThrottle,SnapshotsBurstRateThrottle,SnapshotsSustainedRateThrottle,+ SnapshotsUserBurstRateThrottle,get_cached_org_tier,listing_rates,snapshot_rates,@@ -476,3 +477,16 @@ class TestSessionRecordingViewSetThrottleSelection(BaseTest):throttles = viewset.get_throttles()assert not any(isinstance(t, SharingTokenReplayThrottle) for t in throttles)+++class TestSnapshotUserRateThrottle(SimpleTestCase):+ @patch("posthog.session_recordings.session_recording_api.capture_exception")+ @patch("posthog.session_recordings.session_recording_api.team_is_allowed_to_bypass_throttle", return_value=False)+ @patch("posthog.session_recordings.session_recording_api.is_rate_limit_enabled", return_value=True)+ def test_allows_the_fetch_when_the_cache_errors(self, _enabled, _bypass, mock_capture) -> None:+ throttle = SnapshotsUserBurstRateThrottle()+ throttle.cache = MagicMock(get=MagicMock(side_effect=ConnectionError("cache unavailable")))+ request = type("FakeRequest", (), {"user": type("FakeUser", (), {"is_authenticated": True, "pk": 1})()})()++ assert throttle.allow_request(request, type("FakeView", (), {"team_id": 1})()) is True+ mock_capture.assert_called_once()diff --git a/posthog/settings/session_replay.py b/posthog/settings/session_replay.pyindex 8f1240778204..e8428a1260b2 100644--- a/posthog/settings/session_replay.py+++ b/posthog/settings/session_replay.py@@ -37,5 +37,12 @@ LISTING_RATE_PAID_SUSTAINED = get_from_env("LISTING_RATE_PAID_SUSTAINED", "300/hLISTING_RATE_ENTERPRISE_BURST = get_from_env("LISTING_RATE_ENTERPRISE_BURST", "100/minute")LISTING_RATE_ENTERPRISE_SUSTAINED = get_from_env("LISTING_RATE_ENTERPRISE_SUSTAINED", "400/hour")+# Per-user cap on blob fetches from a browser session or an OAuth token. The tier rates above cover only personal+# API keys, so without this a script, browser agent or MCP client can fetch snapshots with no limit at all.+SNAPSHOT_USER_RATE_BURST = get_from_env("SNAPSHOT_USER_RATE_BURST", "200/minute")+SNAPSHOT_USER_RATE_SUSTAINED = get_from_env("SNAPSHOT_USER_RATE_SUSTAINED", "3000/hour")+# Off means log-only: an over-limit fetch is counted in session_recording_api_throttled_total but still served.+SNAPSHOT_USER_RATE_LIMIT_ENFORCED = get_from_env("SNAPSHOT_USER_RATE_LIMIT_ENFORCED", False, type_cast=str_to_bool)+# Per-token cap for replay endpoints reached via a SharingAccessToken.REPLAY_SHARING_TOKEN_RATE = get_from_env("REPLAY_SHARING_TOKEN_RATE", "600/minute")