: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(flags): cap json nesting depth in flags request decoding (#122475)
Tom Piccirello35cc02a8ae65
diff --git a/docs/internal/feature-flags/rust-service-overview.md b/docs/internal/feature-flags/rust-service-overview.mdindex df18e2704c34..08ef90eb055e 100644--- a/docs/internal/feature-flags/rust-service-overview.md+++ b/docs/internal/feature-flags/rust-service-overview.md@@ -113,7 +113,7 @@ All flag routes accept trailing slashes.The POST handler follows this pipeline:1. **Rate limiting**: IP-based check (DDoS defense), then token-based check (per-project limits)-2. **Body decoding**: JSON, base64, or gzip-compressed bodies+2. **Body decoding**: JSON, base64, or gzip-compressed bodies. A body with arrays or objects nested more than 128 levels deep gets a 400.3. **Authentication**: Extracts API token from body, query params, or headers4. **Team lookup**: HyperCache (Redis -> S3) with PostgreSQL fallback (configurable via `SKIP_PG_TEAM_FALLBACK`)5. **Flag definitions fetch**: HyperCache (Redis -> S3) with PostgreSQL fallbackdiff --git a/rust/feature-flags/src/flags/flag_request.rs b/rust/feature-flags/src/flags/flag_request.rsindex e1193a75add1..ddc8aa74c79b 100644--- a/rust/feature-flags/src/flags/flag_request.rs+++ b/rust/feature-flags/src/flags/flag_request.rs@@ -8,6 +8,7 @@ use serde_json::Value;use crate::api::errors::FlagError;use crate::handler::flags::EvaluationRuntime;+use crate::utils::bounded_json5;/// Maximum length for distinct_id values./// This limit ensures compatibility with batch export destinations (Redshift, Postgres)@@ -128,7 +129,7 @@ impl FlagRequest {};// Use json5 to parse, which handles NaN/Infinity natively- let mut value: Value = match json5::from_str(&payload) {+ let mut value: Value = match bounded_json5::from_str(&payload) {Ok(v) => v,Err(e) => {tracing::warn!("failed to parse JSON: {}", e);@@ -290,6 +291,7 @@ mod tests {use crate::flags::flag_definitions_cache::FlagDefinitionsCache;use crate::flags::flag_request::{FlagRequest, MAX_DISTINCT_ID_LEN, MAX_LIB_LEN};use crate::flags::flag_service::FlagService;+ use crate::utils::bounded_json5::MAX_NESTING_DEPTH;use crate::utils::test_utils::{insert_new_team_in_redis, setup_hypercache_reader, setup_pg_reader_client,setup_redis_client, setup_team_hypercache_reader,@@ -481,6 +483,24 @@ mod tests {assert_eq!(metrics.get("payload"), Some(&json!("{'foo': NaN}")));}+ #[test]+ fn test_nesting_past_depth_limit_is_rejected() {+ // The request object and person_properties are the first two levels.+ let levels = MAX_NESTING_DEPTH - 1;+ let json_str = format!(+ r#"{{"distinct_id": "user123", "token": "my_token1", "person_properties": {{"v": {}{}}}}}"#,+ "[".repeat(levels),+ "]".repeat(levels)+ );++ let result = FlagRequest::from_bytes(Bytes::from(json_str));++ assert!(+ matches!(result, Err(FlagError::RequestDecodingError(_))),+ "{result:?}"+ );+ }+#[test]fn distinct_id_is_returned_correctly() {let json = json!({diff --git a/rust/feature-flags/src/utils/bounded_json5.rs b/rust/feature-flags/src/utils/bounded_json5.rsnew file mode 100644index 000000000000..7d973fa1f15c--- /dev/null+++ b/rust/feature-flags/src/utils/bounded_json5.rs@@ -0,0 +1,118 @@+use json5::char::is_json5_line_terminator;+use serde::Deserialize;++/// serde_json's default recursion limit is also 128, and the form-urlencoded path applies it.+pub const MAX_NESTING_DEPTH: usize = 128;++#[derive(Debug, thiserror::Error)]+pub enum Error {+ #[error("arrays and objects nest deeper than {MAX_NESTING_DEPTH} levels")]+ TooDeep,+ #[error(transparent)]+ Syntax(#[from] json5::Error),+}++/// Parses JSON5 like [`json5::from_str`], but rejects input that nests deeper than+/// [`MAX_NESTING_DEPTH`] before the parse starts.+///+/// json5 recurses once per nested array or object and has no depth limit. Deep enough input+/// overflows the thread stack, and Rust aborts the whole process on a stack overflow.+pub fn from_str<'a, T: Deserialize<'a>>(input: &'a str) -> Result<T, Error> {+ if exceeds_max_depth(input) {+ return Err(Error::TooDeep);+ }+ Ok(json5::from_str(input)?)+}++#[derive(Clone, Copy)]+enum Lexeme {+ Code,+ String { quote: char },+ LineComment,+ BlockComment,+}++/// Brackets inside strings and comments are not structure, so the scan ends strings and comments+/// where json5 ends them. A scan that counts fewer levels than json5 recurses into would let a+/// crafted body through. Where the two disagree, the input is invalid JSON5 at that point, and+/// json5 returns a syntax error there instead of recursing further.+fn exceeds_max_depth(input: &str) -> bool {+ let mut chars = input.chars().peekable();+ let mut lexeme = Lexeme::Code;+ let mut depth = 0;++ while let Some(c) = chars.next() {+ lexeme = match lexeme {+ Lexeme::Code => match c {+ '[' | '{' => {+ depth += 1;+ if depth > MAX_NESTING_DEPTH {+ return true;+ }+ Lexeme::Code+ }+ ']' | '}' => {+ depth = depth.saturating_sub(1);+ Lexeme::Code+ }+ '"' | '\'' => Lexeme::String { quote: c },+ '/' if chars.next_if_eq(&'/').is_some() => Lexeme::LineComment,+ '/' if chars.next_if_eq(&'*').is_some() => Lexeme::BlockComment,+ _ => Lexeme::Code,+ },+ Lexeme::String { quote } => match c {+ '\\' => {+ chars.next();+ lexeme+ }+ c if c == quote => Lexeme::Code,+ _ => lexeme,+ },+ Lexeme::LineComment if is_json5_line_terminator(c) => Lexeme::Code,+ Lexeme::BlockComment if c == '*' && chars.next_if_eq(&'/').is_some() => Lexeme::Code,+ Lexeme::LineComment | Lexeme::BlockComment => lexeme,+ };+ }++ false+}++#[cfg(test)]+mod tests {+ use rstest::rstest;+ use serde_json::Value;++ use super::*;++ fn nested_arrays(levels: usize) -> String {+ "[".repeat(levels) + &"]".repeat(levels)+ }++ #[rstest]+ #[case::at_the_limit(nested_arrays(MAX_NESTING_DEPTH))]+ #[case::brackets_in_double_quoted_string(format!(r#"["{}"]"#, "[{".repeat(MAX_NESTING_DEPTH)))]+ #[case::brackets_in_single_quoted_string(format!("['{}']", "[{".repeat(MAX_NESTING_DEPTH)))]+ #[case::brackets_in_comments(format!("/* {0} */ [] // {0}", "[{".repeat(MAX_NESTING_DEPTH)))]+ fn parses_input_within_the_limit(#[case] input: String) {+ let parsed = from_str::<Value>(&input);+ assert!(parsed.is_ok(), "{parsed:?}");+ }++ #[rstest]+ #[case::arrays(nested_arrays(MAX_NESTING_DEPTH + 1))]+ #[case::objects(+ r#"{"a":"#.repeat(MAX_NESTING_DEPTH + 1) + "1" + &"}".repeat(MAX_NESTING_DEPTH + 1)+ )]+ #[case::after_line_comment_ended_by_line_separator(+ format!("// note\u{2028}{}", nested_arrays(MAX_NESTING_DEPTH + 1))+ )]+ #[case::after_block_comment(format!("/* note */{}", nested_arrays(MAX_NESTING_DEPTH + 1)))]+ #[case::after_escaped_quote(format!(r#"["a\"b", {}]"#, nested_arrays(MAX_NESTING_DEPTH)))]+ #[case::after_double_quote_in_single_quoted_string(+ format!(r#"['"', {}]"#, nested_arrays(MAX_NESTING_DEPTH))+ )]+ fn rejects_nesting_past_the_limit(#[case] input: String) {+ let parsed = from_str::<Value>(&input);+ assert!(matches!(parsed, Err(Error::TooDeep)), "{parsed:?}");+ }+}diff --git a/rust/feature-flags/src/utils/mod.rs b/rust/feature-flags/src/utils/mod.rsindex 6a6bc081e27e..9ac56c80408d 100644--- a/rust/feature-flags/src/utils/mod.rs+++ b/rust/feature-flags/src/utils/mod.rs@@ -1,4 +1,5 @@pub mod bot_detection;+pub mod bounded_json5;pub mod deadline;pub mod graph_utils;pub mod json_size;