hsts: make the HSTS read callback handle name dupes

Now the logic for handling name duplicates and picking the longest
expiry and strictest subdomain is the same for the callback as for when
reading from file.

Also strip trailing dots from the hostname added by the callback.

A minor side-effect is that the hostname provided by the callback can
now enable subdomains by starting the name with a dot, but we discourage
using such hostnames in documentation.

Amended test 1915 to verify.

Closes #21201
This commit is contained in:
Daniel Stenberg 2026-04-02 09:16:17 +02:00
parent dc20c91e04
commit e65ba1bd34
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
4 changed files with 99 additions and 82 deletions

View file

@ -42,13 +42,13 @@ http://%HOSTIP:%NOLISTENPORT/not-there/%TESTNUMBER
%if large-time
[0/4] 1.example.com 25250320 01:02:03
[1/4] 2.example.com 25250320 03:02:01
[2/4] 3.example.com 25250319 01:02:03
[2/4] .3.example.com 25250319 01:02:03
%else
[0/4] 1.example.com 20370320 01:02:03
[1/4] 2.example.com 20370320 03:02:01
[2/4] 3.example.com 20370319 01:02:03
[2/4] .3.example.com 20370319 01:02:03
%endif
[3/4] 4.example.com unlimited
[3/4] .4.example.com unlimited
First request returned 7
Second request returned 42
</stdout>

View file

@ -40,14 +40,17 @@ static CURLSTScode hstsread(CURL *curl, struct curl_hstsentry *e, void *userp)
static const struct entry preload_hosts[] = {
#if (SIZEOF_TIME_T < 5)
{ "1.example.com", "20370320 01:02:03" },
{ "2.example.com", "20370320 03:02:01" },
{ "2.example.com.", "20370320 03:02:01" },
{ "3.example.com", "20370319 01:02:03" },
{ ".3.example.com", "20270319 01:02:03" },
#else
{ "1.example.com", "25250320 01:02:03" },
{ "2.example.com", "25250320 03:02:01" },
{ "2.example.com.", "25250320 03:02:01" },
{ "3.example.com", "25250319 01:02:03" },
{ ".3.example.com", "22250319 01:02:03" },
#endif
{ "4.example.com", "" },
{ "4.example.com", "" }, /* forever */
{ ".4.example.com", "20370319 01:02:03" },
{ NULL, NULL } /* end of list marker */
};
@ -85,7 +88,8 @@ static CURLSTScode hstswrite(CURL *curl, struct curl_hstsentry *e,
{
(void)curl;
(void)userp;
curl_mprintf("[%zu/%zu] %s %s\n", i->index, i->total, e->name, e->expire);
curl_mprintf("[%zu/%zu] %s%s %s\n", i->index, i->total,
e->includeSubDomains ? "." : "", e->name, e->expire);
return CURLSTS_OK;
}