use SecureRandom for web socket mask keys - #9624
Closed
basavaraj-sm05 wants to merge 1 commit into
Closed
Conversation
Collaborator
|
See #6594 |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
OkHttp masks every outbound web socket frame with a four-byte key taken from the java.util.Random that newWebSocket creates for each connection, and the Sec-WebSocket-Key nonce comes from that same instance. RFC 6455 section 5.3 asks for the mask to come from a strong source of entropy and says the key for one frame must not make it simple for a server or proxy to predict the key for a later one, which is the whole point of masking: it keeps a party who can influence message content from choosing the bytes an intermediary sees on a ws:// stream. java.util.Random is a 48-bit linear congruential generator, so the first mask a peer observes hands it the top 32 bits of state and the remaining 16 fall to a 65,536 candidate search that the next frame confirms. I drove WebSocketWriter with a plain Random to check, and after two frames it predicts the rest of the mask stream exactly, enough to read a later frame plaintext straight off the wire, so the property the spec rules out is the one we currently have. What made me look was that MockWebServer already builds its RealWebSocket with SecureRandom even though a server never masks, while the client that does mask does not. SecureRandom is a java.util.Random subclass so nothing downstream changes, and the writer tests that want deterministic masks pass their own Random in.