HSTS: cap the list at 1,000 entries

Avoid never-ending growth.

When adding more entries, it now deletes the first entry in the list,
which is the oldest added entry still held in memory. I decided to avoid
a Least Recently Used concept as I suspect with a list with this many
entries most entries have not been used, and we don't save the timestamp
of recent use anyway.

The net effect might (no matter what) be that the removed entry might
feel a bit "random" in the eyes of the user.

Verify with test 1674

Ref #21183
Closes #21190
This commit is contained in:
Daniel Stenberg 2026-04-01 10:24:06 +02:00
parent 4f3a0ef90d
commit 03a792b186
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
6 changed files with 139 additions and 2 deletions

View file

@ -91,6 +91,20 @@ void Curl_hsts_cleanup(struct hsts **hp)
}
}
/* append the new entry to the list after possibly removing an old entry
first */
static void hsts_append(struct hsts *h, struct stsentry *sts)
{
if(Curl_llist_count(&h->list) == MAX_HSTS_ENTRIES) {
/* It's full. Remove the first entry in the list */
struct Curl_llist_node *e = Curl_llist_head(&h->list);
struct stsentry *oldsts = Curl_node_elem(e);
Curl_node_remove(e);
hsts_free(oldsts);
}
Curl_llist_append(&h->list, sts, &sts->node);
}
static CURLcode hsts_create(struct hsts *h,
const char *hostname,
size_t hlen,
@ -111,7 +125,7 @@ static CURLcode hsts_create(struct hsts *h,
memcpy(sts->host, hostname, hlen);
sts->expires = expires;
sts->includeSubDomains = subdomains;
Curl_llist_append(&h->list, sts, &sts->node);
hsts_append(h, sts);
}
return CURLE_OK;
}

View file

@ -28,6 +28,8 @@
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_HSTS)
#include "llist.h"
#define MAX_HSTS_ENTRIES 1000
#if defined(DEBUGBUILD) || defined(UNITTESTS)
extern time_t deltatime;
#endif