autobahn reject forbidden close code ranges
authorAndy Green <andy.green@linaro.org>
Mon, 28 Dec 2015 10:29:06 +0000 (18:29 +0800)
committerAndy Green <andy.green@linaro.org>
Mon, 28 Dec 2015 14:29:57 +0000 (22:29 +0800)
Complain like autobahn expects if we get a funny close code

Signed-off-by: Andy Green <andy.green@linaro.org>
lib/client-parser.c

index 0811cc9..90fd305 100644 (file)
@@ -24,6 +24,8 @@
 int lws_client_rx_sm(struct lws *wsi, unsigned char c)
 {
        int callback_action = LWS_CALLBACK_CLIENT_RECEIVE;
+       unsigned short close_code;
+       unsigned char *pp;
        struct lws_tokens eff_buf;
        int handled, m;
 
@@ -323,14 +325,26 @@ spill:
                                lwsl_parser("seen server's close ack\n");
                                return -1;
                        }
+                       pp = (unsigned char *)&wsi->u.ws.rx_user_buffer[
+                                               LWS_SEND_BUFFER_PRE_PADDING];
                        lwsl_parser("client sees server close len = %d\n",
                                                 wsi->u.ws.rx_user_buffer_head);
+                       if (wsi->u.ws.rx_user_buffer_head >= 2) {
+                               close_code = (pp[0] << 8) | pp[1];
+                               if (close_code < 1000 || close_code == 1004 ||
+                                   close_code == 1005 || close_code == 1006 ||
+                                   close_code == 1012 || close_code == 1013 ||
+                                   close_code == 1014 || close_code == 1015 ||
+                                   (close_code >= 1016 && close_code < 3000)
+                               ) {
+                                       pp[0] = (LWS_CLOSE_STATUS_PROTOCOL_ERR >> 8) & 0xff;
+                                       pp[1] = LWS_CLOSE_STATUS_PROTOCOL_ERR & 0xff;
+                               }
+                       }
                        if (user_callback_handle_rxflow(
                                        wsi->protocol->callback, wsi,
                                        LWS_CALLBACK_WS_PEER_INITIATED_CLOSE,
-                                       wsi->user_space,
-                                       &wsi->u.ws.rx_user_buffer[
-                                               LWS_SEND_BUFFER_PRE_PADDING],
+                                       wsi->user_space, pp,
                                        wsi->u.ws.rx_user_buffer_head))
                                return -1;
                        /*