diff --git a/backend/crates/gateway-admin/src/model/provider_credentials.rs b/backend/crates/gateway-admin/src/model/provider_credentials.rs index c6e896bbd..9fb987e1d 100644 --- a/backend/crates/gateway-admin/src/model/provider_credentials.rs +++ b/backend/crates/gateway-admin/src/model/provider_credentials.rs @@ -304,6 +304,12 @@ impl PendingAuthorizationMutation { "outbound_proxy_url".to_owned(), Value::String(proxy.expose_url().to_owned()), ); + if proxy.max_websocket_connections() != 0 { + document.insert( + "outbound_proxy_max_websocket_connections".to_owned(), + Value::from(proxy.max_websocket_connections()), + ); + } } if let Some(id) = &self.outbound_proxy_id { document.insert("outbound_proxy_id".to_owned(), Value::String(id.clone())); @@ -344,6 +350,11 @@ impl PendingAuthorizationMutation { .map(gateway_core::account::OutboundProxy::parse) .transpose() .map_err(|_| invalid())?; + let proxy = match (proxy, document.outbound_proxy_max_websocket_connections) { + (Some(proxy), Some(limit)) => Some(proxy.with_max_websocket_connections(limit)), + (None, Some(_)) => return Err(invalid()), + (proxy, None) => proxy, + }; Ok(Self::new( provider_kind, target, @@ -367,6 +378,8 @@ struct StoredAuthorizationMutationV1 { #[serde(default)] outbound_proxy_url: Option, #[serde(default)] + outbound_proxy_max_websocket_connections: Option, + #[serde(default)] outbound_proxy_id: Option, } diff --git a/backend/crates/gateway-admin/tests/model/provider_credentials.rs b/backend/crates/gateway-admin/tests/model/provider_credentials.rs index 743b85e20..031d4fb5a 100644 --- a/backend/crates/gateway-admin/tests/model/provider_credentials.rs +++ b/backend/crates/gateway-admin/tests/model/provider_credentials.rs @@ -267,7 +267,8 @@ fn pending_mutation_v1_should_round_trip_all_targets_and_owners() { gateway_core::account::OutboundProxy::parse( "http://user:secret@proxy.example:8080", ) - .unwrap(), + .unwrap() + .with_max_websocket_connections(16), )) .with_outbound_proxy_id(Some("proxy_saved".to_owned())); assert_eq!( @@ -293,6 +294,27 @@ fn pending_mutation_v1_should_keep_existing_storage_fields_and_reject_invalid_do let restored = PendingAuthorizationMutation::from_storage_v1(legacy.clone()).expect("v1 document"); assert_eq!(Value::Object(restored.to_storage_v1()), legacy); + let mut legacy_proxy = legacy.clone(); + legacy_proxy["outbound_proxy_url"] = json!("socks5h://proxy.example.invalid:1080"); + legacy_proxy["outbound_proxy_id"] = json!("proxy_saved"); + let restored = PendingAuthorizationMutation::from_storage_v1(legacy_proxy.clone()) + .expect("legacy proxy without a capacity field remains readable"); + assert_eq!( + restored + .outbound_proxy() + .unwrap() + .max_websocket_connections(), + 0 + ); + assert_eq!(Value::Object(restored.to_storage_v1()), legacy_proxy); + for invalid in [json!(-1), json!(4294967296_u64), json!("16")] { + let mut document = legacy_proxy.clone(); + document["outbound_proxy_max_websocket_connections"] = invalid; + assert!(PendingAuthorizationMutation::from_storage_v1(document).is_err()); + } + let mut missing_proxy = legacy.clone(); + missing_proxy["outbound_proxy_max_websocket_connections"] = json!(16); + assert!(PendingAuthorizationMutation::from_storage_v1(missing_proxy).is_err()); for (pointer, invalid) in [ ("/provider_kind", json!("")), ("/target/account_id", json!("")), diff --git a/backend/crates/gateway-store/tests/postgres/provider_accounts/authorization.rs b/backend/crates/gateway-store/tests/postgres/provider_accounts/authorization.rs index 65e25a6f0..9601cb9be 100644 --- a/backend/crates/gateway-store/tests/postgres/provider_accounts/authorization.rs +++ b/backend/crates/gateway-store/tests/postgres/provider_accounts/authorization.rs @@ -39,6 +39,130 @@ async fn authorization_persists_an_unnamed_account_with_its_email() { database.close().await; } +#[tokio::test] +async fn authorization_preserves_saved_proxy_capacity_across_pending_storage() { + use gateway_core::account::OutboundProxy; + + let Some(database) = TestDatabase::create("authorization_proxy_snapshot").await else { + return; + }; + let proxy_url = "socks5h://proxy.example.invalid:1080"; + let proxy_id = "proxy_synthetic"; + sqlx::query( + "insert into outbound_proxies (id, name, proxy_url, enabled, max_websocket_connections) \ + values ($1, 'Synthetic proxy', $2, true, 16)", + ) + .bind(proxy_id) + .bind(proxy_url) + .execute(&database.pool) + .await + .unwrap(); + let (mut authorization, context) = command(credential("acct_proxy_test", "synthetic-user")); + let pending = authorization + .pending + .with_outbound_proxy(Some( + OutboundProxy::parse(proxy_url) + .unwrap() + .with_max_websocket_connections(16), + )) + .with_outbound_proxy_id(Some(proxy_id.into())); + authorization.pending = PendingAuthorizationMutation::from_storage_v1( + serde_json::Value::Object(pending.to_storage_v1()), + ) + .unwrap(); + if let AuthorizationCredentialCommit::Create(credential) = &mut authorization.credential { + credential.outbound_proxy = authorization.pending.outbound_proxy().cloned(); + } + let store = admin_account_store(&database.pool); + let committed = store + .commit_authorization(authorization, &context) + .await + .expect("unchanged saved proxy must not cause an authorization conflict"); + let saved_proxy: Option = + sqlx::query_scalar("select outbound_proxy_id from provider_accounts where id = $1") + .bind(committed.result.account_id.as_str()) + .fetch_one(&database.pool) + .await + .unwrap(); + assert_eq!(saved_proxy.as_deref(), Some(proxy_id)); + database.close().await; +} + +#[tokio::test] +async fn authorization_rejects_a_saved_proxy_that_changed_after_start() { + use gateway_admin::ports::store::AdminStoreErrorKind; + use gateway_core::account::OutboundProxy; + + let Some(database) = TestDatabase::create("authorization_proxy_changed").await else { + return; + }; + let proxy_url = "socks5h://proxy.example.invalid:1080"; + let proxy_id = "proxy_synthetic"; + sqlx::query( + "insert into outbound_proxies (id, name, proxy_url, enabled, max_websocket_connections) \ + values ($1, 'Synthetic proxy', $2, true, 16)", + ) + .bind(proxy_id) + .bind(proxy_url) + .execute(&database.pool) + .await + .unwrap(); + let store = admin_account_store(&database.pool); + for (current_url, limit, enabled, expected) in [ + ( + "socks5h://other.example.invalid:1080", + 16_i64, + true, + AdminStoreErrorKind::StaleRevision, + ), + (proxy_url, 17, true, AdminStoreErrorKind::StaleRevision), + (proxy_url, 16, false, AdminStoreErrorKind::Conflict), + ] { + sqlx::query( + "update outbound_proxies set proxy_url=$2, max_websocket_connections=$3, enabled=$4 where id=$1", + ) + .bind(proxy_id) + .bind(current_url) + .bind(limit) + .bind(enabled) + .execute(&database.pool) + .await + .unwrap(); + let (mut authorization, context) = command(credential("acct_proxy_test", "synthetic-user")); + let pending = authorization + .pending + .with_outbound_proxy(Some( + OutboundProxy::parse(proxy_url) + .unwrap() + .with_max_websocket_connections(16), + )) + .with_outbound_proxy_id(Some(proxy_id.into())); + authorization.pending = PendingAuthorizationMutation::from_storage_v1( + serde_json::Value::Object(pending.to_storage_v1()), + ) + .unwrap(); + let receipt_key = authorization.key.clone(); + let error = store + .commit_authorization(authorization, &context) + .await + .expect_err("changed or disabled proxy must fail closed"); + assert_eq!(error.kind(), expected); + assert!( + store + .authorization_receipt(&receipt_key) + .await + .unwrap() + .is_none() + ); + } + let accounts: i64 = sqlx::query_scalar("select count(*) from provider_accounts") + .fetch_one(&database.pool) + .await + .unwrap(); + assert_eq!(accounts, 0); + database.close().await; +} + #[tokio::test] async fn concurrent_authorization_retries_share_one_account_write_and_one_receipt() { let Some(database) = TestDatabase::create("authorization_receipt_race").await else { diff --git a/deploy/README.md b/deploy/README.md index eb52f7969..fd92dd3b7 100644 --- a/deploy/README.md +++ b/deploy/README.md @@ -843,6 +843,7 @@ docker compose -f deploy/compose.yaml build codex-proxy-rs ### 升级前检查 +- OpenAI 或 xAI 授权使用已保存代理,且代理的 WebSocket 连接上限非零:升级或回退后,重新发起尚未完成的授权,不复用旧回调或授权码。上限为 `0` 的在途授权和已保存的账号、凭据不受影响 - Key 配置含 `installationId`:数据库自动转换为 `installationSeed`,API 调用方须使用[当前字段](../docs/api.md#key-客户端身份)。出站安装身份及关联的会话、缓存标识会变化,已有 WebSocket 精确续写需重发完整上下文 - 数据库或备份含 `accountAutoFreeze*` 设置:升级后错误保护关闭、规则为空,需在管理端重新配置并启用;API 调用方使用 [`accountErrorProtection`](../docs/api.md#账号错误保护)。已有冻结到当前冷却截止后解除,账号并发上限保留,需调整时手动设置