Skip to content

Commit 7d73b9d

Browse files
authoredAug 18, 2021
fix: handle malicious keys for hgetall (#1416)
Closes #1267
1 parent 17c7595 commit 7d73b9d

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
 

‎lib/command.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,20 @@ Command.setReplyTransformer("hgetall", function (result) {
427427
if (Array.isArray(result)) {
428428
const obj = {};
429429
for (let i = 0; i < result.length; i += 2) {
430-
obj[result[i]] = result[i + 1];
430+
const key = result[i];
431+
const value = result[i + 1];
432+
if (obj[key]) {
433+
// can only be truthy if the property is special somehow, like '__proto__' or 'constructor'
434+
// https://github.com/luin/ioredis/issues/1267
435+
Object.defineProperty(obj, key, {
436+
value,
437+
configurable: true,
438+
enumerable: true,
439+
writable: true,
440+
});
441+
} else {
442+
obj[key] = value;
443+
}
431444
}
432445
return obj;
433446
}

‎test/functional/hgetall.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Redis from "../../lib/redis";
2+
import { expect } from "chai";
3+
4+
describe("hgetall", function () {
5+
it("should handle __proto__", async function () {
6+
const redis = new Redis();
7+
await redis.hset("test_key", "__proto__", "hello");
8+
const ret = await redis.hgetall("test_key");
9+
expect(ret.__proto__).to.eql("hello");
10+
expect(Object.keys(ret)).to.eql(["__proto__"]);
11+
});
12+
});

0 commit comments

Comments
 (0)
Please sign in to comment.