Skip to content
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

Add validation for server opening handshake response in Ember WebSocket client #7138

Merged

Conversation

danghieutrung
Copy link
Contributor

This is part of the Ember WebSocket client implementation. This validates the opening handshake response from the server.
http://rfc-editor.org/rfc/rfc6455#page-6

@armanbilge armanbilge changed the base branch from series/0.23 to ember-wsclient June 7, 2023 17:06
@armanbilge
Copy link
Member

Thanks! I retargeted to a new ember-wsclient branch. You can continue making small PRs to that branch and when it's ready we can merge into into series/0.23.

Comment on lines 35 to 41
/** Validate the opening handshake response from the server
* https://datatracker.ietf.org/doc/html/rfc6455#page-6
*/
def validateServerHandshake[F[_]](
response: Response[F],
secWebSocketKey: String,
)(implicit F: MonadThrow[F]): F[Boolean] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Can we add a WebSocketHelpersSuite that validates a successful response, and also one response for each of the different failure kinds?

@armanbilge armanbilge marked this pull request as draft June 10, 2023 14:03
@armanbilge armanbilge marked this pull request as ready for review June 12, 2023 15:37
Copy link
Member

@armanbilge armanbilge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this all looks great! Just a couple optimization ideas.


private[client] object WebSocketHelpers {

private[internal] val supportedWebSocketVersion = 13L
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding final makes it a constant that will be inlined, because the right-hand-side is known at compile-time.

Suggested change
private[internal] val supportedWebSocketVersion = 13L
private[internal] final val supportedWebSocketVersion = 13L

Comment on lines +65 to +89
private def serverHandshake[F[_]](res: Response[F]): Either[ServerHandshakeError, ByteVector] = {
val status = res.status match {
case Status.SwitchingProtocols => Either.unit
case _ => Left(InvalidStatus)
}

val connection = res.headers.get[Connection] match {
case Some(header) if header.hasUpgrade => Either.unit
case _ => Left(UpgradeRequired)
}

val upgrade = res.headers.get[Upgrade] match {
case Some(header) if header.values.contains_(webSocketProtocol) => Either.unit
case _ => Left(UpgradeRequired)
}

val secWebSocketAcceptKey = res.headers.get[`Sec-WebSocket-Accept`] match {
case Some(header) => Right(header.hashedKey)
case None => Left(SecWebSocketAcceptNotFound)
}

(status, connection, upgrade, secWebSocketAcceptKey).mapN {
case (_, _, _, secWebSocketAcceptKey) =>
secWebSocketAcceptKey
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way each check evaluates lazily and will short-circuit immediately if there is an error.

Suggested change
private def serverHandshake[F[_]](res: Response[F]): Either[ServerHandshakeError, ByteVector] = {
val status = res.status match {
case Status.SwitchingProtocols => Either.unit
case _ => Left(InvalidStatus)
}
val connection = res.headers.get[Connection] match {
case Some(header) if header.hasUpgrade => Either.unit
case _ => Left(UpgradeRequired)
}
val upgrade = res.headers.get[Upgrade] match {
case Some(header) if header.values.contains_(webSocketProtocol) => Either.unit
case _ => Left(UpgradeRequired)
}
val secWebSocketAcceptKey = res.headers.get[`Sec-WebSocket-Accept`] match {
case Some(header) => Right(header.hashedKey)
case None => Left(SecWebSocketAcceptNotFound)
}
(status, connection, upgrade, secWebSocketAcceptKey).mapN {
case (_, _, _, secWebSocketAcceptKey) =>
secWebSocketAcceptKey
}
private def serverHandshake[F[_]](res: Response[F]): Either[ServerHandshakeError, ByteVector] = {
def status = res.status match {
case Status.SwitchingProtocols => Either.unit
case _ => Left(InvalidStatus)
}
def connection = res.headers.get[Connection] match {
case Some(header) if header.hasUpgrade => Either.unit
case _ => Left(UpgradeRequired)
}
def upgrade = res.headers.get[Upgrade] match {
case Some(header) if header.values.contains_(webSocketProtocol) => Either.unit
case _ => Left(UpgradeRequired)
}
def secWebSocketAcceptKey = res.headers.get[`Sec-WebSocket-Accept`] match {
case Some(header) => Right(header.hashedKey)
case None => Left(SecWebSocketAcceptNotFound)
}
status >> connection >> upgrade >> secWebSocketAcceptKey

Copy link
Member

@ChristopherDavenport ChristopherDavenport left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fantastic! Glad to see the effort you are putting in!

@armanbilge
Copy link
Member

I'll merge, we can do optimizations later 😁 correctness first!

@armanbilge armanbilge merged commit d4505ad into http4s:ember-wsclient Jun 16, 2023
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants