Skip to content

Commit cba83cb

Browse files
authoredAug 18, 2021
fix: improve proto checking for hgetall [skip ci] (#1418)
* fix: improve proto checking for hgetall As mentioned in #1417 * Address feedbacks
1 parent 0587353 commit cba83cb

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed
 

‎lib/command.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ Command.setReplyTransformer("hgetall", function (result) {
429429
for (let i = 0; i < result.length; i += 2) {
430430
const key = result[i];
431431
const value = result[i + 1];
432-
if (obj[key]) {
432+
if (key in obj) {
433433
// can only be truthy if the property is special somehow, like '__proto__' or 'constructor'
434434
// https://github.com/luin/ioredis/issues/1267
435435
Object.defineProperty(obj, key, {

‎test/functional/hgetall.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
import Redis from "../../lib/redis";
22
import { expect } from "chai";
33

4+
const CUSTOM_PROPERTY = "_myCustomProperty";
5+
46
describe("hgetall", function () {
5-
it("should handle __proto__", async function () {
7+
beforeEach(function () {
8+
Object.defineProperty(Object.prototype, CUSTOM_PROPERTY, {
9+
value: false,
10+
configurable: true,
11+
enumerable: false,
12+
writable: false,
13+
});
14+
});
15+
16+
afterEach(function () {
17+
delete (Object.prototype as any)[CUSTOM_PROPERTY];
18+
});
19+
20+
it("should handle special field names", async function () {
621
const redis = new Redis();
7-
await redis.hset("test_key", "__proto__", "hello");
22+
await redis.hmset(
23+
"test_key",
24+
"__proto__",
25+
"hello",
26+
CUSTOM_PROPERTY,
27+
"world"
28+
);
829
const ret = await redis.hgetall("test_key");
930
expect(ret.__proto__).to.eql("hello");
10-
expect(Object.keys(ret)).to.eql(["__proto__"]);
31+
expect(ret[CUSTOM_PROPERTY]).to.eql("world");
32+
expect(Object.keys(ret).sort()).to.eql(
33+
["__proto__", CUSTOM_PROPERTY].sort()
34+
);
35+
expect(Object.getPrototypeOf(ret)).to.eql(Object.prototype);
1136
});
1237
});

0 commit comments

Comments
 (0)
Please sign in to comment.