Skip to content

Commit 0ffa7f2

Browse files
author
Shahar
committed
Prevent prototype pollution (CVE-2023-26136) in cookie memstore (salesforce#283)
1 parent 7c1fdf1 commit 0ffa7f2

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

lib/memstore.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var util = require('util');
3636

3737
function MemoryCookieStore() {
3838
Store.call(this);
39-
this.idx = {};
39+
this.idx = Object.create(null);
4040
}
4141
util.inherits(MemoryCookieStore, Store);
4242
exports.MemoryCookieStore = MemoryCookieStore;
@@ -115,10 +115,10 @@ MemoryCookieStore.prototype.findCookies = function(domain, path, cb) {
115115

116116
MemoryCookieStore.prototype.putCookie = function(cookie, cb) {
117117
if (!this.idx[cookie.domain]) {
118-
this.idx[cookie.domain] = {};
118+
this.idx[cookie.domain] = Object.create(null);
119119
}
120120
if (!this.idx[cookie.domain][cookie.path]) {
121-
this.idx[cookie.domain][cookie.path] = {};
121+
this.idx[cookie.domain][cookie.path] = Object.create(null);
122122
}
123123
this.idx[cookie.domain][cookie.path][cookie.key] = cookie;
124124
cb(null);

test/cookie_jar_test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,32 @@ vows
541541
}
542542
}
543543
})
544+
.addBatch({
545+
// CVE-2023-26136: Prototype pollution via Domain=__proto__
546+
"Issue #282 - Prototype pollution (CVE-2023-26136)": {
547+
"when setting a cookie with the domain __proto__": {
548+
topic: function () {
549+
const jar = new tough.CookieJar(undefined, {
550+
rejectPublicSuffixes: false
551+
});
552+
// Attempt prototype pollution via cookie domain '__proto__'
553+
jar.setCookieSync(
554+
"Slonser=polluted; Domain=__proto__; Path=/notauth",
555+
"https://__proto__/admin"
556+
);
557+
// Set a normal cookie for control
558+
jar.setCookieSync(
559+
"Auth=Lol; Domain=google.com; Path=/notauth",
560+
"https://google.com/"
561+
);
562+
this.callback();
563+
},
564+
"results in a cookie that is not affected by the attempted prototype pollution": function () {
565+
const pollutedObject = {};
566+
// If prototype is polluted, this would return an object
567+
assert.strictEqual(pollutedObject["/notauth"], undefined);
568+
}
569+
}
570+
}
571+
})
544572
.export(module);

0 commit comments

Comments
 (0)