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(experiments): allow query POSTs through read-only guard (#59661)

Paul D'Ambra205ae6fc122d
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts
index 3b0b54d3767d..460a3ef419bd 100644
--- a/frontend/src/lib/api.ts
+++ b/frontend/src/lib/api.ts
@@ -6734,7 +6734,7 @@ const api = {
): Promise<T> {
url = prepareUrl(url)
ensureProjectIdNotInvalid(url)
- assertNotReadOnly(method)
+ assertNotReadOnly(method, url)
const isFormData = data instanceof FormData
const response = await handleFetch(url, method, async () => {
@@ -6770,7 +6770,7 @@ const api = {
async createResponse(url: string, data?: any, options?: ApiMethodOptions): Promise<Response> {
url = prepareUrl(url)
ensureProjectIdNotInvalid(url)
- assertNotReadOnly('POST')
+ assertNotReadOnly('POST', url)
const isFormData = data instanceof FormData
return await handleFetch(url, 'POST', () =>
@@ -6791,7 +6791,7 @@ const api = {
async delete(url: string): Promise<any> {
url = prepareUrl(url)
ensureProjectIdNotInvalid(url)
- assertNotReadOnly('DELETE')
+ assertNotReadOnly('DELETE', url)
return await handleFetch(url, 'DELETE', () =>
fetch(url, {
method: 'DELETE',
diff --git a/frontend/src/lib/readOnlyGuard.test.ts b/frontend/src/lib/readOnlyGuard.test.ts
new file mode 100644
index 000000000000..9ecc50b75f7c
--- /dev/null
+++ b/frontend/src/lib/readOnlyGuard.test.ts
@@ -0,0 +1,143 @@
+import {
+ assertNotReadOnly,
+ isReadOnly,
+ ReadOnlyModeError,
+ setReadOnlyGetter,
+ setReadOnlyNotifier,
+} from 'lib/readOnlyGuard'
+
+describe('readOnlyGuard', () => {
+ afterEach(() => {
+ // Tear down module-level state so each test starts from a clean slate.
+ setReadOnlyGetter(null)
+ setReadOnlyNotifier(null)
+ })
+
+ describe('isReadOnly()', () => {
+ it('returns false when no getter is registered', () => {
+ expect(isReadOnly()).toBe(false)
+ })
+
+ it.each([
+ ['getter returns true', true, true],
+ ['getter returns false', false, false],
+ ])('reflects the registered getter when %s', (_label, getterReturn, expected) => {
+ setReadOnlyGetter(() => getterReturn)
+ expect(isReadOnly()).toBe(expected)
+ })
+ })
+
+ describe('assertNotReadOnly()', () => {
+ describe('when not in read-only mode', () => {
+ it.each([
+ ['POST', '/api/environments/2/dashboards/'],
+ ['PATCH', '/api/environments/2/dashboards/1/'],
+ ['PUT', '/api/environments/2/dashboards/1/'],
+ ['DELETE', '/api/environments/2/dashboards/1/'],
+ ['POST', '/api/environments/2/query/'],
+ ] as const)('lets %s %s through silently', (method, url) => {
+ setReadOnlyGetter(() => false)
+ const notifier = jest.fn()
+ setReadOnlyNotifier(notifier)
+
+ expect(() => assertNotReadOnly(method, url)).not.toThrow()
+ expect(notifier).not.toHaveBeenCalled()
+ })
+ })
+
+ describe('when in read-only mode', () => {
+ beforeEach(() => {
+ setReadOnlyGetter(() => true)
+ })
+
+ it.each([
+ ['POST', '/api/environments/2/dashboards/'],
+ ['PATCH', '/api/environments/2/dashboards/1/'],
+ ['PUT', '/api/environments/2/dashboards/1/'],
+ ['DELETE', '/api/environments/2/dashboards/1/'],
+ ] as const)('blocks %s %s with a ReadOnlyModeError', (method, url) => {
+ expect(() => assertNotReadOnly(method, url)).toThrow(ReadOnlyModeError)
+ })
+
+ it('calls the notifier with the blocked method', () => {
+ const notifier = jest.fn()
+ setReadOnlyNotifier(notifier)
+ expect(() => assertNotReadOnly('POST', '/api/environments/2/dashboards/')).toThrow(ReadOnlyModeError)
+ expect(notifier).toHaveBeenCalledTimes(1)
+ expect(notifier).toHaveBeenCalledWith('POST')
+ })
+
+ it('does not throw when no notifier is registered', () => {
+ // Notifier is optional — guard should still block but stay quiet.
+ expect(() => assertNotReadOnly('POST', '/api/environments/2/dashboards/')).toThrow(ReadOnlyModeError)
+ })
+
+ // Reads-disguised-as-writes (e.g. HogQL/trends/funnels via POST to /query/)
+ // must pass through even in read-only mode — otherwise the app becomes
+ // unusable.
+ it.each([
+ ['plain query', 'POST', '/api/environments/2/query/'],
+ ['query with id', 'POST', '/api/environments/2/query/abc-123/'],
+ ['query upgrade', 'POST', '/api/environments/2/query/upgrade'],
+ ['query log', 'POST', '/api/environments/2/query/abc-123/log'],
+ ['query with trailing query string', 'POST', '/api/environments/2/query/?refresh=true'],
+ ['delete on query path', 'DELETE', '/api/environments/2/query/abc-123/'],
+ ] as const)('lets %s through (%s %s)', (_label, method, url) => {
+ const notifier = jest.fn()
+ setReadOnlyNotifier(notifier)
+ expect(() => assertNotReadOnly(method, url)).not.toThrow()
+ expect(notifier).not.toHaveBeenCalled()
+ })
+
+ it.each([
+ ['endpoint that just contains the word query in a name', 'POST', '/api/environments/2/queryless/'],
+ ['similar prefix without slash', 'POST', '/api/environments/2/queryteam/'],
+ ] as const)('still blocks %s (%s %s) — only paths with /query/ segment are allowed', (_l, method, url) => {
+ expect(() => assertNotReadOnly(method, url)).toThrow(ReadOnlyModeError)
+ })
+ })
+ })
+
+ describe('ReadOnlyModeError', () => {
+ it('has a sensible default detail message so caller error-fallback patterns produce truthful toasts', () => {
+ const err = new ReadOnlyModeError()
+ expect(err.detail).toBe('Read-only mode is on — change blocked. Use Max or the MCP to make this change.')
+ expect(err.name).toBe('ReadOnlyModeError')
+ })
+
+ it('accepts a custom message', () => {
+ const err = new ReadOnlyModeError('custom message')
+ expect(err.message).toBe('custom message')
+ })
+ })
+
+ describe('setReadOnlyGetter', () => {
+ let warnSpy: jest.SpyInstance
+
+ beforeEach(() => {
+ warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined)
+ })
+
+ afterEach(() => {
+ warnSpy.mockRestore()
+ })
+
+ it('warns when overwriting an existing getter (double-mount detection)', () => {
+ setReadOnlyGetter(() => true)
+ setReadOnlyGetter(() => false)
+ expect(warnSpy).toHaveBeenCalledTimes(1)
+ expect(warnSpy.mock.calls[0][0]).toContain('setReadOnlyGetter')
+ })
+
+ it('does not warn when clearing the getter', () => {
+ setReadOnlyGetter(() => true)
+ setReadOnlyGetter(null)
+ expect(warnSpy).not.toHaveBeenCalled()
+ })
+
+ it('does not warn on first registration', () => {
+ setReadOnlyGetter(() => true)
+ expect(warnSpy).not.toHaveBeenCalled()
+ })
+ })
+})
diff --git a/frontend/src/lib/readOnlyGuard.ts b/frontend/src/lib/readOnlyGuard.ts
index 11e08066c62c..d357d33f5b48 100644
--- a/frontend/src/lib/readOnlyGuard.ts
+++ b/frontend/src/lib/readOnlyGuard.ts
@@ -44,10 +44,25 @@ export function isReadOnly(): boolean {
return getter?.() ?? false
}
-export function assertNotReadOnly(method: 'PATCH' | 'PUT' | 'POST' | 'DELETE'): void {
+// URLs that look like writes but are actually reads. The query endpoint is the
+// obvious one — it serves HogQL / trends / funnels / retention via POST because
+// the payload is too large for a query string. Block-listing these would make
+// the entire app unusable in read-only mode.
+const READ_ONLY_ALLOWED_PATTERNS = [
+ /\/query(?:\/|$|\?)/, // /api/environments/:team_id/query, /api/environments/:team_id/query/:queryId/log, etc.
+]
+
+function isReadDisguisedAsWrite(url: string): boolean {
+ return READ_ONLY_ALLOWED_PATTERNS.some((pattern) => pattern.test(url))
+}
+
+export function assertNotReadOnly(method: 'PATCH' | 'PUT' | 'POST' | 'DELETE', url: string): void {
if (!isReadOnly()) {
return
}
+ if (isReadDisguisedAsWrite(url)) {
+ return
+ }
notifier?.(method)
throw new ReadOnlyModeError()
}