Fix CORS issues#5696
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR enhances CORS preflight handling and origin port parsing. It adds ApplicationRequest.isCorsPreflightRequest(), a findPortDigitStartIndex() helper used by isValidOrigin/normalizeOrigin (improving IPv6-literal port handling), treats same-origin preflight OPTIONS as handled by CORS, updates KDoc, and adds tests for same-origin preflight and IPv6 origins. ChangesCORS preflight and origin validation
🎯 3 (Moderate) | ⏱️ ~20 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
ktor-server/ktor-server-tests/common/test/io/ktor/tests/server/plugins/CORSTest.kt (1)
999-1000: ⚡ Quick winUse the repository’s backtick test naming style for the new case.
Please rename this new test to a descriptive backtick form so it matches the test guideline used for new Kotlin tests.
Suggested rename
- fun ipv6LiteralOriginIsAccepted() = testApplication { + fun `accepts bracketed IPv6 origins and rejects malformed ones`() = testApplication {As per coding guidelines, "
**/*Test.{kt,kts}: Prefer descriptive test names in backticks:describe what is being tested."🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@ktor-server/ktor-server-tests/common/test/io/ktor/tests/server/plugins/CORSTest.kt` around lines 999 - 1000, Rename the test function ipv6LiteralOriginIsAccepted to the repository’s backtick descriptive style (e.g. `ipv6 literal origin is accepted`) by changing the Kotlin function declaration fun ipv6LiteralOriginIsAccepted() to a backticked name (fun `ipv6 literal origin is accepted`()) while keeping the `@Test` annotation and the testApplication body unchanged; locate the function ipv6LiteralOriginIsAccepted in CORSTest.kt and update only its identifier to the backtick form to match the test naming guideline.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@ktor-server/ktor-server-plugins/ktor-server-cors/common/src/io/ktor/server/plugins/cors/CORSConfig.kt`:
- Around line 153-155: Update the KDoc in CORSConfig.kt to stop implying
preflight requests always succeed; state that same-origin CORS preflight OPTIONS
requests with Access-Control-Request-Method are still handled/evaluated by the
CORS plugin (not skipped) and may be rejected (e.g., respondPreflight() can
return 403 when the requested method/headers are disallowed). Mention the CORS
plugin's evaluation behavior instead of saying they "receive a successful
response" and keep the language consistent with respondPreflight() and the
plugin's enforcement logic.
In
`@ktor-server/ktor-server-plugins/ktor-server-cors/common/src/io/ktor/server/plugins/cors/CORSUtils.kt`:
- Around line 138-156: The logic in findPortDigitStartIndex has the IPv6 and
non-IPv6 branches swapped; when isIpv6 is true you should look for the closing
']' and then a ':' after that (use ipv6LiteralEndIndex and portSeparatorIndex to
compute port start or origin.length), and when isIpv6 is false you should scan
from hostStartIndex for the first ':' (port), '/' (end of host), or '?'
(malformed) and return the appropriate index or -1; update the branches in
findPortDigitStartIndex accordingly so bracketed IPv6 literals are handled by
the ']' lookup and plain hosts are handled by character scanning.
---
Nitpick comments:
In
`@ktor-server/ktor-server-tests/common/test/io/ktor/tests/server/plugins/CORSTest.kt`:
- Around line 999-1000: Rename the test function ipv6LiteralOriginIsAccepted to
the repository’s backtick descriptive style (e.g. `ipv6 literal origin is
accepted`) by changing the Kotlin function declaration fun
ipv6LiteralOriginIsAccepted() to a backticked name (fun `ipv6 literal origin is
accepted`()) while keeping the `@Test` annotation and the testApplication body
unchanged; locate the function ipv6LiteralOriginIsAccepted in CORSTest.kt and
update only its identifier to the backtick form to match the test naming
guideline.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: e2a69531-4218-4c6f-a3c0-4c838428fac4
📒 Files selected for processing (5)
ktor-server/ktor-server-plugins/ktor-server-cors/common/src/io/ktor/server/plugins/cors/CORS.ktktor-server/ktor-server-plugins/ktor-server-cors/common/src/io/ktor/server/plugins/cors/CORSConfig.ktktor-server/ktor-server-plugins/ktor-server-cors/common/src/io/ktor/server/plugins/cors/CORSUtils.ktktor-server/ktor-server-plugins/ktor-server-cors/common/src/io/ktor/server/plugins/cors/routing/CORS.ktktor-server/ktor-server-tests/common/test/io/ktor/tests/server/plugins/CORSTest.kt
3a86540 to
7fe674e
Compare
7fe674e to
9523c52
Compare
Subsystem
Server CORS
Motivation
KTOR-9659 CORS is skipped when the Origin header contains an IPv6 address
KTOR-9636 CORS plugin drops OPTIONS preflight requests when allowSameOrigin is on
Solution