: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(cdp): absolute dns lookups and negative cache for third-party hosts (#113356)
Meikel Ratz01ed2dec74be
diff --git a/nodejs/src/common/config.ts b/nodejs/src/common/config.tsindex d838ea19834b..3be4932cb6aa 100644--- a/nodejs/src/common/config.ts+++ b/nodejs/src/common/config.ts@@ -230,6 +230,17 @@ export type CommonConfig = BaseServerConfig & {// Takes the buildIntegerMatcherWithPercentage syntax: '2' for team 2 only, '2,*:0.1' for team 2 plus a tenth of// everyone else's requests, '*' for all.EXTERNAL_REQUEST_PROXY_TEAMS: string+ // Resolve third-party hostnames as absolute names (with a trailing dot) on the direct route. The resolver then+ // skips the cluster search domains, so a hostname that does not exist costs one query instead of one per search+ // domain. A customer-supplied hostname never names a cluster service, so the search domains never help it.+ EXTERNAL_REQUEST_DNS_ABSOLUTE_LOOKUP: boolean+ // A process-local cache of hostnames that returned ENOTFOUND on the direct route. 'off' disables it. 'shadow'+ // still runs every lookup and only counts the lookups the cache would have skipped. 'enforce' fails those lookups+ // at once with the same ResolutionError. Only ENOTFOUND is cached: a timeout or SERVFAIL can be the resolver's+ // fault, and caching it would fail healthy destinations while the resolver is under load.+ EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MODE: string+ EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_TTL_MS: number+ EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MAX_ENTRIES: number// PostHog analyticsPOSTHOG_API_KEY: string@@ -270,6 +281,10 @@ export type ExternalRequestConfig = Pick<| 'EXTERNAL_REQUEST_CONNECTIONS'| 'EXTERNAL_REQUEST_H2_CONNECTIONS'| 'EXTERNAL_REQUEST_PROXY_TEAMS'+ | 'EXTERNAL_REQUEST_DNS_ABSOLUTE_LOOKUP'+ | 'EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MODE'+ | 'EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_TTL_MS'+ | 'EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MAX_ENTRIES'>export function getExternalRequestConfig(): ExternalRequestConfig {@@ -283,6 +298,14 @@ export function getExternalRequestConfig(): ExternalRequestConfig {EXTERNAL_REQUEST_CONNECTIONS: Number(process.env.EXTERNAL_REQUEST_CONNECTIONS ?? 500),EXTERNAL_REQUEST_H2_CONNECTIONS: Number(process.env.EXTERNAL_REQUEST_H2_CONNECTIONS ?? 8),EXTERNAL_REQUEST_PROXY_TEAMS: process.env.EXTERNAL_REQUEST_PROXY_TEAMS ?? '',+ EXTERNAL_REQUEST_DNS_ABSOLUTE_LOOKUP: process.env.EXTERNAL_REQUEST_DNS_ABSOLUTE_LOOKUP === 'true',+ EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MODE: process.env.EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MODE ?? 'off',+ EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_TTL_MS: Number(+ process.env.EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_TTL_MS ?? 10_000+ ),+ EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MAX_ENTRIES: Number(+ process.env.EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MAX_ENTRIES ?? 10_000+ ),}}@@ -440,6 +463,10 @@ export function getDefaultCommonConfig(): CommonConfig {EXTERNAL_REQUEST_CONNECTIONS: 500,EXTERNAL_REQUEST_H2_CONNECTIONS: 8,EXTERNAL_REQUEST_PROXY_TEAMS: '',+ EXTERNAL_REQUEST_DNS_ABSOLUTE_LOOKUP: false,+ EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MODE: 'off',+ EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_TTL_MS: 10_000,+ EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MAX_ENTRIES: 10_000,// PostHog analyticsPOSTHOG_API_KEY: '',diff --git a/nodejs/src/common/utils/request.test.ts b/nodejs/src/common/utils/request.test.tsindex 2c5458daaedf..1d3df2e2c991 100644--- a/nodejs/src/common/utils/request.test.ts+++ b/nodejs/src/common/utils/request.test.ts@@ -411,6 +411,130 @@ describe('fetch', () => {})})+type IsolatedRequest = {+ request: typeof import('./request')+ lookup: jest.Mock+ readNegativeCacheCounter: (result: string) => Promise<number>+}++// request.ts reads the DNS settings once, at module load, so each case loads a fresh copy with its own environment.+async function withDnsConfig(env: Record<string, string>, run: (isolated: IsolatedRequest) => Promise<void>) {+ const originalValues = Object.fromEntries(Object.keys(env).map((name) => [name, process.env[name]]))+ Object.assign(process.env, env)+ try {+ await jest.isolateModulesAsync(async () => {+ const request: typeof import('./request') = require('./request')+ const lookup = jest.mocked(require('dns/promises').lookup)+ const registry: typeof register = require('prom-client').register+ const readNegativeCacheCounter = async (result: string): Promise<number> =>+ (await registry.getSingleMetric('node_dns_negative_cache_total')!.get()).values.find(+ (value) => value.labels.result === result+ )?.value ?? 0+ try {+ await run({ request, lookup, readNegativeCacheCounter })+ } finally {+ await request.closeSharedAgents(100)+ }+ })+ } finally {+ for (const [name, value] of Object.entries(originalValues)) {+ if (value === undefined) {+ delete process.env[name]+ } else {+ process.env[name] = value+ }+ }+ }+}++const dnsError = (code: string): Error => Object.assign(new Error(`getaddrinfo ${code} example.com`), { code })++describe('DNS lookup settings', () => {+ it('resolves hostnames as absolute names when absolute lookups are enabled', async () => {+ await withDnsConfig({ EXTERNAL_REQUEST_DNS_ABSOLUTE_LOOKUP: 'true' }, async ({ request, lookup }) => {+ lookup.mockResolvedValue([{ address: '1.1.1.1', family: 4 }])++ await request.raiseIfUserProvidedUrlUnsafe('https://example.com/path')+ await request.raiseIfUserProvidedUrlUnsafe('https://already-absolute.com./path')+ await request.raiseIfUserProvidedUrlUnsafe('https://1.1.1.1/path')++ expect(lookup.mock.calls.map(([hostname]) => hostname)).toEqual([+ 'example.com.',+ 'already-absolute.com.',+ '1.1.1.1',+ ])+ })+ })++ it('resolves hostnames as given when absolute lookups are disabled', async () => {+ await withDnsConfig({ EXTERNAL_REQUEST_DNS_ABSOLUTE_LOOKUP: 'false' }, async ({ request, lookup }) => {+ lookup.mockResolvedValue([{ address: '1.1.1.1', family: 4 }])++ await request.raiseIfUserProvidedUrlUnsafe('https://example.com/path')++ expect(lookup).toHaveBeenCalledWith('example.com', { all: true })+ })+ })++ it('skips the lookup for a hostname that recently returned ENOTFOUND in enforce mode', async () => {+ await withDnsConfig(+ { EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MODE: 'enforce' },+ async ({ request, lookup, readNegativeCacheCounter }) => {+ lookup.mockRejectedValue(dnsError('ENOTFOUND'))++ await expect(request.raiseIfUserProvidedUrlUnsafe('https://example.com')).rejects.toThrow(+ 'Invalid hostname'+ )+ await expect(request.raiseIfUserProvidedUrlUnsafe('https://example.com')).rejects.toThrow(+ 'Invalid hostname'+ )++ expect(lookup).toHaveBeenCalledTimes(1)+ expect(await readNegativeCacheCounter('hit')).toEqual(1)+ }+ )+ })++ // A timeout can come from an overloaded resolver rather than from the hostname. Caching it would fail healthy+ // destinations for as long as the resolver stays slow.+ it.each(['EAI_AGAIN', 'ETIMEOUT'])('does not cache a %s failure', async (code) => {+ await withDnsConfig({ EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MODE: 'enforce' }, async ({ request, lookup }) => {+ lookup.mockRejectedValue(dnsError(code))++ await expect(request.raiseIfUserProvidedUrlUnsafe('https://example.com')).rejects.toThrow()+ await expect(request.raiseIfUserProvidedUrlUnsafe('https://example.com')).rejects.toThrow()++ expect(lookup).toHaveBeenCalledTimes(2)+ })+ })++ it('still looks up a cached hostname in shadow mode and counts what enforce mode would skip', async () => {+ await withDnsConfig(+ { EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MODE: 'shadow' },+ async ({ request, lookup, readNegativeCacheCounter }) => {+ lookup.mockRejectedValueOnce(dnsError('ENOTFOUND'))+ lookup.mockRejectedValueOnce(dnsError('ENOTFOUND'))+ lookup.mockResolvedValue([{ address: '1.1.1.1', family: 4 }])++ await expect(request.raiseIfUserProvidedUrlUnsafe('https://example.com')).rejects.toThrow()+ await expect(request.raiseIfUserProvidedUrlUnsafe('https://example.com')).rejects.toThrow()+ await request.raiseIfUserProvidedUrlUnsafe('https://example.com')+ await request.raiseIfUserProvidedUrlUnsafe('https://example.com')++ expect(lookup).toHaveBeenCalledTimes(4)+ expect(await readNegativeCacheCounter('shadow_hit')).toEqual(3)+ expect(await readNegativeCacheCounter('shadow_hit_resolved')).toEqual(2)+ }+ )+ })++ it('rejects an unknown negative cache mode at startup', async () => {+ await expect(+ withDnsConfig({ EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MODE: 'on' }, () => Promise.resolve())+ ).rejects.toThrow('EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MODE must be one of off, shadow, enforce')+ })+})+describe('legacyFetch', () => {beforeEach(() => {jest.setTimeout(1000)diff --git a/nodejs/src/common/utils/request.ts b/nodejs/src/common/utils/request.tsindex c9ead39bf42b..fdd0cbb2ec52 100644--- a/nodejs/src/common/utils/request.ts+++ b/nodejs/src/common/utils/request.ts@@ -1,6 +1,7 @@import { LookupAddress } from 'dns'import dns from 'dns/promises'import * as ipaddr from 'ipaddr.js'+import { LRUCache } from 'lru-cache'import net from 'node:net'import { Counter, Gauge, Histogram } from 'prom-client'// eslint-disable-next-line no-restricted-imports@@ -73,6 +74,49 @@ const dnsLookupsInFlight = new Gauge({help: 'Number of dns.lookup() calls currently awaiting a result',})+// 'shadow_hit_resolved' counts shadow hits whose lookup then succeeded. Enforce mode would have failed those requests+// wrongly, so this must stay near zero before a deployment moves from shadow to enforce.+const dnsNegativeCacheCounter = new Counter({+ name: 'node_dns_negative_cache_total',+ help: 'Lookups the DNS negative cache skipped (hit), would have skipped in shadow mode (shadow_hit), or would have skipped although the hostname resolved (shadow_hit_resolved)',+ labelNames: ['result'],+})++const DNS_NEGATIVE_CACHE_MODES = ['off', 'shadow', 'enforce'] as const+type DnsNegativeCacheMode = (typeof DNS_NEGATIVE_CACHE_MODES)[number]++const dnsNegativeCacheMode = requestConfig.EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MODE as DnsNegativeCacheMode+if (!DNS_NEGATIVE_CACHE_MODES.includes(dnsNegativeCacheMode)) {+ throw new Error(+ `EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MODE must be one of ${DNS_NEGATIVE_CACHE_MODES.join(', ')}, got '${dnsNegativeCacheMode}'`+ )+}+for (const name of [+ 'EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_TTL_MS',+ 'EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MAX_ENTRIES',+] as const) {+ if (!Number.isInteger(requestConfig[name]) || requestConfig[name] < 1) {+ throw new Error(`${name} must be a positive integer, got ${process.env[name]}`)+ }+}++// The key is the hostname, which is customer-supplied and unbounded, so the entry count is capped as well as the age.+const dnsNegativeCache =+ dnsNegativeCacheMode === 'off'+ ? null+ : new LRUCache<string, true>({+ max: requestConfig.EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_MAX_ENTRIES,+ ttl: requestConfig.EXTERNAL_REQUEST_DNS_NEGATIVE_CACHE_TTL_MS,+ })++// dns.lookup returns an IP literal without a DNS query. With a trailing dot it treats '1.1.1.1.' as a name, and that+// query fails with ENOTFOUND.+function dnsLookupName(hostname: string): string {+ return requestConfig.EXTERNAL_REQUEST_DNS_ABSOLUTE_LOOKUP && !hostname.endsWith('.') && net.isIP(hostname) === 0+ ? `${hostname}.`+ : hostname+}+// NOTE: This isn't exactly fetch - it's meant to be very close but limited to only options we actually want to exposeexport type FetchOptions = {method?: string@@ -224,17 +268,39 @@ function isIPv4(addr: ipaddr.IPv4 | ipaddr.IPv6): addr is ipaddr.IPv4 {async function staticLookupAsync(hostname: string): Promise<LookupAddress[]> {let addrinfo: LookupAddress[]const validAddrinfo: LookupAddress[] = []+ const cacheKey = hostname.toLowerCase().replace(/\.$/, '')+ const cachedFailure = dnsNegativeCache?.has(cacheKey) ?? false+ if (cachedFailure) {+ if (dnsNegativeCacheMode === 'enforce') {+ dnsNegativeCacheCounter.inc({ result: 'hit' })+ throw new ResolutionError('Invalid hostname')+ }+ dnsNegativeCacheCounter.inc({ result: 'shadow_hit' })+ }const lookupStartedAt = performance.now()dnsLookupsInFlight.inc()try {- addrinfo = await dns.lookup(hostname, { all: true })+ addrinfo = await dns.lookup(dnsLookupName(hostname), { all: true })dnsLookupDuration.observe({ outcome: 'success' }, performance.now() - lookupStartedAt)- } catch {+ } catch (error) {dnsLookupDuration.observe({ outcome: 'failure' }, performance.now() - lookupStartedAt)+ // A shadow hit does not refresh the entry, because enforce mode would not have run this lookup. The shadow+ // counts then match what enforce mode would skip.+ if (!cachedFailure && (error as NodeJS.ErrnoException)?.code === 'ENOTFOUND') {+ dnsNegativeCache?.set(cacheKey, true)+ }throw new ResolutionError('Invalid hostname')} finally {dnsLookupsInFlight.dec()}+ if (cachedFailure) {+ // A shadow hit keeps the entry, because enforce mode would not have run this lookup and would keep failing the+ // hostname until the entry expires. Each later success within the TTL then counts here too.+ dnsNegativeCacheCounter.inc({ result: 'shadow_hit_resolved' })+ } else {+ // A lookup that started before another one cached the failure can still succeed. The hostname resolves now.+ dnsNegativeCache?.delete(cacheKey)+ }const resolvedIps = addrinfo.map((a) => a.address)for (const addrInfo of addrinfo) {const parsed = ipaddr.parse(addrInfo.address)