diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index e49a58c8..0deb3a1b 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. - Sending invalid UTF-8 TEXT message should fail and close the connection (fix remaining autobahn test: 6.X UTF-8 Handling) - Fix failing unittest which was sending binary data in text mode with WebSocket::send to call properly call WebSocket::sendBinary instead. - Validate that the reason is proper utf-8. (fix autobahn test 7.5.1) +- Validate close codes. Autobahn 7.9.* ## [5.1.5] - 2019-09-03 diff --git a/ixwebsocket/IXWebSocketCloseConstants.cpp b/ixwebsocket/IXWebSocketCloseConstants.cpp index 6e9036e3..65accb1b 100644 --- a/ixwebsocket/IXWebSocketCloseConstants.cpp +++ b/ixwebsocket/IXWebSocketCloseConstants.cpp @@ -27,4 +27,5 @@ namespace ix const std::string WebSocketCloseConstants::kProtocolErrorCodeDataOpcodeOutOfSequence("Fragmentation: data message out of sequence"); const std::string WebSocketCloseConstants::kProtocolErrorCodeContinuationOpCodeOutOfSequence("Fragmentation: continuation opcode out of sequence"); const std::string WebSocketCloseConstants::kInvalidFramePayloadDataMessage("Invalid frame payload data"); + const std::string WebSocketCloseConstants::kInvalidCloseCodeMessage("Invalid close code"); } diff --git a/ixwebsocket/IXWebSocketCloseConstants.h b/ixwebsocket/IXWebSocketCloseConstants.h index 7a10405f..145777b9 100644 --- a/ixwebsocket/IXWebSocketCloseConstants.h +++ b/ixwebsocket/IXWebSocketCloseConstants.h @@ -32,5 +32,6 @@ namespace ix static const std::string kProtocolErrorCodeDataOpcodeOutOfSequence; static const std::string kProtocolErrorCodeContinuationOpCodeOutOfSequence; static const std::string kInvalidFramePayloadDataMessage; + static const std::string kInvalidCloseCodeMessage; }; } // namespace ix diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index d5664dde..4c7cf492 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -684,6 +684,20 @@ namespace ix code = WebSocketCloseConstants::kInvalidFramePayloadData; reason = WebSocketCloseConstants::kInvalidFramePayloadDataMessage; } + + // Validate close codes. Autobahn 7.9.* + // 1014, 1015 are debattable. The firefox MSDN has a description for them + if (code < 1000 || code == 1004 || code == 1006 || + (code > 1013 && code < 3000)) + { + // build up an error message containing the bad error code + std::stringstream ss; + ss << WebSocketCloseConstants::kInvalidCloseCodeMessage + << ": " << code; + reason = ss.str(); + + code = WebSocketCloseConstants::kProtocolErrorCode; + } } else {