fix(security): persist source_channel to DB, harden cross-channel authorization

Address PR #1590 review feedback:

1. Persist source_channel to DB: Add source_channel column to conversations
   table in both PostgreSQL (V14 migration) and libSQL (incremental migration
   + base schema). Add get_conversation_source_channel trait method to
   ConversationStore with both backend implementations.

2. Fix hydrate_thread_from_db: Read source_channel from DB instead of
   stamping the requesting message's channel, preventing channel confusion
   after server restart.

3. Reject reserved WASM channel names: Validate that WASM channels cannot
   register as "web", "gateway", "cli", or "repl" to prevent authorization
   bypass via name spoofing.

4. Require pending_approval exists: Authorization check now verifies
   thread.pending_approval.is_some() before allowing approval-shaped messages
   to target a thread.

5. Fail-closed for None source_channel: Use "__bootstrap__" sentinel for
   bootstrap threads (authorized from any channel). None now means "deny by
   default" instead of "allow by default".

6. Extract and test authorization predicate: is_approval_authorized() helper
   with 6 unit tests covering same-channel, cross-channel blocked, web/gateway
   always allowed, None denied, and bootstrap sentinel.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
This commit is contained in:
Zaki
2026-03-28 15:11:03 +00:00
committed by Claude
co-authored by Claude Opus 4.6
parent 30feaabb7c
commit 5f50df7583
14 changed files with 219 additions and 25 deletions
+17 -3
View File
@@ -135,14 +135,28 @@ impl Agent {
msg_count = 0;
}
// Create thread with the historical ID and restore messages
// Create thread with the historical ID and restore messages.
// Read source_channel from DB so the authorization check uses the
// original creator's channel, not the requesting message's channel.
let db_source_channel = if let Some(store) = self.store() {
store
.get_conversation_source_channel(thread_uuid)
.await
.unwrap_or(None)
} else {
None
};
let effective_source_channel = db_source_channel
.as_deref()
.or(Some(&*message.channel));
let session_id = {
let sess = session.lock().await;
sess.id
};
let mut thread =
crate::agent::session::Thread::with_id(thread_uuid, session_id, Some(&message.channel));
crate::agent::session::Thread::with_id(thread_uuid, session_id, effective_source_channel);
if !chat_messages.is_empty() {
thread.restore_from_messages(chat_messages);
}
@@ -637,7 +651,7 @@ impl Agent {
user_id: &str,
) -> bool {
match store
.ensure_conversation(thread_id, channel, user_id, None)
.ensure_conversation(thread_id, channel, user_id, None, Some(channel))
.await
{
Ok(true) => true,