Skip to content

Commit

Permalink
Fixed incorrect Sec-WebSocket-Origin header for v13, see #9134 (#9312)
Browse files Browse the repository at this point in the history
Motivation:

Based on https://tools.ietf.org/html/rfc6455#section-1.3 - for non-browser
clients, Origin header field may be sent if it makes sense in the context of those clients.

Modification:

Replace Sec-WebSocket-Origin to Origin

Result:

Fixes #9134 .
  • Loading branch information
amizurov authored and normanmaurer committed Jul 12, 2019
1 parent b02ee11 commit be26f4e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
Expand Up @@ -189,7 +189,7 @@ public WebSocketClientHandshaker13(URI webSocketURL, WebSocketVersion version, S
* Upgrade: websocket
* Connection: Upgrade
* Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
* Sec-WebSocket-Origin: http://example.com
* Origin: http://example.com
* Sec-WebSocket-Protocol: chat, superchat
* Sec-WebSocket-Version: 13
* </pre>
Expand Down Expand Up @@ -225,7 +225,7 @@ protected FullHttpRequest newHandshakeRequest() {
.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE)
.set(HttpHeaderNames.SEC_WEBSOCKET_KEY, key)
.set(HttpHeaderNames.HOST, websocketHostValue(wsURL))
.set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, websocketOriginValue(wsURL));
.set(HttpHeaderNames.ORIGIN, websocketOriginValue(wsURL));

String expectedSubprotocol = expectedSubprotocol();
if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) {
Expand All @@ -251,7 +251,7 @@ protected FullHttpRequest newHandshakeRequest() {
*
* @param response
* HTTP response returned from the server for the request sent by beginOpeningHandshake00().
* @throws WebSocketHandshakeException
* @throws WebSocketHandshakeException if handshake response is invalid.
*/
@Override
protected void verify(FullHttpResponse response) {
Expand Down
Expand Up @@ -115,7 +115,7 @@ public WebSocketServerHandshaker13(
* Upgrade: websocket
* Connection: Upgrade
* Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
* Sec-WebSocket-Origin: http://example.com
* Origin: http://example.com
* Sec-WebSocket-Protocol: chat, superchat
* Sec-WebSocket-Version: 13
* </pre>
Expand Down
Expand Up @@ -46,7 +46,7 @@ protected CharSequence[] getHandshakeHeaderNames() {
HttpHeaderNames.CONNECTION,
HttpHeaderNames.SEC_WEBSOCKET_KEY,
HttpHeaderNames.HOST,
HttpHeaderNames.SEC_WEBSOCKET_ORIGIN,
getOriginHeaderName(),
HttpHeaderNames.SEC_WEBSOCKET_VERSION,
};
}
Expand Down
Expand Up @@ -15,16 +15,24 @@
*/
package io.netty.handler.codec.http.websocketx;

import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;

import java.net.URI;

public class WebSocketClientHandshaker13Test extends WebSocketClientHandshaker07Test {

@Override
protected WebSocketClientHandshaker newHandshaker(URI uri, String subprotocol, HttpHeaders headers,
boolean absoluteUpgradeUrl) {
return new WebSocketClientHandshaker13(uri, WebSocketVersion.V13, subprotocol, false, headers,
1024, true, true, 10000,
absoluteUpgradeUrl);
}

@Override
protected CharSequence getOriginHeaderName() {
return HttpHeaderNames.ORIGIN;
}

}
Expand Up @@ -138,7 +138,11 @@ public FullHttpRequest build() {
headers.set(HttpHeaderNames.SEC_WEBSOCKET_KEY, key);
}
if (origin != null) {
headers.set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, origin);
if (version == WebSocketVersion.V13 || version == WebSocketVersion.V00) {
headers.set(HttpHeaderNames.ORIGIN, origin);
} else {
headers.set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, origin);
}
}
if (version != null) {
headers.set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, version.toHttpHeaderValue());
Expand Down

0 comments on commit be26f4e

Please sign in to comment.