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

Make Sec-WebSocket-Protocol less strict #1034

Merged
merged 2 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/example/ChatServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Collections;

import org.java_websocket.WebSocket;
import org.java_websocket.WebSocketImpl;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.framing.Framedata;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.protocols.IProtocol;
import org.java_websocket.server.WebSocketServer;

/**
Expand All @@ -49,6 +53,10 @@ public ChatServer( InetSocketAddress address ) {
super( address );
}

public ChatServer(int port, Draft_6455 draft) {
super( new InetSocketAddress( port ), Collections.<Draft>singletonList(draft));
}

@Override
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
conn.send("Welcome to the server!"); //This method sends a message to the new client
Expand Down
54 changes: 54 additions & 0 deletions src/main/example/SecWebSocketProtocolServerExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2010-2020 Nathan Rajlich
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.extensions.IExtension;
import org.java_websocket.protocols.IProtocol;
import org.java_websocket.protocols.Protocol;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;

/**
* This example demonstrates how to use a specific Sec-WebSocket-Protocol for your connection.
*/
public class SecWebSocketProtocolServerExample {

public static void main( String[] args ) throws URISyntaxException {
// This draft only allows you to use the specific Sec-WebSocket-Protocol without a fallback.
Draft_6455 draft_ocppOnly = new Draft_6455(Collections.<IExtension>emptyList(), Collections.<IProtocol>singletonList(new Protocol("ocpp2.0")));

// This draft allows the specific Sec-WebSocket-Protocol and also provides a fallback, if the other endpoint does not accept the specific Sec-WebSocket-Protocol
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
protocols.add(new Protocol("ocpp2.0"));
protocols.add(new Protocol(""));
Draft_6455 draft_ocppAndFallBack = new Draft_6455(Collections.<IExtension>emptyList(), protocols);

ChatServer chatServer = new ChatServer(8887, draft_ocppOnly);
chatServer.start();
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/java_websocket/WebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.java_websocket.enums.ReadyState;
import org.java_websocket.exceptions.WebsocketNotConnectedException;
import org.java_websocket.framing.Framedata;
import org.java_websocket.protocols.IProtocol;

import javax.net.ssl.SSLSession;

Expand Down Expand Up @@ -224,4 +225,12 @@ public interface WebSocket {
* @since 1.4.1
*/
SSLSession getSSLSession() throws IllegalArgumentException;

/**
* Returns the used Sec-WebSocket-Protocol for this websocket connection
* @return the Sec-WebSocket-Protocol or null, if no draft available
* @throws IllegalArgumentException the underlying draft does not support a Sec-WebSocket-Protocol
* @since 1.5.2
*/
IProtocol getProtocol();
}
10 changes: 10 additions & 0 deletions src/main/java/org/java_websocket/WebSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.java_websocket.framing.Framedata;
import org.java_websocket.framing.PingFrame;
import org.java_websocket.handshake.*;
import org.java_websocket.protocols.IProtocol;
import org.java_websocket.server.WebSocketServer.WebSocketWorker;
import org.java_websocket.util.Charsetfunctions;

Expand Down Expand Up @@ -832,6 +833,15 @@ public SSLSession getSSLSession() {
return ((ISSLChannel) channel).getSSLEngine().getSession();
}

@Override
public IProtocol getProtocol() {
if (draft == null)
return null;
if (!(draft instanceof Draft_6455))
throw new IllegalArgumentException("This draft does not support Sec-WebSocket-Protocol");
return ((Draft_6455) draft).getProtocol();
}

@Override
public <T> void setAttachment(T attachment) {
this.attachment = attachment;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/java_websocket/client/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.java_websocket.handshake.HandshakeImpl1Client;
import org.java_websocket.handshake.Handshakedata;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.protocols.IProtocol;

/**
* A subclass must implement at least <var>onOpen</var>, <var>onClose</var>, and <var>onMessage</var> to be
Expand Down Expand Up @@ -913,6 +914,9 @@ public SSLSession getSSLSession() {
return engine.getSSLSession();
}

@Override
public IProtocol getProtocol() { return engine.getProtocol();}

/**
* Method to give some additional info for specific IOExceptions
* @param e the IOException causing a eot.
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/java_websocket/protocols/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public Protocol( String providedProtocol ) {

@Override
public boolean acceptProvidedProtocol( String inputProtocolHeader ) {
if ("".equals(providedProtocol)) {
return true;
}
String protocolHeader = patternSpace.matcher(inputProtocolHeader).replaceAll("");
String[] headers = patternComma.split(protocolHeader);
for( String header : headers ) {
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/org/java_websocket/drafts/Draft_6455Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void testGetKnownExtensions() throws Exception {

@Test
public void testGetProtocol() throws Exception {
Draft_6455 draft_6455 = new Draft_6455();
Draft_6455 draft_6455 = new Draft_6455(Collections.<IExtension>emptyList(), Collections.<IProtocol>emptyList());
assertNull( draft_6455.getProtocol() );
draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension );
assertNull( draft_6455.getProtocol() );
Expand Down Expand Up @@ -194,7 +194,7 @@ public void testToString() throws Exception {
Draft_6455 draft_6455 = new Draft_6455();
assertEquals( "Draft_6455 extension: DefaultExtension max frame size: 2147483647", draft_6455.toString() );
draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension );
assertEquals( "Draft_6455 extension: DefaultExtension max frame size: 2147483647", draft_6455.toString() );
assertEquals( "Draft_6455 extension: DefaultExtension protocol: max frame size: 2147483647", draft_6455.toString() );
draft_6455 = new Draft_6455( Collections.<IExtension>emptyList(), Collections.<IProtocol>singletonList( new Protocol( "chat" ) ) );
assertEquals( "Draft_6455 extension: DefaultExtension max frame size: 2147483647", draft_6455.toString() );
draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension );
Expand Down Expand Up @@ -223,15 +223,15 @@ public void testEquals() throws Exception {
draft1.acceptHandshakeAsServer( handshakedataProtocolExtension );
assertNotEquals( draft2, draft3 );
assertNotEquals( draft0, draft2 );
assertEquals( draft0, draft1 );
assertNotEquals( draft0, draft1 );
draft2 = draft2.copyInstance();
draft1 = draft1.copyInstance();
//unequal for draft draft2 due to a provided protocol
draft2.acceptHandshakeAsServer( handshakedataProtocol );
draft1.acceptHandshakeAsServer( handshakedataProtocol );
assertNotEquals( draft2, draft3 );
assertNotEquals( draft0, draft2 );
assertEquals( draft0, draft1 );
assertNotEquals( draft0, draft1 );
draft2 = draft2.copyInstance();
draft1 = draft1.copyInstance();
//unequal for draft draft0 due to a provided protocol (no protocol)
Expand Down Expand Up @@ -297,14 +297,14 @@ public void testHashCode() throws Exception {
public void acceptHandshakeAsServer() throws Exception {
Draft_6455 draft_6455 = new Draft_6455();
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedata ) );
assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataExtension ) );
assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
draft_6455 = new Draft_6455( new TestExtension() );
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedata ) );
assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataExtension ) );
assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocolExtension ) );
draft_6455 = new Draft_6455( Collections.<IExtension>emptyList(), Collections.<IProtocol>singletonList( new Protocol( "chat" ) ) );
assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsServer( handshakedata ) );
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsServer( handshakedataProtocol ) );
Expand Down Expand Up @@ -336,15 +336,15 @@ public void acceptHandshakeAsClient() throws Exception {
response.put( "Sec-WebSocket-Accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=" );
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
response.put( "Sec-WebSocket-Protocol", "chat" );
assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
draft_6455 = new Draft_6455( Collections.<IExtension>emptyList(), Collections.<IProtocol>singletonList( new Protocol( "chat" ) ) );
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
protocols.add( new Protocol( "" ) );
protocols.add( new Protocol( "chat" ) );
draft_6455 = new Draft_6455( Collections.<IExtension>emptyList(), protocols );
assertEquals( HandshakeState.MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
draft_6455 = new Draft_6455();
draft_6455 =new Draft_6455(Collections.<IExtension>emptyList(), Collections.<IProtocol>emptyList());
assertEquals( HandshakeState.NOT_MATCHED, draft_6455.acceptHandshakeAsClient( request, response ) );
protocols.clear();
protocols.add( new Protocol( "chat3" ) );
Expand Down
Loading