Coverage Report

Created: 2025-07-11 06:33

/src/PROJ/curl/lib/hsts.c
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
/*
25
 * The Strict-Transport-Security header is defined in RFC 6797:
26
 * https://datatracker.ietf.org/doc/html/rfc6797
27
 */
28
#include "curl_setup.h"
29
30
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_HSTS)
31
#include <curl/curl.h>
32
#include "urldata.h"
33
#include "llist.h"
34
#include "hsts.h"
35
#include "curl_get_line.h"
36
#include "sendf.h"
37
#include "parsedate.h"
38
#include "fopen.h"
39
#include "rename.h"
40
#include "share.h"
41
#include "strdup.h"
42
#include "curlx/strparse.h"
43
44
/* The last 3 #include files should be in this order */
45
#include "curl_printf.h"
46
#include "curl_memory.h"
47
#include "memdebug.h"
48
49
0
#define MAX_HSTS_LINE 4095
50
0
#define MAX_HSTS_HOSTLEN 2048
51
0
#define MAX_HSTS_DATELEN 256
52
0
#define UNLIMITED "unlimited"
53
54
#if defined(DEBUGBUILD) || defined(UNITTESTS)
55
/* to play well with debug builds, we can *set* a fixed time this will
56
   return */
57
time_t deltatime; /* allow for "adjustments" for unit test purposes */
58
static time_t hsts_debugtime(void *unused)
59
{
60
  const char *timestr = getenv("CURL_TIME");
61
  (void)unused;
62
  if(timestr) {
63
    curl_off_t val;
64
    if(!curlx_str_number(&timestr, &val, TIME_T_MAX))
65
      val += (curl_off_t)deltatime;
66
    return (time_t)val;
67
  }
68
  return time(NULL);
69
}
70
#undef time
71
#define time(x) hsts_debugtime(x)
72
#endif
73
74
struct hsts *Curl_hsts_init(void)
75
0
{
76
0
  struct hsts *h = calloc(1, sizeof(struct hsts));
77
0
  if(h) {
78
0
    Curl_llist_init(&h->list, NULL);
79
0
  }
80
0
  return h;
81
0
}
82
83
static void hsts_free(struct stsentry *e)
84
0
{
85
0
  free(CURL_UNCONST(e->host));
86
0
  free(e);
87
0
}
88
89
void Curl_hsts_cleanup(struct hsts **hp)
90
0
{
91
0
  struct hsts *h = *hp;
92
0
  if(h) {
93
0
    struct Curl_llist_node *e;
94
0
    struct Curl_llist_node *n;
95
0
    for(e = Curl_llist_head(&h->list); e; e = n) {
96
0
      struct stsentry *sts = Curl_node_elem(e);
97
0
      n = Curl_node_next(e);
98
0
      hsts_free(sts);
99
0
    }
100
0
    free(h->filename);
101
0
    free(h);
102
0
    *hp = NULL;
103
0
  }
104
0
}
105
106
static CURLcode hsts_create(struct hsts *h,
107
                            const char *hostname,
108
                            size_t hlen,
109
                            bool subdomains,
110
                            curl_off_t expires)
111
0
{
112
0
  DEBUGASSERT(h);
113
0
  DEBUGASSERT(hostname);
114
115
0
  if(hlen && (hostname[hlen - 1] == '.'))
116
    /* strip off any trailing dot */
117
0
    --hlen;
118
0
  if(hlen) {
119
0
    char *duphost;
120
0
    struct stsentry *sts = calloc(1, sizeof(struct stsentry));
121
0
    if(!sts)
122
0
      return CURLE_OUT_OF_MEMORY;
123
124
0
    duphost = Curl_memdup0(hostname, hlen);
125
0
    if(!duphost) {
126
0
      free(sts);
127
0
      return CURLE_OUT_OF_MEMORY;
128
0
    }
129
130
0
    sts->host = duphost;
131
0
    sts->expires = expires;
132
0
    sts->includeSubDomains = subdomains;
133
0
    Curl_llist_append(&h->list, sts, &sts->node);
134
0
  }
135
0
  return CURLE_OK;
136
0
}
137
138
CURLcode Curl_hsts_parse(struct hsts *h, const char *hostname,
139
                         const char *header)
140
0
{
141
0
  const char *p = header;
142
0
  curl_off_t expires = 0;
143
0
  bool gotma = FALSE;
144
0
  bool gotinc = FALSE;
145
0
  bool subdomains = FALSE;
146
0
  struct stsentry *sts;
147
0
  time_t now = time(NULL);
148
0
  size_t hlen = strlen(hostname);
149
150
0
  if(Curl_host_is_ipnum(hostname))
151
    /* "explicit IP address identification of all forms is excluded."
152
       / RFC 6797 */
153
0
    return CURLE_OK;
154
155
0
  do {
156
0
    curlx_str_passblanks(&p);
157
0
    if(curl_strnequal("max-age", p, 7)) {
158
0
      bool quoted = FALSE;
159
0
      int rc;
160
161
0
      if(gotma)
162
0
        return CURLE_BAD_FUNCTION_ARGUMENT;
163
164
0
      p += 7;
165
0
      curlx_str_passblanks(&p);
166
0
      if(curlx_str_single(&p, '='))
167
0
        return CURLE_BAD_FUNCTION_ARGUMENT;
168
0
      curlx_str_passblanks(&p);
169
170
0
      if(!curlx_str_single(&p, '\"'))
171
0
        quoted = TRUE;
172
173
0
      rc = curlx_str_number(&p, &expires, TIME_T_MAX);
174
0
      if(rc == STRE_OVERFLOW)
175
0
        expires = CURL_OFF_T_MAX;
176
0
      else if(rc)
177
        /* invalid max-age */
178
0
        return CURLE_BAD_FUNCTION_ARGUMENT;
179
180
0
      if(quoted) {
181
0
        if(*p != '\"')
182
0
          return CURLE_BAD_FUNCTION_ARGUMENT;
183
0
        p++;
184
0
      }
185
0
      gotma = TRUE;
186
0
    }
187
0
    else if(curl_strnequal("includesubdomains", p, 17)) {
188
0
      if(gotinc)
189
0
        return CURLE_BAD_FUNCTION_ARGUMENT;
190
0
      subdomains = TRUE;
191
0
      p += 17;
192
0
      gotinc = TRUE;
193
0
    }
194
0
    else {
195
      /* unknown directive, do a lame attempt to skip */
196
0
      while(*p && (*p != ';'))
197
0
        p++;
198
0
    }
199
200
0
    curlx_str_passblanks(&p);
201
0
    if(*p == ';')
202
0
      p++;
203
0
  } while(*p);
204
205
0
  if(!gotma)
206
    /* max-age is mandatory */
207
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
208
209
0
  if(!expires) {
210
    /* remove the entry if present verbatim (without subdomain match) */
211
0
    sts = Curl_hsts(h, hostname, hlen, FALSE);
212
0
    if(sts) {
213
0
      Curl_node_remove(&sts->node);
214
0
      hsts_free(sts);
215
0
    }
216
0
    return CURLE_OK;
217
0
  }
218
219
0
  if(CURL_OFF_T_MAX - now < expires)
220
    /* would overflow, use maximum value */
221
0
    expires = CURL_OFF_T_MAX;
222
0
  else
223
0
    expires += now;
224
225
  /* check if it already exists */
226
0
  sts = Curl_hsts(h, hostname, hlen, FALSE);
227
0
  if(sts) {
228
    /* just update these fields */
229
0
    sts->expires = expires;
230
0
    sts->includeSubDomains = subdomains;
231
0
  }
232
0
  else
233
0
    return hsts_create(h, hostname, hlen, subdomains, expires);
234
235
0
  return CURLE_OK;
236
0
}
237
238
/*
239
 * Return TRUE if the given hostname is currently an HSTS one.
240
 *
241
 * The 'subdomain' argument tells the function if subdomain matching should be
242
 * attempted.
243
 */
244
struct stsentry *Curl_hsts(struct hsts *h, const char *hostname,
245
                           size_t hlen, bool subdomain)
246
0
{
247
0
  struct stsentry *bestsub = NULL;
248
0
  if(h) {
249
0
    time_t now = time(NULL);
250
0
    struct Curl_llist_node *e;
251
0
    struct Curl_llist_node *n;
252
0
    size_t blen = 0;
253
254
0
    if((hlen > MAX_HSTS_HOSTLEN) || !hlen)
255
0
      return NULL;
256
0
    if(hostname[hlen-1] == '.')
257
      /* remove the trailing dot */
258
0
      --hlen;
259
260
0
    for(e = Curl_llist_head(&h->list); e; e = n) {
261
0
      struct stsentry *sts = Curl_node_elem(e);
262
0
      size_t ntail;
263
0
      n = Curl_node_next(e);
264
0
      if(sts->expires <= now) {
265
        /* remove expired entries */
266
0
        Curl_node_remove(&sts->node);
267
0
        hsts_free(sts);
268
0
        continue;
269
0
      }
270
0
      ntail = strlen(sts->host);
271
0
      if((subdomain && sts->includeSubDomains) && (ntail < hlen)) {
272
0
        size_t offs = hlen - ntail;
273
0
        if((hostname[offs-1] == '.') &&
274
0
           curl_strnequal(&hostname[offs], sts->host, ntail) &&
275
0
           (ntail > blen)) {
276
          /* save the tail match with the longest tail */
277
0
          bestsub = sts;
278
0
          blen = ntail;
279
0
        }
280
0
      }
281
      /* avoid curl_strequal because the host name is not null-terminated */
282
0
      if((hlen == ntail) && curl_strnequal(hostname, sts->host, hlen))
283
0
        return sts;
284
0
    }
285
0
  }
286
0
  return bestsub;
287
0
}
288
289
/*
290
 * Send this HSTS entry to the write callback.
291
 */
292
static CURLcode hsts_push(struct Curl_easy *data,
293
                          struct curl_index *i,
294
                          struct stsentry *sts,
295
                          bool *stop)
296
0
{
297
0
  struct curl_hstsentry e;
298
0
  CURLSTScode sc;
299
0
  struct tm stamp;
300
0
  CURLcode result;
301
302
0
  e.name = (char *)CURL_UNCONST(sts->host);
303
0
  e.namelen = strlen(sts->host);
304
0
  e.includeSubDomains = sts->includeSubDomains;
305
306
0
  if(sts->expires != TIME_T_MAX) {
307
0
    result = Curl_gmtime((time_t)sts->expires, &stamp);
308
0
    if(result)
309
0
      return result;
310
311
0
    msnprintf(e.expire, sizeof(e.expire), "%d%02d%02d %02d:%02d:%02d",
312
0
              stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
313
0
              stamp.tm_hour, stamp.tm_min, stamp.tm_sec);
314
0
  }
315
0
  else
316
0
    strcpy(e.expire, UNLIMITED);
317
318
0
  sc = data->set.hsts_write(data, &e, i,
319
0
                            data->set.hsts_write_userp);
320
0
  *stop = (sc != CURLSTS_OK);
321
0
  return sc == CURLSTS_FAIL ? CURLE_BAD_FUNCTION_ARGUMENT : CURLE_OK;
322
0
}
323
324
/*
325
 * Write this single hsts entry to a single output line
326
 */
327
static CURLcode hsts_out(struct stsentry *sts, FILE *fp)
328
0
{
329
0
  struct tm stamp;
330
0
  if(sts->expires != TIME_T_MAX) {
331
0
    CURLcode result = Curl_gmtime((time_t)sts->expires, &stamp);
332
0
    if(result)
333
0
      return result;
334
0
    fprintf(fp, "%s%s \"%d%02d%02d %02d:%02d:%02d\"\n",
335
0
            sts->includeSubDomains ? ".": "", sts->host,
336
0
            stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
337
0
            stamp.tm_hour, stamp.tm_min, stamp.tm_sec);
338
0
  }
339
0
  else
340
0
    fprintf(fp, "%s%s \"%s\"\n",
341
0
            sts->includeSubDomains ? ".": "", sts->host, UNLIMITED);
342
0
  return CURLE_OK;
343
0
}
344
345
346
/*
347
 * Curl_https_save() writes the HSTS cache to file and callback.
348
 */
349
CURLcode Curl_hsts_save(struct Curl_easy *data, struct hsts *h,
350
                        const char *file)
351
0
{
352
0
  struct Curl_llist_node *e;
353
0
  struct Curl_llist_node *n;
354
0
  CURLcode result = CURLE_OK;
355
0
  FILE *out;
356
0
  char *tempstore = NULL;
357
358
0
  if(!h)
359
    /* no cache activated */
360
0
    return CURLE_OK;
361
362
  /* if no new name is given, use the one we stored from the load */
363
0
  if(!file && h->filename)
364
0
    file = h->filename;
365
366
0
  if((h->flags & CURLHSTS_READONLYFILE) || !file || !file[0])
367
    /* marked as read-only, no file or zero length filename */
368
0
    goto skipsave;
369
370
0
  result = Curl_fopen(data, file, &out, &tempstore);
371
0
  if(!result) {
372
0
    fputs("# Your HSTS cache. https://curl.se/docs/hsts.html\n"
373
0
          "# This file was generated by libcurl! Edit at your own risk.\n",
374
0
          out);
375
0
    for(e = Curl_llist_head(&h->list); e; e = n) {
376
0
      struct stsentry *sts = Curl_node_elem(e);
377
0
      n = Curl_node_next(e);
378
0
      result = hsts_out(sts, out);
379
0
      if(result)
380
0
        break;
381
0
    }
382
0
    fclose(out);
383
0
    if(!result && tempstore && Curl_rename(tempstore, file))
384
0
      result = CURLE_WRITE_ERROR;
385
386
0
    if(result && tempstore)
387
0
      unlink(tempstore);
388
0
  }
389
0
  free(tempstore);
390
0
skipsave:
391
0
  if(data->set.hsts_write) {
392
    /* if there is a write callback */
393
0
    struct curl_index i; /* count */
394
0
    i.total = Curl_llist_count(&h->list);
395
0
    i.index = 0;
396
0
    for(e = Curl_llist_head(&h->list); e; e = n) {
397
0
      struct stsentry *sts = Curl_node_elem(e);
398
0
      bool stop;
399
0
      n = Curl_node_next(e);
400
0
      result = hsts_push(data, &i, sts, &stop);
401
0
      if(result || stop)
402
0
        break;
403
0
      i.index++;
404
0
    }
405
0
  }
406
0
  return result;
407
0
}
408
409
/* only returns SERIOUS errors */
410
static CURLcode hsts_add(struct hsts *h, const char *line)
411
0
{
412
  /* Example lines:
413
     example.com "20191231 10:00:00"
414
     .example.net "20191231 10:00:00"
415
   */
416
0
  struct Curl_str host;
417
0
  struct Curl_str date;
418
419
0
  if(curlx_str_word(&line, &host, MAX_HSTS_HOSTLEN) ||
420
0
     curlx_str_singlespace(&line) ||
421
0
     curlx_str_quotedword(&line, &date, MAX_HSTS_DATELEN) ||
422
0
     curlx_str_newline(&line))
423
0
    ;
424
0
  else {
425
0
    CURLcode result = CURLE_OK;
426
0
    bool subdomain = FALSE;
427
0
    struct stsentry *e;
428
0
    char dbuf[MAX_HSTS_DATELEN + 1];
429
0
    time_t expires;
430
0
    const char *hp = curlx_str(&host);
431
432
    /* The date parser works on a null-terminated string. The maximum length
433
       is upheld by curlx_str_quotedword(). */
434
0
    memcpy(dbuf, curlx_str(&date), curlx_strlen(&date));
435
0
    dbuf[curlx_strlen(&date)] = 0;
436
437
0
    expires = strcmp(dbuf, UNLIMITED) ? Curl_getdate_capped(dbuf) :
438
0
      TIME_T_MAX;
439
440
0
    if(hp[0] == '.') {
441
0
      curlx_str_nudge(&host, 1);
442
0
      subdomain = TRUE;
443
0
    }
444
    /* only add it if not already present */
445
0
    e = Curl_hsts(h, curlx_str(&host), curlx_strlen(&host), subdomain);
446
0
    if(!e)
447
0
      result = hsts_create(h, curlx_str(&host), curlx_strlen(&host),
448
0
                           subdomain, expires);
449
0
    else if(curlx_str_casecompare(&host, e->host)) {
450
      /* the same hostname, use the largest expire time */
451
0
      if(expires > e->expires)
452
0
        e->expires = expires;
453
0
    }
454
0
    if(result)
455
0
      return result;
456
0
  }
457
458
0
  return CURLE_OK;
459
0
}
460
461
/*
462
 * Load HSTS data from callback.
463
 *
464
 */
465
static CURLcode hsts_pull(struct Curl_easy *data, struct hsts *h)
466
0
{
467
  /* if the HSTS read callback is set, use it */
468
0
  if(data->set.hsts_read) {
469
0
    CURLSTScode sc;
470
0
    DEBUGASSERT(h);
471
0
    do {
472
0
      char buffer[MAX_HSTS_HOSTLEN + 1];
473
0
      struct curl_hstsentry e;
474
0
      e.name = buffer;
475
0
      e.namelen = sizeof(buffer)-1;
476
0
      e.includeSubDomains = FALSE; /* default */
477
0
      e.expire[0] = 0;
478
0
      e.name[0] = 0; /* just to make it clean */
479
0
      sc = data->set.hsts_read(data, &e, data->set.hsts_read_userp);
480
0
      if(sc == CURLSTS_OK) {
481
0
        time_t expires;
482
0
        CURLcode result;
483
0
        DEBUGASSERT(e.name[0]);
484
0
        if(!e.name[0])
485
          /* bail out if no name was stored */
486
0
          return CURLE_BAD_FUNCTION_ARGUMENT;
487
0
        if(e.expire[0])
488
0
          expires = Curl_getdate_capped(e.expire);
489
0
        else
490
0
          expires = TIME_T_MAX; /* the end of time */
491
0
        result = hsts_create(h, e.name, strlen(e.name),
492
                             /* bitfield to bool conversion: */
493
0
                             e.includeSubDomains ? TRUE : FALSE,
494
0
                             expires);
495
0
        if(result)
496
0
          return result;
497
0
      }
498
0
      else if(sc == CURLSTS_FAIL)
499
0
        return CURLE_ABORTED_BY_CALLBACK;
500
0
    } while(sc == CURLSTS_OK);
501
0
  }
502
0
  return CURLE_OK;
503
0
}
504
505
/*
506
 * Load the HSTS cache from the given file. The text based line-oriented file
507
 * format is documented here: https://curl.se/docs/hsts.html
508
 *
509
 * This function only returns error on major problems that prevent hsts
510
 * handling to work completely. It will ignore individual syntactical errors
511
 * etc.
512
 */
513
static CURLcode hsts_load(struct hsts *h, const char *file)
514
0
{
515
0
  CURLcode result = CURLE_OK;
516
0
  FILE *fp;
517
518
  /* we need a private copy of the filename so that the hsts cache file
519
     name survives an easy handle reset */
520
0
  free(h->filename);
521
0
  h->filename = strdup(file);
522
0
  if(!h->filename)
523
0
    return CURLE_OUT_OF_MEMORY;
524
525
0
  fp = fopen(file, FOPEN_READTEXT);
526
0
  if(fp) {
527
0
    struct dynbuf buf;
528
0
    curlx_dyn_init(&buf, MAX_HSTS_LINE);
529
0
    while(Curl_get_line(&buf, fp)) {
530
0
      const char *lineptr = curlx_dyn_ptr(&buf);
531
0
      curlx_str_passblanks(&lineptr);
532
533
      /*
534
       * Skip empty or commented lines, since we know the line will have a
535
       * trailing newline from Curl_get_line we can treat length 1 as empty.
536
       */
537
0
      if((*lineptr == '#') || strlen(lineptr) <= 1)
538
0
        continue;
539
540
0
      hsts_add(h, lineptr);
541
0
    }
542
0
    curlx_dyn_free(&buf); /* free the line buffer */
543
0
    fclose(fp);
544
0
  }
545
0
  return result;
546
0
}
547
548
/*
549
 * Curl_hsts_loadfile() loads HSTS from file
550
 */
551
CURLcode Curl_hsts_loadfile(struct Curl_easy *data,
552
                            struct hsts *h, const char *file)
553
0
{
554
0
  DEBUGASSERT(h);
555
0
  (void)data;
556
0
  return hsts_load(h, file);
557
0
}
558
559
/*
560
 * Curl_hsts_loadcb() loads HSTS from callback
561
 */
562
CURLcode Curl_hsts_loadcb(struct Curl_easy *data, struct hsts *h)
563
0
{
564
0
  if(h)
565
0
    return hsts_pull(data, h);
566
0
  return CURLE_OK;
567
0
}
568
569
void Curl_hsts_loadfiles(struct Curl_easy *data)
570
0
{
571
0
  struct curl_slist *l = data->state.hstslist;
572
0
  if(l) {
573
0
    Curl_share_lock(data, CURL_LOCK_DATA_HSTS, CURL_LOCK_ACCESS_SINGLE);
574
575
0
    while(l) {
576
0
      (void)Curl_hsts_loadfile(data, data->hsts, l->data);
577
0
      l = l->next;
578
0
    }
579
0
    Curl_share_unlock(data, CURL_LOCK_DATA_HSTS);
580
0
  }
581
0
}
582
583
#endif /* CURL_DISABLE_HTTP || CURL_DISABLE_HSTS */