From e408c7023bc65348c9e18ce65f6115a78f1bc927 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Fri, 15 Nov 2024 13:51:28 -0800 Subject: [PATCH 1/2] Loosen cookie name/value validation --- src/index.ts | 11 ++- src/serialize.spec.ts | 174 ++++++++++++++++++++---------------------- 2 files changed, 90 insertions(+), 95 deletions(-) diff --git a/src/index.ts b/src/index.ts index af222fc..ed7d11a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,8 +8,11 @@ * tchar = "!" / "#" / "$" / "%" / "&" / "'" / * "*" / "+" / "-" / "." / "^" / "_" / * "`" / "|" / "~" / DIGIT / ALPHA + * + * Note: Allowing more characters - https://github.com/jshttp/cookie/issues/191 + * Allow same range as cookie value, except `=`, which delimits end of name. */ -const cookieNameRegExp = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/; +const cookieNameRegExp = /^[\u0021-\u003A\u003C\u003E-\u007E]+$/; /** * RegExp to match cookie-value in RFC 6265 sec 4.1.1 @@ -19,9 +22,11 @@ const cookieNameRegExp = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/; * ; US-ASCII characters excluding CTLs, * ; whitespace DQUOTE, comma, semicolon, * ; and backslash + * + * Allowing more characters: https://github.com/jshttp/cookie/issues/191 + * Comma, backslash, and DQUOTE are not part of the parsing algorithm. */ -const cookieValueRegExp = - /^("?)[\u0021\u0023-\u002B\u002D-\u003A\u003C-\u005B\u005D-\u007E]*\1$/; +const cookieValueRegExp = /^[\u0021-\u003A\u003C-\u007E]*$/; /** * RegExp to match domain-value in RFC 6265 sec 4.1.1 diff --git a/src/serialize.spec.ts b/src/serialize.spec.ts index 7091027..458de3f 100644 --- a/src/serialize.spec.ts +++ b/src/serialize.spec.ts @@ -14,91 +14,80 @@ describe("cookie.serialize(name, value)", function () { expect(cookie.serialize("foo", "")).toEqual("foo="); }); - it("should serialize valid name", function () { - var validNames = [ - "foo", - "foo!bar", - "foo#bar", - "foo$bar", - "foo'bar", - "foo*bar", - "foo+bar", - "foo-bar", - "foo.bar", - "foo^bar", - "foo_bar", - "foo`bar", - "foo|bar", - "foo~bar", - "foo7bar", - ]; - - validNames.forEach(function (name) { - expect(cookie.serialize(name, "baz")).toEqual(name + "=baz"); - }); + it.each([ + ["foo"], + ["foo,bar"], + ["foo!bar"], + ["foo#bar"], + ["foo$bar"], + ["foo'bar"], + ["foo*bar"], + ["foo+bar"], + ["foo-bar"], + ["foo.bar"], + ["foo^bar"], + ["foo_bar"], + ["foo`bar"], + ["foo|bar"], + ["foo~bar"], + ["foo7bar"], + ["foo/bar"], + ["foo@bar"], + ["foo[bar"], + ["foo]bar"], + ["foo:bar"], + ["foo{bar"], + ["foo}bar"], + ['foo"bar'], + ["foobar"], + ["foo?bar"], + ["foo\\bar"], + ])("should serialize name: %s", (name) => { + expect(cookie.serialize(name, "baz")).toEqual(`${name}=baz`); }); - it("should throw for invalid name", function () { - var invalidNames = [ - "foo\n", - "foo\u280a", - "foo/foo", - "foo,foo", - "foo;foo", - "foo@foo", - "foo[foo]", - "foo?foo", - "foo:foo", - "foo{foo}", - "foo foo", - "foo\tfoo", - 'foo"foo', - "foo