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(proxy): skip proxy_records fetch when user is unauthenticated (#64970)

posthog[bot]fe3c4c054249
diff --git a/frontend/src/layout/navigation/projectNoticeLogic.test.tsx b/frontend/src/layout/navigation/projectNoticeLogic.test.tsx
index fd52c3046298..c0dfb92433be 100644
--- a/frontend/src/layout/navigation/projectNoticeLogic.test.tsx
+++ b/frontend/src/layout/navigation/projectNoticeLogic.test.tsx
@@ -74,6 +74,86 @@ describe('projectNoticeLogic', () => {
})
})
+ describe('unauthenticated session', () => {
+ let getItemSpy: jest.SpyInstance
+ let getDateSpy: jest.SpyInstance
+ let originalAppContext: AppContext | undefined
+
+ beforeEach(() => {
+ originalAppContext = window.POSTHOG_APP_CONTEXT
+ // Simulate a missing/expired session — userLogic boots to a null user.
+ window.POSTHOG_APP_CONTEXT = {
+ ...originalAppContext,
+ current_user: null,
+ } as unknown as AppContext
+ useMocks({
+ get: {
+ '/api/organizations/:organization_id/proxy_records': [200, { results: [] }],
+ },
+ post: {
+ '/api/environments/:team_id/query/:kind': () => [200, { results: [] }],
+ },
+ })
+ initKeaTests()
+ getItemSpy = jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => null)
+ // Inside the first-7-days nudge window, so only the auth guard can hold the fetch back.
+ getDateSpy = jest.spyOn(Date.prototype, 'getDate').mockReturnValue(3)
+ })
+
+ afterEach(() => {
+ getItemSpy.mockRestore()
+ getDateSpy.mockRestore()
+ window.POSTHOG_APP_CONTEXT = originalAppContext
+ })
+
+ it('does not load proxy records when the user is unauthenticated', async () => {
+ const logic = projectNoticeLogic()
+ logic.mount()
+
+ await expectLogic(logic).toNotHaveDispatchedActions(['loadRecords'])
+ expect(logic.values.proxyRecords).toBeNull()
+
+ logic.unmount()
+ })
+ })
+
+ describe('proxy records 401 handling', () => {
+ let getItemSpy: jest.SpyInstance
+ let getDateSpy: jest.SpyInstance
+
+ beforeEach(() => {
+ useMocks({
+ get: {
+ // Function form so the [status, body] tuple is honored — a static array value
+ // would be served as a 200 JSON body instead of a 401.
+ '/api/organizations/:organization_id/proxy_records': () => [401, {}],
+ },
+ post: {
+ '/api/environments/:team_id/query/:kind': () => [200, { results: [] }],
+ },
+ })
+ initKeaTests()
+ getItemSpy = jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => null)
+ getDateSpy = jest.spyOn(Date.prototype, 'getDate').mockReturnValue(3)
+ })
+
+ afterEach(() => {
+ getItemSpy.mockRestore()
+ getDateSpy.mockRestore()
+ })
+
+ it('swallows a 401 instead of surfacing a load failure', async () => {
+ const logic = projectNoticeLogic()
+ logic.mount()
+
+ await expectLogic(logic).toDispatchActions(['loadRecords', 'loadRecordsSuccess'])
+ await expectLogic(logic).toNotHaveDispatchedActions(['loadRecordsFailure'])
+ expect(logic.values.proxyRecords).toBeNull()
+
+ logic.unmount()
+ })
+ })
+
describe('reverse proxy checker connection', () => {
let getItemSpy: jest.SpyInstance
let getDateSpy: jest.SpyInstance
diff --git a/frontend/src/layout/navigation/projectNoticeLogic.tsx b/frontend/src/layout/navigation/projectNoticeLogic.tsx
index c09e34cc6051..eb7d5ded0a79 100644
--- a/frontend/src/layout/navigation/projectNoticeLogic.tsx
+++ b/frontend/src/layout/navigation/projectNoticeLogic.tsx
@@ -4,7 +4,7 @@ import { router } from 'kea-router'
import { IconGear, IconPlus } from '@posthog/icons'
-import api from 'lib/api'
+import api, { ApiError } from 'lib/api'
import { reverseProxyCheckerLogic } from 'lib/components/ReverseProxyChecker/reverseProxyCheckerLogic'
import { superpowersLogic } from 'lib/components/Superpowers/superpowersLogic'
import { LemonBannerProps } from 'lib/lemon-ui/LemonBanner/LemonBanner'
@@ -27,7 +27,7 @@ import { urls } from 'scenes/urls'
import { userLogic } from 'scenes/userLogic'
import { ProductKey } from '~/queries/schema/schema-general'
-import { OnboardingStepKey } from '~/types'
+import { OnboardingStepKey, UserType } from '~/types'
import type { projectNoticeLogicType } from './projectNoticeLogicType'
@@ -70,9 +70,11 @@ function storeNoticeDismissal(key: string): void {
/**
* Whether the missing-reverse-proxy notice could be eligible to show (and its data should be fetched).
* Limited to the first 7 days of each month so the nudge stays noticeable without causing fatigue.
+ * Requires an authenticated user and a loaded organization — otherwise the proxy_records GET fires
+ * without a valid session and 401s, polluting error tracking with no banner to show anyway.
*/
-function shouldFetchProxyRecords(): boolean {
- return new Date().getDate() <= 7 && !isNoticeDismissed('missing_reverse_proxy')
+function shouldFetchProxyRecords(user: UserType | null, currentOrganizationId: string | null): boolean {
+ return !!user && !!currentOrganizationId && new Date().getDate() <= 7 && !isNoticeDismissed('missing_reverse_proxy')
}
function buildBillingAlertNotice(
@@ -122,6 +124,8 @@ export const projectNoticeLogic = kea<projectNoticeLogicType>([
['memberCount'],
organizationLogic,
['currentOrganizationId'],
+ userLogic,
+ ['user'],
// Connecting reverseProxyCheckerLogic mounts it and exposes hasReverseProxy reactively.
// A self-managed (DIY) reverse proxy never appears in proxy_records, but it does stamp
// $lib_custom_api_host on events, which the checker detects — this keeps the "set up a
@@ -147,8 +151,17 @@ export const projectNoticeLogic = kea<projectNoticeLogicType>([
proxyRecords: {
__default: null as null | ProxyRecord[],
loadRecords: async () => {
- const response = await api.get(`api/organizations/${values.currentOrganizationId}/proxy_records`)
- return response.results
+ try {
+ const response = await api.get(`api/organizations/${values.currentOrganizationId}/proxy_records`)
+ return response.results
+ } catch (error) {
+ // A missing or expired session makes this boot-time GET 401. There's no banner to
+ // show an unauthenticated user, so swallow it rather than polluting error tracking.
+ if (error instanceof ApiError && error.status === 401) {
+ return null
+ }
+ throw error
+ }
},
},
})),
@@ -248,7 +261,7 @@ export const projectNoticeLogic = kea<projectNoticeLogicType>([
// Only show the reverse proxy nudge on Cloud (or dev) — self-hosted users
// control their own infrastructure and don't need managed proxies.
isCloudOrDev &&
- shouldFetchProxyRecords() &&
+ shouldFetchProxyRecords(user, organization?.id ?? null) &&
proxyRecords !== null &&
proxyRecords.length === 0 &&
// ...and only once the checker has confirmed there's no self-managed proxy
@@ -465,8 +478,8 @@ export const projectNoticeLogic = kea<projectNoticeLogicType>([
}
},
})),
- afterMount(({ actions }) => {
- if (shouldFetchProxyRecords()) {
+ afterMount(({ actions, values }) => {
+ if (shouldFetchProxyRecords(values.user, values.currentOrganizationId)) {
actions.loadRecords()
}
}),
diff --git a/frontend/src/scenes/settings/environment/proxyLogic.ts b/frontend/src/scenes/settings/environment/proxyLogic.ts
index e2f006c7fec8..07d76f61290e 100644
--- a/frontend/src/scenes/settings/environment/proxyLogic.ts
+++ b/frontend/src/scenes/settings/environment/proxyLogic.ts
@@ -339,8 +339,12 @@ export const proxyLogic = kea<proxyLogicType>([
},
},
})),
- afterMount(({ actions, cache }) => {
- actions.loadRecords()
+ afterMount(({ actions, values, cache }) => {
+ // Only fetch when authenticated and the org is loaded — otherwise the GET fires without a
+ // valid session and 401s, polluting error tracking.
+ if (values.user && values.currentOrganizationId) {
+ actions.loadRecords()
+ }
cache.disposables.add(() => {
const timerId = setInterval(() => actions.maybeRefreshRecords(), 5000)
return () => clearInterval(timerId)