Add a typed, explicit CORS policy evaluator

未关闭
#201 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
冷清
领域
api, backend

调研方向

先从所需的 contract 和链接的 WHATWG Fetch Standard CORS 章节开始,然后定位 repository 中的 Roc request/response 类型以及 real-listener 测试设置。在实现之前定义 policy 和 result 的边界。完成标准是:通过 real-listener 测试覆盖实际情况和 preflight 情况、normalization、wildcard/credential、Vary、disabled-by-default 以及缓存变体。

由索引模型根据 Issue 内容生成。

描述

Summary

Add a pure Cors module that evaluates actual and preflight requests against an immutable policy.

Illustrative flow:

match Cors.evaluate(policy, request) {
    NotCors => handle(request)
    Preflight(response) => Ok(Server.respond(response))
    Rejected(response) => Ok(Server.respond(response))
    Allowed(cors_headers) => {
        response = handle(request)?
        Ok(Server.respond(Cors.apply(cors_headers, response)?))
    }
}

Why this belongs in the platform

CORS is widely supplied by web-framework middleware, including tower-http. Its header interactions are sufficiently subtle that applications should not independently reproduce them.

It should remain an explicit Roc request/response transformation. It does not require a new middleware runtime or host callback.

Required contract

  • CORS is disabled unless an application constructs and invokes a policy.
  • Exact serialized origins—scheme, host, and port—are the initial allowlist mechanism.
  • Treat the special null origin as denied unless explicitly allowed.
  • Validate requested methods and header names for preflight requests.
  • Support configured exposed headers, credential permission, and bounded preflight max age.
  • Reject policy construction that combines credentialed requests with wildcard origin.
  • Dynamic origin responses merge Vary: Origin case-insensitively without discarding existing Vary values.
  • Access-Control-Allow-Credentials must serialize as lowercase true.
  • Preflight responses do not depend on receiving credentials; browsers do not attach credentials to the preflight itself.
  • Malformed or conflicting Origin fields produce a deterministic denial.
  • The result distinguishes non-CORS, allowed actual, successful preflight, and rejected requests.

Documentation must emphasize that CORS is not CSRF protection. A "simple" cross-origin request can still perform a server-side action even when the browser refuses to expose its response.

Non-goals

  • Regex or suffix origin matching in the first version
  • Guessing trusted origins from Host or forwarded headers
  • CSRF protection
  • Authentication or authorization
  • Applying CORS automatically to native file or operational routes
  • A generic middleware stack

Acceptance criteria

  • Actual and preflight requests are handled from the same validated policy.
  • Wildcard/credentials and Vary behavior follow the Fetch Standard.
  • Origin, method, and header matching have explicit case and normalization rules.
  • No CORS behavior is enabled by default.
  • Real-listener tests cover allowed, denied, credentialed, and cached variants.

References

主要语言
HTML
星标
110
派生
25
平均合并
18 小时 28 分钟
30 天内合并 PR
16

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

roc-lang/basic-webserver 的其他 Issue

查看 roc-lang/basic-webserver 的全部 Issue

相似的 Issue

更多 Backend & API Design Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。