test(api): 补一条 HTTP 层的 secretKey 去敏回归

**影响**
- 改动前:R10 的回归只在 **CRUD 引擎层**(`tests/crud_engine.rs`)覆盖,
  路由 + 业务信封层没有断言。
- 改动后:`/api/ssw/tdc/Identity` 的**详情 / 列表 / 新建**三处出参都断言
  「没有 `secretKey` 这个键」,且入参送 `secretKey` 不落库。

**改动**
- `tests/api_smoke.rs` 新增 `identity_secret_key_is_never_exposed_over_http`:
  直插一条**带密钥**的身份行(绕开 CRUD 写路径的「静默忽略」)⇒
  详情 / 列表(按 `identityCode` 过滤)都不得出现 `secretKey` 键;
  再 POST 一条带 `secretKey` 的身份 ⇒ 响应不含该键、库里那列仍为空。

**设计**
- 🔴 断言只看**键名**,不回显任何值 ⇒ 密钥不会进 transcript。
- **变异验证过是 load-bearing**:把 `row_to_json_spec` 里的 `obj.remove(*key)` 关掉
  ⇒ 本用例在 `tests/api_smoke.rs:559` 变红(已还原,`shasum` 与提交版逐字节一致)。
- 验收:`cargo test` ⇒ **223 passed / 0 failed / 1 ignored**;clippy 干净;`cargo fmt --check` 干净。

Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
adminandClaude Code committed 2026-10-25 15:27:07 +08:00
1 parent 1044af8a47
commit 77c2df577c
1 file changed
+112 -1
+112 -1
View File
@@ -9,7 +9,7 @@ use axum::http::{Request, StatusCode};
use axum::Router;
use http_body_util::BodyExt;
use pglite::PGlite;
use serde_json::Value;
use serde_json::{json, Value};
use tdc_connector::auth::{auth_middleware, AuthState};
use tdc_connector::config::Config;
use tdc_connector::routes::build_router;
@@ -515,3 +515,114 @@ async fn method_not_allowed_returns_json() {
.expect("Allow 头必须是合法 ASCII");
assert!(!allow.is_empty(), "Allow 头不得为空:{:?}", allow);
}
/// 🔴 **R10 回归(HTTP 层)**:`/api/ssw/tdc/Identity` 不得回显 `secretKey`。
///
/// 上游 `IdentityEntity.f_secret_key` 标了 `@JsonIgnore`,注释明写「防 CRUD list/detail 泄露」。
/// Jackson 的 `@JsonIgnore` **管双向**:出参**整条键不出现**(不是 null)、
/// 入参**绑不上实体** ⇒ 客户端送什么都不会写库。
///
/// 本仓此前 `crud.rs` 是 `SELECT *` + camelCase 直出 ⇒ 密钥原样进 JSON(= 泄漏)。
/// `tests/crud_engine.rs` 已在 **CRUD 引擎层**覆盖;这里补 **路由 + 业务信封层**。
#[tokio::test]
async fn identity_secret_key_is_never_exposed_over_http() {
let (app, token, state) = build().await;
// 造一条**带密钥**的身份行(直插 SQL,绕开 CRUD 写路径的「静默忽略」)。
let id = "SMOKE-REDACT-IDENTITY".to_string();
let code = "SMOKE-REDACT-CODE".to_string();
let name = "冒烟去敏身份".to_string();
let secret = "SENTINEL-SECRET-MUST-NOT-LEAK".to_string();
{
let _g = state.db_lock.lock().await;
state
.db
.as_ref()
.query(
"INSERT INTO tdc_identity (f_id, f_identity_code, f_name, f_secret_key) \
VALUES ($1, $2, $3, $4)",
&[&id, &code, &name, &secret],
)
.await
.expect("插入测试身份行失败");
}
// ① 详情:响应里**没有** secretKey 这个键
let (status, body) = call(
&app,
get(
&format!("/api/ssw/tdc/Identity/{}", id),
Some(token.as_str()),
),
)
.await;
assert_eq!(status, 200, "详情应可用:{:?}", body);
assert_eq!(body["code"], 200, "详情应返回业务信封:{:?}", body);
assert_eq!(body["data"]["identityCode"], code.as_str());
assert!(
body["data"].get("secretKey").is_none(),
"详情不得回显 secretKey(上游 @JsonIgnore ⇒ 整条不出现)"
);
// ② 列表(按 identityCode 过滤出那一行):同样没有
let (status, body) = call(
&app,
get(
&format!("/api/ssw/tdc/Identity?keyword={}", code),
Some(token.as_str()),
),
)
.await;
assert_eq!(status, 200, "列表应可用:{:?}", body);
assert_eq!(body["code"], 200);
let list = body["data"]["list"].as_array().expect("list 应是数组");
assert_eq!(list.len(), 1, "按 identityCode 过滤应只剩一行:{:?}", body);
assert_eq!(list[0]["id"], id.as_str());
assert!(list[0].get("secretKey").is_none(), "列表不得回显 secretKey");
// ③ 入参:POST 带 secretKey ⇒ **静默忽略**(不写库),响应也不得带该键
let post_code = "SMOKE-REDACT-POST".to_string();
let post_name = "冒烟去敏身份2".to_string();
let post_secret = "SENTINEL-SECOND-MUST-NOT-LEAK".to_string();
let (status, body) = call(
&app,
json_req(
"POST",
"/api/ssw/tdc/Identity",
Some(token.as_str()),
&json!({
"identityCode": post_code,
"name": post_name,
"secretKey": post_secret,
})
.to_string(),
),
)
.await;
assert_eq!(status, 200, "新建应可用:{:?}", body);
assert_eq!(body["code"], 200, "新建应成功:{:?}", body);
let new_id = body["data"].as_str().expect("应返回 id").to_string();
assert!(
body["data"].get("secretKey").is_none(),
"新建响应不得回显 secretKey"
);
// ④ 库里那一列仍是空(**只判空,不回显值** —— 免得把密钥写进 transcript)
let stored = {
let _g = state.db_lock.lock().await;
tdc_connector::db::query_scalar_text(
state.db.as_ref(),
"SELECT f_secret_key FROM tdc_identity WHERE f_id = CAST($1 AS text)",
&[&new_id],
)
.await
.expect("查询失败")
};
assert!(
stored
.as_deref()
.map(|s| s.trim().is_empty())
.unwrap_or(true),
"客户端送的 secretKey 不应被写库"
);
}