Skip to content

Commit 981cac7

Browse files
committed
Throw for invalid keys
1 parent 5ecc2f7 commit 981cac7

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

packages/snaps-rpc-methods/src/permitted/getState.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ describe('get', () => {
2929
expect(get(object, 'a.b.c.d')).toBeNull();
3030
});
3131

32-
it('returns `null` if the key is a prototype pollution attempt', () => {
33-
expect(get(object, '__proto__.polluted')).toBeNull();
32+
it('throws an error if the key is a prototype pollution attempt', () => {
33+
expect(() => get(object, '__proto__.polluted')).toThrow(
34+
'Invalid params: Key contains forbidden characters.',
35+
);
3436
});
3537

3638
it('returns `null` if the key is a constructor pollution attempt', () => {
37-
expect(get(object, 'constructor.polluted')).toBeNull();
39+
expect(() => get(object, 'constructor.polluted')).toThrow(
40+
'Invalid params: Key contains forbidden characters.',
41+
);
3842
});
3943
});

packages/snaps-rpc-methods/src/permitted/getState.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ export function get(
166166

167167
for (const currentKey of keys) {
168168
if (['__proto__', 'constructor'].includes(currentKey)) {
169-
return null;
169+
throw rpcErrors.invalidParams(
170+
'Invalid params: Key contains forbidden characters.',
171+
);
170172
}
171173

172174
if (isPlainObject(result)) {

packages/snaps-rpc-methods/src/permitted/setState.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,16 @@ describe('set', () => {
8888
},
8989
});
9090
});
91+
92+
it('throws an error if the key is a prototype pollution attempt', () => {
93+
expect(() => set({}, '__proto__.polluted', 'value')).toThrow(
94+
'Invalid params: Key contains forbidden characters.',
95+
);
96+
});
97+
98+
it('throws an error if the key is a constructor pollution attempt', () => {
99+
expect(() => set({}, 'constructor.polluted', 'value')).toThrow(
100+
'Invalid params: Key contains forbidden characters.',
101+
);
102+
});
91103
});

packages/snaps-rpc-methods/src/permitted/setState.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ export function set(
202202
for (let i = 0; i < keys.length; i++) {
203203
const currentKey = keys[i];
204204
if (['__proto__', 'constructor'].includes(currentKey)) {
205-
return {};
205+
throw rpcErrors.invalidParams(
206+
'Invalid params: Key contains forbidden characters.',
207+
);
206208
}
207209

208210
if (i === keys.length - 1) {

0 commit comments

Comments
 (0)