fix(ai): 供应商改名时不得用空口令清空已存密钥

- 列表/详情的 credential 恒空(安全收紧)⇒ 前端保存会带回空串
- update 时把空串当「不修改」;只有空 credential 的请求直接 200(不报「没有字段」)
- 补测试:改名 + 空口令后 credentialConfigured 仍为 true
This commit is contained in:
admin committed 2026-11-03 02:36:57 +08:00
1 parent adfd954303
commit 929fe0592a
2 files changed
+41 -1

No files matched your search

+26 -1
View File
@@ -643,7 +643,32 @@ pub async fn supplier_update(
Ok(u) => u,
Err(resp) => return resp,
};
res::update_json(&state, &SUPPLIER, &id, &body, &user.account).await
let mut obj = match parse_body(&body) {
Ok(Value::Object(o)) => o,
Ok(_) => return fail(code::FAIL, "请求参数格式错误"),
Err(resp) => return resp,
};
// 🔴 口令**不下发**(见 `mask_supplier`)⇒ 前端保存时带回的是空串。
// 空串必须当成「不修改」,否则一次改名就把密钥清空了。
if obj
.get("credential")
.and_then(Value::as_str)
.map(|s| s.trim().is_empty())
.unwrap_or(true)
{
obj.remove("credential");
}
if obj
.iter()
.all(|(k, _)| !SUPPLIER.fields.iter().any(|f| f.json == k))
{
return ok(json!({ "id": id }));
}
match res::update(&state.db, &SUPPLIER, &id, &obj, &user.account).await {
Ok(true) => ok(json!({ "id": id })),
Ok(false) => fail(code::FAIL, "数据不存在"),
Err(e) => db_error(e, "更新供应商"),
}
}
pub async fn supplier_delete(
+15
View File
@@ -243,6 +243,21 @@ async fn ai_center_crud_and_recall_work() {
assert_eq!(row["credential"], "", "口令**不得**下发");
assert_eq!(row["credentialConfigured"], true, "必须告诉前端「已配置」");
// 改名时带回空口令 ⇒ 不能把已存的密钥清空。
let sup_id = row["id"].as_str().expect("供应商 id").to_string();
let upd = call(
port,
"PUT",
&format!("/api/ai/AiSupplier/{sup_id}"),
&token,
Some(r#"{"fullName":"IT供应商2","credential":""}"#),
)
.await;
assert_eq!(upd["code"], 200, "{upd}");
let again = call(port, "GET", &format!("/api/ai/AiSupplier/{sup_id}"), &token, None).await;
assert_eq!(again["data"]["credentialConfigured"], true, "空口令不得清空密钥: {again}");
assert_eq!(again["data"]["fullName"], "IT供应商2");
let k = call(port, "POST", "/api/ai/Knowledge", &token, Some(r#"{"fullName":"制度库","way":1}"#)).await;
let kid = id_of(&k);
let d = call(