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.

feat(ai-research): send a us.posthog.com referer from the image fetcher (#110771)

Robbiea080a3fa33ec
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/README.md b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/README.md
index 76eaf1b886e6..abe85c396570 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/README.md
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/README.md
@@ -548,7 +548,7 @@ Recognised image resizes on `cdn.shopify.com` and single-label `*.myshopify.com`
**14.3** The lane never sends a credential. That covers an `Authorization` header, a proxy credential,
the userinfo of a URL, cookies, and known credential query parameters
-**14.4** The lane never sends a `Referer`
+**14.4** Every request sends `Referer: https://us.posthog.com/`, including requests for `robots.txt` and the other policy files. The lane never sends the URL of the page that showed the image
**14.5** The lane refuses a response where `Content-Length` is over the byte limit
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/configuration-policy.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/configuration-policy.test.ts
index 666b6a7d6a6e..e42438f3adf3 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/configuration-policy.test.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/configuration-policy.test.ts
@@ -559,7 +559,7 @@ describe('HttpConfigurationFetcher', () => {
expect(fetchStreamedMock).toHaveBeenCalledTimes(1)
})
- it('follows and signs each configuration redirect target', async () => {
+ it('follows, signs and identifies each configuration redirect target', async () => {
fetchStreamedMock
.mockResolvedValueOnce(response(302, [{ name: 'location', value: 'https://cdn.example.com/robots' }]))
.mockResolvedValueOnce(
@@ -578,6 +578,10 @@ describe('HttpConfigurationFetcher', () => {
['https://cdn.example.com/robots'],
])
expect(fetchStreamedMock.mock.calls.map(([, options]) => options.allowH2)).toEqual([true, true])
+ expect(fetchStreamedMock.mock.calls.map(([, options]) => options.headers)).toEqual([
+ expect.objectContaining({ referer: 'https://us.posthog.com/' }),
+ expect.objectContaining({ referer: 'https://us.posthog.com/' }),
+ ])
})
it('does not follow a configuration redirect to another registrable domain', async () => {
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/configuration-policy.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/configuration-policy.ts
index 263cc2c38857..d2b6254105f8 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/configuration-policy.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/configuration-policy.ts
@@ -8,11 +8,10 @@ import { ConfigurationCacheItem, ConfigurationFile, HttpCacheMetadata, configura
import { ImageFetchRequestMetrics } from './metrics'
import { canonicalizeUrl, politenessKey } from './politeness-key'
import { ConfigurationFetchReason, ImageFetchProcessingMetrics } from './processing-metrics'
+import { BOT_NAME, REQUEST_IDENTITY_HEADERS } from './request-identity'
import { WebBotAuthRequestSigner } from './web-bot-auth'
import { wildcardPatternMatchesPathname } from './wildcard-pattern'
-const BOT_NAME = 'PostHogImageFetcherBot'
-const USER_AGENT = `${BOT_NAME}/1.0 (+https://posthog.com/docs/ai-research/image-fetcher-bot)`
const CONFIG_BODY_LIMIT = 500 * 1024
const CONFIG_REDIRECT_LIMIT = 5
const CONFIG_FRESH_MS = 24 * 60 * 60 * 1000
@@ -127,7 +126,7 @@ export class HttpConfigurationFetcher {
timeoutMs: Math.max(1, deadlineMs - Date.now()),
allowH2: true,
headers: {
- 'user-agent': USER_AGENT,
+ ...REQUEST_IDENTITY_HEADERS,
accept: file === 'robots' ? 'text/plain,*/*;q=0.1' : 'application/json,*/*;q=0.1',
'accept-encoding': 'identity',
...this.signer.headersForGet(target.toString()),
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetcher.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetcher.test.ts
index 949bd7249a51..3c9129236879 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetcher.test.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetcher.test.ts
@@ -67,7 +67,7 @@ describe('HttpImageFetcher', () => {
)
})
- it('identifies every request as PostHogImageFetcherBot', async () => {
+ it('identifies every request as PostHogImageFetcherBot referred by us.posthog.com', async () => {
fetchStreamedMock.mockResolvedValue(image(PNG, 'image/png'))
await fetcher().fetch('https://cdn.example.com/a.png', OPTIONS)
@@ -78,6 +78,7 @@ describe('HttpImageFetcher', () => {
headers: expect.objectContaining({
'user-agent':
'PostHogImageFetcherBot/1.0 (+https://posthog.com/docs/ai-research/image-fetcher-bot)',
+ referer: 'https://us.posthog.com/',
}),
})
)
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetcher.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetcher.ts
index 46576f2dc5b5..a6793e6d7743 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetcher.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/image-fetcher.ts
@@ -5,6 +5,7 @@ import { OriginPolicyReason, ResponseOptOutReason, responseOptOutReason } from '
import { HttpCacheMetadata } from './crawl-history'
import { ImageFetchRequestMetrics } from './metrics'
import { canonicalizeUrl } from './politeness-key'
+import { REQUEST_IDENTITY_HEADERS } from './request-identity'
import { WebBotAuthRequestSigner } from './web-bot-auth'
/**
@@ -107,10 +108,8 @@ export interface ImageFetcher {
fetch(url: string, options: ImageFetchOptions): Promise<ImageFetchResult>
}
-const USER_AGENT = 'PostHogImageFetcherBot/1.0 (+https://posthog.com/docs/ai-research/image-fetcher-bot)'
-
const REQUEST_HEADERS: Record<string, string> = {
- 'user-agent': USER_AGENT,
+ ...REQUEST_IDENTITY_HEADERS,
accept: 'image/*',
'accept-encoding': 'gzip, deflate, br, zstd',
}
@@ -118,9 +117,10 @@ const REQUEST_HEADERS: Record<string, string> = {
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308])
/**
- * This sends no user credential: no cookie, no `Authorization`, and no `Referer`. This lane must not
- * reach an image behind a login, and a `Referer` would tell the origin which page of the customer's
- * site the image sat on. Web Bot Auth identifies PostHog as the operator of the request.
+ * This sends no user credential: no cookie and no `Authorization`, because this lane must not reach an
+ * image behind a login. The `Referer` names PostHog and never the page that showed the image, because
+ * that page would tell the origin where on the customer's site the image sat. Web Bot Auth identifies
+ * PostHog as the operator of the request.
*
* Every refusal is an outcome rather than a throw. This runs inside a Kafka batch, one URL at a
* time, and a throw would abandon the URLs after it in the same batch.
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/request-identity.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/request-identity.ts
new file mode 100644
index 000000000000..d93e1aaaa1d1
--- /dev/null
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-fetch/request-identity.ts
@@ -0,0 +1,7 @@
+export const BOT_NAME = 'PostHogImageFetcherBot'
+
+export const REQUEST_IDENTITY_HEADERS: Readonly<Record<string, string>> = {
+ 'user-agent': `${BOT_NAME}/1.0 (+https://posthog.com/docs/ai-research/image-fetcher-bot)`,
+ // Replay customers already allow requests from this host so that the replay viewer works. Change it before this lane runs outside prod-us.
+ referer: 'https://us.posthog.com/',
+}