use super::config::{MinimalSanitizeConfig, SanitizeError};
use super::history::drop_blocks;
use super::types::BlockType;
use serde_json::Value;
use std::collections::HashSet;
pub fn sanitize(
messages: Vec<Value>,
cfg: &MinimalSanitizeConfig,
) -> Result<Vec<Value>, SanitizeError> {
if cfg.max_messages_turns.unwrap_or(0) > 0
&& messages.len() as u64 > cfg.max_messages_turns.unwrap_or(0)
{
return Err(SanitizeError::HistoryTooDeep);
}
if cfg.max_image_bytes.unwrap_or(0) > 0
|| cfg.max_video_bytes.unwrap_or(0) > 0
|| cfg.max_pdf_bytes.unwrap_or(0) > 0
{
check_media_sizes(&messages, cfg)?;
}
if !cfg.permanent_deny_blocks.is_empty() {
let deny_set: HashSet<&'static str> = cfg
.permanent_deny_blocks
.iter()
.map(|b| b.as_str())
.collect();
let messages = drop_blocks(messages, &|block| {
block
.get("type")
.and_then(Value::as_str)
.map(|t| deny_set.contains(t))
.unwrap_or(false)
});
return Ok(messages);
}
Ok(messages)
}
fn check_media_sizes(messages: &[Value], cfg: &MinimalSanitizeConfig) -> Result<(), SanitizeError> {
for msg in messages {
let Some(content) = msg
.as_object()
.and_then(|o| o.get("content"))
.and_then(Value::as_array)
else {
continue;
};
for raw in content {
let Some(block) = raw.as_object() else {
continue;
};
let Some(bt) = block.get("type").and_then(Value::as_str) else {
continue;
};
let (block_type, limit) = match bt {
"image" => (BlockType::Image, cfg.max_image_bytes.unwrap_or(0)),
"video" => (BlockType::Video, cfg.max_video_bytes.unwrap_or(0)),
"document" => (BlockType::Document, cfg.max_pdf_bytes.unwrap_or(0)),
_ => continue,
};
if limit == 0 {
continue;
}
let data = extract_base64_data(block);
if data.is_empty() {
continue; }
let actual = base64_decoded_len(data);
if actual > limit {
return Err(SanitizeError::Size {
block_type,
actual,
limit,
});
}
}
}
Ok(())
}
fn extract_base64_data(block: &serde_json::Map<String, Value>) -> &str {
let Some(src) = block.get("source").and_then(Value::as_object) else {
return "";
};
if src.get("type").and_then(Value::as_str) != Some("base64") {
return "";
}
let Some(data) = src.get("data").and_then(Value::as_str) else {
return "";
};
match data.find("base64,") {
Some(i) => &data[i + "base64,".len()..],
None => data,
}
}
fn base64_decoded_len(b64: &str) -> u64 {
let bytes = b64.as_bytes();
let n = bytes.len();
let mut pad: u64 = 0;
if n >= 1 && bytes[n - 1] == b'=' {
pad += 1;
}
if n >= 2 && bytes[n - 2] == b'=' {
pad += 1;
}
((n as u64) * 3 / 4).saturating_sub(pad)
}