: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
fix(cdp): block warehouse source credential rows fail-closed and with an error (#125154)
Tom Owers5f0e9cca94c1
diff --git a/nodejs/src/cdp/services/hog-executor.service.test.ts b/nodejs/src/cdp/services/hog-executor.service.test.tsindex 994f90a42ca5..d110b6434e04 100644--- a/nodejs/src/cdp/services/hog-executor.service.test.ts+++ b/nodejs/src/cdp/services/hog-executor.service.test.ts@@ -2092,6 +2092,51 @@ describe('Hog Executor', () => {])})+ it('does not substitute the access token of a warehouse source row', async () => {+ jest.mocked(fetch).mockImplementation(() =>+ Promise.resolve({+ status: 200,+ body: 'Hello, world!',+ headers: {},+ json: () => Promise.resolve({}),+ text: () => Promise.resolve(''),+ dump: () => Promise.resolve(),+ })+ )++ const team = (await createTestTeamFixture(hub.postgres)).team+ const warehouse = await insertIntegration(hub.postgres, team.id, {+ kind: 'postgresql' as any,+ config: { warehouse_source_type: 'Postgres' },+ sensitive_config: { access_token: hub.encryptedFields.encrypt('warehouse_secret_token') },+ })+ const placeholder = (+ new HogInputsService(+ hub.integrationManager,+ new RecipientTokensService(hub.ENCRYPTION_SALT_KEYS, hub.SITE_URL),+ hub.encryptedFields+ ) as any+ ).accessTokenPlaceholder(warehouse.id)++ const invocation = createExampleInvocation({ ...hogFunction, team_id: team.id })+ invocation.hogFunction.inputs = { connection: { value: warehouse.id } }+ invocation.hogFunction.inputs_schema = [{ key: 'connection', type: 'integration', required: true }]+ invocation.state.vmState = { stack: [] } as any+ invocation.queueParameters = {+ type: 'fetch',+ url: 'https://example.com/test',+ method: 'POST',+ headers: { Authorization: `Bearer ${placeholder}` },+ body: JSON.stringify({ token: placeholder }),+ } as any++ await createExecutor(hub.encryptedFields).executeFetch(invocation)++ const [, options] = jest.mocked(fetch).mock.calls[0] as any+ expect(options.headers).toEqual({ Authorization: `Bearer ${placeholder}` })+ expect(options.body).not.toContain('warehouse_secret_token')+ })+describe('with non_failure_status_codes', () => {beforeEach(() => {const actualRequest = jest.requireActual('~/common/utils/request') as { fetch: typeof fetch }diff --git a/nodejs/src/cdp/services/hog-inputs.service.test.ts b/nodejs/src/cdp/services/hog-inputs.service.test.tsindex e6281fd8eca2..92004765774c 100644--- a/nodejs/src/cdp/services/hog-inputs.service.test.ts+++ b/nodejs/src/cdp/services/hog-inputs.service.test.ts@@ -93,6 +93,17 @@ describe('Hog Inputs', () => {await closeHub(hub)})+ const insertWarehouseIntegration = async (config: unknown): Promise<number> => {+ const id = team.id + 3+ await insertIntegration(hub.postgres, team.id, {+ id,+ kind: 'postgresql' as any,+ config: (typeof config === 'string' ? JSON.stringify(config) : config) as any,+ sensitive_config: { password: hub.encryptedFields.encrypt('secret-password') },+ })+ return id+ }+describe('formatInput', () => {it('can handle null values in input objects', async () => {const globals = {@@ -363,27 +374,51 @@ describe('Hog Inputs', () => {})it.each([- ['a warehouse source marker', { warehouse_source_type: 'Postgres', host: 'db.example.com' }, null],- ['a null warehouse source marker', { warehouse_source_type: null, team: 'foobar' }, 'loaded'],- ['no warehouse source marker', { team: 'foobar' }, 'loaded'],- ])('handles an integration with %s', async (_name, config, expected) => {- const warehouseId = team.id + 3- await insertIntegration(hub.postgres, team.id, {- id: warehouseId,- kind: 'slack',- config,- sensitive_config: { password: hub.encryptedFields.encrypt('secret-password') },- })+ ['a marker with a string value', { warehouse_source_type: 'Postgres', host: 'db.example.com' }],+ ['a marker with an empty string', { warehouse_source_type: '', host: 'db.example.com' }],+ ['a marker with a null value', { warehouse_source_type: null, host: 'db.example.com' }],+ ['a config that is a string', 'host=db.example.com'],+ ['a config that is an array', ['db.example.com']],+ ])('blocks an integration with %s', async (_name, config) => {+ const warehouseId = await insertWarehouseIntegration(config)hogFunction.inputs = { connection: { value: warehouseId } }hogFunction.inputs_schema = [{ key: 'connection', type: 'integration', required: true }]- const inputs = await hogInputsService.buildInputs(hogFunction, globals)+ const error = await hogInputsService.buildInputs(hogFunction, globals).catch((e) => e)- if (expected === null) {- expect(inputs.connection).toBeNull()- } else {- expect(inputs.connection).toMatchObject({ $integration_id: warehouseId, password: 'secret-password' })+ expect(error.message).toContain("The integration for input 'connection' can't be used as a function input.")+ expect(classifyHogError(error, { runtimeContract: 'any' })).toBe('config')+ const loaded = await hogInputsService.loadIntegrationInputs(hogFunction)+ expect(loaded.connection.value).toBeNull()+ })++ it.each([+ ['no marker', { team: 'foobar' }],+ ['a null config', null],+ ])('loads an integration with %s', async (_name, config) => {+ const warehouseId = await insertWarehouseIntegration({ team: 'foobar' })+ if (config === null) {+ const row = (await hub.integrationManager.getMany([warehouseId]))[warehouseId]+ jest.spyOn(hub.integrationManager, 'getMany').mockResolvedValue({+ [warehouseId]: { ...row!, config: null as any },+ })}+ hogFunction.inputs = { connection: { value: warehouseId } }+ hogFunction.inputs_schema = [{ key: 'connection', type: 'integration', required: true }]++ const inputs = await hogInputsService.buildInputs(hogFunction, globals)++ expect(inputs.connection).toMatchObject({ $integration_id: warehouseId, password: 'secret-password' })+ })++ it('fails an integration_multi input that holds a marked row', async () => {+ const warehouseId = await insertWarehouseIntegration({ warehouse_source_type: 'Postgres' })+ hogFunction.inputs = { channels: { value: [integrationId1, warehouseId] } }+ hogFunction.inputs_schema = [{ key: 'channels', type: 'integration_multi', required: true }]++ await expect(hogInputsService.buildInputs(hogFunction, globals)).rejects.toThrow(+ "The integration for input 'channels' can't be used as a function input."+ )})it.each([diff --git a/nodejs/src/cdp/services/hog-inputs.service.ts b/nodejs/src/cdp/services/hog-inputs.service.tsindex 95212bd67314..fe2db80db986 100644--- a/nodejs/src/cdp/services/hog-inputs.service.ts+++ b/nodejs/src/cdp/services/hog-inputs.service.ts@@ -270,19 +270,18 @@ export class HogInputsService {if (integration.kind === 'posthog') {return null}- // A row with this marker holds data warehouse source credentials. They are never Hog function inputs.- if (integration.config?.warehouse_source_type != null) {+ if (isWarehouseSourceIntegration(integration)) {return null}return {$integration_id: integration.id,...integration.config,...integration.sensitive_config,- ...(integration.sensitive_config.access_token || integration.config.access_token+ ...(integration.sensitive_config.access_token || integration.config?.access_token? {access_token: this.accessTokenPlaceholder(integration.id),access_token_raw:- integration.sensitive_config.access_token ?? integration.config.access_token,+ integration.sensitive_config.access_token ?? integration.config?.access_token,}: {}),}@@ -296,10 +295,26 @@ export class HogInputsService {`The integration for input '${key}' no longer exists. It may have been deleted. Select a connected integration for this input.`)}+ if (isUsed && isWarehouseSourceIntegration(integrations[id])) {+ throw new HogConfigurationError(warehouseIntegrationMessage(key))+ }returnInputs[key] = { value: resolveIntegration(id) }})Object.entries(multiInputsToLoad).forEach(([key, ids]) => {+ const usedValue = options?.throwIfMissingIn?.[key]?.value+ const usedIds = Array.isArray(usedValue) ? usedValue : []+ // Only classify this team's rows, so another team's IDs can't be probed through the error.+ if (+ ids.some(+ (id) =>+ usedIds.includes(id) &&+ integrations[id]?.team_id === hogFunction.team_id &&+ isWarehouseSourceIntegration(integrations[id])+ )+ ) {+ throw new HogConfigurationError(warehouseIntegrationMessage(key))+ }returnInputs[key] = { value: ids.map(resolveIntegration).filter((v) => v !== null) }})@@ -312,6 +327,22 @@ export class HogInputsService {}}+// A row with the `warehouse_source_type` key holds data warehouse source credentials. They are never Hog function+// inputs. This matches the Python check: the key blocks with any value, and a config that is not an object blocks too.+const isWarehouseSourceIntegration = (integration: { config?: unknown } | null | undefined): boolean => {+ const config = integration?.config+ if (config === null || config === undefined) {+ return false+ }+ if (typeof config !== 'object' || Array.isArray(config)) {+ return true+ }+ return 'warehouse_source_type' in config+}++const warehouseIntegrationMessage = (key: string): string =>+ `The integration for input '${key}' can't be used as a function input. Select a different integration for this input.`+export const formatHogInput = async (bytecode: any,globals: HogFunctionInvocationGlobalsWithInputs,