-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed handling of CORS within Vert.x (#4232)
- Loading branch information
1 parent
916d9ff
commit 9ee00fe
Showing
7 changed files
with
149 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
examples/src/main/scala/sttp/tapir/examples/security/corsInterceptorVertxServer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// {cat=Security; effects=Future; server=Vert.x}: CORS interceptor | ||
|
||
//> using dep com.softwaremill.sttp.tapir::tapir-vertx-server:1.11.11 | ||
//> using dep com.softwaremill.sttp.client3::core:3.10.2 | ||
|
||
package sttp.tapir.examples.security | ||
|
||
import io.vertx.core.Vertx | ||
import io.vertx.ext.web.* | ||
import sttp.client3.* | ||
import sttp.model.headers.Origin | ||
import sttp.model.{Header, HeaderNames, Method, StatusCode} | ||
import sttp.tapir.* | ||
import sttp.tapir.server.interceptor.cors.{CORSConfig, CORSInterceptor} | ||
import sttp.tapir.server.vertx.VertxFutureServerInterpreter.* | ||
import sttp.tapir.server.vertx.{VertxFutureServerInterpreter, VertxFutureServerOptions} | ||
|
||
import scala.concurrent.duration.* | ||
import scala.concurrent.{Await, ExecutionContext, Future} | ||
|
||
@main def corsInterceptorVertxServer() = | ||
given ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global | ||
val vertx = Vertx.vertx() | ||
|
||
val server = vertx.createHttpServer() | ||
val router = Router.router(vertx) | ||
|
||
val myEndpoint = endpoint.get | ||
.in("path") | ||
.out(plainBody[String]) | ||
.serverLogic(_ => Future(Right("OK"))) | ||
|
||
val corsInterceptor = VertxFutureServerOptions.customiseInterceptors | ||
.corsInterceptor( | ||
CORSInterceptor.customOrThrow( | ||
CORSConfig.default | ||
.allowOrigin(Origin.Host("http", "my.origin")) | ||
.allowMethods(Method.GET) | ||
) | ||
) | ||
.options | ||
|
||
val attach = VertxFutureServerInterpreter(corsInterceptor).route(myEndpoint) | ||
attach(router) | ||
|
||
// starting the server | ||
val bindAndCheck = server.requestHandler(router).listen(9000).asScala.map { binding => | ||
val backend = HttpClientSyncBackend() | ||
|
||
// Sending preflight request with allowed origin | ||
val preflightResponse = basicRequest | ||
.options(uri"http://localhost:9000/path") | ||
.headers( | ||
Header.origin(Origin.Host("http", "my.origin")), | ||
Header.accessControlRequestMethod(Method.GET) | ||
) | ||
.send(backend) | ||
|
||
assert(preflightResponse.code == StatusCode.NoContent) | ||
assert(preflightResponse.headers.contains(Header.accessControlAllowOrigin("http://my.origin"))) | ||
assert(preflightResponse.headers.contains(Header.accessControlAllowMethods(Method.GET))) | ||
|
||
println("Got expected response for preflight request") | ||
|
||
// Sending preflight request with disallowed origin | ||
val preflightResponseForDisallowedOrigin = basicRequest | ||
.options(uri"http://localhost:9000/path") | ||
.headers( | ||
Header.origin(Origin.Host("http", "disallowed.com")), | ||
Header.accessControlRequestMethod(Method.GET) | ||
) | ||
.send(backend) | ||
|
||
// Check response does not contain allowed origin header | ||
assert(preflightResponseForDisallowedOrigin.code == StatusCode.NoContent) | ||
assert(!preflightResponseForDisallowedOrigin.headers.contains(Header.accessControlAllowOrigin("http://example.com"))) | ||
|
||
println("Got expected response for preflight request for wrong origin. No allowed origin header in response") | ||
|
||
// Sending regular request from allowed origin | ||
val requestResponse = basicRequest | ||
.response(asStringAlways) | ||
.get(uri"http://localhost:9000/path") | ||
.headers(Header.origin(Origin.Host("http", "my.origin"))) | ||
.send(backend) | ||
|
||
assert(requestResponse.code == StatusCode.Ok) | ||
assert(requestResponse.body == "OK") | ||
assert(requestResponse.headers.contains(Header.vary(HeaderNames.Origin))) | ||
assert(requestResponse.headers.contains(Header.accessControlAllowOrigin("http://my.origin"))) | ||
|
||
println("Got expected response for regular request") | ||
|
||
binding | ||
} | ||
|
||
Await.result(bindAndCheck.flatMap(_.close().asScala), 1.minute) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...-server/src/main/scala/sttp/tapir/server/vertx/interpreters/CommonServerInterpreter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters