Skip to main content

inject_fields_bytes

Function inject_fields_bytes 

Source
pub fn inject_fields_bytes(
    body: &[u8],
    fields: &[(FieldName, Value)],
) -> Result<Vec<u8>, RewriteError>
Expand description

Splices fields into the top level of the JSON object in body, returning the new bytes, without parsing body into a Value or re-serializing it (ADR-014). The body is scanned once for its top-level keys (to reject a spoofed reserved field) and the injected fields are written right after the opening {; the rest of the document is copied verbatim. The byte-level twin of inject_fields for the streaming write path.

A field that already exists is a RewriteError::ReservedFieldCollision, as in inject_fields: a client must not pre-seed a tenancy field and defeat isolation (docs/03). Escaped key names are decoded before the check, so the collision cannot be smuggled past as "_tenant".

§Errors

RewriteError::NotAnObject if body is not a JSON object, RewriteError::InvalidJson if it is malformed, or RewriteError::ReservedFieldCollision if an injected field is already present.

§Examples

use serde_json::Value;
use osproxy_core::FieldName;
use osproxy_rewrite::inject_fields_bytes;

let out = inject_fields_bytes(
    br#"{"msg":"hi"}"#,
    &[(FieldName::from("_tenant"), Value::from("acme"))],
).unwrap();
assert_eq!(out, br#"{"_tenant":"acme","msg":"hi"}"#);