Skip to content

Conversation

@Tuntii
Copy link
Owner

@Tuntii Tuntii commented Jan 17, 2026

Motivation

  • The permissive CORS configuration that uses "*" for allowed headers should reflect the browser's requested headers on preflight so the response matches what the client expects.
  • Previously the preflight response always wrote Access-Control-Allow-Headers: *, which can cause issues when the browser sends specific Access-Control-Request-Headers that must be echoed back.

Description

  • When any configured header value is "*", the middleware now reads the incoming Access-Control-Request-Headers header and uses that value for Access-Control-Allow-Headers if present, otherwise falls back to "*".
  • The code introduces a local allow_headers computed value and uses it when building the preflight response instead of the static header string.
  • No other behavior changes were made for non-wildcard header lists or other CORS response headers.

Testing

  • No automated tests were run for this change.

Codex Task

Copilot AI review requested due to automatic review settings January 17, 2026 21:11
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes CORS preflight handling to properly echo back requested headers when a wildcard configuration is used, rather than always returning "*" in the Access-Control-Allow-Headers response header.

Changes:

  • Modified the CORS middleware to detect wildcard header configuration and echo back the browser's Access-Control-Request-Headers value in preflight responses
  • Computed allow_headers dynamically based on whether wildcards are configured and what headers the client requests
  • Maintained backward compatibility for non-wildcard configurations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 198 to 207
let allow_headers = if self.headers.iter().any(|value| value == "*") {
req.headers()
.get(header::ACCESS_CONTROL_REQUEST_HEADERS)
.and_then(|value| value.to_str().ok())
.filter(|value| !value.trim().is_empty())
.map(str::to_string)
.unwrap_or_else(|| "*".to_string())
} else {
self.headers_header_value()
};
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

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

The allow_headers variable is computed for all requests (lines 198-207), but it's only used during preflight requests (line 268). This adds unnecessary overhead for normal requests.

Consider moving this computation inside the if is_preflight block (after line 236) so it only executes when needed.

Copilot uses AI. Check for mistakes.
Comment on lines 198 to 207
let allow_headers = if self.headers.iter().any(|value| value == "*") {
req.headers()
.get(header::ACCESS_CONTROL_REQUEST_HEADERS)
.and_then(|value| value.to_str().ok())
.filter(|value| !value.trim().is_empty())
.map(str::to_string)
.unwrap_or_else(|| "*".to_string())
} else {
self.headers_header_value()
};
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

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

This CORS functionality lacks test coverage. The change modifies critical CORS preflight behavior but includes no tests to verify the new wildcard header echoing logic works correctly or that it doesn't break existing functionality.

Many other modules in this crate (e.g., jwt, rate_limit, api_key) have comprehensive test coverage. Consider adding tests that verify:

  • Wildcard headers echo the request headers correctly
  • Empty or whitespace-only request headers fall back to "*"
  • Non-wildcard configurations still work as before
  • Edge cases like missing Access-Control-Request-Headers are handled properly

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Tuntii Tuntii merged commit 714082c into main Jan 18, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants