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.

fix(property-defs-rs): sanitize null bytes in property keys (#57591)

Eli Reisman2b509ca134a6
diff --git a/rust/property-defs-rs/src/types.rs b/rust/property-defs-rs/src/types.rs
index df93e16f91b9..2be08895868e 100644
--- a/rust/property-defs-rs/src/types.rs
+++ b/rust/property-defs-rs/src/types.rs
@@ -335,11 +335,15 @@ impl Event {
continue;
}
+ // Property keys flow into posthog_eventproperty.property and
+ // posthog_propertydefinition.name — both text columns. Postgres rejects null bytes
+ // in text, so any payload with an embedded \u{0000} burns the full 3-retry cycle and
+ // is then dropped. Mirror the existing sanitize on event names (line 222 above).
updates.push(Update::EventProperty(EventProperty {
team_id: self.team_id,
project_id: self.project_id,
event: sanitize_string(&self.event),
- property: key.clone(),
+ property: sanitize_string(key),
}));
let property_type = detect_property_type(key, value);
@@ -348,7 +352,7 @@ impl Event {
updates.push(Update::Property(PropertyDefinition {
team_id: self.team_id,
project_id: self.project_id,
- name: key.clone(),
+ name: sanitize_string(key),
is_numerical,
property_type,
event_type: parent_type,
diff --git a/rust/property-defs-rs/tests/types.rs b/rust/property-defs-rs/tests/types.rs
index ed252f3daabd..6b4d2beb7a5e 100644
--- a/rust/property-defs-rs/tests/types.rs
+++ b/rust/property-defs-rs/tests/types.rs
@@ -1,5 +1,7 @@
use chrono::Utc;
-use property_defs_rs::types::{detect_property_type, get_floored_last_seen, PropertyValueType};
+use property_defs_rs::types::{
+ detect_property_type, get_floored_last_seen, Event, PropertyValueType, Update,
+};
use serde_json::{Number, Value};
#[test]
@@ -423,3 +425,65 @@ fn test_bare_utm_properties_still_string() {
);
}
}
+
+#[test]
+fn test_property_keys_are_sanitized_of_null_bytes() {
+ // Property keys with embedded null bytes must be sanitized before reaching Postgres,
+ // otherwise the INSERT fails with 22021 (invalid_text_representation) and burns the
+ // 3-retry batch. We mirror the existing sanitize on event names.
+ let event = Event {
+ team_id: 1,
+ project_id: 1,
+ event: "$pageview".to_string(),
+ properties: Some(r#"{"clean_key":"v","key\u0000with\u0000nulls":"v"}"#.to_string()),
+ };
+
+ let updates = event.into_updates(1000);
+
+ // Expect: 1 EventDefinition + 2 EventProperty + 2 PropertyDefinition = 5 updates
+ assert_eq!(
+ updates.len(),
+ 5,
+ "all keys should produce updates: {updates:?}"
+ );
+
+ let replacement_char = '\u{FFFD}';
+
+ let mut saw_sanitized_event_property = false;
+ let mut saw_sanitized_property_definition = false;
+
+ for update in &updates {
+ match update {
+ Update::EventProperty(ep) => {
+ assert!(
+ !ep.property.contains('\u{0000}'),
+ "EventProperty.property must not contain null bytes: {:?}",
+ ep.property
+ );
+ if ep.property.contains(replacement_char) {
+ saw_sanitized_event_property = true;
+ }
+ }
+ Update::Property(pd) => {
+ assert!(
+ !pd.name.contains('\u{0000}'),
+ "PropertyDefinition.name must not contain null bytes: {:?}",
+ pd.name
+ );
+ if pd.name.contains(replacement_char) {
+ saw_sanitized_property_definition = true;
+ }
+ }
+ Update::Event(_) => {}
+ }
+ }
+
+ assert!(
+ saw_sanitized_event_property,
+ "expected at least one EventProperty with the sanitized key: {updates:?}"
+ );
+ assert!(
+ saw_sanitized_property_definition,
+ "expected at least one PropertyDefinition with the sanitized name: {updates:?}"
+ );
+}