aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/node_modules/undici/lib/web/cookies/util.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/undici/lib/web/cookies/util.js')
-rw-r--r--node_modules/undici/lib/web/cookies/util.js88
1 files changed, 79 insertions, 9 deletions
diff --git a/node_modules/undici/lib/web/cookies/util.js b/node_modules/undici/lib/web/cookies/util.js
index 254f541..ca408e1 100644
--- a/node_modules/undici/lib/web/cookies/util.js
+++ b/node_modules/undici/lib/web/cookies/util.js
@@ -105,7 +105,7 @@ function validateCookiePath (path) {
if (
code < 0x20 || // exclude CTLs (0-31)
- code === 0x7F || // DEL
+ code > 0x7E || // exclude DEL and non-ascii
code === 0x3B // ;
) {
throw new Error('Invalid cookie path')
@@ -114,16 +114,80 @@ function validateCookiePath (path) {
}
/**
- * I have no idea why these values aren't allowed to be honest,
- * but Deno tests these. - Khafra
+ * <let-dig> ::= <letter> | <digit>
+ *
+ * <letter> ::= any one of the 52 alphabetic characters A through Z in
+ * upper case and a through z in lower case
+ *
+ * <digit> ::= any one of the ten digits 0 through 9r
+ *
+ * @see https://www.rfc-editor.org/rfc/rfc1034#section-3.5
+ * @param {number} code
+ */
+function isLetterOrDigit (code) {
+ return (
+ (code >= 0x30 && code <= 0x39) || // 0-9
+ (code >= 0x41 && code <= 0x5A) || // A-Z
+ (code >= 0x61 && code <= 0x7A) // a-z
+ )
+}
+
+/**
+ * Validates a cookie domain against the "preferred name syntax".
+ *
+ * <domain> ::= <subdomain> | " "
+ * <subdomain> ::= <label> | <subdomain> "." <label>
+ * <label> ::= <let-dig> [ [ <ldh-str> ] <let-dig> ]
+ * <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
+ * <let-dig-hyp> ::= <let-dig> | "-"
+ *
+ * @see https://www.rfc-editor.org/rfc/rfc1034#section-3.5
+ * @see https://www.rfc-editor.org/rfc/rfc1123#section-2.1
+ * @see https://www.rfc-editor.org/rfc/rfc1035#section-2.3.4
* @param {string} domain
*/
function validateCookieDomain (domain) {
- if (
- domain.startsWith('-') ||
- domain.endsWith('.') ||
- domain.endsWith('-')
- ) {
+ // <domain> ::= <subdomain> | " "
+ if (domain === ' ') {
+ return
+ }
+
+ if (domain.length > 255) {
+ throw new Error('Invalid cookie domain')
+ }
+
+ let labelLength = 0
+
+ for (let i = 0; i < domain.length; ++i) {
+ const code = domain.charCodeAt(i)
+
+ if (code === 0x2E) {
+ if (labelLength === 0) {
+ throw new Error('Invalid cookie domain')
+ }
+
+ if (domain.charCodeAt(i - 1) === 0x2D) { // "-"
+ throw new Error('Invalid cookie domain')
+ }
+
+ labelLength = 0
+ continue
+ }
+
+ if (labelLength === 0 && !isLetterOrDigit(code)) {
+ throw new Error('Invalid cookie domain')
+ }
+
+ if (!isLetterOrDigit(code) && code !== 0x2D) { // "-"
+ throw new Error('Invalid cookie domain')
+ }
+
+ if (++labelLength > 63) {
+ throw new Error('Invalid cookie domain')
+ }
+ }
+
+ if (labelLength === 0 || domain.charCodeAt(domain.length - 1) === 0x2D) { // "-"
throw new Error('Invalid cookie domain')
}
}
@@ -266,7 +330,13 @@ function stringify (cookie) {
const [key, ...value] = part.split('=')
- out.push(`${key.trim()}=${value.join('=')}`)
+ const trimmedKey = key.trim()
+ const joinedValue = value.join('=')
+
+ validateCookieName(trimmedKey)
+ validateCookieValue(joinedValue)
+
+ out.push(`${trimmedKey}=${joinedValue}`)
}
return out.join('; ')