Skip to main content

hennery_kernel/
plugin_context.rs

1//! Bounds and redaction for decoded, untrusted startup data. Source validation
2//! belongs to the collector, under its owner/scope transaction.
3use anyhow::Result;
4use hennery_proto::plugin::{PluginContext, Provenance};
5
6pub fn sanitize(input: PluginContext) -> Result<PluginContext> {
7    // Decode strictly before this function; Value is safe only after duplicate
8    // and unknown keys have already been refused by the typed decoder.
9    let mut value = serde_json::to_value(input)?;
10    let redacted = crate::redact::value(&mut value);
11    let mut context: PluginContext = serde_json::from_value(value)?;
12    context.truncated |= redacted;
13    if context.generated_at.len() > 128 {
14        context.generated_at.clear();
15        context.truncated = true;
16    }
17    if context.entries.len() > 32 {
18        context.entries.truncate(32);
19        context.truncated = true;
20    }
21    for entry in &mut context.entries {
22        if entry.provenance.is_empty() {
23            entry.provenance.push(Provenance::Unverified {});
24        }
25        if entry.text.len() > 4096 {
26            let mut end = 4096;
27            while !entry.text.is_char_boundary(end) {
28                end -= 1;
29            }
30            entry.text.truncate(end);
31            context.truncated = true;
32        }
33    }
34    while serde_json::to_vec(&context)?.len() > 8192 {
35        context.entries.pop();
36        context.truncated = true;
37    }
38    Ok(context)
39}