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

Issue #6407 - Fix URI validation for WebSocket ClientUpgradeRequest #6411

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -88,25 +88,17 @@ public CoreClientUpgradeRequest(WebSocketCoreClient webSocketClient, URI request

// Validate websocket URI
if (!requestURI.isAbsolute())
{
throw new IllegalArgumentException("WebSocket URI must be absolute");
}

if (StringUtil.isBlank(requestURI.getScheme()))
{
throw new IllegalArgumentException("WebSocket URI must include a scheme");
}

String scheme = requestURI.getScheme();
if (!HttpScheme.WS.is(scheme) && !HttpScheme.WSS.is(scheme))
{
throw new IllegalArgumentException("WebSocket URI scheme only supports [ws] and [wss], not [" + scheme + "]");
}

if (requestURI.getHost() == null)
{
throw new IllegalArgumentException("Invalid WebSocket URI: host not present");
}

this.wsClient = webSocketClient;
this.futureCoreSession = new CompletableFuture<>();
Expand Down Expand Up @@ -437,7 +429,7 @@ else if (values.length == 1)
Negotiated negotiated = new Negotiated(
request.getURI(),
negotiatedSubProtocol,
HttpScheme.HTTPS.is(request.getScheme()), // TODO better than this?
HttpClient.isSchemeSecure(request.getScheme()),
extensionStack,
WebSocketConstants.SPEC_VERSION_STRING);

Expand Down
Expand Up @@ -22,6 +22,7 @@
import java.util.Objects;
import java.util.stream.Collectors;

import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.util.MultiMap;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.UrlEncoded;
Expand Down Expand Up @@ -134,24 +135,14 @@ public static URI toWebsocket(final URI uri)
String httpScheme = uri.getScheme();
if (httpScheme == null)
return uri;

if ("ws".equalsIgnoreCase(httpScheme) || "wss".equalsIgnoreCase(httpScheme))
{
// keep as-is
if (HttpScheme.WS.is(httpScheme) || HttpScheme.WSS.is(httpScheme))
return uri;
}

if ("http".equalsIgnoreCase(httpScheme))
{
// convert to ws
return new URI("ws" + uri.toString().substring(httpScheme.length()));
}

if ("https".equalsIgnoreCase(httpScheme))
{
// convert to wss
return new URI("wss" + uri.toString().substring(httpScheme.length()));
}
String afterScheme = uri.toString().substring(httpScheme.length());
if (HttpScheme.HTTP.is(httpScheme))
return new URI("ws" + afterScheme);
if (HttpScheme.HTTPS.is(httpScheme))
return new URI("wss" + afterScheme);

throw new URISyntaxException(uri.toString(), "Unrecognized HTTP scheme");
}
Expand Down
Expand Up @@ -104,22 +104,13 @@ public static URI toWebsocket(final URI inputUri) throws URISyntaxException
Objects.requireNonNull(inputUri, "Input URI must not be null");
String httpScheme = inputUri.getScheme();
if ("ws".equalsIgnoreCase(httpScheme) || "wss".equalsIgnoreCase(httpScheme))
lachlan-roberts marked this conversation as resolved.
Show resolved Hide resolved
{
// keep as-is
return inputUri;
}

String afterScheme = inputUri.toString().substring(httpScheme.length());
lachlan-roberts marked this conversation as resolved.
Show resolved Hide resolved
if ("http".equalsIgnoreCase(httpScheme))
{
// convert to ws
return new URI("ws" + inputUri.toString().substring(httpScheme.length()));
}

return new URI("ws" + afterScheme);
if ("https".equalsIgnoreCase(httpScheme))
{
// convert to wss
return new URI("wss" + inputUri.toString().substring(httpScheme.length()));
}
return new URI("wss" + afterScheme);

throw new URISyntaxException(inputUri.toString(), "Unrecognized HTTP scheme");
}
Expand Down
Expand Up @@ -50,11 +50,12 @@ public ClientUpgradeRequest()
this.host = null;
}

@Deprecated
lachlan-roberts marked this conversation as resolved.
Show resolved Hide resolved
public ClientUpgradeRequest(URI uri)
{
this.requestURI = uri;
String scheme = uri.getScheme();
if (!HttpScheme.WS.is(scheme) || !HttpScheme.WSS.is(scheme))
if (!HttpScheme.WS.is(scheme) && !HttpScheme.WSS.is(scheme))
throw new IllegalArgumentException("URI scheme must be 'ws' or 'wss'");
this.host = this.requestURI.getHost();
}
Expand Down