fix(auth): prevent apiKeyAuth middleware pollution in sub-routes#647
Open
icebear0828 wants to merge 2 commits into
Open
fix(auth): prevent apiKeyAuth middleware pollution in sub-routes#647icebear0828 wants to merge 2 commits into
icebear0828 wants to merge 2 commits into
Conversation
icebear0828
commented
Jun 17, 2026
icebear0828
left a comment
Owner
Author
There was a problem hiding this comment.
Review
根因定位准确:全局 app.use("*", apiKeyAuth(...)) 在子路由挂载到根路径后会污染不该鉴权的端点(如 /health、/v1/models),改为精确绑定是正确解法。
问题:PR 缺少配套测试。按项目规范 无测试 = 未完成。建议在合并前补一个集成测试,验证不鉴权的路由(/health、/v1/models)在不带 key 的情况下不会返回 401,确保 middleware 不再泄漏。
次要:PR body 过于简短,建议说明哪些端点之前会被误拦截,方便 reviewer 理解影响范围。
Verify that:
- POST /v1/chat/completions, /v1/messages, /v1/messages/count_tokens,
/v1beta/models/:action, /v1/responses, /v1/responses/compact all return
401 when proxy_api_key is set and no key is provided
- GET /v1/models returns 200 (public, no auth required)
These tests would have caught the global app.use('*') leak described in #647.
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.
This PR fixes the apiKeyAuth middleware issue where mounting sub-routes to Hono under root path elevated the middleware to act globally, causing 401 leaks on non-API routes. Precised endpoint bindings are applied to replace global use("*") matches in routes/chat.ts, embeddings.ts, gemini.ts, messages.ts, and responses.ts.
Closes #653