From 852bf842465fe3dad7461e946690c1ea95047abe Mon Sep 17 00:00:00 2001 From: liumangmang Date: Thu, 2 Jul 2026 17:05:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E4=B8=8A=E6=B8=B8=E6=A8=A1=E5=9E=8B=EF=BC=8C=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E5=85=8B=E9=9A=86=E8=BD=BD=E8=8D=B7=E4=B8=AD=E7=9A=84=E6=95=8F?= =?UTF-8?q?=E6=84=9F=20credentials=20=E5=AD=97=E6=AE=B5=EF=BC=8C=E8=A7=84?= =?UTF-8?q?=E9=81=BF=E9=87=8D=E5=86=99=E5=AF=BC=E8=87=B4=E8=84=B1=E6=95=8F?= =?UTF-8?q?=E5=AF=86=E9=92=A5=E4=B8=A2=E5=A4=B1/=E6=B8=85=E7=A9=BA?= =?UTF-8?q?=E7=9A=84=E4=BA=8B=E6=95=85=E9=A3=8E=E9=99=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/routers/websites.py | 13 +++++++++++-- backend/test_sync_upstream_models.py | 8 ++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/backend/app/routers/websites.py b/backend/app/routers/websites.py index b6981d2..180461d 100644 --- a/backend/app/routers/websites.py +++ b/backend/app/routers/websites.py @@ -67,6 +67,12 @@ logger = logging.getLogger(__name__) MASK = "***" SECRET_KEYS = {"password", "token", "key", "secret", "api_key"} +SENSITIVE_CREDENTIAL_KEYS = { + "access_token", "refresh_token", "id_token", + "api_key", "session_key", "cookie", + "aws_secret_access_key", "aws_session_token", + "service_account_json", "service_account", "private_key", +} ALGORITHMS = {"max_plus_percent", "average_plus_percent", "min_plus_percent"} @@ -1391,8 +1397,11 @@ def sync_website_accounts_upstream_models( continue # 1. 先安全修复 base_url (不依赖同步模型结果) - # 在 remote_acc.get("credentials") 基础上进行修改,保留其他非敏感 credentials (例如 compact_model_mapping) - current_creds = dict(remote_acc.get("credentials") or {}) + # 在 remote_acc.get("credentials") 基础上过滤掉敏感字段,保留其他非敏感 credentials (例如 compact_model_mapping) + current_creds = { + k: v for k, v in (remote_acc.get("credentials") or {}).items() + if k not in SENSITIVE_CREDENTIAL_KEYS + } current_creds["base_url"] = upstream_base_url try: diff --git a/backend/test_sync_upstream_models.py b/backend/test_sync_upstream_models.py index 7230a23..23e04e3 100644 --- a/backend/test_sync_upstream_models.py +++ b/backend/test_sync_upstream_models.py @@ -191,14 +191,16 @@ def test_sync_upstream_models_workflow(db_session, monkeypatch): # 1001 的两次 update 调用检测 calls_1001 = [call for call in update_calls if call[0] == "1001"] assert len(calls_1001) == 2 - # 第一次安全修复:应当在保留其他非敏感 credentials (如 compact_model_mapping) 的基础上,只将 base_url 变更为 http://upstream-a + # 第一次安全修复:应当在保留其他非敏感 credentials (如 compact_model_mapping) 的基础上,只将 base_url 变更为 http://upstream-a,并且过滤掉敏感字段 api_key assert calls_1001[0][1]["credentials"]["base_url"] == "http://upstream-a" assert calls_1001[0][1]["credentials"]["compact_model_mapping"] == {"compact": "compact"} assert calls_1001[0][1]["credentials"]["openai_capabilities"] == "caps" - # 第二次同步成功写入:同样保留其他非敏感 credentials,但将 model_mapping 也更新进去 + assert "api_key" not in calls_1001[0][1]["credentials"] + # 第二次同步成功写入:同样保留其他非敏感 credentials,过滤掉敏感字段 api_key,将 model_mapping 更新进去 assert calls_1001[1][1]["credentials"]["base_url"] == "http://upstream-a" assert calls_1001[1][1]["credentials"]["compact_model_mapping"] == {"compact": "compact"} assert calls_1001[1][1]["credentials"]["openai_capabilities"] == "caps" + assert "api_key" not in calls_1001[1][1]["credentials"] assert calls_1001[1][1]["credentials"]["model_mapping"] == { "gpt-3.5-turbo": "gpt-3.5-turbo", "gpt-4": "gpt-4" @@ -222,6 +224,7 @@ def test_sync_upstream_models_workflow(db_session, monkeypatch): calls_1005 = [call for call in update_calls if call[0] == "1005"] assert len(calls_1005) == 1 assert calls_1005[0][1]["credentials"]["base_url"] == "http://upstream-a" + assert "api_key" not in calls_1005[0][1]["credentials"] # 1006 失败 (抛错,但应当执行了第一次安全修复的 update_account) item_1006 = next(i for i in items if i.account_id == "1006") @@ -231,6 +234,7 @@ def test_sync_upstream_models_workflow(db_session, monkeypatch): calls_1006 = [call for call in update_calls if call[0] == "1006"] assert len(calls_1006) == 1 assert calls_1006[0][1]["credentials"]["base_url"] == "http://upstream-a" + assert "api_key" not in calls_1006[0][1]["credentials"] # 1007 失败 (上游 base_url 为空,不应当调用 update_account 且不调用同步模型) item_1007 = next(i for i in items if i.account_id == "1007")