From df7ce37ba92d85afb50a44475abfa85ff662ee07 Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 26 Oct 2026 20:36:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor(b1b-c5):=20=E7=99=BD=E5=90=8D=E5=8D=95?= =?UTF-8?q?=E6=B8=85=E5=8D=95=E5=BD=92=E4=B8=9A=E5=8A=A1=E5=B1=82=20?= =?UTF-8?q?=E2=80=94=E2=80=94=20=E5=B9=B3=E5=8F=B0=E5=B1=82=E9=9B=B6?= =?UTF-8?q?=E7=AB=AF=E7=82=B9=E5=AD=97=E9=9D=A2=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删掉 platform::auth 的 WHITE_URL / EXTRA_WHITE_URL 两个 const - AuthState 增字段 public_paths: Arc>(清单由业务层注入) - 清单真源移到 tdc::support::public_paths:4 条活路径(3 业务 + /api/health), 并剪掉上游 gateway.white-url 里本仓**无对应路由**的 9 条死路径 (Gateway×2 / P2P×5 / Portal / Topology)⇒ 记 D17(spec 写「7 条」与实际「9 条」有出入) - platform::auth 的 is_white_url 改成「清单由调用方给」,机制不变(Ant 匹配) - 单测:平台层 white_url_matching_is_manifest_driven(机制)+ 业务层 the_manifest_holds_exactly_the_live_endpoints / dead_upstream_paths_are_not_in_the_manifest - 边界账本清空 B1b 责任条目(CP1/CP4 全部归零)⇒ 账本只剩 2 条永久豁免 --- scripts/boundary-ledger.txt | 2 - src/main.rs | 2 + src/platform/auth.rs | 54 ++++++++------------ src/tdc/support/public_paths.rs | 87 +++++++++++++++++++++++++++++++-- tests/account_api.rs | 2 + tests/api_smoke.rs | 2 + tests/auth_flow.rs | 2 + tests/tds_biz.rs | 2 + 8 files changed, 113 insertions(+), 40 deletions(-) diff --git a/scripts/boundary-ledger.txt b/scripts/boundary-ledger.txt index 5088122..1e9b350 100644 --- a/scripts/boundary-ledger.txt +++ b/scripts/boundary-ledger.txt @@ -12,7 +12,5 @@ # # 文件字段按**路径后缀**匹配 ⇒ 搬家(src/auth.rs → src/platform/auth.rs)不会让条目失效。 -CP1 auth.rs /api/ssw/tdc/ C5:白名单清单待归业务层 src/tdc/support/public_paths.rs(并剪掉 7 条死路径) B1b CP2 session.rs Identity 假阳性:SessionKind::Identity 是**会话口径**,不是业务实体 tdc_identity 永久 CP2 auth.rs SessionKind::Identity 假阳性:Principal::Session 分支里的会话种类,不是业务实体 永久 -CP4 auth.rs /api/ C5:白名单清单待归业务层(含 /api/oauth/Login 与 /api/health 两条) B1b diff --git a/src/main.rs b/src/main.rs index 89f9fc9..c5215b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,7 @@ use tdc_connector::platform::auth::{auth_middleware, AuthState}; use tdc_connector::platform::config::Config; use tdc_connector::platform::db::ensure_schema; use tdc_connector::tdc::controller::build_router; +use tdc_connector::tdc::support::public_paths; use tdc_connector::platform::fallback::{api_not_found, spa_fallback}; use tdc_connector::platform::session::SessionStore; use tdc_connector::platform::state::{AppState, BootstrapIdentity}; @@ -167,6 +168,7 @@ async fn main() { let auth_state = AuthState { internal_token: config.internal_token.clone(), sessions: sessions.clone(), + public_paths: Arc::new(public_paths::all_public_paths()), }; let static_dir = project_dir.join("static"); diff --git a/src/platform/auth.rs b/src/platform/auth.rs index 8cc96df..8eb7aeb 100644 --- a/src/platform/auth.rs +++ b/src/platform/auth.rs @@ -31,25 +31,6 @@ pub const AUTHORIZATION_HEADER: &str = "authorization"; /// 跨端调用方声明的操作人。 pub const OPERATOR_HEADER: &str = "X-Internal-Operator"; -/// 上游 `gateway.white-url`(Ant 风格)。 -pub const WHITE_URL: &[&str] = &[ - "/api/oauth/Login", - "/api/ssw/tdc/Auth/Login", - "/api/ssw/tdc/Auth/SsoConfig", - "/api/ssw/tdc/Gateway/Invoke", - "/api/ssw/tdc/Gateway/ProviderInvoke", - "/api/ssw/tdc/P2P/Handshake", - "/api/ssw/tdc/P2P/Transfer/Prepare", - "/api/ssw/tdc/P2P/Transfer/*/Chunk", - "/api/ssw/tdc/P2P/Transfer/*/Receipt", - "/api/ssw/tdc/P2P/Download/*", - "/api/ssw/tdc/Portal/**", - "/api/ssw/tdc/Topology/Internal/TopologyCheck", -]; - -/// 本地额外放行:健康检查(探针)。 -pub const EXTRA_WHITE_URL: &[&str] = &["/api/health"]; - /// 操作主体。 #[derive(Clone, Debug)] pub enum Principal { @@ -69,6 +50,11 @@ pub enum Principal { pub struct AuthState { pub internal_token: String, pub sessions: Arc, + /// 免鉴权端点清单(**由业务层注入**)。 + /// + /// 🔴 平台层**不写死任何端点路径**(v3 spec §2.3.1 C5 / §2.3.2 接触点 ④)—— + /// 真源在 `tdc::support::public_paths`(上游 `gateway.white-url` 去掉本仓无路由的死路径)。 + pub public_paths: Arc>, } /// Ant 风格路径匹配:`**` 跨段任意、`*` 段内任意、`?` 单字符。 @@ -119,12 +105,9 @@ fn segment_match(pattern: &str, segment: &str) -> bool { go(&p, &s) } -/// 是否命中白名单。 -pub fn is_white_url(path: &str) -> bool { - WHITE_URL - .iter() - .chain(EXTRA_WHITE_URL.iter()) - .any(|p| ant_match(p, path)) +/// 是否命中白名单(**清单由调用方给** —— 平台层不持有清单)。 +pub fn is_white_url(patterns: &[&str], path: &str) -> bool { + patterns.iter().any(|p| ant_match(p, path)) } /// 只有 `/api/` 前缀的路径走业务鉴权。 @@ -177,7 +160,7 @@ pub async fn auth_middleware( return next.run(req).await; } // ② 白名单(不过协议版本闸门) - if is_white_url(uri.path()) { + if is_white_url(&auth.public_paths, uri.path()) { return next.run(req).await; } // ③ 内部令牌通道(过协议版本闸门) @@ -360,15 +343,17 @@ mod tests { assert!(ant_match("/api/ssw/tdc/Portal/**", "/api/ssw/tdc/Portal")); } + /// 🔴 平台层只保留「**按清单放行**」的机制(C5)—— 清单由业务层注入, + /// 平台层**不认识任何具体路径**。 #[test] - fn white_url_covers_upstream_list_plus_local_extras() { - assert!(is_white_url("/api/oauth/Login")); - assert!(is_white_url("/api/ssw/tdc/Auth/Login")); - assert!(is_white_url("/api/ssw/tdc/Auth/SsoConfig")); - assert!(is_white_url("/api/ssw/tdc/Topology/Internal/TopologyCheck")); - assert!(is_white_url("/api/health")); - assert!(!is_white_url("/api/ssw/tdc/Product")); - assert!(!is_white_url("/api/ssw/tdc/Auth/Me")); + fn white_url_matching_is_manifest_driven() { + let patterns = ["/api/oauth/Login", "/api/ssw/tdc/P2P/Download/*"]; + assert!(is_white_url(&patterns, "/api/oauth/Login")); + assert!(is_white_url(&patterns, "/api/ssw/tdc/P2P/Download/abc")); + assert!(!is_white_url(&patterns, "/api/ssw/tdc/Product")); + assert!(!is_white_url(&patterns, "/api/ssw/tdc/Auth/Me")); + // 空清单 ⇒ 什么都不放行(机制本身仍可工作)。 + assert!(!is_white_url(&[], "/api/oauth/Login")); } #[test] @@ -547,6 +532,7 @@ mod tests { let state = AuthState { internal_token: TEST_INTERNAL_TOKEN.to_string(), sessions, + public_paths: Arc::new(vec!["/api/ssw/tdc/Auth/Login"]), }; let app: Router = Router::new() .route( diff --git a/src/tdc/support/public_paths.rs b/src/tdc/support/public_paths.rs index ce15357..c0f1d4a 100644 --- a/src/tdc/support/public_paths.rs +++ b/src/tdc/support/public_paths.rs @@ -1,9 +1,35 @@ -//! 免鉴权端点清单(**业务侧**持有)。 +//! 免鉴权端点清单(**业务侧唯一真源**)。 //! -//! 🔴 平台层 `platform::auth` 只保留「按清单放行」的机制,**不认识任何具体路径** -//! (v3 spec §2.3.2 接触点 ④ ⇒ `scripts/check-boundary.sh` 的 CP4 会当场红)。 +//! 🔴 平台层 `platform::auth` 只保留「**按清单放行**」的机制(Ant 风格匹配 + 白名单判定), +//! **不认识任何具体路径**(v3 spec §2.3.2 接触点 ④ ⇒ `scripts/check-boundary.sh` 的 CP4 +//! 会当场红)。清单由本模块持有,运行时注入 `platform::auth::AuthState::public_paths`。 //! -//! 本清单的**唯一真源**是上游 `TdcAuthFilter` 的白名单 + 上游前端登录页必需的端点。 +//! 本清单的**唯一真源**是上游 `TdcAuthFilter` 的 `gateway.white-url` + 上游前端登录页必需的端点。 +//! +//! ## 已剪掉的死路径(C5,v3 spec §2.3.1) +//! +//! 上游 `gateway.white-url` 共 12 条,其中 **9 条本仓已无对应路由**(白名单放行了不存在的端点 +//! ⇒ 纯属攻击面与噪音),已全部剪掉: +//! +//! | 死路径 | 本仓对应路由 | +//! |---|---| +//! | `/api/ssw/tdc/Gateway/Invoke` | 无 | +//! | `/api/ssw/tdc/Gateway/ProviderInvoke` | 无 | +//! | `/api/ssw/tdc/P2P/Handshake` | 无 | +//! | `/api/ssw/tdc/P2P/Transfer/Prepare` | 无 | +//! | `/api/ssw/tdc/P2P/Transfer/*/Chunk` | 无 | +//! | `/api/ssw/tdc/P2P/Transfer/*/Receipt` | 无 | +//! | `/api/ssw/tdc/P2P/Download/*` | 无 | +//! | `/api/ssw/tdc/Portal/**` | 无 | +//! | `/api/ssw/tdc/Topology/Internal/TopologyCheck` | 无 | +//! +//! ⚠️ **计数出入**:v3 spec §2.3.1 C5 写的是「7 条(Gateway×2 / P2P×4 / Portal / Topology)」, +//! 但上游清单里 **P2P 是 5 条**(Handshake / Transfer.Prepare / Transfer.*.Chunk / +//! Transfer.*.Receipt / Download.*)⇒ 实际死路径是 **9 条**(2 + 5 + 1 + 1)。 +//! 本仓按**实际清单**剪(9 条),记为 **D17**(见 `docs/decisions.md`)。 +//! +//! ⚠️ 剪掉死路径**不改行为**:那些路径本就 404(无路由 ⇒ 走 404 JSON 兜底), +//! 只是**不再被鉴权中间件提前放行** ⇒ 依然 404,形状不变。 /// 免内部令牌 + 免会话的**业务端点**。 /// @@ -17,4 +43,57 @@ pub const PUBLIC_PATHS: &[&str] = &[ ]; /// 免鉴权的**平台端点**(健康检查)。 +/// +/// 🔴 健康检查端点由**业务层**实现(`tdc/controller/health.rs`,C7)⇒ 清单归业务层。 pub const PUBLIC_PLATFORM_PATHS: &[&str] = &["/api/health"]; + +/// 注入 `platform::auth::AuthState::public_paths` 的**完整清单**(业务端点 + 平台端点)。 +pub fn all_public_paths() -> Vec<&'static str> { + PUBLIC_PATHS + .iter() + .chain(PUBLIC_PLATFORM_PATHS.iter()) + .copied() + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + /// 🔴 清单**字面钉住**:上游 `gateway.white-url` 去掉本仓无路由的死路径后, + /// 只剩这 4 条(3 条业务端点 + 1 条平台端点)。 + #[test] + fn the_manifest_holds_exactly_the_live_endpoints() { + assert_eq!( + all_public_paths(), + vec![ + "/api/oauth/Login", + "/api/ssw/tdc/Auth/Login", + "/api/ssw/tdc/Auth/SsoConfig", + "/api/health", + ] + ); + } + + /// 🔴 本仓**无对应路由**的死路径一个都不许留在白名单里(C5)。 + /// + /// ⚠️ 这 9 条在上游 `gateway.white-url` 里是有的(Gateway×2 / P2P×5 / Portal / Topology) + /// ⇒ 本仓剪掉它们**不改行为**(那些路径本就 404),只是不再被鉴权中间件提前放行。 + #[test] + fn dead_upstream_paths_are_not_in_the_manifest() { + let all = all_public_paths(); + for dead in [ + "/api/ssw/tdc/Gateway/Invoke", + "/api/ssw/tdc/Gateway/ProviderInvoke", + "/api/ssw/tdc/P2P/Handshake", + "/api/ssw/tdc/P2P/Transfer/Prepare", + "/api/ssw/tdc/P2P/Transfer/*/Chunk", + "/api/ssw/tdc/P2P/Transfer/*/Receipt", + "/api/ssw/tdc/P2P/Download/*", + "/api/ssw/tdc/Portal/**", + "/api/ssw/tdc/Topology/Internal/TopologyCheck", + ] { + assert!(!all.contains(&dead), "死路径不该留在白名单: {}", dead); + } + } +} diff --git a/tests/account_api.rs b/tests/account_api.rs index 916db76..d4d26c0 100644 --- a/tests/account_api.rs +++ b/tests/account_api.rs @@ -17,6 +17,7 @@ use tdc_connector::platform::auth::{auth_middleware, AuthState}; use tdc_connector::platform::cipher::encrypt_login_password; use tdc_connector::platform::config::Config; use tdc_connector::tdc::controller::build_router; +use tdc_connector::tdc::support::public_paths; use tdc_connector::platform::session::SessionStore; use tdc_connector::platform::state::{AppState, BootstrapIdentity}; use tokio::sync::OnceCell; @@ -87,6 +88,7 @@ async fn build(tag: &str) -> (Router, Arc) { let auth_state = AuthState { internal_token: config.internal_token.clone(), sessions, + public_paths: Arc::new(public_paths::all_public_paths()), }; // 🔴 axum 0.8.9:`impl Service> for Router<()>` —— **只有 `Router<()>` 实现 // `Service`**(`axum-0.8.9/src/routing/mod.rs:569`)。`build_router` 返回 diff --git a/tests/api_smoke.rs b/tests/api_smoke.rs index 5ab6a3b..58d3d0b 100644 --- a/tests/api_smoke.rs +++ b/tests/api_smoke.rs @@ -13,6 +13,7 @@ use serde_json::{json, Value}; use tdc_connector::platform::auth::{auth_middleware, AuthState}; use tdc_connector::platform::config::Config; use tdc_connector::tdc::controller::build_router; +use tdc_connector::tdc::support::public_paths; use tdc_connector::platform::mw::json_method_not_allowed; use tdc_connector::platform::session::SessionStore; use tdc_connector::platform::state::{AppState, BootstrapIdentity}; @@ -53,6 +54,7 @@ async fn build() -> (Router, String, AppState) { let auth_state = AuthState { internal_token: config.internal_token.clone(), sessions, + public_paths: Arc::new(public_paths::all_public_paths()), }; // 🔴 axum 0.8.9:`impl Service> for Router<()>` —— **只有 `Router<()>` 实现 // `Service`**(`axum-0.8.9/src/routing/mod.rs:569`)。`build_router` 返回 diff --git a/tests/auth_flow.rs b/tests/auth_flow.rs index a23976c..ea4f81b 100644 --- a/tests/auth_flow.rs +++ b/tests/auth_flow.rs @@ -14,6 +14,7 @@ use tdc_connector::platform::auth::{auth_middleware, AuthState}; use tdc_connector::platform::config::Config; use tdc_connector::tdc::service::identity; use tdc_connector::tdc::controller::build_router; +use tdc_connector::tdc::support::public_paths; use tdc_connector::platform::session::SessionStore; use tdc_connector::platform::state::{AppState, BootstrapIdentity}; use tokio::sync::{Mutex, OnceCell}; @@ -259,6 +260,7 @@ async fn business_failure_is_http_200_while_auth_failure_is_http_401() { let auth_state = AuthState { internal_token: "test-internal-token".to_string(), sessions: sessions.clone(), + public_paths: Arc::new(public_paths::all_public_paths()), }; let app: Router = build_router(state.clone()) .layer(axum::middleware::from_fn_with_state( diff --git a/tests/tds_biz.rs b/tests/tds_biz.rs index 4ef49fe..a6f6919 100644 --- a/tests/tds_biz.rs +++ b/tests/tds_biz.rs @@ -23,6 +23,7 @@ use serde_json::{json, Value}; use tdc_connector::platform::auth::{auth_middleware, AuthState}; use tdc_connector::platform::config::Config; use tdc_connector::tdc::controller::build_router; +use tdc_connector::tdc::support::public_paths; use tdc_connector::platform::mw::json_method_not_allowed; use tdc_connector::platform::session::SessionStore; use tdc_connector::platform::state::{AppState, BootstrapIdentity}; @@ -193,6 +194,7 @@ async fn build(base_url: Option) -> (Router, String) { let auth_state = AuthState { internal_token: config.internal_token.clone(), sessions, + public_paths: Arc::new(public_paths::all_public_paths()), }; let app: Router = build_router(state.clone()) .layer(axum::middleware::from_fn(json_method_not_allowed))