Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(web_chat): validate typed voice note media against stored audio

Steven Enamakelefa20044e501
diff --git a/crates/openhuman-core/src/web_chat/voice_notes.rs b/crates/openhuman-core/src/web_chat/voice_notes.rs
index 9b03ed52b7..8b86986a83 100644
--- a/crates/openhuman-core/src/web_chat/voice_notes.rs
+++ b/crates/openhuman-core/src/web_chat/voice_notes.rs
@@ -130,6 +130,47 @@ pub(crate) fn read_note(
Ok(None)
}
+/// Ensure a typed chat attachment names and carries the already stored note.
+/// The RPC upload happens before chat submission; this comparison binds the
+/// following typed part to those durable bytes instead of treating it as a
+/// disposable or model-visible attachment marker.
+pub(crate) fn validate_typed_media(
+ workspace: &Path,
+ note_id: &str,
+ media: &tinychannels_bus::MediaReference,
+) -> Result<(), String> {
+ if !is_valid_note_id(note_id) || media.opaque_id.as_deref() != Some(note_id) {
+ return Err("voice note media must name its stored note id".into());
+ }
+ let url = media
+ .url
+ .as_deref()
+ .ok_or_else(|| "voice note media needs an inline data: URL".to_string())?;
+ let (header, encoded) = url
+ .strip_prefix("data:")
+ .and_then(|data| data.split_once(','))
+ .ok_or_else(|| "voice note media needs an inline data: URL".to_string())?;
+ let mime = header
+ .strip_suffix(";base64")
+ .ok_or_else(|| "voice note media must use base64 encoding".to_string())?;
+ use base64::Engine as _;
+ let bytes = base64::engine::general_purpose::STANDARD
+ .decode(encoded)
+ .map_err(|error| format!("voice note media has invalid base64: {error}"))?;
+ let (stored, stored_mime) = read_note(workspace, note_id)?
+ .ok_or_else(|| "typed voice note has no stored audio".to_string())?;
+ let normalized_mime = mime.split(';').next().unwrap_or(mime).trim();
+ if normalized_mime != stored_mime
+ || media.content_type.as_deref().is_some_and(|content_type| {
+ content_type.split(';').next().unwrap_or(content_type).trim() != stored_mime
+ })
+ || bytes != stored
+ {
+ return Err("typed voice note does not match the stored audio".into());
+ }
+ Ok(())
+}
+
/// The `voiceNote` metadata for a reply whose audio was stored, if any.
pub(crate) fn reply_note_metadata(workspace: &Path, request_id: &str) -> Option<Value> {
let id = reply_note_id(request_id);