Skip to contentSkip to conversation

io: ignore case when matching websockets HTTP headers

PATCH In review Daniel P. Berrange 5 participants 10 messages Feb 27, 2017 – Feb 28, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 io: ignore case when matching websockets HTTP headers

According to RFC7230 Section 3.2, header field name is case-insensitive. Convert the header data into all lowercase before doing string matching on the headers.

---
 io/channel-websock.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
io/channel-websock.c +9 -5
index a06a4a8..32b7f37 100644
@@ -33,9 +33,9 @@
 #define QIO_CHANNEL_WEBSOCK_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
 #define QIO_CHANNEL_WEBSOCK_GUID_LEN strlen(QIO_CHANNEL_WEBSOCK_GUID)
 
-#define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "Sec-WebSocket-Protocol"
-#define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "Sec-WebSocket-Version"
-#define QIO_CHANNEL_WEBSOCK_HEADER_KEY "Sec-WebSocket-Key"
+#define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "sec-websocket-protocol"
+#define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "sec-websocket-version"
+#define QIO_CHANNEL_WEBSOCK_HEADER_KEY "sec-websocket-key"
 
 #define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary"
 
@@ -223,7 +223,7 @@ static int qio_channel_websock_handshake_process(QIOChannelWebsock *ioc,
 static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
                                               Error **errp)
 {
-    char *handshake_end;
+    char *handshake_end, *tmp;
     ssize_t ret;
     /* Typical HTTP headers from novnc are 512 bytes, so limiting
      * total header size to 4096 is easily enough. */
@@ -249,9 +249,13 @@ static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
         }
     }
 
+    for (tmp = (char *)ioc->encinput.buffer; tmp < handshake_end; tmp++) {
+        *tmp = g_ascii_tolower(*tmp);
+    }
+
     if (qio_channel_websock_handshake_process(ioc,
                                               (char *)ioc->encinput.buffer,
-                                              ioc->encinput.offset,
+                                              handshake_end - (char *)ioc->encinput.buffer,
                                               errp) < 0) {
         return -1;
     }
Signature · 1 line
2.9.3
#1 lore raw
Quoted 11 lines · Daniel P. Berrange

According to RFC7230 Section 3.2, header field name is case-insensitive. Convert the header data into all lowercase before doing string matching on the headers.

---
 io/channel-websock.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

io/channel-websock.c static int qio_channel_websock_handshake_process(QIOChannelWebsock *ioc, · line 229

Quoted 8 lines
io/channel-websock.c +1 -1
@@ -223,7 +223,7 @@ static int qio_channel_websock_handshake_process(QIOChannelWebsock *ioc,
 static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
                                               Error **errp)
 {
-    char *handshake_end;
+    char *handshake_end, *tmp;
     ssize_t ret;
     /* Typical HTTP headers from novnc are 512 bytes, so limiting
      * total header size to 4096 is easily enough. */

Drive-by grammar nit: s/easily/easy/

io/channel-websock.c static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc, · line 258

Quoted 12 lines
io/channel-websock.c +5 -1
@@ -249,9 +249,13 @@ static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
         }
     }
 
+    for (tmp = (char *)ioc->encinput.buffer; tmp < handshake_end; tmp++) {
+        *tmp = g_ascii_tolower(*tmp);
+    }
+
     if (qio_channel_websock_handshake_process(ioc,
                                               (char *)ioc->encinput.buffer,
-                                              ioc->encinput.offset,
+                                              handshake_end - (char *)ioc->encinput.buffer,

I'm not sure why this change is here; nothing else in the patch changed ioc->encinput.offset.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
Quoted 25 lines · Eric Blake
Quoted 10 lines · Daniel P. Berrange

According to RFC7230 Section 3.2, header field name is case-insensitive. Convert the header data into all lowercase before doing string matching on the headers.

---
 io/channel-websock.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

io/channel-websock.c static int qio_channel_websock_handshake_process(QIOChannelWebsock *ioc, · line 229

Quoted 8 lines
io/channel-websock.c +1 -1
@@ -223,7 +223,7 @@ static int qio_channel_websock_handshake_process(QIOChannelWebsock *ioc,
 static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
                                               Error **errp)
 {
-    char *handshake_end;
+    char *handshake_end, *tmp;
     ssize_t ret;
     /* Typical HTTP headers from novnc are 512 bytes, so limiting
      * total header size to 4096 is easily enough. */

Drive-by grammar nit: s/easily/easy/

Quoted 4 lines
(quoted diff) +0 -0
@@ -249,9 +249,13 @@ static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
         }
     }
 
Quoted 11 lines

io/channel-websock.c static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc, · line 258

Quoted 8 lines
io/channel-websock.c +5 -1
+    for (tmp = (char *)ioc->encinput.buffer; tmp < handshake_end; tmp++) {
+        *tmp = g_ascii_tolower(*tmp);
+    }
+
     if (qio_channel_websock_handshake_process(ioc,
                                               (char *)ioc->encinput.buffer,
-                                              ioc->encinput.offset,
+                                              handshake_end - (char *)ioc->encinput.buffer,

I'm not sure why this change is here; nothing else in the patch changed ioc->encinput.offset.

yep. The rest seems fine to me.

Usage of g_ascii_strdown () seems overkill now. We do not need the header anymore after this call.

Den

#3 lore raw
In reply to Eric Blake
Quoted 27 lines · Eric Blake
Quoted 11 lines · Daniel P. Berrange

According to RFC7230 Section 3.2, header field name is case-insensitive. Convert the header data into all lowercase before doing string matching on the headers.

---
 io/channel-websock.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

io/channel-websock.c static int qio_channel_websock_handshake_process(QIOChannelWebsock *ioc, · line 229

Quoted 8 lines
io/channel-websock.c +1 -1
@@ -223,7 +223,7 @@ static int qio_channel_websock_handshake_process(QIOChannelWebsock *ioc,
 static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
                                               Error **errp)
 {
-    char *handshake_end;
+    char *handshake_end, *tmp;
     ssize_t ret;
     /* Typical HTTP headers from novnc are 512 bytes, so limiting
      * total header size to 4096 is easily enough. */

Drive-by grammar nit: s/easily/easy/

Quoted 4 lines
(quoted diff) +0 -0
@@ -249,9 +249,13 @@ static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
         }
     }
 
Quoted 11 lines

io/channel-websock.c static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc, · line 258

Quoted 8 lines
io/channel-websock.c +5 -1
+    for (tmp = (char *)ioc->encinput.buffer; tmp < handshake_end; tmp++) {
+        *tmp = g_ascii_tolower(*tmp);
+    }
+
     if (qio_channel_websock_handshake_process(ioc,
                                               (char *)ioc->encinput.buffer,
-                                              ioc->encinput.offset,
+                                              handshake_end - (char *)ioc->encinput.buffer,

I'm not sure why this change is here; nothing else in the patch changed ioc->encinput.offset.

It is a related bug. The ioc->encinput buffer contains upto 4096 bytes, but that data may cover both the header & payload. The handshake_end pointer refers to the end of the header region. So if we just pass encinput.offset, then handshake_process() will be searching part of the payload as well as the header. This might cause it do match the wrong data. I guess I should have made that clear in the commit, or even tput it in a separate commit

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|
Reviewed-by
#4 lore raw
Quoted 45 lines · Daniel P. Berrange
Quoted 25 lines · Eric Blake
Quoted 10 lines · Daniel P. Berrange

According to RFC7230 Section 3.2, header field name is case-insensitive. Convert the header data into all lowercase before doing string matching on the headers.

---
 io/channel-websock.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

io/channel-websock.c static int qio_channel_websock_handshake_process(QIOChannelWebsock *ioc, · line 229

Quoted 8 lines
io/channel-websock.c +1 -1
@@ -223,7 +223,7 @@ static int qio_channel_websock_handshake_process(QIOChannelWebsock *ioc,
 static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
                                               Error **errp)
 {
-    char *handshake_end;
+    char *handshake_end, *tmp;
     ssize_t ret;
     /* Typical HTTP headers from novnc are 512 bytes, so limiting
      * total header size to 4096 is easily enough. */

Drive-by grammar nit: s/easily/easy/

Quoted 4 lines
(quoted diff) +0 -0
@@ -249,9 +249,13 @@ static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
         }
     }
 
Quoted 10 lines

io/channel-websock.c static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc, · line 258

Quoted 8 lines
io/channel-websock.c +5 -1
+    for (tmp = (char *)ioc->encinput.buffer; tmp < handshake_end; tmp++) {
+        *tmp = g_ascii_tolower(*tmp);
+    }
+
     if (qio_channel_websock_handshake_process(ioc,
                                               (char *)ioc->encinput.buffer,
-                                              ioc->encinput.offset,
+                                              handshake_end - (char *)ioc->encinput.buffer,

I'm not sure why this change is here; nothing else in the patch changed ioc->encinput.offset.

It is a related bug. The ioc->encinput buffer contains upto 4096 bytes, but that data may cover both the header & payload. The handshake_end pointer refers to the end of the header region. So if we just pass encinput.offset, then handshake_process() will be searching part of the payload as well as the header. This might cause it do match the wrong data. I guess I should have made that clear in the commit, or even tput it in a separate commit

Quoted 2 lines

Regards,
Daniel

with this comment this is obviously correct :)

#5 lore raw
In reply to Daniel P. Berrange
This series seems to have some coding style problems. See output below for more information:

Hi,

This series seems to have some coding style problems. See output below for more information:

Message-id: [email protected]
Type: series
Subject: [Qemu-devel] [PATCH] io: ignore case when matching websockets HTTP headers

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

# Useful git options
git config --local diff.renamelimit 0
git config --local diff.renames True

commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do

    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))

done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 From https://github.com/patchew-project/qemu
- [tag update] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
b941173 io: ignore case when matching websockets HTTP headers

=== OUTPUT BEGIN ===
Checking PATCH 1/1: io: ignore case when matching websockets HTTP headers...
ERROR: line over 90 characters
#50: FILE: io/channel-websock.c:258:
+ handshake_end - (char *)ioc->encinput.buffer,

total: 1 errors, 0 warnings, 34 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

=== OUTPUT END ===

Test command exited with code: 1

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]

#6 lore raw
In reply to Daniel P. Berrange
Quoted 36 lines · Daniel P. Berrange

According to RFC7230 Section 3.2, header field name is case-insensitive. Convert the header data into all lowercase before doing string matching on the headers.

---
 io/channel-websock.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
io/channel-websock.c +4 -4
index a06a4a8..32b7f37 100644
@@ -33,9 +33,9 @@
 #define QIO_CHANNEL_WEBSOCK_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
 #define QIO_CHANNEL_WEBSOCK_GUID_LEN strlen(QIO_CHANNEL_WEBSOCK_GUID)
 
-#define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "Sec-WebSocket-Protocol"
-#define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "Sec-WebSocket-Version"
-#define QIO_CHANNEL_WEBSOCK_HEADER_KEY "Sec-WebSocket-Key"
+#define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "sec-websocket-protocol"
+#define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "sec-websocket-version"
+#define QIO_CHANNEL_WEBSOCK_HEADER_KEY "sec-websocket-key"
 
 #define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary"
 
@@ -223,7 +223,7 @@ static int qio_channel_websock_handshake_process(QIOChannelWebsock *ioc,
 static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
                                               Error **errp)
 {
-    char *handshake_end;
+    char *handshake_end, *tmp;
     ssize_t ret;
     /* Typical HTTP headers from novnc are 512 bytes, so limiting
      * total header size to 4096 is easily enough. */

io/channel-websock.c static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc, · line 255

Quoted 7 lines
io/channel-websock.c +4 -0
@@ -249,9 +249,13 @@ static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
         }
     }
 
+    for (tmp = (char *)ioc->encinput.buffer; tmp < handshake_end; tmp++) {
+        *tmp = g_ascii_tolower(*tmp);
+    }
+

self-nack - this does not in fact work - while it is fine to lowercase the header keys, we must not touch the header values as some data is case-sensitive

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|
#7 lore raw
Quoted 48 lines · Daniel P. Berrange
Quoted 36 lines · Daniel P. Berrange

According to RFC7230 Section 3.2, header field name is case-insensitive. Convert the header data into all lowercase before doing string matching on the headers.

---
 io/channel-websock.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
io/channel-websock.c +4 -4
index a06a4a8..32b7f37 100644
@@ -33,9 +33,9 @@
 #define QIO_CHANNEL_WEBSOCK_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
 #define QIO_CHANNEL_WEBSOCK_GUID_LEN strlen(QIO_CHANNEL_WEBSOCK_GUID)
 
-#define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "Sec-WebSocket-Protocol"
-#define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "Sec-WebSocket-Version"
-#define QIO_CHANNEL_WEBSOCK_HEADER_KEY "Sec-WebSocket-Key"
+#define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "sec-websocket-protocol"
+#define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "sec-websocket-version"
+#define QIO_CHANNEL_WEBSOCK_HEADER_KEY "sec-websocket-key"
 
 #define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary"
 
@@ -223,7 +223,7 @@ static int qio_channel_websock_handshake_process(QIOChannelWebsock *ioc,
 static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
                                               Error **errp)
 {
-    char *handshake_end;
+    char *handshake_end, *tmp;
     ssize_t ret;
     /* Typical HTTP headers from novnc are 512 bytes, so limiting
      * total header size to 4096 is easily enough. */

io/channel-websock.c static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc, · line 255

Quoted 7 lines
io/channel-websock.c +4 -0
@@ -249,9 +249,13 @@ static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
         }
     }
 
+    for (tmp = (char *)ioc->encinput.buffer; tmp < handshake_end; tmp++) {
+        *tmp = g_ascii_tolower(*tmp);
+    }
+

self-nack - this does not in fact work - while it is fine to lowercase the header keys, we must not touch the header values as some data is case-sensitive

Quoted 2 lines

Regards,
Daniel

g-ascii-tolower() will help

Den

#8 lore raw
Quoted 49 lines · Denis V. Lunev
Quoted 48 lines · Daniel P. Berrange
Quoted 36 lines · Daniel P. Berrange

According to RFC7230 Section 3.2, header field name is case-insensitive. Convert the header data into all lowercase before doing string matching on the headers.

---
 io/channel-websock.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
io/channel-websock.c +4 -4
index a06a4a8..32b7f37 100644
@@ -33,9 +33,9 @@
 #define QIO_CHANNEL_WEBSOCK_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
 #define QIO_CHANNEL_WEBSOCK_GUID_LEN strlen(QIO_CHANNEL_WEBSOCK_GUID)
 
-#define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "Sec-WebSocket-Protocol"
-#define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "Sec-WebSocket-Version"
-#define QIO_CHANNEL_WEBSOCK_HEADER_KEY "Sec-WebSocket-Key"
+#define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "sec-websocket-protocol"
+#define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "sec-websocket-version"
+#define QIO_CHANNEL_WEBSOCK_HEADER_KEY "sec-websocket-key"
 
 #define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary"
 
@@ -223,7 +223,7 @@ static int qio_channel_websock_handshake_process(QIOChannelWebsock *ioc,
 static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
                                               Error **errp)
 {
-    char *handshake_end;
+    char *handshake_end, *tmp;
     ssize_t ret;
     /* Typical HTTP headers from novnc are 512 bytes, so limiting
      * total header size to 4096 is easily enough. */

io/channel-websock.c static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc, · line 255

Quoted 7 lines
io/channel-websock.c +4 -0
@@ -249,9 +249,13 @@ static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
         }
     }
 
+    for (tmp = (char *)ioc->encinput.buffer; tmp < handshake_end; tmp++) {
+        *tmp = g_ascii_tolower(*tmp);
+    }
+

self-nack - this does not in fact work - while it is fine to lowercase the header keys, we must not touch the header values as some data is case-sensitive

Quoted 5 lines
Quoted 2 lines

Regards,
Daniel

g-ascii-tolower() will help

Den

ah, sorry, wrong copy/paste. I meant 'g_ascii_strdown ()'

Den

#9 lore raw
Quoted 50 lines · Denis V. Lunev
Quoted 49 lines · Denis V. Lunev
Quoted 48 lines · Daniel P. Berrange
Quoted 36 lines · Daniel P. Berrange

According to RFC7230 Section 3.2, header field name is case-insensitive. Convert the header data into all lowercase before doing string matching on the headers.

---
 io/channel-websock.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
io/channel-websock.c +4 -4
index a06a4a8..32b7f37 100644
@@ -33,9 +33,9 @@
 #define QIO_CHANNEL_WEBSOCK_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
 #define QIO_CHANNEL_WEBSOCK_GUID_LEN strlen(QIO_CHANNEL_WEBSOCK_GUID)
 
-#define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "Sec-WebSocket-Protocol"
-#define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "Sec-WebSocket-Version"
-#define QIO_CHANNEL_WEBSOCK_HEADER_KEY "Sec-WebSocket-Key"
+#define QIO_CHANNEL_WEBSOCK_HEADER_PROTOCOL "sec-websocket-protocol"
+#define QIO_CHANNEL_WEBSOCK_HEADER_VERSION "sec-websocket-version"
+#define QIO_CHANNEL_WEBSOCK_HEADER_KEY "sec-websocket-key"
 
 #define QIO_CHANNEL_WEBSOCK_PROTOCOL_BINARY "binary"
 
@@ -223,7 +223,7 @@ static int qio_channel_websock_handshake_process(QIOChannelWebsock *ioc,
 static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
                                               Error **errp)
 {
-    char *handshake_end;
+    char *handshake_end, *tmp;
     ssize_t ret;
     /* Typical HTTP headers from novnc are 512 bytes, so limiting
      * total header size to 4096 is easily enough. */

io/channel-websock.c static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc, · line 255

Quoted 7 lines
io/channel-websock.c +4 -0
@@ -249,9 +249,13 @@ static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
         }
     }
 
+    for (tmp = (char *)ioc->encinput.buffer; tmp < handshake_end; tmp++) {
+        *tmp = g_ascii_tolower(*tmp);
+    }
+

self-nack - this does not in fact work - while it is fine to lowercase the header keys, we must not touch the header values as some data is case-sensitive

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Regards,
Daniel

g-ascii-tolower() will help

Den

ah, sorry, wrong copy/paste. I meant 'g_ascii_strdown ()'

That would still lowercase both the key & value part of the headers. We need to only lowercase the key, ie text between start of line and first ':'. This requires properly parsing the HTTP header.

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|