[PATCH] hsts: improve subdomain handling
authorDaniel Stenberg <daniel@haxx.se>
Wed, 9 Oct 2024 08:04:35 +0000 (10:04 +0200)
committerSamuel Henrique <samueloph@debian.org>
Sun, 9 Mar 2025 10:45:45 +0000 (10:45 +0000)
- on load, only replace existing HSTS entries if there is a full host
  match

- on matching, prefer a full host match and secondary the longest tail
  subdomain match

Closes #15210

Backported by: Aquila Macedo Costa <aquilamacedo@riseup.net>.

Changes:
- Refresh patch context.

Gbp-Pq: Name CVE-2024-9681-0.patch

lib/hsts.c
tests/data/test1660

index 64cbae10c9872df0cb4032a06f1de39fd338941c..770170330490e121ad91cfcb94ebb3047b25ccb8 100644 (file)
@@ -248,12 +248,14 @@ CURLcode Curl_hsts_parse(struct hsts *h, const char *hostname,
 struct stsentry *Curl_hsts(struct hsts *h, const char *hostname,
                            bool subdomain)
 {
+  struct stsentry *bestsub = NULL;
   if(h) {
     char buffer[MAX_HSTS_HOSTLEN + 1];
     time_t now = time(NULL);
     size_t hlen = strlen(hostname);
     struct Curl_llist_element *e;
     struct Curl_llist_element *n;
+    size_t blen = 0;
 
     if((hlen > MAX_HSTS_HOSTLEN) || !hlen)
       return NULL;
@@ -278,15 +280,19 @@ struct stsentry *Curl_hsts(struct hsts *h, const char *hostname,
         if(ntail < hlen) {
           size_t offs = hlen - ntail;
           if((hostname[offs-1] == '.') &&
-             strncasecompare(&hostname[offs], sts->host, ntail))
-            return sts;
+             strncasecompare(&hostname[offs], sts->host, ntail) &&
+             (ntail > blen)) {
+            /* save the tail match with the longest tail */
+            bestsub = sts;
+            blen = ntail;
+          }
         }
       }
       if(strcasecompare(hostname, sts->host))
         return sts;
     }
   }
-  return NULL; /* no match */
+  return bestsub;
 }
 
 /*
@@ -438,8 +444,8 @@ static CURLcode hsts_add(struct hsts *h, char *line)
     e = Curl_hsts(h, p, subdomain);
     if(!e)
       result = hsts_create(h, p, subdomain, expires);
-    else {
-      /* the same host name, use the largest expire time */
+    else if(strcasecompare(p, e->host)) {
+      /* the same hostname, use the largest expire time */
       if(expires > e->expires)
         e->expires = expires;
     }
index cbbcf7587ce621f3493fa9d9409d3318615aa667..662026b400e425308da1aef78dbd3728a5c16a23 100644 (file)
@@ -52,7 +52,7 @@ this.example [this.example]: 1548400797
 Input 12: error 43
 Input 13: error 43
 Input 14: error 43
-3.example.com [example.com]: 1569905261 includeSubDomains
+3.example.com [3.example.com]: 1569905261 includeSubDomains
 3.example.com [example.com]: 1569905261 includeSubDomains
 foo.example.com [example.com]: 1569905261 includeSubDomains
 'foo.xample.com' is not HSTS