-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Websocket frame validator #1825
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2b4ea20
websocket plugin to filter content
Zwiterrion 01c527d
start writing websocket plugins
Zwiterrion f26bd7d
apply reject strategy
Zwiterrion 6ec8398
add WebsocketSizeValidator plugin
Zwiterrion 52178bd
add Websocket event
Zwiterrion 9ba1cc3
akka websocket not working
Zwiterrion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,22 +6,22 @@ import akka.stream.Materializer | |
import akka.stream.scaladsl.{Flow, Source} | ||
import akka.util.ByteString | ||
import com.github.blemale.scaffeine.{Cache, Scaffeine} | ||
import otoroshi.auth.{AuthModule, BasicAuthModule, BasicAuthModuleConfig, SessionCookieValues} | ||
import otoroshi.env.Env | ||
import otoroshi.models.{ApiKey, BackOfficeUser, GlobalConfig, PrivateAppsUser, ServiceDescriptor} | ||
import otoroshi.gateway.Errors | ||
import otoroshi.models.{ApiKey, PrivateAppsUser, Target} | ||
import otoroshi.next.models.{NgMatchedRoute, NgPluginInstance, NgRoute, NgTarget} | ||
import otoroshi.next.plugins.RejectStrategy | ||
import otoroshi.next.proxy.{NgExecutionReport, NgProxyEngineError, NgReportPluginSequence, NgReportPluginSequenceItem} | ||
import otoroshi.next.utils.JsonHelpers | ||
import otoroshi.script.{InternalEventListener, NamedPlugin, PluginType, StartableAndStoppable} | ||
import otoroshi.security.IdGenerator | ||
import otoroshi.utils.TypedMap | ||
import otoroshi.utils.http.WSCookieWithSameSite | ||
import otoroshi.utils.syntax.implicits._ | ||
import play.api.http.HttpEntity | ||
import play.api.http.websocket.Message | ||
import play.api.http.websocket.{CloseMessage, Message, PingMessage, PongMessage, BinaryMessage => PlayWSBinaryMessage, TextMessage => PlayWSTextMessage} | ||
import play.api.libs.json._ | ||
import play.api.libs.ws.{WSCookie, WSResponse} | ||
import play.api.mvc.{AnyContent, Cookie, Request, RequestHeader, Result, Results} | ||
import play.api.mvc.{Cookie, RequestHeader, Result, Results} | ||
|
||
import java.security.cert.X509Certificate | ||
import scala.concurrent.duration.DurationInt | ||
|
@@ -216,6 +216,7 @@ object NgPluginCategory { | |
case object Wasm extends NgPluginCategory { def name: String = "Wasm" } | ||
case object Classic extends NgPluginCategory { def name: String = "Classic" } | ||
case object ServiceDiscovery extends NgPluginCategory { def name: String = "ServiceDiscovery" } | ||
case object Websocket extends NgPluginCategory { def name: String = "Websocket" } | ||
|
||
val all = Seq( | ||
Classic, | ||
|
@@ -233,7 +234,8 @@ object NgPluginCategory { | |
TrafficControl, | ||
Transformations, | ||
Tunnel, | ||
Wasm | ||
Wasm, | ||
Websocket | ||
) | ||
} | ||
|
||
|
@@ -1230,3 +1232,119 @@ class NgMergedAccessValidator(plugins: Seq[NgPluginWrapper.NgSimplePluginWrapper | |
next(plugins, plugins.size) | ||
} | ||
} | ||
|
||
case class NgWebsocketPluginContext( | ||
config: JsValue, | ||
idx: Int = 0, | ||
request: RequestHeader, | ||
route: NgRoute, | ||
attrs: TypedMap, | ||
target: Target | ||
) extends NgCachedConfigContext { | ||
def json: JsValue = Json.obj( | ||
"config" -> config | ||
) | ||
} | ||
|
||
sealed trait WebsocketMessage[A] { | ||
def data: A | ||
def str()(implicit m: Materializer, ec: ExecutionContext): Future[String] | ||
def size()(implicit m: Materializer, ec: ExecutionContext): Future[Int] | ||
def isBinary: Boolean | ||
def isText: Boolean = !isBinary | ||
} | ||
|
||
object WebsocketMessage { | ||
case class AkkaMessage(override val data: akka.http.scaladsl.model.ws.Message) extends WebsocketMessage[akka.http.scaladsl.model.ws.Message] { | ||
override def str()(implicit m: Materializer, ec: ExecutionContext): Future[String] = data match { | ||
case akka.http.scaladsl.model.ws.TextMessage.Strict(text) => text.future | ||
case akka.http.scaladsl.model.ws.TextMessage.Streamed(source) => | ||
source.runFold("")((concat, str) => concat + str) | ||
case akka.http.scaladsl.model.ws.BinaryMessage.Strict(data) => data.utf8String.future | ||
case akka.http.scaladsl.model.ws.BinaryMessage.Streamed(source) => | ||
source | ||
.runFold(ByteString.empty)((concat, str) => concat ++ str).map(_.utf8String) | ||
case _ => "".future | ||
} | ||
|
||
override def size()(implicit m: Materializer, ec: ExecutionContext): Future[Int] = data match { | ||
case akka.http.scaladsl.model.ws.TextMessage.Strict(text) => text.length.future | ||
case akka.http.scaladsl.model.ws.TextMessage.Streamed(source) => | ||
source.runFold("")((concat, str) => concat + str).map(_.length) | ||
case akka.http.scaladsl.model.ws.BinaryMessage.Strict(data) => data.size.future | ||
case akka.http.scaladsl.model.ws.BinaryMessage.Streamed(source) => | ||
source | ||
.runFold(ByteString.empty)((concat, str) => concat ++ str).map(_.size) | ||
case _ => 0.future | ||
} | ||
|
||
override def isBinary: Boolean = !data.isText | ||
} | ||
case class PlayMessage(override val data: play.api.http.websocket.Message) extends WebsocketMessage[play.api.http.websocket.Message] { | ||
override def str()(implicit m: Materializer, ec: ExecutionContext): Future[String] = (data match { | ||
case PlayWSTextMessage(data) => data | ||
case PlayWSBinaryMessage(data) => data.utf8String | ||
case CloseMessage(_, _) => "" | ||
case PingMessage(data) => data.utf8String | ||
case PongMessage(data) => data.utf8String | ||
}).future | ||
|
||
override def size()(implicit m: Materializer, ec: ExecutionContext): Future[Int] = (data match { | ||
case PlayWSTextMessage(data) => data.length | ||
case PlayWSBinaryMessage(data) => data.size | ||
case CloseMessage(_, _) => 0 | ||
case PingMessage(data) => data.size | ||
case PongMessage(data) => data.size | ||
}).future | ||
|
||
override def isBinary: Boolean = data.isInstanceOf[play.api.http.websocket.BinaryMessage] | ||
} | ||
} | ||
|
||
case class NgWebsocketResponse( | ||
result: NgAccess = NgAccess.NgAllowed, | ||
statusCode: Option[Int] = None, | ||
reason: Option[String] = None) | ||
|
||
object NgWebsocketResponse { | ||
def default: Future[NgWebsocketResponse] = NgWebsocketResponse().future | ||
def error[A](ctx: NgWebsocketPluginContext, | ||
message: WebsocketMessage[A], | ||
statusCode: Int, | ||
reason: String)(implicit env: Env, ec: ExecutionContext): Future[NgWebsocketResponse] = { | ||
implicit val m: Materializer = env.otoroshiMaterializer | ||
(for { | ||
frame <- message.str | ||
size <- message.size() | ||
} yield (frame, size)) | ||
.collect { | ||
case (frame, frameSize) => | ||
NgWebsocketResponse.denied(Errors | ||
.craftWebsocketResponseResultSync( | ||
frame = frame, | ||
frameSize = frameSize, | ||
statusCode = statusCode.some, | ||
reason = reason.some, | ||
req = ctx.request, | ||
route = ctx.route, | ||
target = ctx.target | ||
), statusCode, reason) | ||
} | ||
} | ||
private def denied(result: Result, statusCode: Int, reason: String) = NgWebsocketResponse(NgAccess.NgDenied(result), statusCode.some, reason.some) | ||
} | ||
|
||
trait NgWebsocketPlugin extends NgNamedPlugin { | ||
def onRequestFlow: Boolean = false | ||
def onResponseFlow: Boolean = false | ||
|
||
def canAccess[A](ctx: NgWebsocketPluginContext, message: WebsocketMessage[A]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe move this one in the validator plugin |
||
(implicit env: Env, ec: ExecutionContext): Future[NgWebsocketResponse] = NgWebsocketResponse.default | ||
|
||
def canAccessResponse[A](ctx: NgWebsocketPluginContext, message: WebsocketMessage[A]) | ||
(implicit env: Env, ec: ExecutionContext): Future[NgWebsocketResponse] = NgWebsocketResponse.default | ||
} | ||
|
||
trait NgWebsocketValidatorPlugin extends NgWebsocketPlugin { | ||
def rejectStrategy(ctx: NgWebsocketPluginContext): RejectStrategy | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add a transformer plugin |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe a bytes() method ?