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.

2.0 KB
Raw Blame
//! The json->hog deserialization recursion guard (`MAX_JSON_SERDE_DEPTH`) must clear any realistic
//! event nesting. The reference VMs impose no explicit JSON-nesting limit, so an over-tight cap here
//! surfaces as a shadow `status_mismatch` (Rust errors where Node succeeds) on legitimately deep
//! event properties. Accessing a nested global runs it through `json_to_hog`, so this exercises the
//! guard end to end.

use hogvm::{sync_execute, ExecutionContext, Program, MAX_JSON_SERDE_DEPTH};
use serde_json::{json, Value};

const OP_GET_GLOBAL: i64 = 1;
const OP_STRING: i64 = 32;
const OP_RETURN: i64 = 38;

// A value nested `depth` arrays deep: [[[ ... 1 ... ]]].
fn nested_array(depth: usize) -> Value {
    let mut v = json!(1);
    for _ in 0..depth {
        v = json!([v]);
    }
    v
}

// Program: `return globals.deep` — forces json_to_hog over the whole nested subtree on access.
fn return_deep_global(globals: Value) -> Result<Value, hogvm::VmFailure> {
    let bc = vec![
        json!("_H"),
        json!(1),
        json!(OP_STRING),
        json!("deep"),
        json!(OP_GET_GLOBAL),
        json!(1),
        json!(OP_RETURN),
    ];
    let program = Program::new(bc).expect("valid program");
    let ctx = ExecutionContext::with_defaults(program).with_globals(globals);
    sync_execute(&ctx, false)
}

#[test]
fn nesting_within_the_cap_deserializes() {
    // Just under the cap (and well past the old value of 64): must round-trip unchanged.
    let value = nested_array(MAX_JSON_SERDE_DEPTH - 50);
    let result = return_deep_global(json!({ "deep": value.clone() })).expect("execution succeeds");
    assert_eq!(result, value);
}

#[test]
fn nesting_past_the_cap_still_errors() {
    // The guard is raised, not removed — pathologically deep input still errors rather than risking
    // native stack exhaustion.
    assert!(
        return_deep_global(json!({ "deep": nested_array(MAX_JSON_SERDE_DEPTH + 50) })).is_err(),
        "nesting beyond MAX_JSON_SERDE_DEPTH should still error"
    );
}