Skip to content

Fix cors preflight#287

Merged
ding113 merged 1 commit intoding113:devfrom
ylxmf2005:fix/cors-preflight
Dec 7, 2025
Merged

Fix cors preflight#287
ding113 merged 1 commit intoding113:devfrom
ylxmf2005:fix/cors-preflight

Conversation

@ylxmf2005
Copy link
Contributor

@ylxmf2005 ylxmf2005 commented Dec 7, 2025

问题背景

在 Obsidian(基于 Electron)或浏览器环境中调用 /v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse 时,客户端会先发送 OPTIONS 预检请求。由于预检请求不携带认证头,导致以下问题:

  1. 401 错误:预检请求被认证守卫拦截
  2. 500 错误:流式响应尝试追加 CORS 头时,因 headers 不可变抛出异常

解决方案

在路由层统一处理 CORS,避免预检请求进入业务守卫链。

新增 src/app/v1/_lib/cors.ts

  • 动态回显 OriginAccess-Control-Request-Headers
  • 预检请求直接返回 204 No Content
  • 添加 Access-Control-Expose-Headers 暴露 x-request-idx-ratelimit-* 等响应头
  • Vary 头用 append 追加,避免覆盖已有值

路由层集成

  • /v1/v1beta 路由统一调用 registerCors(app)
  • 业务请求(POST/GET 等)仍完整走鉴权/限流/代理链

技术细节

为什么需要处理 OPTIONS?

Electron 应用和浏览器对跨域请求会自动发送 OPTIONS 预检。当请求携带自定义头(Authorizationx-api-keyanthropic-version 等)时触发。若预检被拒绝,实际业务请求不会发送。

当前配置

"Access-Control-Allow-Origin": "*"  // 允许所有来源

安全考虑

安全性未降低

  • 预检请求放行 ≠ 业务请求放行
  • 实际 API 调用仍需有效的 API Key
  • 原有认证/限流/会话守卫链完全不变

变更文件

  • ✨ 新增:src/app/v1/_lib/cors.ts
  • 🔧 修改:src/app/v1/[...route]/route.ts
  • 🔧 修改:src/app/v1beta/[...route]/route.ts

验证

bun run typecheck  # ✅ 通过
bun run lint       # ✅ 通过

Checklist

  • 目标分支为 dev
  • 所有状态检查(Docker Build Test)已通过
  • main 无直接冲突

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ylxmf2005, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在解决跨域资源共享(CORS)预检请求在 Electron 和浏览器环境中遇到的认证和响应头处理问题。通过在路由层引入统一的 CORS 处理机制,确保 OPTIONS 请求能够正确响应,避免了不必要的 401 和 500 错误,同时不影响后续业务请求的鉴权和限流逻辑,从而提升了 API 的兼容性和稳定性。

Highlights

  • 解决 CORS 预检请求问题: 修复了在 Obsidian 或浏览器环境中调用 API 时,OPTIONS 预检请求因未携带认证头导致 401 错误,以及流式响应尝试修改不可变 headers 导致 500 错误的问题。
  • 统一 CORS 处理逻辑: 引入了新的 src/app/v1/_lib/cors.ts 文件,在路由层统一处理 CORS 逻辑,确保预检请求在进入业务鉴权链之前得到正确响应。
  • 动态 CORS 响应头与安全性: 实现了动态构建 CORS 响应头,包括回显 OriginAccess-Control-Request-Headers,并为预检请求返回 204 No Content。同时强调此变更不会降低安全性,业务请求仍需完整鉴权。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

本次 PR 通过在路由层统一处理 CORS,优雅地解决了预检请求(OPTIONS)导致的认证失败和流式响应头不可变的问题。新增的 cors.ts 模块逻辑清晰,覆盖了动态 Origin、预检请求响应和为业务响应追加 CORS 头等场景。代码整体质量很高。我提出了一些关于代码风格和微小优化的建议,以提高代码的可维护性和简洁性。

@ding113 ding113 added bug Something isn't working size/S Small PR (< 200 lines) labels Dec 7, 2025
@ylxmf2005 ylxmf2005 force-pushed the fix/cors-preflight branch 3 times, most recently from 9ded55b to 6ebceb7 Compare December 7, 2025 08:31
@ding113 ding113 merged commit bd459f2 into ding113:dev Dec 7, 2025
@github-project-automation github-project-automation bot moved this from Backlog to Done in Claude Code Hub Roadmap Dec 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size/S Small PR (< 200 lines)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants

Comments