Blame SOURCES/0002-WebSockets-allow-null-characters-in-text-messages-da.patch

6a7655
From 109bb2f692c746bc63a0ade8737b584aecb0b1ad Mon Sep 17 00:00:00 2001
6a7655
From: Carlos Garcia Campos <cgarcia@igalia.com>
6a7655
Date: Thu, 27 Jun 2019 16:03:21 +0200
6a7655
Subject: [PATCH] WebSockets: allow null characters in text messages data
6a7655
6a7655
RFC 6455 says that text messages should contains valid UTF-8, and null
6a7655
characters valid according to RFC 3629. However, we are using
6a7655
g_utf8_validate(), which considers null characters as errors, to
6a7655
validate WebSockets text messages. This patch adds an internal
6a7655
utf8_validate() function based on g_utf8_validate() but allowing null
6a7655
characters and just returning a gboolean since we are always ignoring
6a7655
the end parameter in case of errors.
6a7655
soup_websocket_connection_send_text() assumes the given text is null
6a7655
terminated, so we need a new public function to allow sending text
6a7655
messages containing null characters. This patch adds
6a7655
soup_websocket_connection_send_message() that receives a
6a7655
SoupWebsocketDataType and GBytes, which is consistent with
6a7655
SoupWebsocketConnection::message signal.
6a7655
6a7655
For RHEL backport, drop the addition of soup_websocket_connection_send_message()
6a7655
as we don't need it and don't want to expose new API.
6a7655
diff --git libsoup/soup-websocket-connection.c libsoup/soup-websocket-connection.c
6a7655
index 66bd6871..67a98731 100644
6a7655
--- a/libsoup/soup-websocket-connection.c
6a7655
+++ b/libsoup/soup-websocket-connection.c
6a7655
@@ -155,6 +155,82 @@
6a7655
 
6a7655
 static void protocol_error_and_close (SoupWebsocketConnection *self);
6a7655
 
6a7655
+/* Code below is based on g_utf8_validate() implementation,
6a7655
+ * but handling NULL characters as valid, as expected by
6a7655
+ * WebSockets and compliant with RFC 3629.
6a7655
+ */
6a7655
+#define VALIDATE_BYTE(mask, expect)                             \
6a7655
+        G_STMT_START {                                          \
6a7655
+          if (G_UNLIKELY((*(guchar *)p & (mask)) != (expect)))  \
6a7655
+                  return FALSE;                                 \
6a7655
+        } G_STMT_END
6a7655
+
6a7655
+/* see IETF RFC 3629 Section 4 */
6a7655
+static gboolean
6a7655
+utf8_validate (const char *str,
6a7655
+               gsize max_len)
6a7655
+
6a7655
+{
6a7655
+        const gchar *p;
6a7655
+
6a7655
+        for (p = str; ((p - str) < max_len); p++) {
6a7655
+                if (*(guchar *)p < 128)
6a7655
+                        /* done */;
6a7655
+                else {
6a7655
+                        if (*(guchar *)p < 0xe0) { /* 110xxxxx */
6a7655
+                                if (G_UNLIKELY (max_len - (p - str) < 2))
6a7655
+                                        return FALSE;
6a7655
+
6a7655
+                                if (G_UNLIKELY (*(guchar *)p < 0xc2))
6a7655
+                                        return FALSE;
6a7655
+                        } else {
6a7655
+                                if (*(guchar *)p < 0xf0) { /* 1110xxxx */
6a7655
+                                        if (G_UNLIKELY (max_len - (p - str) < 3))
6a7655
+                                                return FALSE;
6a7655
+
6a7655
+                                        switch (*(guchar *)p++ & 0x0f) {
6a7655
+                                        case 0:
6a7655
+                                                VALIDATE_BYTE(0xe0, 0xa0); /* 0xa0 ... 0xbf */
6a7655
+                                                break;
6a7655
+                                        case 0x0d:
6a7655
+                                                VALIDATE_BYTE(0xe0, 0x80); /* 0x80 ... 0x9f */
6a7655
+                                                break;
6a7655
+                                        default:
6a7655
+                                                VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
6a7655
+                                        }
6a7655
+                                } else if (*(guchar *)p < 0xf5) { /* 11110xxx excluding out-of-range */
6a7655
+                                        if (G_UNLIKELY (max_len - (p - str) < 4))
6a7655
+                                                return FALSE;
6a7655
+
6a7655
+                                        switch (*(guchar *)p++ & 0x07) {
6a7655
+                                        case 0:
6a7655
+                                                VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
6a7655
+                                                if (G_UNLIKELY((*(guchar *)p & 0x30) == 0))
6a7655
+                                                        return FALSE;
6a7655
+                                                break;
6a7655
+                                        case 4:
6a7655
+                                                VALIDATE_BYTE(0xf0, 0x80); /* 0x80 ... 0x8f */
6a7655
+                                                break;
6a7655
+                                        default:
6a7655
+                                                VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
6a7655
+                                        }
6a7655
+                                        p++;
6a7655
+                                        VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
6a7655
+                                } else {
6a7655
+                                        return FALSE;
6a7655
+                                }
6a7655
+                        }
6a7655
+
6a7655
+                        p++;
6a7655
+                        VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
6a7655
+                }
6a7655
+        }
6a7655
+
6a7655
+        return TRUE;
6a7655
+}
6a7655
+
6a7655
+#undef VALIDATE_BYTE
6a7655
+
6a7655
 static void
6a7655
 frame_free (gpointer data)
6a7655
 {
6a7655
@@ -629,7 +705,7 @@
6a7655
 		data += 2;
6a7655
 		len -= 2;
6a7655
 		
6a7655
-		if (!g_utf8_validate ((char *)data, len, NULL)) {
6a7655
+		if (!utf8_validate ((const char *)data, len)) {
6a7655
 			g_debug ("received non-UTF8 close data: %d '%.*s' %d", (int)len, (int)len, (char *)data, (int)data[0]);
6a7655
 			protocol_error_and_close (self);
6a7655
 			return;
6a7655
@@ -777,9 +853,8 @@
6a7655
 		/* Actually deliver the message? */
6a7655
 		if (fin) {
6a7655
 			if (pv->message_opcode == 0x01 &&
6a7655
-			    !g_utf8_validate((char *)pv->message_data->data,
6a7655
-			                     pv->message_data->len,
6a7655
-			                     NULL)) {
6a7655
+			    !utf8_validate((const char *)pv->message_data->data,
6a7655
+					   pv->message_data->len)) {
6a7655
 
6a7655
 				g_debug ("received invalid non-UTF8 text data");
6a7655
 
6a7655
@@ -1699,7 +1774,9 @@
6a7655
  * @self: the WebSocket
6a7655
  * @text: the message contents
6a7655
  *
6a7655
- * Send a text (UTF-8) message to the peer.
6a7655
+ * Send a %NULL-terminated text (UTF-8) message to the peer. If you need
6a7655
+ * to send text messages containing %NULL characters use
6a7655
+ * soup_websocket_connection_send_message() instead.
6a7655
  *
6a7655
  * The message is queued to be sent and will be sent when the main loop
6a7655
  * is run.
6a7655
@@ -1717,7 +1794,7 @@
6a7655
 	g_return_if_fail (text != NULL);
6a7655
 
6a7655
 	length = strlen (text);
6a7655
-	g_return_if_fail (g_utf8_validate (text, length, NULL));
6a7655
+        g_return_if_fail (utf8_validate (text, length));
6a7655
 
6a7655
 	send_message (self, SOUP_WEBSOCKET_QUEUE_NORMAL, 0x01, (const guint8 *) text, length);
6a7655
 }
6a7655
-- 
6a7655
2.26.2
6a7655