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(replay): extract session-replay scrubber into publishable crate (#71648)

Robbie378146d74610
diff --git a/.github/workflows/ci-nodejs-container.yml b/.github/workflows/ci-nodejs-container.yml
index 2a575891213d..bf957a8c226c 100644
--- a/.github/workflows/ci-nodejs-container.yml
+++ b/.github/workflows/ci-nodejs-container.yml
@@ -14,6 +14,7 @@ on:
- 'common/replay-headless/**'
- 'rust/cyclotron-node/**'
- 'rust/common/hogvm/**'
+ - 'rust/replay-anonymizer/**'
- 'rust/replay-anonymizer-node/**'
- 'Dockerfile.node'
- '.github/workflows/ci-nodejs-container.yml'
@@ -66,6 +67,7 @@ jobs:
- 'common/replay-headless/**'
- 'rust/cyclotron-node/**'
- 'rust/common/hogvm/**'
+ - 'rust/replay-anonymizer/**'
- 'rust/replay-anonymizer-node/**'
- 'Dockerfile.node'
- '.github/workflows/ci-nodejs-container.yml'
diff --git a/.github/workflows/ci-nodejs.yml b/.github/workflows/ci-nodejs.yml
index c0fe54de966d..bfb301bcd9b3 100644
--- a/.github/workflows/ci-nodejs.yml
+++ b/.github/workflows/ci-nodejs.yml
@@ -61,6 +61,7 @@ jobs:
- 'nodejs/**'
# The Jest shared-fixtures suite pins the native replay anonymizer against
# its Rust tests; a rust-side change (incl. the shared fixtures) must run it.
+ - 'rust/replay-anonymizer/**'
- 'rust/replay-anonymizer-node/**'
- 'posthog/clickhouse/**'
- 'ee/migrations/**'
diff --git a/.github/workflows/ci-rust.yml b/.github/workflows/ci-rust.yml
index 0b3f4783776d..5bdc22d36f3a 100644
--- a/.github/workflows/ci-rust.yml
+++ b/.github/workflows/ci-rust.yml
@@ -156,7 +156,8 @@ jobs:
"personhog-router": 38,
"posthog-symbol-data": 19,
"property-defs-rs": 136,
- "replay-anonymizer-node": 20,
+ "posthog-replay-anonymizer": 20,
+ "replay-anonymizer-node": 10,
"property-vals-rs": 5,
"serve-metrics": 14,
}
diff --git a/.github/workflows/publish-replay-anonymizer-crate.yml b/.github/workflows/publish-replay-anonymizer-crate.yml
new file mode 100644
index 000000000000..971494df7033
--- /dev/null
+++ b/.github/workflows/publish-replay-anonymizer-crate.yml
@@ -0,0 +1,51 @@
+name: Publish posthog-replay-anonymizer to crates.io
+
+on:
+ push:
+ tags:
+ - 'posthog-replay-anonymizer/v*'
+
+env:
+ CARGO_TERM_COLOR: always
+
+jobs:
+ publish:
+ name: Publish to crates.io
+ runs-on: ubuntu-24.04
+ timeout-minutes: 10
+ if: github.repository == 'PostHog/posthog'
+
+ permissions:
+ contents: read
+ id-token: write
+
+ defaults:
+ run:
+ working-directory: rust
+
+ steps:
+ - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+
+ - name: Install rust
+ uses: dtolnay/rust-toolchain@3c5f7ea28cd621ae0bf5283f0e981fb97b8a7af9
+ with:
+ toolchain: 1.91.1
+
+ - name: Verify tag matches crate version
+ run: |
+ TAG_VERSION="${GITHUB_REF_NAME#posthog-replay-anonymizer/v}"
+ CRATE_VERSION=$(grep '^version' replay-anonymizer/Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
+ if [ "$TAG_VERSION" != "$CRATE_VERSION" ]; then
+ echo "::error::Tag version ($TAG_VERSION) does not match crate version ($CRATE_VERSION)"
+ exit 1
+ fi
+ echo "Publishing posthog-replay-anonymizer v$CRATE_VERSION"
+
+ - name: Authenticate with crates.io
+ id: auth
+ uses: rust-lang/crates-io-auth-action@bbd81622f20ce9e2dd9622e3218b975523e45bbe # v1.0.4
+
+ - name: Publish to crates.io
+ env:
+ CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
+ run: cargo publish -p posthog-replay-anonymizer
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/kafka/types.ts b/nodejs/src/ingestion/pipelines/sessionreplay/kafka/types.ts
index 9b8c72af6b2a..0edbf4995763 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/kafka/types.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/kafka/types.ts
@@ -55,7 +55,7 @@ export const EventSchema = z.object({
// Ready-to-write JSONL lines plus per-event metadata from the native anonymizer.
-// Per-event flag bits, mirroring `rust/replay-anonymizer-node/src/snapshot.rs` (EVENT_FLAG_*).
+// Per-event flag bits, mirroring `rust/replay-anonymizer/src/snapshot.rs` (EVENT_FLAG_*).
export const PRE_SERIALIZED_FLAG_ACTIVE = 1
export const PRE_SERIALIZED_FLAG_CLICK = 2
export const PRE_SERIALIZED_FLAG_KEYPRESS = 4
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/README.md b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/README.md
index 1bb7b23c6892..a5b402e7ab6b 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/README.md
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/README.md
@@ -60,7 +60,7 @@ src/ (production — ships)
main.ts entrypoint: load models -> start servers
server.ts the /scrub + /metrics listeners; scrub implementation injected
config.ts env-driven runtime config
- blur.ts baseline blur (kept in sync with rust/replay-anonymizer-node/src/blur.rs)
+ blur.ts baseline blur (kept in sync with rust/replay-anonymizer/src/blur.rs)
scrub.ts the ML scrub pipeline: decode-once, NSFW gate, solid-fill of faces/text/codes
yunet.ts YuNet face detector (ONNX)
dbnet.ts DBNet text-region detector (ONNX)
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/blur.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/blur.ts
index 4ea6de00d0d1..803c1dfa2925 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/blur.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/blur.ts
@@ -1,4 +1,4 @@
-// These params must stay in sync with rust/replay-anonymizer-node/src/blur.rs, or the mirror diverges from the inline anonymizer.
+// These params must stay in sync with rust/replay-anonymizer/src/blur.rs, or the mirror diverges from the inline anonymizer.
import sharp from 'sharp'
// One libvips thread per op and no cross-request cache, so N concurrent scrubs cost ~N threads (not N x CPU)
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/scrub.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/scrub.ts
index 36c33d17a37c..a29dc6d44eea 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/scrub.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror-image-scrub-sidecar/src/scrub.ts
@@ -1,7 +1,7 @@
/**
* The image-scrub pipelines. Two entry points over the same input so throughput stays comparable:
* - blurOnly: the cheap baseline (downsample + gaussian blur), kept in sync with the inline
- * anonymizer's rust/replay-anonymizer-node/src/blur.rs so comparisons are apples-to-apples.
+ * anonymizer's rust/replay-anonymizer/src/blur.rs so comparisons are apples-to-apples.
* - advancedScrub: NSFW/gore gate -> faces, text regions, and QR/barcodes solid-filled.
*
* All three models (safety gate, YuNet faces, DBNet text) run on native onnxruntime-node —
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/shared-fixtures.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/shared-fixtures.test.ts
index 10dc248c7663..6cf0fd2573e9 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/shared-fixtures.test.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/shared-fixtures.test.ts
@@ -6,8 +6,8 @@ import { parseJSON } from '~/common/utils/json-parse'
import { logger } from '~/common/utils/logger'
// Shared fixtures pin the addon's behavior through the FFI; the pure-Rust side of the same
-// fixtures is covered by rust/replay-anonymizer-node/tests/parity.rs.
-const FIXTURE_DIR = path.resolve(__dirname, '../../../../../../rust/replay-anonymizer-node/tests/fixtures')
+// fixtures is covered by rust/replay-anonymizer/tests/parity.rs.
+const FIXTURE_DIR = path.resolve(__dirname, '../../../../../../rust/replay-anonymizer/tests/fixtures')
interface AllowSpec {
text: string[]
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/sessions/snappy-session-recorder.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/sessions/snappy-session-recorder.test.ts
index 79fb8a2d6568..8b1d183cca61 100644
--- a/nodejs/src/ingestion/pipelines/sessionreplay/sessions/snappy-session-recorder.test.ts
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/sessions/snappy-session-recorder.test.ts
@@ -1122,7 +1122,7 @@ describe('SnappySessionRecorder', () => {
describe('Pre-serialized messages (native anonymizer fast path)', () => {
it('appends the block lines verbatim and derives counts from the per-event metadata', async () => {
- // Flag bits mirror rust/replay-anonymizer-node `snapshot.rs`: 1=active 2=click 4=keypress 8=mouse.
+ // Flag bits mirror rust/replay-anonymizer `snapshot.rs`: 1=active 2=click 4=keypress 8=mouse.
const t0 = DateTime.fromISO('2025-01-01T01:00:00Z').toMillis()
const lines = Buffer.from(
`["w1",{"type":4,"timestamp":${t0},"data":{"href":"https://example.com/[redacted]"}}]\n` +
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 5fae8e973834..41c084b6440a 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -8012,6 +8012,24 @@ dependencies = [
"portable-atomic",
]
+[[package]]
+name = "posthog-replay-anonymizer"
+version = "0.1.0"
+dependencies = [
+ "ahash 0.8.12",
+ "anyhow",
+ "base64 0.22.1",
+ "flate2",
+ "image",
+ "libdeflater",
+ "lz4",
+ "memchr",
+ "serde",
+ "serde_json",
+ "simd-json",
+ "zstd",
+]
+
[[package]]
name = "posthog-rs"
version = "0.20.0"
@@ -9098,19 +9116,10 @@ checksum = "51743d3e274e2b18df81c4dc6caf8a5b8e15dbe799e0dca05c7617380094e884"
name = "replay-anonymizer-node"
version = "0.0.1"
dependencies = [
- "ahash 0.8.12",
- "anyhow",
- "base64 0.22.1",
- "flate2",
- "image",
- "libdeflater",
- "lz4",
- "memchr",
"neon",
+ "posthog-replay-anonymizer",
"serde",
"serde_json",
- "simd-json",
- "zstd",
]
[[package]]
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index c4915e422a04..2c76dc60ff05 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -40,6 +40,7 @@ members = [
"cyclotron-core",
"cyclotron-node",
"cyclotron-janitor",
+ "replay-anonymizer",
"replay-anonymizer-node",
"cymbal",
"cymbal-proto",
diff --git a/rust/owners.yaml b/rust/owners.yaml
index e4421d2ee5ef..9b6d9000a01c 100644
--- a/rust/owners.yaml
+++ b/rust/owners.yaml
@@ -26,5 +26,7 @@ rules:
- '/property-defs-rs/'
- '/property-vals-rs/'
owners: team-ingestion
- - match: '/replay-anonymizer-node/'
+ - match:
+ - '/replay-anonymizer/'
+ - '/replay-anonymizer-node/'
owners: team-ai-research
diff --git a/rust/replay-anonymizer-node/Cargo.toml b/rust/replay-anonymizer-node/Cargo.toml
index cedabf421d2f..6ce1165dc238 100644
--- a/rust/replay-anonymizer-node/Cargo.toml
+++ b/rust/replay-anonymizer-node/Cargo.toml
@@ -6,28 +6,13 @@ exclude = ["index.node"]
publish = false
[lib]
-# `cdylib` for the Neon `index.node` addon; `rlib` so `cargo test` can exercise the scrub logic.
-crate-type = ["cdylib", "rlib"]
+crate-type = ["cdylib"]
[dependencies]
-ahash = { workspace = true }
-anyhow = { workspace = true }
-base64 = { workspace = true }
-libdeflater = { workspace = true }
-lz4 = { workspace = true }
-memchr = "2"
-image = { version = "0.25", default-features = false, features = ["png", "jpeg", "gif", "webp", "bmp"] }
# `external-buffers`: the JSONL lines cross the FFI as an externally-backed Buffer (no copy).
# Safe here: the addon only runs in PostHog's Node ingestion workers, never in V8
# sandboxed-pointer environments (Electron), where external buffers abort the VM.
neon = { workspace = true, features = ["external-buffers"] }
+posthog-replay-anonymizer = { path = "../replay-anonymizer" }
serde = { workspace = true }
serde_json = { workspace = true }
-simd-json = { version = "0.15", features = ["serde_impl"] }
-zstd = { workspace = true }
-
-[dev-dependencies]
-# flate2: tests verify reference gzip streams with an independent codec (not libdeflate), so a
-# codec regression can't hide by round-tripping through itself.
-flate2 = { workspace = true }
-serde_json = { workspace = true }
diff --git a/rust/replay-anonymizer-node/package.json b/rust/replay-anonymizer-node/package.json
index b4728fea2c80..763614f379d6 100644
--- a/rust/replay-anonymizer-node/package.json
+++ b/rust/replay-anonymizer-node/package.json
@@ -5,7 +5,7 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
- "test": "cargo test -p replay-anonymizer-node",
+ "test": "cargo test -p posthog-replay-anonymizer -p replay-anonymizer-node",
"build": "pnpm run build:cargo --release && pnpm run build:move-lib && pnpm run build:typescript",
"build:dev": "pnpm run build:cargo && pnpm run build:move-lib:dev && pnpm run build:typescript",
"build:move-lib": "TARGET_DIR=${CARGO_TARGET_DIR:-../target} && cp \"$TARGET_DIR/release/libreplay_anonymizer_node.dylib\" index.node 2>/dev/null || cp \"$TARGET_DIR/release/libreplay_anonymizer_node.so\" index.node || (echo \"No valid library file found in $TARGET_DIR/release/\" && exit 1)",
diff --git a/rust/replay-anonymizer-node/src/lib.rs b/rust/replay-anonymizer-node/src/lib.rs
index 13acdbff360f..b32ea2c35b95 100644
--- a/rust/replay-anonymizer-node/src/lib.rs
+++ b/rust/replay-anonymizer-node/src/lib.rs
@@ -1,79 +1,17 @@
-//! Session-replay anonymizer: PII-scrubs rrweb events for the ml-mirror pipeline, exposed to Node as
-//! a Neon native addon.
+//! Neon native addon exposing the `posthog-replay-anonymizer` scrubbers to Node for the ml-mirror
+//! pipeline.
//!
-//! The scrubbers cover text/URL redaction, native image blur, and `cv` de/recompression. Behavior
-//! is pinned by the shared JSON fixtures under `tests/fixtures/`, which both `tests/parity.rs` and
-//! the Jest suite (through the addon) run against.
-//!
-//! The production surface is the byte-buffer pipeline in [`snapshot`]: the decompressed Kafka payload
-//! goes in, ready-to-write JSONL block lines plus envelope/per-event metadata come out — Rust owns
-//! the parse, the scrub, and the serialize, so no JSON crosses the FFI boundary as a string. The
-//! crate builds both an `rlib` (for `cargo test`) and a `cdylib` (the `index.node` addon).
-
-pub mod allow_lists;
-pub mod assets;
-pub mod blur;
-pub mod bytewalk;
-pub mod canvas;
-pub mod context;
-pub mod css;
-pub mod cv;
-pub mod dom;
-pub mod event;
-pub mod gzip;
-pub mod json;
-pub mod scan;
-pub mod snapshot;
-pub mod text;
-pub mod url;
-pub mod value;
-
-pub use allow_lists::AllowLists;
-pub use context::Ctx;
-pub use event::{anonymize_event, anonymize_event_str, anonymize_message};
-pub use snapshot::{
- anonymize_kafka_payload, anonymize_kafka_payload_opts, AnonymizeOpts, AnonymizedMessage,
- FailKind, Failure, Route,
-};
-
-/// Shared helpers for the image-neutralization tests across modules.
-#[cfg(test)]
-pub(crate) mod testkit {
- use base64::Engine;
-
- fn encode_png(w: u32, h: u32, color: [u8; 4]) -> Vec<u8> {
- let img =
- image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(w, h, image::Rgba(color)));
- let mut buf = Vec::new();
- img.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png)
- .unwrap();
- buf
- }
-
- /// Base64 of an encoded PNG (no `data:` prefix) — e.g. for a canvas Blob's ArrayBuffer.
- pub fn png_base64(w: u32, h: u32, color: [u8; 4]) -> String {
- base64::engine::general_purpose::STANDARD.encode(encode_png(w, h, color))
- }
-
- pub fn png_data_uri(w: u32, h: u32, color: [u8; 4]) -> String {
- format!("data:image/png;base64,{}", png_base64(w, h, color))
- }
-
- /// Base64 of `w*h` non-uniform RGBA pixels — e.g. for a canvas `ImageData` ArrayBuffer.
- pub fn rgba_base64(w: u32, h: u32) -> String {
- let mut raw = Vec::with_capacity((w * h * 4) as usize);
- for i in 0..(w * h) {
- let b = (i % 256) as u8;
- raw.extend_from_slice(&[b, b.wrapping_add(50), b.wrapping_add(100), 255]);
- }
- base64::engine::general_purpose::STANDARD.encode(raw)
- }
-}
+//! The production surface is the byte-buffer pipeline in `posthog_replay_anonymizer::snapshot`: the
+//! decompressed Kafka payload goes in, ready-to-write JSONL block lines plus envelope/per-event
+//! metadata come out — Rust owns the parse, the scrub, and the serialize, so no JSON crosses the FFI
+//! boundary as a string. Behavior is pinned by the shared JSON fixtures in the core crate's
+//! `tests/fixtures/`, which the Jest suite runs against through this addon.
use std::sync::RwLock;
use neon::prelude::*;
use neon::types::buffer::TypedArray;
+use posthog_replay_anonymizer::{snapshot, AllowLists, FailKind};
use serde::Deserialize;
// The fail-closed contract depends on `catch_unwind` containing panics on untrusted input. Under
diff --git a/rust/replay-anonymizer/Cargo.toml b/rust/replay-anonymizer/Cargo.toml
new file mode 100644
index 000000000000..75bc154de219
--- /dev/null
+++ b/rust/replay-anonymizer/Cargo.toml
@@ -0,0 +1,30 @@
+[package]
+name = "posthog-replay-anonymizer"
+version = "0.1.0"
+edition = "2021"
+description = "PII scrubber for rrweb session-replay events: text/URL redaction, native image blur, and canvas/cv neutralization"
+license = "MIT"
+homepage = "https://posthog.com"
+repository = "https://github.com/PostHog/posthog"
+
+[lints]
+workspace = true
+
+[dependencies]
+ahash = { workspace = true }
+anyhow = { workspace = true }
+base64 = { workspace = true }
+libdeflater = { workspace = true }
+lz4 = { workspace = true }
+memchr = "2"
+image = { version = "0.25", default-features = false, features = ["png", "jpeg", "gif", "webp", "bmp"] }
+serde = { workspace = true }
+serde_json = { workspace = true }
+simd-json = { version = "0.15", features = ["serde_impl"] }
+zstd = { workspace = true }
+
+[dev-dependencies]
+# flate2: tests verify reference gzip streams with an independent codec (not libdeflate), so a
+# codec regression can't hide by round-tripping through itself.
+flate2 = { workspace = true }
+serde_json = { workspace = true }
diff --git a/rust/replay-anonymizer/README.md b/rust/replay-anonymizer/README.md
new file mode 100644
index 000000000000..5157ca7fb11c
--- /dev/null
+++ b/rust/replay-anonymizer/README.md
@@ -0,0 +1,19 @@
+# posthog-replay-anonymizer
+
+PII scrubber for rrweb session-replay events: text/URL redaction, native image blur, and canvas/`cv` neutralization. This is the scrub core behind PostHog's ml-mirror pipelines.
+
+Consumers:
+
+- `replay-anonymizer-node` (this workspace) wraps it as a Neon addon for the Node ingestion workers.
+- The MLHog training pipelines consume it from crates.io.
+
+Behavior is pinned by the JSON fixtures under `tests/fixtures/`, shared with the Node addon's Jest suite. Scrubbing operates on untrusted input and may panic on pathological payloads; callers that must fail closed should wrap calls in `catch_unwind` under `panic = "unwind"`.
+
+## Releasing
+
+Publishing to crates.io is automated by `.github/workflows/publish-replay-anonymizer-crate.yml`:
+
+1. Bump `version` in `Cargo.toml` and merge to master.
+2. Tag the merged commit `posthog-replay-anonymizer/v<version>` and push the tag.
+
+The workflow verifies the tag matches the crate version and publishes via crates.io trusted publishing (GitHub OIDC, no long-lived token).
diff --git a/rust/replay-anonymizer-node/src/allow_lists.rs b/rust/replay-anonymizer/src/allow_lists.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/allow_lists.rs
rename to rust/replay-anonymizer/src/allow_lists.rs
diff --git a/rust/replay-anonymizer-node/src/assets.rs b/rust/replay-anonymizer/src/assets.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/assets.rs
rename to rust/replay-anonymizer/src/assets.rs
diff --git a/rust/replay-anonymizer-node/src/blur.rs b/rust/replay-anonymizer/src/blur.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/blur.rs
rename to rust/replay-anonymizer/src/blur.rs
diff --git a/rust/replay-anonymizer-node/src/bytewalk.rs b/rust/replay-anonymizer/src/bytewalk.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/bytewalk.rs
rename to rust/replay-anonymizer/src/bytewalk.rs
diff --git a/rust/replay-anonymizer-node/src/canvas.rs b/rust/replay-anonymizer/src/canvas.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/canvas.rs
rename to rust/replay-anonymizer/src/canvas.rs
diff --git a/rust/replay-anonymizer-node/src/context.rs b/rust/replay-anonymizer/src/context.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/context.rs
rename to rust/replay-anonymizer/src/context.rs
diff --git a/rust/replay-anonymizer-node/src/css.rs b/rust/replay-anonymizer/src/css.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/css.rs
rename to rust/replay-anonymizer/src/css.rs
diff --git a/rust/replay-anonymizer-node/src/cv.rs b/rust/replay-anonymizer/src/cv.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/cv.rs
rename to rust/replay-anonymizer/src/cv.rs
diff --git a/rust/replay-anonymizer-node/src/dom.rs b/rust/replay-anonymizer/src/dom.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/dom.rs
rename to rust/replay-anonymizer/src/dom.rs
diff --git a/rust/replay-anonymizer-node/src/event.rs b/rust/replay-anonymizer/src/event.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/event.rs
rename to rust/replay-anonymizer/src/event.rs
diff --git a/rust/replay-anonymizer-node/src/gzip.rs b/rust/replay-anonymizer/src/gzip.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/gzip.rs
rename to rust/replay-anonymizer/src/gzip.rs
diff --git a/rust/replay-anonymizer-node/src/json.rs b/rust/replay-anonymizer/src/json.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/json.rs
rename to rust/replay-anonymizer/src/json.rs
diff --git a/rust/replay-anonymizer/src/lib.rs b/rust/replay-anonymizer/src/lib.rs
new file mode 100644
index 000000000000..5d5481afa8c6
--- /dev/null
+++ b/rust/replay-anonymizer/src/lib.rs
@@ -0,0 +1,73 @@
+//! Session-replay anonymizer: PII-scrubs rrweb events for PostHog's ml-mirror pipelines.
+//!
+//! The scrubbers cover text/URL redaction, native image blur, and `cv` de/recompression. Behavior
+//! is pinned by the shared JSON fixtures under `tests/fixtures/`, which both `tests/parity.rs` and
+//! the Node addon's Jest suite (through `replay-anonymizer-node`) run against.
+//!
+//! The production surface is the byte-buffer pipeline in [`snapshot`]: the decompressed Kafka payload
+//! goes in, ready-to-write JSONL block lines plus envelope/per-event metadata come out — this crate
+//! owns the parse, the scrub, and the serialize.
+//!
+//! Scrubbing operates on untrusted input and may panic on pathological payloads; callers that must
+//! fail closed (drop the message rather than crash) should wrap calls in `catch_unwind` under
+//! `panic = "unwind"`, as `replay-anonymizer-node` does.
+
+pub mod allow_lists;
+pub mod assets;
+pub mod blur;
+pub mod bytewalk;
+pub mod canvas;
+pub mod context;
+pub mod css;
+pub mod cv;
+pub mod dom;
+pub mod event;
+pub mod gzip;
+pub mod json;
+pub mod scan;
+pub mod snapshot;
+pub mod text;
+pub mod url;
+pub mod value;
+
+pub use allow_lists::AllowLists;
+pub use context::Ctx;
+pub use event::{anonymize_event, anonymize_event_str, anonymize_message};
+pub use snapshot::{
+ anonymize_kafka_payload, anonymize_kafka_payload_opts, AnonymizeOpts, AnonymizedMessage,
+ FailKind, Failure, Route,
+};
+
+/// Shared helpers for the image-neutralization tests across modules.
+#[cfg(test)]
+pub(crate) mod testkit {
+ use base64::Engine;
+
+ fn encode_png(w: u32, h: u32, color: [u8; 4]) -> Vec<u8> {
+ let img =
+ image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(w, h, image::Rgba(color)));
+ let mut buf = Vec::new();
+ img.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png)
+ .unwrap();
+ buf
+ }
+
+ /// Base64 of an encoded PNG (no `data:` prefix) — e.g. for a canvas Blob's ArrayBuffer.
+ pub fn png_base64(w: u32, h: u32, color: [u8; 4]) -> String {
+ base64::engine::general_purpose::STANDARD.encode(encode_png(w, h, color))
+ }
+
+ pub fn png_data_uri(w: u32, h: u32, color: [u8; 4]) -> String {
+ format!("data:image/png;base64,{}", png_base64(w, h, color))
+ }
+
+ /// Base64 of `w*h` non-uniform RGBA pixels — e.g. for a canvas `ImageData` ArrayBuffer.
+ pub fn rgba_base64(w: u32, h: u32) -> String {
+ let mut raw = Vec::with_capacity((w * h * 4) as usize);
+ for i in 0..(w * h) {
+ let b = (i % 256) as u8;
+ raw.extend_from_slice(&[b, b.wrapping_add(50), b.wrapping_add(100), 255]);
+ }
+ base64::engine::general_purpose::STANDARD.encode(raw)
+ }
+}
diff --git a/rust/replay-anonymizer-node/src/scan.rs b/rust/replay-anonymizer/src/scan.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/scan.rs
rename to rust/replay-anonymizer/src/scan.rs
diff --git a/rust/replay-anonymizer-node/src/snapshot.rs b/rust/replay-anonymizer/src/snapshot.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/snapshot.rs
rename to rust/replay-anonymizer/src/snapshot.rs
diff --git a/rust/replay-anonymizer-node/src/text.rs b/rust/replay-anonymizer/src/text.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/text.rs
rename to rust/replay-anonymizer/src/text.rs
diff --git a/rust/replay-anonymizer-node/src/url.rs b/rust/replay-anonymizer/src/url.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/url.rs
rename to rust/replay-anonymizer/src/url.rs
diff --git a/rust/replay-anonymizer-node/src/value.rs b/rust/replay-anonymizer/src/value.rs
similarity index 100%
rename from rust/replay-anonymizer-node/src/value.rs
rename to rust/replay-anonymizer/src/value.rs
diff --git a/rust/replay-anonymizer-node/tests/fixtures/events.json b/rust/replay-anonymizer/tests/fixtures/events.json
similarity index 100%
rename from rust/replay-anonymizer-node/tests/fixtures/events.json
rename to rust/replay-anonymizer/tests/fixtures/events.json
diff --git a/rust/replay-anonymizer-node/tests/fixtures/messages.json b/rust/replay-anonymizer/tests/fixtures/messages.json
similarity index 100%
rename from rust/replay-anonymizer-node/tests/fixtures/messages.json
rename to rust/replay-anonymizer/tests/fixtures/messages.json
diff --git a/rust/replay-anonymizer-node/tests/fixtures/text.json b/rust/replay-anonymizer/tests/fixtures/text.json
similarity index 100%
rename from rust/replay-anonymizer-node/tests/fixtures/text.json
rename to rust/replay-anonymizer/tests/fixtures/text.json
diff --git a/rust/replay-anonymizer-node/tests/fixtures/url-scheme-allowlist.json b/rust/replay-anonymizer/tests/fixtures/url-scheme-allowlist.json
similarity index 100%
rename from rust/replay-anonymizer-node/tests/fixtures/url-scheme-allowlist.json
rename to rust/replay-anonymizer/tests/fixtures/url-scheme-allowlist.json
diff --git a/rust/replay-anonymizer-node/tests/fixtures/url.json b/rust/replay-anonymizer/tests/fixtures/url.json
similarity index 100%
rename from rust/replay-anonymizer-node/tests/fixtures/url.json
rename to rust/replay-anonymizer/tests/fixtures/url.json
diff --git a/rust/replay-anonymizer-node/tests/parity.rs b/rust/replay-anonymizer/tests/parity.rs
similarity index 97%
rename from rust/replay-anonymizer-node/tests/parity.rs
rename to rust/replay-anonymizer/tests/parity.rs
index 7b1496d97964..e225df41a49e 100644
--- a/rust/replay-anonymizer-node/tests/parity.rs
+++ b/rust/replay-anonymizer/tests/parity.rs
@@ -4,8 +4,8 @@
use std::path::Path;
use std::time::Instant;
-use replay_anonymizer_node::allow_lists::AllowLists;
-use replay_anonymizer_node::{
+use posthog_replay_anonymizer::allow_lists::AllowLists;
+use posthog_replay_anonymizer::{
anonymize_event_str, anonymize_message, context::Ctx, text::scrub_text, url::scrub_url_opts,
url::URL_SCHEME_ALLOWLIST,
};
@@ -153,7 +153,7 @@ fn email_scanner_worst_case_is_linear() {
let input = format!("{local}@{domain}");
let allow = AllowLists::new(Vec::<String>::new(), Vec::<String>::new());
let start = Instant::now();
- let _ = scrub_text(&allow, &input);
+ let _scrubbed = scrub_text(&allow, &input);
assert!(
start.elapsed().as_millis() < 2000,
"email scanner should be linear on an @-bearing run, took {:?}",
diff --git a/rust/replay-anonymizer-node/tests/snapshot.rs b/rust/replay-anonymizer/tests/snapshot.rs
similarity index 99%
rename from rust/replay-anonymizer-node/tests/snapshot.rs
rename to rust/replay-anonymizer/tests/snapshot.rs
index 4c7d598e91bb..3b6eb296260c 100644
--- a/rust/replay-anonymizer-node/tests/snapshot.rs
+++ b/rust/replay-anonymizer/tests/snapshot.rs
@@ -5,13 +5,13 @@
//! keys, wrong dlq/drop classification), so the suite centers on a differential check against the
//! tree path plus targeted leak/contract cases.
-use replay_anonymizer_node::allow_lists::AllowLists;
-use replay_anonymizer_node::snapshot::{
+use posthog_replay_anonymizer::allow_lists::AllowLists;
+use posthog_replay_anonymizer::snapshot::{
anonymize_kafka_payload, anonymize_kafka_payload_opts, anonymize_via_tree, AnonymizeOpts,
AnonymizedMessage, FailKind, Failure, FLAG_ACTIVE, FLAG_CLICK, FLAG_KEYPRESS,
FLAG_MOUSE_ACTIVITY,
};
-use replay_anonymizer_node::Ctx;
+use posthog_replay_anonymizer::Ctx;
use serde_json::{json, Value};
use std::path::Path;
@@ -291,7 +291,7 @@ fn node_objects_with_too_many_keys_decline_to_the_parse() {
#[test]
fn decompress_payload_matches_the_capture_producer_format() {
- use replay_anonymizer_node::snapshot::decompress_payload;
+ use posthog_replay_anonymizer::snapshot::decompress_payload;
let body = br#"{"distinct_id":"d","data":"{}"}"#.to_vec();
// lz4: capture writes `block::compress` output behind a 4-byte LE uncompressed-size prefix,