Skip to content

feat(app): 加可配置的 CORS 中间件 - #140

Closed
johnnyzhang-eng wants to merge 2 commits into
1024XEngineer:mainfrom
johnnyzhang-eng:feat/backend-cors
Closed

feat(app): 加可配置的 CORS 中间件#140
johnnyzhang-eng wants to merge 2 commits into
1024XEngineer:mainfrom
johnnyzhang-eng:feat/backend-cors

Conversation

@johnnyzhang-eng

@johnnyzhang-eng johnnyzhang-eng commented Aug 6, 2026

Copy link
Copy Markdown

变更内容

#115 拆出的 CORS 部分。只动 bootstrap/app.py,15 行。

  • allow_origins / allow_methods / allow_headers = ["*"],开发阶段全放行
  • allow_credentials=False。鉴权走 Authorization: Bearer 不走 cookie;CORS 规范禁止 *allow_credentials: true 并存
  • WINDUP_CORS_ORIGINS(逗号分隔)非空即覆盖,供部署收窄

/health、Dockerfile、compose、README 部署章节在 #115

关联

Refs #139

本地验证

ruff check / lint-imports(2 kept, 0 broken)/ pytest -q(1 passed)全过。

preflight:默认任意来源放行且无 allow-credentials 头;设 WINDUP_CORS_ORIGINS 后仅放行配置来源。

…h the API

没有这个中间件,浏览器会把前端的每一个请求都拦在预检那一步:
OPTIONS 返回 405、响应无 access-control-* 头,而后端日志里连请求都看不到
——现场极易被误判成后端挂了。

- 来源由 WINDUP_CORS_ORIGINS 覆盖,默认值含 4173(vite preview,本地看真实
  生产构建走这个端口)、5173(dev server)、3000
- 预览域名走 WINDUP_CORS_ORIGIN_REGEX 显式配置,**默认不开**:这里同时开了
  allow_credentials,写死一条平台通配正则等于把带凭证的跨域请求放行给该平台上
  任意第三方应用,且显式配了 WINDUP_CORS_ORIGINS 也关不掉它

四条断言进 CI。已用摘掉中间件的控制样本验证:其中三条在没有中间件时确实失败
(configured origin 拿到 405、4173 无放行头、regex 不生效),第四条是护栏断言。

Refs 1024XEngineer#139

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@vercel

vercel Bot commented Aug 6, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
windup Ignored Ignored Preview Aug 6, 2026 8:18am

johnnyzhang-eng pushed a commit to johnnyzhang-eng/game-asset-character that referenced this pull request Aug 6, 2026
…l probe

让 main 上的后端能一条命令起在服务器上,并被容器健康检查正确判活。
本分支 stack 在 1024XEngineer#140(CORS)之上——两者都改 create_app(),1024XEngineer#140 合并后本分支 rebase。

- Dockerfile + docker-compose.yml:后端 + Postgres 一套起,uv sync --frozen 装依赖
- .dockerignore:构建上下文此前会带走 161MB 的本地虚拟环境
- GET /health 探针:HEALTHCHECK 原先打 /docs,而生产通常关掉交互文档
  (docs_url=None),那时探针永远失败、容器被反复判死
- 声明 qiniu 依赖:media service 在函数体里延迟 import,不声明的话镜像能构建、
  能启动、/docs 也正常,直到第一次 POST /media/upload 才 ModuleNotFoundError
- README 补部署步骤与环境变量表

两条断言进 CI(find_spec("qiniu")、/health 可达)。

Refs 1024XEngineer#115

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I found one security-sensitive CORS configuration edge case.

Verification: git diff --check 00c8d112e06290239db3707d907ffc87c38d5333...7bf9320fe4bcc2c31b284f25ac1e6fe6112f6ce0 passed. I could not run the Python tests or ruff in this container because pytest, fastapi, uv, and ruff are not installed.

Comment thread backend/packages/app/src/windup_app/bootstrap/app.py Outdated
@minorcell minorcell changed the title feat(app): 加可配置的 CORS 中间件,让浏览器能连上后端(Refs #139,从 #115 拆出) feat(app): 加可配置的 CORS 中间件 Aug 6, 2026
Comment thread backend/tests/test_cors.py Outdated
Comment thread backend/tests/test_cors.py Outdated
@minorcell

Copy link
Copy Markdown
Member

@johnnyzhang-eng

另外,注意到你的 agent 生成的内容整体偏口语化、像在聊天,工程感比较弱。
技术说明建议更简洁、结构化,减少叙事感和过多解释,突出决策理由和关键点即可。

- allow_origins=["*"] + allow_credentials=False;鉴权走 Authorization 头不走 cookie
- 删 _cors_origin_regex(),收窄入口保留 WINDUP_CORS_ORIGINS
- 删 backend/tests/test_cors.py

Refs 1024XEngineer#139

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@johnnyzhang-eng

johnnyzhang-eng commented Aug 6, 2026

Copy link
Copy Markdown
Author

ee76330 按评审改完。

  • backend/tests/test_cors.py
  • _cors_origin_regex()app.py docstring 压到一行
  • 开发阶段全放行,WINDUP_CORS_ORIGINS 保留为收窄入口

@fennoai:改为 allow_credentials=False,未采用校验拒绝 *。鉴权走 Authorization 头(frontend/src/shared/api/index.ts:165),前端无 credentials: 'include',后端无 set_cookie

@fennoai

fennoai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Verification Complete

Checked the locked PR range 00c8d11...ee76330 and confirmed the final diff only changes backend/packages/app/src/windup_app/bootstrap/app.py; backend/tests/test_cors.py is no longer present in the PR diff.

What I verified:

  • _cors_origins() now uses WINDUP_CORS_ORIGINS as a comma-separated override and falls back to ['*'].
  • CORSMiddleware is configured with allow_credentials=False, allow_methods=['*'], and allow_headers=['*'].
  • Default preflight returns 200, access-control-allow-origin: *, and no access-control-allow-credentials header.
  • With WINDUP_CORS_ORIGINS=https://windup.example.com, that origin is allowed and an unknown origin gets no allow-origin header.

Gates run in backend:

  • uv run ruff check . passed
  • uv run lint-imports passed: Contracts: 2 kept, 0 broken
  • uv run pytest -q passed: 1 passed

Note: uv was not preinstalled in the sandbox, so I installed it in the sandbox user environment first; no repository files were changed.

View job run

johnnyzhang-eng pushed a commit to johnnyzhang-eng/game-asset-character that referenced this pull request Aug 6, 2026
…l probe

让 main 上的后端能一条命令起在服务器上,并被容器健康检查正确判活。
本分支 stack 在 1024XEngineer#140(CORS)之上——两者都改 create_app(),1024XEngineer#140 合并后本分支 rebase。

- Dockerfile + docker-compose.yml:后端 + Postgres 一套起,uv sync --frozen 装依赖
- .dockerignore:构建上下文此前会带走 161MB 的本地虚拟环境
- GET /health 探针:HEALTHCHECK 原先打 /docs,而生产通常关掉交互文档
  (docs_url=None),那时探针永远失败、容器被反复判死
- 声明 qiniu 依赖:media service 在函数体里延迟 import,不声明的话镜像能构建、
  能启动、/docs 也正常,直到第一次 POST /media/upload 才 ModuleNotFoundError
- README 补部署步骤与环境变量表

两条断言进 CI(find_spec("qiniu")、/health 可达)。

Refs 1024XEngineer#115

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@johnnyzhang-eng

Copy link
Copy Markdown
Author

关闭。#75 已自带 CORS 中间件,合并后 main 即具备该能力,本 PR 只会与之冲突。

#75 那份存在两个问题,改到那边一并处理,不再单开 PR:

  • allow_origin_regex=r"https://.*\.vercel\.app"allow_credentials=True,等于对任意 vercel 应用放行带凭证跨域
  • 默认来源缺 4173(vite preview)

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.

2 participants