Skip to content

Commit f9d8435

Browse files
authored
Revert "KTOR-2872 Add check to prevent anyHost with allowCredentials (#2536)" (#2896)
This reverts commit a82e199. Co-authored-by: hfhbd <hfhbd@users.noreply.github.com>
1 parent 36948cb commit f9d8435

File tree

3 files changed

+56
-23
lines changed
  • ktor-server
    • ktor-server-core/jvm
    • ktor-server-tests/jvm/test/io/ktor/tests/server/features

3 files changed

+56
-23
lines changed

ktor-server/ktor-server-core/jvm/src/io/ktor/features/CORS.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,6 @@ public class CORS(configuration: Configuration) {
9393
}
9494
)
9595

96-
init {
97-
if (configuration.allowCredentials) {
98-
require(!allowsAnyHost) {
99-
"AnyHost * is not allowed in combination with Allow-Credentials, see " +
100-
"https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSNotSupportingCredentials."
101-
}
102-
}
103-
}
104-
10596
/**
10697
* Feature's call interceptor that does all the job. Usually there is no need to install it as it is done during
10798
* feature installation
@@ -116,8 +107,7 @@ public class CORS(configuration: Configuration) {
116107
val origin = call.request.headers.getAll(HttpHeaders.Origin)?.singleOrNull() ?: return
117108

118109
when (checkOrigin(origin, call.request.origin)) {
119-
OriginCheckResult.OK -> {
120-
}
110+
OriginCheckResult.OK -> {}
121111
OriginCheckResult.SkipCORS -> return
122112
OriginCheckResult.Failed -> {
123113
context.respondCorsFailed()
@@ -193,7 +183,7 @@ public class CORS(configuration: Configuration) {
193183
}
194184

195185
private fun ApplicationCall.accessControlAllowOrigin(origin: String) {
196-
if (allowsAnyHost) {
186+
if (allowsAnyHost && !allowCredentials) {
197187
response.header(HttpHeaders.AccessControlAllowOrigin, "*")
198188
} else {
199189
response.header(HttpHeaders.AccessControlAllowOrigin, origin)

ktor-server/ktor-server-core/jvm/test/io/ktor/tests/features/CORSTest.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,6 @@ import kotlin.test.*
1111

1212
class CORSTest {
1313

14-
@Test
15-
fun anyHostWithAllowCredentialsShouldFail() {
16-
val config = CORS.Configuration().apply {
17-
allowCredentials = true
18-
anyHost()
19-
}
20-
assertFailsWith<IllegalArgumentException> {
21-
CORS(config)
22-
}
23-
}
24-
2514
@Test
2615
fun originValidation() {
2716
val feature = CORS(

ktor-server/ktor-server-tests/jvm/test/io/ktor/tests/server/features/CORSTest.kt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class CORSTest {
2020
withTestApplication {
2121
application.install(CORS) {
2222
anyHost()
23+
allowCredentials = true
2324
}
2425

2526
application.routing {
@@ -41,6 +42,7 @@ class CORSTest {
4142
withTestApplication {
4243
application.install(CORS) {
4344
anyHost()
45+
allowCredentials = true
4446
}
4547

4648
application.routing {
@@ -64,6 +66,7 @@ class CORSTest {
6466
withTestApplication {
6567
application.install(CORS) {
6668
anyHost()
69+
allowCredentials = true
6770
}
6871

6972
application.routing {
@@ -303,6 +306,32 @@ class CORSTest {
303306
}
304307
}
305308

309+
@Test
310+
fun testSimpleStarCredentials() {
311+
withTestApplication {
312+
application.install(CORS) {
313+
anyHost()
314+
allowCredentials = true
315+
}
316+
317+
application.routing {
318+
get("/") {
319+
call.respond("OK")
320+
}
321+
}
322+
323+
handleRequest(HttpMethod.Get, "/") {
324+
addHeader(HttpHeaders.Origin, "http://my-host")
325+
}.let { call ->
326+
assertEquals(HttpStatusCode.OK, call.response.status())
327+
assertEquals("http://my-host", call.response.headers[HttpHeaders.AccessControlAllowOrigin])
328+
assertEquals("true", call.response.headers[HttpHeaders.AccessControlAllowCredentials])
329+
assertEquals(HttpHeaders.Origin, call.response.headers[HttpHeaders.Vary])
330+
assertEquals("OK", call.response.content)
331+
}
332+
}
333+
}
334+
306335
@Test
307336
fun testSimpleNull() {
308337
withTestApplication {
@@ -326,6 +355,30 @@ class CORSTest {
326355
}
327356
}
328357

358+
@Test
359+
fun testSimpleNullAllowCredentials() {
360+
withTestApplication {
361+
application.install(CORS) {
362+
anyHost()
363+
allowCredentials = true
364+
}
365+
366+
application.routing {
367+
get("/") {
368+
call.respond("OK")
369+
}
370+
}
371+
372+
handleRequest(HttpMethod.Get, "/") {
373+
addHeader(HttpHeaders.Origin, "null")
374+
}.let { call ->
375+
assertEquals(HttpStatusCode.OK, call.response.status())
376+
assertEquals("null", call.response.headers[HttpHeaders.AccessControlAllowOrigin])
377+
assertEquals("OK", call.response.content)
378+
}
379+
}
380+
}
381+
329382
@Test
330383
fun testSameOriginEnabled() {
331384
withTestApplication {
@@ -397,6 +450,7 @@ class CORSTest {
397450
withTestApplication {
398451
application.install(CORS) {
399452
anyHost()
453+
allowCredentials = true
400454
}
401455

402456
application.routing {

0 commit comments

Comments
 (0)