Skip to content

fix(middleware): reflect matched request origin in CORS responses - #3245

Open
Twiink wants to merge 1 commit into
labstack:masterfrom
Twiink:fix/cors-origin-casing-20261122
Open

Twiink wants to merge 1 commit into
labstack:masterfrom
Twiink:fix/cors-origin-casing-20261122

Conversation

@Twiink

@Twiink Twiink commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

A configuration such as AllowOrigins: []string{"https://EXAMPLE.com"} accepts the browser origin https://example.com via strings.EqualFold, but sends the configured spelling back in Access-Control-Allow-Origin. The browser then rejects both normal and preflight responses: Fetch §4.10 requires the header to equal the byte-serialized request origin.

Return the matched request origin from the default matcher so the accepted origin also passes that browser check.

Validation:

  • New GET and OPTIONS regressions fail on the original source.
  • go test -race -count=1 ./... passes with proxy variables removed for loopback tests.
  • go vet ./..., gofmt and git diff --check pass.

Summary by CodeRabbit

  • Bug Fixes
    • CORS responses now reflect the origin value sent with a request when it matches an allowed origin, even if its capitalization differs from the configured value. This keeps the response origin consistent with the request and supports credentialed requests, including preflight requests, with case-insensitive origin matching.

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: CORS Origin Reflection

Layer / File(s) Summary
Origin matching and response headers
middleware/cors.go, middleware/cors_origin_case_test.go
The origin matcher returns the request origin on a case-insensitive match. The test checks reflected origin and credentials headers for GET and OPTIONS requests.

Suggested reviewers: aldas

Change: Bug fix

Merge Risk: Minimal · up to 3915a

CORS responses now return the request's origin spelling when a configured origin matches case-insensitively, which aligns with the Fetch specification. The change is small, covered by new GET and OPTIONS tests, and easy to revert, so merge-readiness risk is minimal.

Security architecture risk: Minimal · up to 3915a

The middleware now returns the request’s origin only after the existing allowlist match succeeds. The inspected change restores access for intended case-insensitive matches without weakening the matching rule or credential safeguards. No introduced security concern was established.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed behavior affects routes using the default matcher when a request origin matches a configured entry but differs in spelling. Wildcard and custom matchers retain their separate behavior. Actual applications, tenants, and protected resources using these configurations were not identified.

Security Findings and Attack Paths

  • observed — Request-controlled origin reflection remains conditional on the existing allowlist predicate. A nonmatching origin still returns allowed=false, and this middleware does not execute its allow-origin or credentials header assignments on that path.

Trust Boundaries and Controls

  • observed — CORS rejection remains a browser response-access control rather than application authentication: disallowed preflights terminate without the relevant CORS headers, while disallowed non-preflight requests continue to the next handler. This behavior predates and is unchanged by the PR.

Resilience and Maintainability Implications

  • observed — The reflected value is request-local rather than written back into the configured allowlist. The middleware retains its configuration copy and Vary: Origin handling, preserving the existing request-isolation and cache-variation mechanisms.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 770658ec-17a7-4182-b700-abe51a19733c
Commits

Reviewing files that changed from the base of the PR and between 59aacdd and 3915a70.

Files selected for processing: 2

  • middleware/cors.go
  • middleware/cors_origin_case_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.23%. Comparing base (dcb05f0) to head (3915a70).
⚠️ Report is 64 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3245      +/-   ##
==========================================
+ Coverage   93.34%   96.23%   +2.88%     
==========================================
  Files          43       46       +3     
  Lines        4735     4430     -305     
==========================================
- Hits         4420     4263     -157     
+ Misses        192      167      -25     
+ Partials      123        0     -123     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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