From b795eb8fa79ce7503567963f45c8124c2fa1af41 Mon Sep 17 00:00:00 2001 From: Luki Zainur Date: Thu, 10 Sep 2026 18:10:27 +0000 Subject: [PATCH] feat(gateway): operator allowlist for who can pair (#11979) --- crates/goose-cli/src/cli.rs | 5 +- crates/goose/src/gateway/handler.rs | 92 ++++++++++++++++++++++++++++- crates/goose/src/gateway/manager.rs | 39 ++++++++++++ 3 files changed, 134 insertions(+), 2 deletions(-) diff --git a/crates/goose-cli/src/cli.rs b/crates/goose-cli/src/cli.rs index 7d0aada12..99a25d0b4 100644 --- a/crates/goose-cli/src/cli.rs +++ b/crates/goose-cli/src/cli.rs @@ -2345,7 +2345,10 @@ async fn handle_gateway_command(command: GatewayCommand) -> Result<()> { gateway_type, bot_token, } => { - let platform_config = serde_json::json!({ "bot_token": bot_token }); + let mut platform_config = serde_json::json!({ "bot_token": bot_token }); + if let Some(ids) = goose::gateway::manager::saved_allowed_user_ids(&gateway_type) { + platform_config["allowed_user_ids"] = serde_json::json!(ids); + } gateway::handle_gateway_start(gateway_type, platform_config).await } GatewayCommand::Stop { gateway_type } => gateway::handle_gateway_stop(gateway_type).await, diff --git a/crates/goose/src/gateway/handler.rs b/crates/goose/src/gateway/handler.rs index fb9f0351c..f5091ab2a 100644 --- a/crates/goose/src/gateway/handler.rs +++ b/crates/goose/src/gateway/handler.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, VecDeque}; +use std::collections::{HashMap, HashSet, VecDeque}; use std::path::PathBuf; use std::sync::Arc; use std::time::Duration; @@ -60,6 +60,8 @@ pub struct GatewayHandler { turn_locks: PerUserLocks, /// Serializes confirmation replies without waiting for the active turn to finish. confirmation_reply_locks: PerUserLocks, + /// When set, only platform user IDs in this list may enter the pairing flow. + allowed_user_ids: Option>, } impl GatewayHandler { @@ -69,6 +71,7 @@ impl GatewayHandler { gateway: Arc, config: GatewayConfig, ) -> Self { + let allowed_user_ids = allowed_user_ids_from_config(&config); Self { agent_manager, pairing_store, @@ -77,6 +80,7 @@ impl GatewayHandler { pending_confirmations: Arc::new(Mutex::new(HashMap::new())), turn_locks: Arc::new(Mutex::new(HashMap::new())), confirmation_reply_locks: Arc::new(Mutex::new(HashMap::new())), + allowed_user_ids, } } @@ -121,6 +125,15 @@ impl GatewayHandler { } pub async fn handle_message(&self, message: IncomingMessage) -> anyhow::Result<()> { + if !is_user_allowed(&self.allowed_user_ids, &message.user) { + tracing::info!( + platform = %message.user.platform, + user_id = %message.user.user_id, + "gateway allowlist denied unlisted user" + ); + return Ok(()); + } + let pairing = self.pairing_store.get(&message.user).await?; match pairing { @@ -825,6 +838,34 @@ fn gateway_working_dir(platform: &str, user_id: &str) -> PathBuf { .join(user_id) } +/// Operator-configured list of platform user IDs that may pair with the gateway, +/// read from `platform_config.allowed_user_ids`. An absent or empty list keeps +/// the default behavior of letting anyone attempt pairing. +fn allowed_user_ids_from_config(config: &GatewayConfig) -> Option> { + let ids: HashSet = config.platform_config["allowed_user_ids"] + .as_array() + .into_iter() + .flatten() + .filter_map(|id| { + id.as_str() + .map(ToOwned::to_owned) + .or_else(|| id.as_u64().map(|n| n.to_string())) + }) + .collect(); + if ids.is_empty() { + None + } else { + Some(ids) + } +} + +fn is_user_allowed(allowed: &Option>, user: &PlatformUser) -> bool { + match allowed { + None => true, + Some(ids) => ids.contains(&user.user_id), + } +} + #[cfg(test)] mod tests { use super::*; @@ -851,4 +892,53 @@ mod tests { fn gateway_override_used_when_global_unset() { assert_eq!(resolve_gateway_max_turns(Some(25), None), 25); } + + fn platform_user(user_id: &str) -> PlatformUser { + PlatformUser { + platform: "telegram".to_string(), + user_id: user_id.to_string(), + display_name: None, + } + } + + fn config_with_platform(platform_config: serde_json::Value) -> GatewayConfig { + GatewayConfig { + gateway_type: "telegram".to_string(), + platform_config, + max_sessions: 1, + } + } + + #[test] + fn allowlist_absent_or_empty_allows_everyone() { + for platform_config in [ + serde_json::json!({}), + serde_json::json!({"allowed_user_ids": []}), + ] { + let allowed = allowed_user_ids_from_config(&config_with_platform(platform_config)); + assert!(allowed.is_none()); + assert!(is_user_allowed(&allowed, &platform_user("999999"))); + } + } + + #[test] + fn allowlist_admits_listed_and_blocks_unlisted_users() { + let config = config_with_platform(serde_json::json!({"allowed_user_ids": ["111", 222]})); + let allowed = allowed_user_ids_from_config(&config).expect("allowlist should be set"); + assert_eq!(allowed.len(), 2); + assert!(is_user_allowed( + &Some(allowed.clone()), + &platform_user("111") + )); + assert!(is_user_allowed(&Some(allowed), &platform_user("222"))); + + let allowed = allowed_user_ids_from_config(&config).expect("allowlist should be set"); + assert!(!is_user_allowed(&Some(allowed), &platform_user("333"))); + } + + #[test] + fn allowlist_ignores_non_id_values() { + let config = config_with_platform(serde_json::json!({"allowed_user_ids": [true, null]})); + assert!(allowed_user_ids_from_config(&config).is_none()); + } } diff --git a/crates/goose/src/gateway/manager.rs b/crates/goose/src/gateway/manager.rs index 19e4ad327..8c0b473a9 100644 --- a/crates/goose/src/gateway/manager.rs +++ b/crates/goose/src/gateway/manager.rs @@ -23,6 +23,39 @@ fn secret_key_for(gateway_type: &str) -> String { struct SavedGatewayEntry { gateway_type: String, max_sessions: usize, + #[serde(default)] + allowed_user_ids: Option>, +} + +/// Extract the operator allowlist from a platform_config value, if present. +fn allowed_user_ids_from_platform_config( + platform_config: &serde_json::Value, +) -> Option> { + platform_config + .get("allowed_user_ids") + .and_then(|v| v.as_array()) + .map(|ids| { + ids.iter() + .filter_map(|id| { + id.as_str() + .map(ToOwned::to_owned) + .or_else(|| id.as_u64().map(|n| n.to_string())) + }) + .collect() + }) + .filter(|ids: &Vec| !ids.is_empty()) +} + +/// Operator allowlist saved for a gateway type, if any. +pub fn saved_allowed_user_ids(gateway_type: &str) -> Option> { + let entries: Vec = match Config::global().get_param(GATEWAY_CONFIGS_KEY) { + Ok(entries) => entries, + Err(_) => return None, + }; + entries + .into_iter() + .find(|entry| entry.gateway_type == gateway_type) + .and_then(|entry| entry.allowed_user_ids) } #[allow(dead_code)] @@ -344,6 +377,11 @@ impl GatewayManager { } }; + let mut platform_config = platform_config; + if let Some(ids) = &entry.allowed_user_ids { + platform_config["allowed_user_ids"] = serde_json::json!(ids); + } + configs.push(GatewayConfig { gateway_type: entry.gateway_type, platform_config, @@ -372,6 +410,7 @@ impl GatewayManager { entries.push(SavedGatewayEntry { gateway_type: gw_config.gateway_type.clone(), max_sessions: gw_config.max_sessions, + allowed_user_ids: allowed_user_ids_from_platform_config(&gw_config.platform_config), }); config