From 9ebee9c96d0591432adb8644bc660cf189374879 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 12 Sep 2026 12:39:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(llm):=20=E5=87=BA=E7=AB=99=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=A4=B4=E9=BB=98=E8=AE=A4=E4=B8=8D=E6=94=BE=E8=A1=8C?= =?UTF-8?q?=EF=BC=88allowlist=EF=BC=89=EF=BC=8C=E5=A0=B5=E4=BD=8F=E7=BD=91?= =?UTF-8?q?=E5=85=B3=20Key=20=E7=BB=8F=20x-api-key=20=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 行业做法(LiteLLM / one-api / Higress 完全一致):**不由客户端决定**上游该收到哪些头, 而是网关按「上游需要什么」重新构造。nginx 侧对应 proxy_pass_request_headers off。 改造前实测(本机靶机 + .11 生产双确认):本块只覆写了 Authorization,客户端放在 x-api-key 里的**网关自己的 Key** 会被原样送给上游厂商,厂商可拿它反调本网关挂的 **全部**上游(横向越权)。MiniMax 两处都认,把这个缺陷掩盖成了 200。 改动: - conf/nginx.conf 与两个测试模板:off + 凭据头显式抹空(x-api-key / api-key / x-goog-api-key,两层叠加,将来谁改回 on 仍兜底)+ 显式放行 Content-Type / Accept / anthropic-version / anthropic-beta / openai-beta / User-Agent - tests/llm/e2e.sh:+12 条断言。凭据头不泄漏、必需头存活(漏 Content-Type 会让所有 POST 直接废)、以及 host 粒度 base 下一条记录吃 OpenAI + Anthropic 两种路径 - 反证:把模板还原成改造前状态(无 off、无抹空)重跑,3 条凭据断言如实变红(86/3), 证明这组断言不是空转 回归:LLM 89 + 网关 78,失败 0。调研与实测证据见 docs/llm/UPSTREAM_PROTOCOL_AND_AUTH.md --- conf/nginx.conf | 52 ++++++++++++++++++++++++++++++++++++----- tests/llm/e2e.sh | 50 +++++++++++++++++++++++++++++++++++++++ tests/llm/nginx.conf.in | 25 ++++++++++++++++---- tests/llm/tls.conf.in | 22 +++++++++++++---- 4 files changed, 135 insertions(+), 14 deletions(-) diff --git a/conf/nginx.conf b/conf/nginx.conf index 98c5a92..3d3e72d 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -126,16 +126,56 @@ http { proxy_pass $llm_target; proxy_http_version 1.1; + + # ================================================================ + # 出站请求头策略:默认不放行(allowlist) + # + # 这是 LiteLLM / one-api / Higress 的共同做法——**不把客户端带了什么 + # 头当成上游该收什么头**,而是网关自己按「上游需要什么」重新构造。 + # nginx 里对应的开关就是 proxy_pass_request_headers off(只发下面 + # 显式声明的头)。调研与实测:docs/llm/UPSTREAM_PROTOCOL_AND_AUTH.md + # + # 不这么做的后果(本机靶机 + .11 生产双确认):nginx 默认透传全部 + # 客户端头,而本块只覆写了 Authorization —— 客户端放在 x-api-key 里的 + # **网关自己的 Key** 会被原样送给上游厂商,厂商可拿它反调本网关挂的 + # **全部**上游(横向越权)。宽松厂商(MiniMax 两处都认)会把这个缺陷 + # 掩盖成 200,换到只认 x-api-key 的严格厂商才变成 401。 + # ================================================================ + proxy_pass_request_headers off; + + # ── 第一层:凭据头显式抹空(nginx 空串语义 = 不发该头)── + # 与 off 叠加是刻意的冗余:将来谁把 off 改回 on,这层仍然兜底。 + # 名单取自「主流 SDK 把凭据放在哪个头里」。 + proxy_set_header x-api-key ""; + proxy_set_header api-key ""; + proxy_set_header x-goog-api-key ""; + + # ── 第二层:显式放行上游真正需要的头 ── + # ★ 漏一条的后果都是实测过的: + # Content-Type → 所有 POST 直接废(上游收到无 content-type 的 body) + # anthropic-version → Anthropic 协议必填,缺失即被拒 + # anthropic-beta → Claude Code 的 prompt caching 等扩展静默失效 + # Accept → SSE 流式协商受影响 + # 将来厂商新增扩展头(如新的 openai-beta 值)**必须同步加到这里**, + # 漏掉的表现是「静默功能缺失」而不是报错。 + # # 变量形式的 proxy_pass 不会推导 Host,必须显式设; # 上游多为 Cloudflare 前端,Host 不符会被直接拒 - proxy_set_header Host $llm_host; - # 换上上游真实密钥。下游那把 sk_ 到此为止,不往上游传 - proxy_set_header Authorization $llm_auth; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $llm_host; + # 换上上游真实密钥。下行那把 sk_ 到此为止,不往上游传 + proxy_set_header Authorization $llm_auth; + proxy_set_header Content-Type $http_content_type; + proxy_set_header Accept $http_accept; + proxy_set_header anthropic-version $http_anthropic_version; + proxy_set_header anthropic-beta $http_anthropic_beta; + proxy_set_header openai-beta $http_openai_beta; + # 客户端身份,便于厂商侧排障(非语义必需,但丢了就查不动账) + proxy_set_header User-Agent $http_user_agent; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 置空才能与上游复用长连接(这是 API 调用,不是 WebSocket) - proxy_set_header Connection ""; + proxy_set_header Connection ""; # —— SSE 三件套:缺一条,流式就会退化成「攒完一次性返回」—— proxy_buffering off; diff --git a/tests/llm/e2e.sh b/tests/llm/e2e.sh index e78ab40..b357f6c 100755 --- a/tests/llm/e2e.sh +++ b/tests/llm/e2e.sh @@ -52,6 +52,23 @@ print(json.loads(last).get(sys.argv[2], '') if last else '') " "$UP_LOG" "$1" 2>/dev/null } up_count() { if [ -f "$UP_LOG" ]; then awk 'NF{n++} END{print n+0}' "$UP_LOG"; else echo 0; fi } +# 靶机日志:取最后一条记录里**某个请求头**的值(头名大小写不敏感)。 +# 专门为「出站头策略」服务:要证明某个头没被发出去 / 某个头还活着, +# 只能看上游实际收到了什么。取不到的空值 = 该头确实没发。 +up_hdr() { + "$PY" -c " +import json, sys +last = None +try: + for l in open(sys.argv[1], encoding='utf-8'): + l = l.strip() + if l: last = l +except FileNotFoundError: + pass +h = (json.loads(last).get('headers') or {}) if last else {} +print(h.get(sys.argv[2].lower(), '')) +" "$UP_LOG" "$1" 2>/dev/null +} post() { curl -s -H "Authorization: Bearer $1" -X POST "$GW$2" -H 'Content-Type: application/json' -d "$3"; } # 从 stdin 的 JSON 里按点分路径取值 @@ -99,6 +116,30 @@ ck "Host 头 = 上游主机(非下游域名)" "127.0.0.1:18999" "$(up_field ck "★路径去 slot 且保留 query" "/v1/chat/completions?a=1" "$(up_field path)" ck "请求体原样透传" "$BODY" "$(up_field body)" +# ★ 出站头策略(对齐 LiteLLM 的两层:默认不放行 + 凭据头显式抹空)。 +# 构造最坏情形:客户端把**网关自己的 Key** 塞进每一个凭据头, +# 同时带上两套协议的扩展头,然后看上游究竟收到了什么。 +# 注意这里**故意不发 Authorization**,顺带验证「入站只靠 x-api-key 也能过网关」。 +curl -s -o /dev/null -X POST "$GW/openai/v1/chat/completions" \ + -H "x-api-key: $ALICE_KEY" \ + -H "api-key: $ALICE_KEY" \ + -H "x-goog-api-key: $ALICE_KEY" \ + -H 'anthropic-version: 2023-06-01' \ + -H 'anthropic-beta: prompt-caching-2024-07-31' \ + -H 'openai-beta: assistants=v2' \ + -H 'Content-Type: application/json' -d "$BODY" +ck "★x-api-key 未泄漏给上游(默认摘除清单)" "" "$(up_hdr x-api-key)" +ck "★api-key 未泄漏给上游" "" "$(up_hdr api-key)" +ck "★x-goog-api-key 未泄漏给上游" "" "$(up_hdr x-goog-api-key)" +ck "★Authorization 仍是上游真 Key(客户端塞不进凭据)" "Bearer sk-UPSTREAM-REAL-SECRET" "$(up_field auth)" +ck "★Content-Type 已注入(漏了所有 POST 都会废)" "application/json" "$(up_hdr content-type)" +ck " Accept 已注入(SSE 协商要靠它)" "*/*" "$(up_hdr accept)" +ck "★anthropic-version 已注入(Anthropic 协议必填)" "2023-06-01" "$(up_hdr anthropic-version)" +ck "★anthropic-beta 已注入(Claude Code 扩展依赖)" "prompt-caching-2024-07-31" "$(up_hdr anthropic-beta)" +ck " openai-beta 已注入" "assistants=v2" "$(up_hdr openai-beta)" +ck " User-Agent 已透传(厂商侧排障要靠它)" yes \ + "$([ -n "$(up_hdr user-agent)" ] && echo yes || echo no)" + echo echo "【4】上游 base 带路径时的拼接" post "$BOB_KEY" /other/v1/chat/completions "$BODY" >/dev/null @@ -113,6 +154,15 @@ ck " 客户端不带 /v1 → 照常前缀上 base 的 /v1" "/v1/chat/completion post "$BOB_KEY" /deepseeklike/v2/chat/completions "$BODY" >/dev/null ck " 不同版本段不误判为重复" "/v1/v2/chat/completions" "$(up_field path)" +# ★ 「统一注册」= base 只写到 host,一条记录同时服务两套协议。 +# 这是 LiteLLM(/anthropic/* 前缀)/ one-api(根地址 + 路径天然区分)/ +# Higress(按路径后缀判协议)三个开源实现的共同做法: +# **协议由客户端路径表达,网关不做任何推断、不需要分发代码**。 +# 夹具里 openai 的 base = http://127.0.0.1:18999(纯 host),正好验这条。 +post "$ALICE_KEY" "/openai/anthropic/v1/messages" "$BODY" >/dev/null +ck "★Anthropic 路径原样拼在 host 粒度 base 之后" "/anthropic/v1/messages" "$(up_field path)" +ck " 同一条记录也换上游真 Key(不是只有 OpenAI 路径才换)" "Bearer sk-UPSTREAM-REAL-SECRET" "$(up_field auth)" + echo echo "【5】★ 流式(SSE 不被缓冲)" SSE=$(GW="$GW" KEY="$ALICE_KEY" "$PY" "$HERE/sse_test.py" 2>&1) diff --git a/tests/llm/nginx.conf.in b/tests/llm/nginx.conf.in index 888f7bc..b0c99ab 100644 --- a/tests/llm/nginx.conf.in +++ b/tests/llm/nginx.conf.in @@ -46,10 +46,27 @@ http { proxy_pass $llm_target; proxy_http_version 1.1; - proxy_set_header Host $llm_host; - proxy_set_header Authorization $llm_auth; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header Connection ""; + + # ⚠️ 这一块必须与生产 conf/nginx.conf 的 LLM server **逐条对齐**, + # 否则回归证明的不是生产行为。 + # 出站头策略:默认不放行(allowlist)+ 凭据头显式抹空。 + # 背景见 docs/llm/UPSTREAM_PROTOCOL_AND_AUTH.md + proxy_pass_request_headers off; + + proxy_set_header x-api-key ""; + proxy_set_header api-key ""; + proxy_set_header x-goog-api-key ""; + + proxy_set_header Host $llm_host; + proxy_set_header Authorization $llm_auth; + proxy_set_header Content-Type $http_content_type; + proxy_set_header Accept $http_accept; + proxy_set_header anthropic-version $http_anthropic_version; + proxy_set_header anthropic-beta $http_anthropic_beta; + proxy_set_header openai-beta $http_openai_beta; + proxy_set_header User-Agent $http_user_agent; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header Connection ""; # SSE 三件套:少一个流式就会退化成「攒完一次性返回」 proxy_buffering off; diff --git a/tests/llm/tls.conf.in b/tests/llm/tls.conf.in index bdee00f..486359f 100644 --- a/tests/llm/tls.conf.in +++ b/tests/llm/tls.conf.in @@ -24,11 +24,25 @@ server { proxy_pass $llm_target; proxy_http_version 1.1; - proxy_set_header Host $llm_host; - proxy_set_header Authorization $llm_auth; - proxy_set_header X-Real-IP $remote_addr; + + # 与生产 / 明文模板保持同一套出站头策略(默认不放行 + 凭据头抹空) + proxy_pass_request_headers off; + + proxy_set_header x-api-key ""; + proxy_set_header api-key ""; + proxy_set_header x-goog-api-key ""; + + proxy_set_header Host $llm_host; + proxy_set_header Authorization $llm_auth; + proxy_set_header Content-Type $http_content_type; + proxy_set_header Accept $http_accept; + proxy_set_header anthropic-version $http_anthropic_version; + proxy_set_header anthropic-beta $http_anthropic_beta; + proxy_set_header openai-beta $http_openai_beta; + proxy_set_header User-Agent $http_user_agent; + proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto https; - proxy_set_header Connection ""; + proxy_set_header Connection ""; proxy_buffering off; proxy_request_buffering off;