Skip to content

fix(auth): prevent apiKeyAuth middleware pollution in sub-routes#647

Open
icebear0828 wants to merge 2 commits into
devfrom
fix/api-key-auth-leak
Open

fix(auth): prevent apiKeyAuth middleware pollution in sub-routes#647
icebear0828 wants to merge 2 commits into
devfrom
fix/api-key-auth-leak

Conversation

@icebear0828

@icebear0828 icebear0828 commented Jun 6, 2026

Copy link
Copy Markdown
Owner

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

@icebear0828 icebear0828 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

错误请求数量太多,删除直接返回登录页面

1 participant