]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
noproxy: fix tail-matching
authorDaniel Stenberg <daniel@haxx.se>
Fri, 28 Oct 2022 08:51:49 +0000 (10:51 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Fri, 28 Oct 2022 15:54:48 +0000 (17:54 +0200)
Also ignore trailing dots in both host name and comparison pattern.

Regression in 7.86.0 (from 1e9a538e05c0)

Extended test 1614 to verify better.

Reported-by: Henning Schild
Fixes #9821
Closes #9822

lib/noproxy.c
tests/unit/unit1614.c

index 58bc69a2dc15447c970a9ba4abe15fd67dff71e9..2832ae166a5bc70478fa5dd790db42190bc477e4 100644 (file)
@@ -153,9 +153,14 @@ bool Curl_check_noproxy(const char *name, const char *no_proxy)
     }
     else {
       unsigned int address;
+      namelen = strlen(name);
       if(1 == Curl_inet_pton(AF_INET, name, &address))
         type = TYPE_IPV4;
-      namelen = strlen(name);
+      else {
+        /* ignore trailing dots in the host name */
+        if(name[namelen - 1] == '.')
+          namelen--;
+      }
     }
 
     while(*p) {
@@ -177,12 +182,23 @@ bool Curl_check_noproxy(const char *name, const char *no_proxy)
       if(tokenlen) {
         switch(type) {
         case TYPE_HOST:
-          if(*token == '.') {
-            ++token;
-            --tokenlen;
-            /* tailmatch */
-            match = (tokenlen <= namelen) &&
-              strncasecompare(token, name + (namelen - tokenlen), namelen);
+          /* ignore trailing dots in the token to check */
+          if(token[tokenlen - 1] == '.')
+            tokenlen--;
+
+          if(tokenlen && (*token == '.')) {
+            /* A: example.com matches '.example.com'
+               B: www.example.com matches '.example.com'
+               C: nonexample.com DOES NOT match '.example.com'
+            */
+            if((tokenlen - 1) == namelen)
+              /* case A, exact match without leading dot */
+              match = strncasecompare(token + 1, name, namelen);
+            else if(tokenlen < namelen)
+              /* case B, tailmatch with leading dot */
+              match = strncasecompare(token, name + (namelen - tokenlen),
+                                      tokenlen);
+            /* case C passes through, not a match */
           }
           else
             match = (tokenlen == namelen) &&
index c2f563a0dc1d3cb861b1e337fd7aa32b5359372d..8f62b70d4d829aa7996b93b9b2debe57d14fc044 100644 (file)
@@ -77,6 +77,15 @@ UNITTEST_START
     { NULL, NULL, 0, FALSE} /* end marker */
   };
   struct noproxy list[]= {
+    { "www.example.com", "localhost,.example.com,.example.de", TRUE},
+    { "www.example.com.", "localhost,.example.com,.example.de", TRUE},
+    { "example.com", "localhost,.example.com,.example.de", TRUE},
+    { "example.com.", "localhost,.example.com,.example.de", TRUE},
+    { "www.example.com", "localhost,.example.com.,.example.de", TRUE},
+    { "www.example.com", "localhost,www.example.com.,.example.de", TRUE},
+    { "example.com", "localhost,example.com,.example.de", TRUE},
+    { "example.com.", "localhost,example.com,.example.de", TRUE},
+    { "www.example.com", "localhost,example.com,.example.de", FALSE},
     { "127.0.0.1", "127.0.0.1,localhost", TRUE},
     { "127.0.0.1", "127.0.0.1,localhost,", TRUE},
     { "127.0.0.1", "127.0.0.1/8,localhost,", TRUE},