Skip to content

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

fix(channels): reject malformed relay deliveries instead of falling back

Steven Enamakel3832e78914e9
diff --git a/crates/openhuman-core/src/channels/runtime/startup/callback.rs b/crates/openhuman-core/src/channels/runtime/startup/callback.rs
index 967931220a..4decfc9568 100644
--- a/crates/openhuman-core/src/channels/runtime/startup/callback.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/callback.rs
@@ -108,10 +108,13 @@ impl ChannelHostCallbacks {
impl ChannelHostCallbacks {
async fn deliver_inbound(&self, channel: String, message: serde_json::Value) -> Result<()> {
Self::validate_callback_size(&message)?;
- if let Ok(delivery) = serde_json::from_value::<
- tinychannels_bus::module_relay::RelayInboundDelivery,
- >(message.clone())
- {
+ let relay_shaped = message.get("envelope").is_some()
+ || message.get("delivered_via_authenticated_relay").is_some();
+ if relay_shaped {
+ let delivery = serde_json::from_value::<
+ tinychannels_bus::module_relay::RelayInboundDelivery,
+ >(message.clone())
+ .map_err(|_| Error::failed("invalid relay delivery"))?;
if delivery.envelope.channel.id != channel {
return Err(Error::failed(
"relay callback channel does not match envelope",
diff --git a/crates/openhuman-core/src/channels/runtime/startup/callback_inbound_tests.rs b/crates/openhuman-core/src/channels/runtime/startup/callback_inbound_tests.rs
index 0626aad6b1..336b3c60db 100644
--- a/crates/openhuman-core/src/channels/runtime/startup/callback_inbound_tests.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/callback_inbound_tests.rs
@@ -106,19 +106,41 @@ async fn typed_inbound_rejects_malformed_or_cross_account_envelopes_without_queu
wrong_account.channel.account_id = Some("telegram:bot-b".into());
let mut wrong_provider = valid_envelope();
wrong_provider.channel.id = "slack".into();
+ let mut empty_message_id = valid_envelope();
+ empty_message_id.message_id.clear();
+ let mut empty_conversation_id = valid_envelope();
+ empty_conversation_id.conversation.id.clear();
let invalid_payloads = [
serde_json::to_value(wrong_account).expect("encode wrong-account envelope"),
serde_json::to_value(wrong_provider).expect("encode wrong-provider envelope"),
+ serde_json::to_value(empty_message_id).expect("encode empty-message-id envelope"),
+ serde_json::to_value(empty_conversation_id).expect("encode empty-conversation-id envelope"),
serde_json::json!({
"channel": { "id": "telegram", "account_id": "telegram:bot-a" },
"conversation": "not-a-conversation",
"parts": []
}),
- serde_json::json!({
- "envelope": "malformed",
- "delivered_via_authenticated_relay": true
- }),
];
+ let legacy = tinychannels_bus::ChannelMessage {
+ channel: "telegram:bot-a".into(),
+ content: "legacy message".into(),
+ ..Default::default()
+ };
+ let mut malformed_relay_with_legacy_fields =
+ serde_json::to_value(&legacy).expect("encode legacy message");
+ malformed_relay_with_legacy_fields["envelope"] = serde_json::json!("malformed");
+ let mut malformed_provenance_with_legacy_fields =
+ serde_json::to_value(&legacy).expect("encode legacy message");
+ malformed_provenance_with_legacy_fields["delivered_via_authenticated_relay"] =
+ serde_json::json!("not-a-boolean");
+ let mut malformed_typed_with_legacy_fields =
+ serde_json::to_value(&legacy).expect("encode legacy message");
+ malformed_typed_with_legacy_fields["parts"] = serde_json::json!("malformed");
+ let invalid_payloads = invalid_payloads.into_iter().chain([
+ malformed_relay_with_legacy_fields,
+ malformed_provenance_with_legacy_fields,
+ malformed_typed_with_legacy_fields,
+ ]);
for payload in invalid_payloads {
assert!(
@@ -130,7 +152,10 @@ async fn typed_inbound_rejects_malformed_or_cross_account_envelopes_without_queu
);
}
assert!(
- received.try_recv().is_err(),
+ matches!(
+ received.try_recv(),
+ Err(tokio::sync::mpsc::error::TryRecvError::Empty)
+ ),
"rejected input must not reach channel dispatch"
);
}
@@ -173,7 +198,7 @@ async fn trusted_relay_provenance_and_legacy_inbound_remain_supported() {
let legacy = tinychannels_bus::ChannelMessage {
channel: "telegram".into(),
- text: "legacy message".into(),
+ content: "legacy message".into(),
..Default::default()
};
callback
@@ -186,5 +211,5 @@ async fn trusted_relay_provenance_and_legacy_inbound_remain_supported() {
let legacy_runtime_message = received.try_recv().unwrap();
assert!(!legacy_runtime_message.authenticated_relay);
assert!(legacy_runtime_message.inbound_envelope.is_none());
- assert_eq!(legacy_runtime_message.message.text, "legacy message");
+ assert_eq!(legacy_runtime_message.message.content, "legacy message");
}