]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: http_fetch: make hdr_ip() reject trailing characters
authorWilly Tarreau <w@1wt.eu>
Thu, 25 Mar 2021 13:12:29 +0000 (14:12 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 25 Mar 2021 14:30:06 +0000 (15:30 +0100)
The hdr_ip() sample fetch function will try to extract IP addresses
from a header field. These IP addresses are parsed using url2ipv4()
and if it fails it will fall back to inet_pton(AF_INET6), otherwise
will fail.

There is a small problem there which is that if a field starts with
an IP address and is immediately followed by some garbage, the IP
address part is still returned. This is a problem with fields such
as x-forwarded-for because it prevents detection of accidental
corruption or bug along the chain. For example, the following string:

   x-forwarded-for: 1.2.3.4; 5.6.7.8

or this one:

   x-forwarded-for: 1.2.3.4O    ( the last one being the letter 'O')

would still return "1.2.3.4" despite the trailing characters. This is
bad because it will silently cover broken code running on intermediary
proxies and may even in some cases allow haproxy to pass improperly
formatted headers after they were apparently validated, for example,
if someone extracts the address from this field to place it into
another one.

This issue would only affect the IPv4 parser, because the IPv6 parser
already uses inet_pton() which fails at the first invalid character and
rejects trailing port numbers.

In strict compliance with RFC7239, let's make sure that if there are any
characters left in the string, the parsing fails and makes hdr_ip()
return nothing. However, a special case has to be handled to support
IPv4 addresses followed by a colon and a valid port number, because till
now the parser used to implicitly accept them and it appears that this
practice, though rare, does exist at least in Azure:
   https://docs.microsoft.com/en-us/azure/application-gateway/how-application-gateway-works

This issue has always been there so the fix may be backported to all
versions. It will need the following commit in order to work as expected:

    MINOR: tools: make url2ipv4 return the exact number of bytes parsed

Many thanks to https://twitter.com/melardev and the BitMEX Security Team
for their detailed report.

doc/configuration.txt
src/http_fetch.c

index b2a3df97ae88184fec9edefa093360aa4e247fa0..aef9b4522d08caa27666a7f2d5bf32c4fb4a8444 100644 (file)
@@ -18757,7 +18757,11 @@ hdr_ip([<name>[,<occ>]]) : ip (deprecated)
   This extracts the last occurrence of header <name> in an HTTP request,
   converts it to an IPv4 or IPv6 address and returns this address. When used
   with ACLs, all occurrences are checked, and if <name> is omitted, every value
-  of every header is checked.
+  of every header is checked. The parser strictly adheres to the format
+  described in RFC7239, with the extension that IPv4 addresses may optionally
+  be followed by a colon (':') and a valid decimal port number (0 to 65535),
+  which will be silently dropped. All other forms will not match and will
+  cause the address to be ignored.
 
   The <occ> parameter is processed as with req.hdr().
 
index 3d627a926a0efa87062e5e7eaef785ac862b7e42..a4169452f9bf8958f6e9ce920ece4b9c8a8f7602 100644 (file)
@@ -988,19 +988,30 @@ static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const c
 
 /* Fetch an HTTP header's IP value. takes a mandatory argument of type string
  * and an optional one of type int to designate a specific occurrence.
- * It returns an IPv4 or IPv6 address.
+ * It returns an IPv4 or IPv6 address. Addresses surrounded by invalid chars
+ * are rejected. However IPv4 addresses may be followed with a colon and a
+ * valid port number.
  */
 static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
 {
-       int ret;
        struct buffer *temp = get_trash_chunk();
+       int ret, len;
+       int port;
 
        while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
                if (smp->data.u.str.data < temp->size - 1) {
                        memcpy(temp->area, smp->data.u.str.area,
                               smp->data.u.str.data);
                        temp->area[smp->data.u.str.data] = '\0';
-                       if (url2ipv4((char *) temp->area, &smp->data.u.ipv4)) {
+                       len = url2ipv4((char *) temp->area, &smp->data.u.ipv4);
+                       if (len == smp->data.u.str.data) {
+                               /* plain IPv4 address */
+                               smp->data.type = SMP_T_IPV4;
+                               break;
+                       } else if (len > 0 && temp->area[len] == ':' &&
+                                  strl2irc(temp->area + len + 1, smp->data.u.str.data - len - 1, &port) == 0 &&
+                                  port >= 0 && port <= 65535) {
+                               /* IPv4 address suffixed with ':' followed by a valid port number */
                                smp->data.type = SMP_T_IPV4;
                                break;
                        } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {