refactor(cors): warn when credentials is used with wildcard options#1323
Conversation
📝 WalkthroughWalkthroughAdds a runtime console warning in resolveCorsOptions when Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/utils/internal/cors.ts`:
- Around line 41-59: The warning inside resolveCorsOptions fires on every
request because handleCors calls it per-request; change it to emit once per
unique misconfiguration by deduplicating warnings at module scope (e.g., add a
module-level WeakSet for seen options objects and/or a Set<string> for
serialized keys) and check that set before calling console.warn; specifically,
in resolveCorsOptions check resolved.credentials and compute a dedupe key from
options.origin, options.methods, options.allowHeaders, options.exposeHeaders (or
use the options object identity in a WeakSet) and only log if that key/object
hasn’t been seen, then add it to the dedupe set so subsequent calls skip the
warning.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: f8a39dbc-6cbb-4c66-a9e8-11bb34298977
📒 Files selected for processing (1)
src/utils/internal/cors.ts
The CORS spec prohibits using Access-Control-Allow-Origin: * (and other wildcard values) when Access-Control-Allow-Credentials: true is set. Browsers silently reject such responses, which is confusing to debug. Now emits a console.warn when resolveCorsOptions detects credentials: true combined with wildcard values for origin, methods, allowHeaders, or exposeHeaders — matching the existing JSDoc documentation on CorsOptions. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Use a module-level WeakSet to track options objects that have already been warned about, so the warning only fires once per unique CorsOptions object instead of on every request. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Address maintainer review: - Remove global WeakSet state (unacceptable) - Remove temporary array allocation (runtime performance) - Use a single compact if condition checking only origin wildcard with credentials (the only combination browsers actually reject) - Add regression tests that fail without the warning and pass with it Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
d7d77f3 to
774022a
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/utils/internal/cors.ts`:
- Around line 41-45: The warning in resolveCorsOptions is emitted on every
request; change behavior by moving validation out of the per-request path and
into a startup-time check or opt-in deduplication: add an optional warnOnce
boolean to CorsOptions and update resolveCorsOptions to only log the warning
when warnOnce is true (and set/track it in the local function scope, not
module-global), or better, extract the validation logic into a new
validateCorsOptions(options: CorsOptions) helper that callers (e.g., startup
code that currently calls resolveCorsOptions) can invoke once; ensure
validateCorsOptions (or the new warnOnce behavior) checks wildcard usage for all
four fields mentioned (origin, methods, allowHeaders, exposeHeaders) and update
handleCors and resolveCorsOptions usages so they no longer emit the warning
per-request; also update docs/comments to state resolveCorsOptions is expected
to be used at startup when possible.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: c08031f2-0e51-4d3a-af6a-0743583f22af
📒 Files selected for processing (2)
src/utils/internal/cors.tstest/unit/cors.test.ts
Summary
Test plan
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Tests