Browse Source

fix: cookies prototype pollution

e22m4u 3 weeks ago
parent
commit
7f5986f032
3 changed files with 13 additions and 0 deletions
  1. 3 0
      dist/cjs/index.cjs
  2. 3 0
      src/utils/parse-cookie-string.js
  3. 7 0
      src/utils/parse-cookie-string.spec.js

+ 3 - 0
dist/cjs/index.cjs

@@ -343,6 +343,9 @@ function parseCookieString(input) {
   }
   return input.split(";").filter((v) => v !== "").map((v) => v.split("=")).reduce((cookies, tuple) => {
     const key = decodeURIComponent(tuple[0]).trim();
+    if (key === "__proto__" || key === "constructor" || key === "prototype") {
+      return cookies;
+    }
     const value = tuple[1] !== void 0 ? decodeURIComponent(tuple[1]).trim() : "";
     cookies[key] = value;
     return cookies;

+ 3 - 0
src/utils/parse-cookie-string.js

@@ -26,6 +26,9 @@ export function parseCookieString(input) {
     .map(v => v.split('='))
     .reduce((cookies, tuple) => {
       const key = decodeURIComponent(tuple[0]).trim();
+      if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
+        return cookies;
+      }
       const value =
         tuple[1] !== undefined ? decodeURIComponent(tuple[1]).trim() : '';
       cookies[key] = value;

+ 7 - 0
src/utils/parse-cookie-string.spec.js

@@ -39,4 +39,11 @@ describe('parseCookieString', function () {
     const result = parseCookieString('foo=bar; baz');
     expect(result).to.be.eql({foo: 'bar', baz: ''});
   });
+
+  it('should ignore prototype properties', function () {
+    const result = parseCookieString(
+      '__proto__=a; constructor=b; prototype=c; foo=bar',
+    );
+    expect(result).to.be.eql({foo: 'bar'});
+  });
 });