修复: WebSocket Origin 校验拒绝同源请求导致新会话无法发消息#557
Open
SinlinLi wants to merge 1 commit into
Open
Conversation
ed775b8 新增的 CSWSH 防护对 WebSocket 升级请求做了 Origin 白名单校验, 但 isAllowedOrigin() 只放行 localhost 和 CORS_ALLOWED_ORIGINS 白名单, 未处理同源请求。通过域名或非 localhost IP 访问时,浏览器发出的 Origin header 不匹配任何白名单条目,WebSocket 连接被 403 拒绝。 主会话不受影响(走 HTTP POST /api/messages),但新会话/Agent 对话 走 WebSocket send_message,连接断开导致 "WebSocket 未连接" 报错。 修复:在白名单校验前增加同源检查——如果 Origin 的 host 与 HTTP Host header 一致,视为同源请求直接放行。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
0786b30 to
9ad6997
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题描述
ed775b8 新增的 CSWSH 防护对 WebSocket 升级请求做了 Origin 白名单校验,但
isAllowedOrigin()只放行 localhost 和CORS_ALLOWED_ORIGINS白名单,未处理同源请求。通过域名或非 localhost IP 访问时,浏览器发出的 Origin header 不匹配任何白名单条目,WebSocket 连接被 403 Forbidden 拒绝。
症状:创建新会话发消息时提示 "WebSocket 未连接,输入已保留,请稍后重试",主会话不受影响。
原因:主会话走 HTTP POST
/api/messages(同源请求不需要 CORS),新会话/Agent 对话走 WebSocketsend_message,WS 连接被 Origin 校验挡掉。修复方案
src/web.ts在 Origin 白名单校验前增加同源检查:比较
Originheader 的 host 与 HTTPHostheader,匹配则视为同源请求直接放行,不再走白名单校验。跨站请求(Origin ≠ Host)仍走原有的
isAllowedOrigin()白名单逻辑,CSWSH 防护不受影响。🤖 Generated with Claude Code