Skip to contentSkip to conversation

http: parse HTTP headers case-insensitive

PATCH Superseded Jamo 2 participants 2 messages Jan 14, 2022 – Jan 15, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jamo

author
#0 lore raw

PATCH 1/1 http: parse HTTP headers case-insensitive

According to https://www.ietf.org/rfc/rfc2616.txt 4.2, header names shall be case insensitive and we are now forced to read headers like "Content-Length" capitalized.

The problem with that is when a HTTP server responds with a "content-length" header in lowercase, GRUB gets stuck because HTTP module doesn't know the length of the transmission and the call never ends.

---
 grub-core/net/http.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/net/http.c +3 -3
index b616cf40b..d3ba8c799 100644
@@ -130,7 +130,7 @@ parse_line (grub_file_t file, http_data_t data, char *ptr, grub_size_t len)
       data->first_line_recv = 1;
       return GRUB_ERR_NONE;
     }
-  if (grub_memcmp (ptr, "Content-Length: ", sizeof ("Content-Length: ") - 1)
+  if (grub_strncasecmp (ptr, "Content-Length: ", sizeof ("Content-Length: ") - 1)
       == 0 && !data->size_recv)
     {
       ptr += sizeof ("Content-Length: ") - 1;
@@ -138,8 +138,8 @@ parse_line (grub_file_t file, http_data_t data, char *ptr, grub_size_t len)
       data->size_recv = 1;
       return GRUB_ERR_NONE;
     }
-  if (grub_memcmp (ptr, "Transfer-Encoding: chunked",
-		   sizeof ("Transfer-Encoding: chunked") - 1) == 0)
+  if (grub_strncasecmp (ptr, "Transfer-Encoding: chunked",
+			sizeof ("Transfer-Encoding: chunked") - 1) == 0)
     {
       data->chunked = 1;
       return GRUB_ERR_NONE;
Signature · 1 line
2.32.0
#1 lore raw
Quoted 8 lines · Jamo

According to https://www.ietf.org/rfc/rfc2616.txt 4.2, header names shall be case insensitive and we are now forced to read headers like "Content-Length" capitalized.

The problem with that is when a HTTP server responds with a "content-length" header in lowercase, GRUB gets stuck because HTTP module doesn't know the length of the transmission and the call never ends.

This seems to be v2 of the patch.

To make reviewing easier , please, consider the following for future submissions:

Put the version number into the subject line:

[PATCH v2] http: parse HTTP headers case-insensitive

and add a description of the change between the patches after triple hyphens:

---
v2:
typos fixed

No need to resend this time.

Quoted 9 lines
---
  grub-core/net/http.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/net/http.c +0 -0
index b616cf40b..d3ba8c799 100644

grub-core/net/http.c parse_line (grub_file_t file, http_data_t data, char *ptr, grub_size_t len) · line 133

Quoted 5 lines
grub-core/net/http.c +1 -1
@@ -130,7 +130,7 @@ parse_line (grub_file_t file, http_data_t data, char *ptr, grub_size_t len)
        data->first_line_recv = 1;
        return GRUB_ERR_NONE;
      }
-  if (grub_memcmp (ptr, "Content-Length: ", sizeof ("Content-Length: ") - 1)
+  if (grub_strncasecmp (ptr, "Content-Length: ", sizeof ("Content-Length: ") - 1)

This comparison might still be too restrictive: The RFC 2616 specification says:

"The field value MAY be preceded by any amount of LWS (linear white space), though a single SP is preferred."

LWS = [CRLF] 1*( SP | HT )

So the header might look like

"Content-length:\t 3495".

or like

"Content-length:3495".

So here you should not test for the space but only for the string "Content-Length:".

grub-core/net/http.c parse_line (grub_file_t file, http_data_t data, char *ptr, grub_size_t len) · line 142

Quoted 11 lines
grub-core/net/http.c +2 -2
        == 0 && !data->size_recv)
      {
        ptr += sizeof ("Content-Length: ") - 1;
@@ -138,8 +138,8 @@ parse_line (grub_file_t file, http_data_t data, char *ptr, grub_size_t len)
        data->size_recv = 1;
        return GRUB_ERR_NONE;
      }
-  if (grub_memcmp (ptr, "Transfer-Encoding: chunked",
-		   sizeof ("Transfer-Encoding: chunked") - 1) == 0)
+  if (grub_strncasecmp (ptr, "Transfer-Encoding: chunked",
+			sizeof ("Transfer-Encoding: chunked") - 1) == 0)

Here you need two comparisons and a function to skip the optional whitespace between the tokens. You should check that "chunked" is followed by LWS.

"Transfer-Encoding: chunkedMeat" should not match. "Transfer-Encoding:chunked\t" should match.

Best regards

Heinrich

Quoted 3 lines
grub-core/net/http.c +0 -0
      {
        data->chunked = 1;
        return GRUB_ERR_NONE;