tough-cookie 6.0.0 → 6.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -928,7 +928,7 @@ declare class ParameterError extends Error {
928
928
  * The version of `tough-cookie`
929
929
  * @public
930
930
  */
931
- declare const version = "6.0.0";
931
+ declare const version = "6.0.1";
932
932
 
933
933
  /**
934
934
  * Transforms a domain name into a canonical domain name. The canonical domain name is a domain name
@@ -1754,91 +1754,13 @@ declare function formatDate(date: Date): string;
1754
1754
  *
1755
1755
  * @remarks
1756
1756
  *
1757
- * ### RFC6265 - 5.1.1. Dates
1757
+ * This implementation is compliant with RFC6265 Section 5.1.1 and incorporates
1758
+ * {@link https://www.rfc-editor.org/errata/eid4148 | RFC6265 Erratum 4148 - Grammar Fixed}
1759
+ * which corrects the ABNF grammar for day-of-month, year, and time to make trailing
1760
+ * non-digit characters optional (changing `( )` to `[ ]`).
1758
1761
  *
1759
- * The user agent MUST use an algorithm equivalent to the following
1760
- * algorithm to parse a cookie-date. Note that the various boolean
1761
- * flags defined as a part of the algorithm (i.e., found-time, found-
1762
- * day-of-month, found-month, found-year) are initially "not set".
1763
- *
1764
- * 1. Using the grammar below, divide the cookie-date into date-tokens.
1765
- *
1766
- * ```
1767
- * cookie-date = *delimiter date-token-list *delimiter
1768
- * date-token-list = date-token *( 1*delimiter date-token )
1769
- * date-token = 1*non-delimiter
1770
- *
1771
- * delimiter = %x09 / %x20-2F / %x3B-40 / %x5B-60 / %x7B-7E
1772
- * non-delimiter = %x00-08 / %x0A-1F / DIGIT / ":" / ALPHA / %x7F-FF
1773
- * non-digit = %x00-2F / %x3A-FF
1774
- *
1775
- * day-of-month = 1*2DIGIT ( non-digit *OCTET )
1776
- * month = ( "jan" / "feb" / "mar" / "apr" /
1777
- * "may" / "jun" / "jul" / "aug" /
1778
- * "sep" / "oct" / "nov" / "dec" ) *OCTET
1779
- * year = 2*4DIGIT ( non-digit *OCTET )
1780
- * time = hms-time ( non-digit *OCTET )
1781
- * hms-time = time-field ":" time-field ":" time-field
1782
- * time-field = 1*2DIGIT
1783
- * ```
1784
- *
1785
- * 2. Process each date-token sequentially in the order the date-tokens
1786
- * appear in the cookie-date:
1787
- *
1788
- * 1. If the found-time flag is not set and the token matches the
1789
- * time production, set the found-time flag and set the hour-
1790
- * value, minute-value, and second-value to the numbers denoted
1791
- * by the digits in the date-token, respectively. Skip the
1792
- * remaining sub-steps and continue to the next date-token.
1793
- *
1794
- * 2. If the found-day-of-month flag is not set and the date-token
1795
- * matches the day-of-month production, set the found-day-of-
1796
- * month flag and set the day-of-month-value to the number
1797
- * denoted by the date-token. Skip the remaining sub-steps and
1798
- * continue to the next date-token.
1799
- *
1800
- * 3. If the found-month flag is not set and the date-token matches
1801
- * the month production, set the found-month flag and set the
1802
- * month-value to the month denoted by the date-token. Skip the
1803
- * remaining sub-steps and continue to the next date-token.
1804
- *
1805
- * 4. If the found-year flag is not set and the date-token matches
1806
- * the year production, set the found-year flag and set the
1807
- * year-value to the number denoted by the date-token. Skip the
1808
- * remaining sub-steps and continue to the next date-token.
1809
- *
1810
- * 3. If the year-value is greater than or equal to 70 and less than or
1811
- * equal to 99, increment the year-value by 1900.
1812
- *
1813
- * 4. If the year-value is greater than or equal to 0 and less than or
1814
- * equal to 69, increment the year-value by 2000.
1815
- *
1816
- * 1. NOTE: Some existing user agents interpret two-digit years differently.
1817
- *
1818
- * 5. Abort these steps and fail to parse the cookie-date if:
1819
- *
1820
- * - at least one of the found-day-of-month, found-month, found-
1821
- * year, or found-time flags is not set,
1822
- *
1823
- * - the day-of-month-value is less than 1 or greater than 31,
1824
- *
1825
- * - the year-value is less than 1601,
1826
- *
1827
- * - the hour-value is greater than 23,
1828
- *
1829
- * - the minute-value is greater than 59, or
1830
- *
1831
- * - the second-value is greater than 59.
1832
- *
1833
- * (Note that leap seconds cannot be represented in this syntax.)
1834
- *
1835
- * 6. Let the parsed-cookie-date be the date whose day-of-month, month,
1836
- * year, hour, minute, and second (in UTC) are the day-of-month-
1837
- * value, the month-value, the year-value, the hour-value, the
1838
- * minute-value, and the second-value, respectively. If no such
1839
- * date exists, abort these steps and fail to parse the cookie-date.
1840
- *
1841
- * 7. Return the parsed-cookie-date as the result of this algorithm.
1762
+ * Also compatible with {@link https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-21 | draft-ietf-httpbis-rfc6265bis-21}
1763
+ * which maintains the same date parsing algorithm with additional clarifications.
1842
1764
  *
1843
1765
  * @example
1844
1766
  * ```
@@ -1846,6 +1768,7 @@ declare function formatDate(date: Date): string;
1846
1768
  * ```
1847
1769
  *
1848
1770
  * @param cookieDate - the cookie date string
1771
+ * @returns Date if valid, undefined if invalid
1849
1772
  * @public
1850
1773
  */
1851
1774
  declare function parseDate(cookieDate: Nullable<string>): Date | undefined;
package/dist/index.d.ts CHANGED
@@ -928,7 +928,7 @@ declare class ParameterError extends Error {
928
928
  * The version of `tough-cookie`
929
929
  * @public
930
930
  */
931
- declare const version = "6.0.0";
931
+ declare const version = "6.0.1";
932
932
 
933
933
  /**
934
934
  * Transforms a domain name into a canonical domain name. The canonical domain name is a domain name
@@ -1754,91 +1754,13 @@ declare function formatDate(date: Date): string;
1754
1754
  *
1755
1755
  * @remarks
1756
1756
  *
1757
- * ### RFC6265 - 5.1.1. Dates
1757
+ * This implementation is compliant with RFC6265 Section 5.1.1 and incorporates
1758
+ * {@link https://www.rfc-editor.org/errata/eid4148 | RFC6265 Erratum 4148 - Grammar Fixed}
1759
+ * which corrects the ABNF grammar for day-of-month, year, and time to make trailing
1760
+ * non-digit characters optional (changing `( )` to `[ ]`).
1758
1761
  *
1759
- * The user agent MUST use an algorithm equivalent to the following
1760
- * algorithm to parse a cookie-date. Note that the various boolean
1761
- * flags defined as a part of the algorithm (i.e., found-time, found-
1762
- * day-of-month, found-month, found-year) are initially "not set".
1763
- *
1764
- * 1. Using the grammar below, divide the cookie-date into date-tokens.
1765
- *
1766
- * ```
1767
- * cookie-date = *delimiter date-token-list *delimiter
1768
- * date-token-list = date-token *( 1*delimiter date-token )
1769
- * date-token = 1*non-delimiter
1770
- *
1771
- * delimiter = %x09 / %x20-2F / %x3B-40 / %x5B-60 / %x7B-7E
1772
- * non-delimiter = %x00-08 / %x0A-1F / DIGIT / ":" / ALPHA / %x7F-FF
1773
- * non-digit = %x00-2F / %x3A-FF
1774
- *
1775
- * day-of-month = 1*2DIGIT ( non-digit *OCTET )
1776
- * month = ( "jan" / "feb" / "mar" / "apr" /
1777
- * "may" / "jun" / "jul" / "aug" /
1778
- * "sep" / "oct" / "nov" / "dec" ) *OCTET
1779
- * year = 2*4DIGIT ( non-digit *OCTET )
1780
- * time = hms-time ( non-digit *OCTET )
1781
- * hms-time = time-field ":" time-field ":" time-field
1782
- * time-field = 1*2DIGIT
1783
- * ```
1784
- *
1785
- * 2. Process each date-token sequentially in the order the date-tokens
1786
- * appear in the cookie-date:
1787
- *
1788
- * 1. If the found-time flag is not set and the token matches the
1789
- * time production, set the found-time flag and set the hour-
1790
- * value, minute-value, and second-value to the numbers denoted
1791
- * by the digits in the date-token, respectively. Skip the
1792
- * remaining sub-steps and continue to the next date-token.
1793
- *
1794
- * 2. If the found-day-of-month flag is not set and the date-token
1795
- * matches the day-of-month production, set the found-day-of-
1796
- * month flag and set the day-of-month-value to the number
1797
- * denoted by the date-token. Skip the remaining sub-steps and
1798
- * continue to the next date-token.
1799
- *
1800
- * 3. If the found-month flag is not set and the date-token matches
1801
- * the month production, set the found-month flag and set the
1802
- * month-value to the month denoted by the date-token. Skip the
1803
- * remaining sub-steps and continue to the next date-token.
1804
- *
1805
- * 4. If the found-year flag is not set and the date-token matches
1806
- * the year production, set the found-year flag and set the
1807
- * year-value to the number denoted by the date-token. Skip the
1808
- * remaining sub-steps and continue to the next date-token.
1809
- *
1810
- * 3. If the year-value is greater than or equal to 70 and less than or
1811
- * equal to 99, increment the year-value by 1900.
1812
- *
1813
- * 4. If the year-value is greater than or equal to 0 and less than or
1814
- * equal to 69, increment the year-value by 2000.
1815
- *
1816
- * 1. NOTE: Some existing user agents interpret two-digit years differently.
1817
- *
1818
- * 5. Abort these steps and fail to parse the cookie-date if:
1819
- *
1820
- * - at least one of the found-day-of-month, found-month, found-
1821
- * year, or found-time flags is not set,
1822
- *
1823
- * - the day-of-month-value is less than 1 or greater than 31,
1824
- *
1825
- * - the year-value is less than 1601,
1826
- *
1827
- * - the hour-value is greater than 23,
1828
- *
1829
- * - the minute-value is greater than 59, or
1830
- *
1831
- * - the second-value is greater than 59.
1832
- *
1833
- * (Note that leap seconds cannot be represented in this syntax.)
1834
- *
1835
- * 6. Let the parsed-cookie-date be the date whose day-of-month, month,
1836
- * year, hour, minute, and second (in UTC) are the day-of-month-
1837
- * value, the month-value, the year-value, the hour-value, the
1838
- * minute-value, and the second-value, respectively. If no such
1839
- * date exists, abort these steps and fail to parse the cookie-date.
1840
- *
1841
- * 7. Return the parsed-cookie-date as the result of this algorithm.
1762
+ * Also compatible with {@link https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-21 | draft-ietf-httpbis-rfc6265bis-21}
1763
+ * which maintains the same date parsing algorithm with additional clarifications.
1842
1764
  *
1843
1765
  * @example
1844
1766
  * ```
@@ -1846,6 +1768,7 @@ declare function formatDate(date: Date): string;
1846
1768
  * ```
1847
1769
  *
1848
1770
  * @param cookieDate - the cookie date string
1771
+ * @returns Date if valid, undefined if invalid
1849
1772
  * @public
1850
1773
  */
1851
1774
  declare function parseDate(cookieDate: Nullable<string>): Date | undefined;
package/dist/index.js CHANGED
@@ -384,7 +384,7 @@ var ParameterError = class extends Error {
384
384
  };
385
385
 
386
386
  // lib/version.ts
387
- var version = "6.0.0";
387
+ var version = "6.0.1";
388
388
 
389
389
  // lib/cookie/constants.ts
390
390
  var PrefixSecurityEnum = {
@@ -439,145 +439,114 @@ function formatDate(date) {
439
439
  }
440
440
 
441
441
  // lib/cookie/parseDate.ts
442
- var DATE_DELIM = /[\x09\x20-\x2F\x3B-\x40\x5B-\x60\x7B-\x7E]/;
443
- var MONTH_TO_NUM = {
444
- jan: 0,
445
- feb: 1,
446
- mar: 2,
447
- apr: 3,
448
- may: 4,
449
- jun: 5,
450
- jul: 6,
451
- aug: 7,
452
- sep: 8,
453
- oct: 9,
454
- nov: 10,
455
- dec: 11
456
- };
457
- function parseDigits(token, minDigits, maxDigits, trailingOK) {
458
- let count = 0;
459
- while (count < token.length) {
460
- const c = token.charCodeAt(count);
461
- if (c <= 47 || c >= 58) {
462
- break;
463
- }
464
- count++;
465
- }
466
- if (count < minDigits || count > maxDigits) {
467
- return;
468
- }
469
- if (!trailingOK && count != token.length) {
470
- return;
471
- }
472
- return parseInt(token.slice(0, count), 10);
473
- }
474
- function parseTime(token) {
475
- const parts = token.split(":");
476
- const result = [0, 0, 0];
477
- if (parts.length !== 3) {
478
- return;
479
- }
480
- for (let i = 0; i < 3; i++) {
481
- const trailingOK = i == 2;
482
- const numPart = parts[i];
483
- if (numPart === void 0) {
484
- return;
485
- }
486
- const num = parseDigits(numPart, 1, 2, trailingOK);
487
- if (num === void 0) {
488
- return;
489
- }
490
- result[i] = num;
491
- }
492
- return result;
493
- }
494
- function parseMonth(token) {
495
- token = String(token).slice(0, 3).toLowerCase();
496
- switch (token) {
497
- case "jan":
498
- return MONTH_TO_NUM.jan;
499
- case "feb":
500
- return MONTH_TO_NUM.feb;
501
- case "mar":
502
- return MONTH_TO_NUM.mar;
503
- case "apr":
504
- return MONTH_TO_NUM.apr;
505
- case "may":
506
- return MONTH_TO_NUM.may;
507
- case "jun":
508
- return MONTH_TO_NUM.jun;
509
- case "jul":
510
- return MONTH_TO_NUM.jul;
511
- case "aug":
512
- return MONTH_TO_NUM.aug;
513
- case "sep":
514
- return MONTH_TO_NUM.sep;
515
- case "oct":
516
- return MONTH_TO_NUM.oct;
517
- case "nov":
518
- return MONTH_TO_NUM.nov;
519
- case "dec":
520
- return MONTH_TO_NUM.dec;
521
- default:
522
- return;
523
- }
524
- }
525
442
  function parseDate(cookieDate) {
526
443
  if (!cookieDate) {
527
- return;
528
- }
529
- const tokens = cookieDate.split(DATE_DELIM);
530
- let hour;
531
- let minute;
532
- let second;
533
- let dayOfMonth;
534
- let month;
535
- let year;
536
- for (let i = 0; i < tokens.length; i++) {
537
- const token = (tokens[i] ?? "").trim();
538
- if (!token.length) {
539
- continue;
540
- }
541
- if (second === void 0) {
542
- const result = parseTime(token);
543
- if (result) {
544
- hour = result[0];
545
- minute = result[1];
546
- second = result[2];
547
- continue;
444
+ return void 0;
445
+ }
446
+ const flags = {
447
+ foundTime: void 0,
448
+ foundDayOfMonth: void 0,
449
+ foundMonth: void 0,
450
+ foundYear: void 0
451
+ };
452
+ const dateTokens = cookieDate.split(DELIMITER).filter((token) => token.length > 0);
453
+ for (const dateToken of dateTokens) {
454
+ if (flags.foundTime === void 0) {
455
+ const [, hours, minutes, seconds] = TIME.exec(dateToken) || [];
456
+ if (hours != void 0 && minutes != void 0 && seconds != void 0) {
457
+ const parsedHours = parseInt(hours, 10);
458
+ const parsedMinutes = parseInt(minutes, 10);
459
+ const parsedSeconds = parseInt(seconds, 10);
460
+ if (!isNaN(parsedHours) && !isNaN(parsedMinutes) && !isNaN(parsedSeconds)) {
461
+ flags.foundTime = {
462
+ hours: parsedHours,
463
+ minutes: parsedMinutes,
464
+ seconds: parsedSeconds
465
+ };
466
+ continue;
467
+ }
548
468
  }
549
469
  }
550
- if (dayOfMonth === void 0) {
551
- const result = parseDigits(token, 1, 2, true);
552
- if (result !== void 0) {
553
- dayOfMonth = result;
470
+ if (flags.foundDayOfMonth === void 0 && DAY_OF_MONTH.test(dateToken)) {
471
+ const dayOfMonth = parseInt(dateToken, 10);
472
+ if (!isNaN(dayOfMonth)) {
473
+ flags.foundDayOfMonth = dayOfMonth;
554
474
  continue;
555
475
  }
556
476
  }
557
- if (month === void 0) {
558
- const result = parseMonth(token);
559
- if (result !== void 0) {
560
- month = result;
477
+ if (flags.foundMonth === void 0 && MONTH.test(dateToken)) {
478
+ const month = months.indexOf(dateToken.substring(0, 3).toLowerCase());
479
+ if (month >= 0 && month <= 11) {
480
+ flags.foundMonth = month;
561
481
  continue;
562
482
  }
563
483
  }
564
- if (year === void 0) {
565
- const result = parseDigits(token, 2, 4, true);
566
- if (result !== void 0) {
567
- year = result;
568
- if (year >= 70 && year <= 99) {
569
- year += 1900;
570
- } else if (year >= 0 && year <= 69) {
571
- year += 2e3;
572
- }
484
+ if (flags.foundYear === void 0 && YEAR.test(dateToken)) {
485
+ const parsedYear = parseInt(dateToken, 10);
486
+ if (!isNaN(parsedYear)) {
487
+ flags.foundYear = parsedYear;
488
+ continue;
573
489
  }
574
490
  }
575
491
  }
576
- if (dayOfMonth === void 0 || month === void 0 || year === void 0 || hour === void 0 || minute === void 0 || second === void 0 || dayOfMonth < 1 || dayOfMonth > 31 || year < 1601 || hour > 23 || minute > 59 || second > 59) {
577
- return;
492
+ if (flags.foundYear !== void 0 && flags.foundYear >= 70 && flags.foundYear <= 99) {
493
+ flags.foundYear += 1900;
494
+ }
495
+ if (flags.foundYear !== void 0 && flags.foundYear >= 0 && flags.foundYear <= 69) {
496
+ flags.foundYear += 2e3;
497
+ }
498
+ if (flags.foundDayOfMonth === void 0 || flags.foundMonth === void 0 || flags.foundYear === void 0 || flags.foundTime === void 0) {
499
+ return void 0;
500
+ }
501
+ if (flags.foundDayOfMonth < 1 || flags.foundDayOfMonth > 31) {
502
+ return void 0;
503
+ }
504
+ if (flags.foundYear < 1601) {
505
+ return void 0;
578
506
  }
579
- return new Date(Date.UTC(year, month, dayOfMonth, hour, minute, second));
507
+ if (flags.foundTime.hours > 23) {
508
+ return void 0;
509
+ }
510
+ if (flags.foundTime.minutes > 59) {
511
+ return void 0;
512
+ }
513
+ if (flags.foundTime.seconds > 59) {
514
+ return void 0;
515
+ }
516
+ const date = new Date(
517
+ Date.UTC(
518
+ flags.foundYear,
519
+ flags.foundMonth,
520
+ flags.foundDayOfMonth,
521
+ flags.foundTime.hours,
522
+ flags.foundTime.minutes,
523
+ flags.foundTime.seconds
524
+ )
525
+ );
526
+ if (date.getUTCFullYear() !== flags.foundYear || date.getUTCMonth() !== flags.foundMonth || date.getUTCDate() !== flags.foundDayOfMonth) {
527
+ return void 0;
528
+ }
529
+ return date;
580
530
  }
531
+ var months = [
532
+ "jan",
533
+ "feb",
534
+ "mar",
535
+ "apr",
536
+ "may",
537
+ "jun",
538
+ "jul",
539
+ "aug",
540
+ "sep",
541
+ "oct",
542
+ "nov",
543
+ "dec"
544
+ ];
545
+ var DELIMITER = /[\x09\x20-\x2F\x3B-\x40\x5B-\x60\x7B-\x7E]/;
546
+ var TIME = /^(\d{1,2}):(\d{1,2}):(\d{1,2})(?:[\x00-\x2F\x3A-\xFF][\x00-\xFF]*)?$/;
547
+ var DAY_OF_MONTH = /^[0-9]{1,2}(?:[\x00-\x2F\x3A-\xFF][\x00-\xFF]*)?$/;
548
+ var MONTH = /^(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[\x00-\xFF]*$/i;
549
+ var YEAR = /^[\x30-\x39]{2,4}(?:[\x00-\x2F\x3A-\xFF][\x00-\xFF]*)?$/;
581
550
 
582
551
  // lib/cookie/cookie.ts
583
552
  var COOKIE_OCTETS = /^[\x21\x23-\x2B\x2D-\x3A\x3C-\x5B\x5D-\x7E]+$/;
@@ -842,7 +811,7 @@ var _Cookie = class _Cookie {
842
811
  });
843
812
  this.creationIndex = _Cookie.cookiesCreated;
844
813
  }
845
- [Symbol.for("nodejs.util.inspect.custom")]() {
814
+ [/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
846
815
  const now = Date.now();
847
816
  const hostOnly = this.hostOnly != null ? this.hostOnly.toString() : "?";
848
817
  const createAge = this.creation && this.creation !== "Infinity" ? `${String(now - this.creation.getTime())}ms` : "?";