: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
import { DateTime } from 'luxon'
import type { AnonymizeKafkaPayloadResult, AnonymizeMeta } from '@posthog/replay-anonymizer'
import { parseJSON } from '~/common/utils/json-parse'
import { logger } from '~/common/utils/logger'
import { normalizeSessionId } from '~/common/utils/utils'
import { dlq, drop, ok } from '~/ingestion/framework/results'
import { ProcessingStep } from '~/ingestion/framework/steps'
import { recordAnonymizeTimingSpans } from '~/ingestion/pipelines/sessionreplay/anonymize-timing-spans'
import { ParsedMessageData } from '~/ingestion/pipelines/sessionreplay/kafka/types'
import { SessionRecordingIngesterMetrics } from '~/ingestion/pipelines/sessionreplay/metrics'
import {
hashImageBytes,
imageRef,
isImageRef,
urlRef,
} from '~/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub/content-ref'
import {
ParseMessageStepInput,
ParseMessageStepOutput,
getContentEncoding,
isGzipped,
} from '~/ingestion/pipelines/sessionreplay/parse-message-step'
import { TeamForReplay } from '~/ingestion/pipelines/sessionreplay/teams/types'
import { MlSessionKeys } from './keys/key-store'
import { MlMirrorMetrics } from './metrics'
import { ProducedRefs, producedRefDedup } from './produced-refs'
import {
PSEUDONYM_IMAGE_CONTENT_KEY,
PSEUDONYM_IMAGE_URL_GLOBAL_VALUE,
PSEUDONYM_IMAGE_URL_KEY,
PSEUDONYM_TEAM,
pseudonymize,
} from './pseudonymize'
import { getRustAnonymizer } from './rust-anonymizer'
import { mlDatasetVersion, sessionStartMonth, usesRawSessionIdentifiers } from './session-identifier-format'
const MESSAGE_TIMESTAMP_DIFF_THRESHOLD_DAYS = 7
// Addon failure reasons that map to a DLQ (mirroring the TS parse step's classifications).
const DLQ_REASONS = new Set([
'invalid_compressed_data',
'invalid_json',
'invalid_message_payload',
'received_non_snapshot_message',
])
/** An original image the addon collected for the out-of-band scrub lane, ready to produce. */
export interface CollectedImage {
/** `image:<teamId>:<hash>`, the Kafka key the scrub consumer indexes the bytes under. */
ref: string
bytes: Buffer
}
/**
* A remote image URL the addon collected for the fetch lane.
*
* `url` is the original, unscrubbed URL. It carries whatever the page put in its path and query.
* It is therefore as sensitive as the raw replay payload. It must not reach a log line, a metric
* label, or any destination outside the fetch topic.
*/
export interface CollectedUrl {
/** `imageurl:<hash>` stored in the mirrored line's namespaced ref attribute. */
ref: string
teamId: string
url: string
/** The host the request goes to. robots.txt and the connection limit are scoped to this. */
host: string
/** The registrable domain of `host` — the fetch topic's Kafka key, so every URL of one operator
* lands on one partition. */
domain: string
}
export interface ParseAndAnonymizeStepOutput extends ParseMessageStepOutput {
collectedImages?: CollectedImage[]
collectedUrls?: CollectedUrl[]
}
export interface ImageCollectionConfig {
/** Only derived image content and URL keys cross the FFI; the root key stays here. */
pseudonymSecret: string | Buffer
/** Replace inlined images with refs and return their bytes for the scrub topic. */
collectImages: boolean
maxImageBytes: number
/**
* Keep a remote image's placeholder, stash its ref, and return its URL for the fetch lane.
*
* Independent of `collectImages`. The two lanes have separate destinations and separate
* rollouts, so tying them together would make the URL measurement wait on the scrub lane.
*/
collectUrls: boolean
}
/**
* Fused parse + anonymize through the native Rust addon (`@posthog/replay-anonymizer`): the
* decompressed Kafka payload bytes go in, scrubbed JSONL block lines plus the envelope/per-event
* metadata come out — no per-event JS objects are ever built, and no JSON crosses the FFI boundary
* as a string.
*
* Fail-closed: any addon failure drops (or DLQs) the message — un-anonymized data never reaches the
* unencrypted ML bucket. Failure classification matches the TS parse step so DLQ/drop behavior and
* ingestion warnings are unchanged.
*/
export function createParseAndAnonymizeMessageStep<
T extends ParseMessageStepInput & { team: TeamForReplay; mlKeys?: MlSessionKeys },
>(
imageCollection?: ImageCollectionConfig,
producedRefs?: ProducedRefs
): ProcessingStep<T, T & ParseAndAnonymizeStepOutput> {
// The addon rejects a bad cap on every call, which would drop every message while the consumer looks healthy.
if (
imageCollection &&
!(Number.isSafeInteger(imageCollection.maxImageBytes) && imageCollection.maxImageBytes > 0)
) {
throw new Error(`maxImageBytes must be a positive integer, got ${imageCollection.maxImageBytes}`)
}
const globalUrlKey =
imageCollection?.collectUrls === true
? pseudonymize(imageCollection.pseudonymSecret, PSEUDONYM_IMAGE_URL_KEY, PSEUDONYM_IMAGE_URL_GLOBAL_VALUE)
: undefined
// Cache the team values rather than re-deriving them for every message. The content key keys
// the inline image hash. The URL key is global and does not use this cache.
interface TeamImageKeys {
teamId: string
contentKey?: string
}
const teamKeysCache = new Map<string, TeamImageKeys>()
const teamKeysFor = (teamId: number, sessionId: string): TeamImageKeys | undefined => {
if (!imageCollection) {
return undefined
}
const rawIdentifiers = usesRawSessionIdentifiers(sessionId)
const cacheKey = `${teamId}:${rawIdentifiers}`
let keys = teamKeysCache.get(cacheKey)
if (!keys) {
const teamIdString = rawIdentifiers
? String(teamId)
: pseudonymize(imageCollection.pseudonymSecret, PSEUDONYM_TEAM, String(teamId))
const contentKey = pseudonymize(
imageCollection.pseudonymSecret,
PSEUDONYM_IMAGE_CONTENT_KEY,
String(teamId)
)
// Reject refs the consumer cannot parse so images fall back to inline scrubbing.
if (!isImageRef(imageRef(teamIdString, hashImageBytes(contentKey, Buffer.alloc(0))))) {
logger.error('🖼️', 'ml_image_scrub_team_id_shape_invalid', { teamId })
MlMirrorMetrics.incrementMlImageTeamIdInvalid()
return undefined
}
keys = {
teamId: teamIdString,
contentKey: imageCollection.collectImages ? contentKey : undefined,
}
teamKeysCache.set(cacheKey, keys)
}
return keys
}
return async function parseAndAnonymizeMessageStep(input) {
const { message, headers } = input
if (!message.value || !message.timestamp) {
return dlq('message_value_or_timestamp_is_empty')
}
// Decompression happens inside the addon, off the event loop (gunzipSync here would block
// it); the encoding metric only needs the header and the magic bytes.
const contentEncoding = getContentEncoding(message.headers)
SessionRecordingIngesterMetrics.incrementMessagesByEncoding(
contentEncoding ?? (isGzipped(message.value) ? 'gzip' : 'none')
)
const teamKeys = teamKeysFor(input.team.teamId, headers.session_id)
const sessionKeys = input.mlKeys
let referenceNamespace: string | undefined
let imageTeamId: string | undefined
const t0 = performance.now()
const callStartEpochMs = performance.timeOrigin + t0
let result
try {
referenceNamespace =
sessionKeys && usesRawSessionIdentifiers(headers.session_id)
? `v${mlDatasetVersion(headers.session_id)}:${input.team.teamId}:${sessionStartMonth(headers.session_id)}`
: undefined
imageTeamId = referenceNamespace ?? teamKeys?.teamId
result = await getRustAnonymizer().anonymizeKafkaPayload(
message.value,
contentEncoding,
imageTeamId,
teamKeys?.contentKey,
globalUrlKey,
referenceNamespace,
producedRefDedup(producedRefs, Date.now()),
imageCollection?.maxImageBytes
)
} catch (error) {
// A rejected promise (native panic, addon load failure) must fail closed.
logger.warn('🙈', 'anonymize_event_failed', { error: String(error) })
MlMirrorMetrics.incrementMlAnonymizeFailed('rust')
return drop('anonymize_failed')
}
MlMirrorMetrics.observeMlAnonymizeDuration('rust', performance.now() - t0, result.route ?? '')
recordAnonymizeTimingSpans(callStartEpochMs, result.timings, {
route: result.route,
failureReason: result.failed ? (result.reason ?? 'anonymize_failed') : null,
})
if (result.failed) {
if (result.reason && DLQ_REASONS.has(result.reason)) {
return dlq(result.reason, new Error(result.error ?? result.reason))
}
if (result.reason === 'ml_mirror_non_web_replay') {
return drop('ml_mirror_non_web_replay')
}
if (result.reason === 'message_contained_no_valid_rrweb_events') {
return drop(
'message_contained_no_valid_rrweb_events',
[],
[
{
type: 'message_contained_no_valid_rrweb_events',
details: {},
},
]
)
}
// anonymize_failed (or anything unclassified): fail closed.
logger.warn('🙈', 'anonymize_event_failed', { error: result.error ?? 'rust anonymizer failed' })
MlMirrorMetrics.incrementMlAnonymizeFailed('rust')
return drop('anonymize_failed')
}
let meta: AnonymizeMeta
try {
meta = parseJSON(result.meta!) as AnonymizeMeta
} catch (error) {
// Fail closed: an uncaught throw here poisons the pipeline instead of dropping one message.
logger.warn('🙈', 'anonymize_event_failed', { error: String(error) })
MlMirrorMetrics.incrementMlAnonymizeFailed('rust')
return drop('anonymize_failed')
}
if (!isWebReplay(meta.snapshotSource)) {
return drop('ml_mirror_non_web_replay')
}
const startDateTime = DateTime.fromMillis(meta.startTs)
const endDateTime = DateTime.fromMillis(meta.endTs)
const startDiff = Math.abs(startDateTime.diffNow('day').days)
const endDiff = Math.abs(endDateTime.diffNow('day').days)
if (startDiff >= MESSAGE_TIMESTAMP_DIFF_THRESHOLD_DAYS || endDiff >= MESSAGE_TIMESTAMP_DIFF_THRESHOLD_DAYS) {
return drop(
'message_timestamp_diff_too_large',
[],
[
{
type: 'message_timestamp_diff_too_large',
details: {
startDiffDays: startDiff,
endDiffDays: endDiff,
thresholdDays: MESSAGE_TIMESTAMP_DIFF_THRESHOLD_DAYS,
},
},
]
)
}
const sessionId = normalizeSessionId(meta.sessionId)
// session_id and distinct_id are carried both in the headers (set by capture) and in the
// message body; they must agree — a mismatch means the message is corrupt or mis-routed.
if (headers.session_id !== sessionId) {
return dlq('session_id_header_body_mismatch')
}
if (headers.distinct_id !== meta.distinctId) {
return dlq('distinct_id_header_body_mismatch')
}
recordImageSources(meta)
for (const drop of meta.scrubDrops ?? []) {
MlMirrorMetrics.incrementMlScrubDrops(drop.reason, drop.count)
}
MlMirrorMetrics.incrementMlJsonLdEvents(meta.jsonLdEventCount)
const parsedMessage: ParsedMessageData = {
metadata: {
partition: message.partition,
topic: message.topic,
rawSize: message.size,
offset: message.offset,
timestamp: message.timestamp,
},
distinct_id: meta.distinctId,
session_id: sessionId,
token: headers.token,
// Events live in `preSerialized` — consumers use its lines + per-event metadata.
eventsByWindowId: {},
preSerialized: {
windowId: meta.windowId,
lines: result.lines!,
events: meta.events,
consoleLogCount: meta.consoleLogCount,
consoleWarnCount: meta.consoleWarnCount,
consoleErrorCount: meta.consoleErrorCount,
},
eventsRange: {
start: startDateTime,
end: endDateTime,
},
snapshot_source: meta.snapshotSource,
snapshot_library: meta.snapshotLibrary,
}
const collectedImages = teamKeys?.contentKey ? unpackCollectedImages(imageTeamId!, meta, result) : undefined
const collectedUrls =
globalUrlKey && teamKeys
? unpackCollectedUrls(teamKeys.teamId, meta, result, referenceNamespace)
: undefined
return ok({ ...input, parsedMessage, collectedImages, collectedUrls })
}
}
/**
* The anonymizer scrubs the web rrweb shapes only. Mobile SDKs send wireframes and screenshots, which
* none of its scrubbers read, so only a web replay is mirrored. Capture writes `web` when a client
* sends no source, so a missing one never comes from capture.
*/
export function isWebReplay(snapshotSource: string | null | undefined): boolean {
return snapshotSource === 'web'
}
function recordImageSources(meta: AnonymizeMeta): void {
for (const imageSource of meta.imageSources ?? []) {
MlMirrorMetrics.incrementMlImageReferencesByProperty(
imageSource.source,
imageSource.property,
imageSource.kind,
imageSource.count
)
}
}
/**
* Slice the addon's packed image buffer into per-image produce records. The lines already carry the
* refs, so a skipped slice only means that ref stays dangling (same outcome as a failed produce) —
* never a blocked message.
*
* The addon left out the images an earlier message produced. They still count as collected.
*/
function unpackCollectedImages(
teamId: string,
meta: AnonymizeMeta,
result: Pick<AnonymizeKafkaPayloadResult, 'images' | 'dedupedImageCount'>
): CollectedImage[] | undefined {
const deduped = result.dedupedImageCount ?? 0
MlMirrorMetrics.incrementMlImagesCollected('deduped', deduped)
const packed = result.images
if (!meta.images?.length || !packed) {
MlMirrorMetrics.incrementMlImagesCollected('collected', deduped)
return undefined
}
const images: CollectedImage[] = []
for (const entry of meta.images) {
if (entry.offset < 0 || entry.len < 0 || entry.offset + entry.len > packed.length) {
logger.warn('🙈', 'collected_image_entry_out_of_bounds', { ...entry, packedLength: packed.length })
continue
}
images.push({
ref: imageRef(teamId, entry.hash),
bytes: packed.subarray(entry.offset, entry.offset + entry.len),
})
}
MlMirrorMetrics.incrementMlImagesCollected('collected', images.length + deduped)
return images.length > 0 ? images : undefined
}
/**
* Turn the addon's `meta.urls` into produce-ready records.
*
* The domain count is observed for a message with no URL too. A count taken only from messages
* that carry one describes an image-heavy page, and this number exists to size a topic that
* carries all the traffic.
*
* The addon left out the URLs an earlier message produced. They still count as collected, and the
* addon counted the domains before it left them out.
*/
function unpackCollectedUrls(
teamId: string,
meta: AnonymizeMeta,
result: Pick<AnonymizeKafkaPayloadResult, 'dedupedUrlCount' | 'collectedUrlDomainCount'>,
referenceNamespace?: string
): CollectedUrl[] | undefined {
const urls: CollectedUrl[] = (meta.urls ?? []).map((entry) => ({
ref: referenceNamespace ? `imageurl:${referenceNamespace}:${entry.hash}` : urlRef(entry.hash),
teamId,
url: entry.url,
host: entry.host,
domain: entry.domain,
}))
for (const decline of meta.urlDeclines ?? []) {
MlMirrorMetrics.incrementMlUrlsDeclined(decline.reason, decline.count)
}
const deduped = result.dedupedUrlCount ?? 0
const collected = urls.length + deduped
MlMirrorMetrics.observeMlUrlDomainsPerMessage(
result.collectedUrlDomainCount ?? new Set(urls.map(({ domain }) => domain)).size
)
if (collected === 0) {
return undefined
}
MlMirrorMetrics.incrementMlUrlsCollected('collected', collected)
MlMirrorMetrics.incrementMlUrlsCollected('deduped', deduped)
MlMirrorMetrics.observeMlUrlsPerMessage(collected)
return urls.length > 0 ? urls : undefined
}