Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/node-cache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,10 @@ export class NodeCache<T> extends Hookified {
public mget<V = T>(
keys: Array<string | number>,
): Record<string, V | undefined> {
const result: Record<string, V | undefined> = {};
const result: Record<string, V | undefined> = Object.create(null) as Record<
string,
V | undefined
>;

for (const key of keys) {
const value = this.get(key);
Expand Down
5 changes: 4 additions & 1 deletion packages/node-cache/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ export class NodeCacheStore<T> extends Hookified {
public async mget<V = T>(
keys: Array<string | number>,
): Promise<Record<string, V | undefined>> {
const result: Record<string, V | undefined> = {};
const result: Record<string, V | undefined> = Object.create(null) as Record<
string,
V | undefined
>;
for (const key of keys) {
const value = await this._keyv.get<V>(key.toString());
if (value === undefined) {
Expand Down
9 changes: 9 additions & 0 deletions packages/node-cache/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ describe("NodeCache", () => {
expect(list[key2]).toBe(value2);
});

test("should not pollute Object.prototype via mget with __proto__ key", () => {
cache.set("__proto__", { polluted: true });
const result = cache.mget(["__proto__"]);
// biome-ignore lint/suspicious/noExplicitAny: testing prototype pollution
expect((Object.prototype as any).polluted).toBeUndefined();
expect(Object.getPrototypeOf(result)).toBeNull();
expect(Object.hasOwn(result, "__proto__")).toBe(true);
});

test("should take a key", () => {
const key = faker.string.uuid();
const val = faker.lorem.word();
Expand Down
9 changes: 9 additions & 0 deletions packages/node-cache/test/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ describe("NodeCacheStore", () => {
const result1 = await store.mget(["test1", "test2"]);
expect(result1).toEqual({ test1: "value1", test2: "value2" });
});
test("should not pollute Object.prototype via mget with __proto__ key", async () => {
const store = new NodeCacheStore();
await store.set("__proto__", { polluted: true });
const result = await store.mget(["__proto__"]);
// biome-ignore lint/suspicious/noExplicitAny: testing prototype pollution
expect((Object.prototype as any).polluted).toBeUndefined();
expect(Object.getPrototypeOf(result)).toBeNull();
expect(Object.hasOwn(result, "__proto__")).toBe(true);
});
test("should be able to set multiple keys", async () => {
const store = new NodeCacheStore();
const data = [
Expand Down
Loading