Skip to content
This repository was archived by the owner on Jan 3, 2022. It is now read-only.

Commit 0d6000f

Browse files
authored
Add filterResponse deep compare with fast-deep-equal (#125)
1 parent 3888429 commit 0d6000f

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
"@typescript-eslint/parser": "^2.17.0",
3333
"clone": "^2.1.2",
3434
"eslint": "^6.8.0",
35-
"fast-deep-equal": "^2.0.1",
3635
"nyc": "^15.0.0",
3736
"tape": "^4.9.2",
3837
"typescript": "^4.1.2"
3938
},
4039
"dependencies": {
40+
"fast-deep-equal": "^2.0.1",
4141
"@metamask/controllers": "^5.0.0",
4242
"eth-rpc-errors": "^3.0.0",
4343
"is-subset": "^0.1.1",

src/caveats.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { JsonRpcMiddleware } from 'json-rpc-engine';
22
import { IOcapLdCaveat } from './@types/ocap-ld';
33
import { unauthorized } from './errors';
44
import isSubset from 'is-subset';
5+
import equal from 'fast-deep-equal';
56

67
export type ICaveatFunction = JsonRpcMiddleware;
78

@@ -25,7 +26,7 @@ export const requireParams: ICaveatFunctionGenerator = function requireParams (s
2526
};
2627

2728
/*
28-
* Filters array results shallowly.
29+
* Filters array results.
2930
*/
3031
export const filterResponse: ICaveatFunctionGenerator = function filterResponse (serialized: IOcapLdCaveat) {
3132
const { value } = serialized;
@@ -34,7 +35,10 @@ export const filterResponse: ICaveatFunctionGenerator = function filterResponse
3435
next((done) => {
3536
if (Array.isArray(res.result)) {
3637
res.result = res.result.filter((item) => {
37-
return value.includes(item);
38+
const findResult = value.find((v: any) => {
39+
return equal(v, item);
40+
});
41+
return findResult !== undefined;
3842
});
3943
}
4044
done();

test/caveats.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,62 @@ test('filterResponse caveat returns empty if params are not a subset.', async (t
186186
t.end();
187187
});
188188

189+
test('filterResponse caveat passes through subset portion of response when objects are used', async (t) => {
190+
const domain = { origin: 'www.metamask.io' };
191+
192+
const ctrl = new CapabilitiesController({
193+
restrictedMethods: {
194+
'write': {
195+
method: (_req, res, _next, end) => {
196+
res.result = [{ foo: 'bar' }, { baz: 'boo' }, 3, 4, 5];
197+
end();
198+
},
199+
},
200+
},
201+
202+
// User approves on condition of first arg being 'foo',
203+
// and second arg having the 'bar': 'baz' value.
204+
requestUserApproval: async (permissionsRequest) => {
205+
const perms = permissionsRequest.permissions;
206+
perms.write.caveats = [
207+
{ type: 'filterResponse', value: [{ foo: 'bar' }, { baz: 'boo' }, 3] },
208+
];
209+
return perms;
210+
},
211+
},
212+
{
213+
domains: {},
214+
});
215+
216+
try {
217+
let req = {
218+
method: 'requestPermissions',
219+
params: [
220+
{
221+
'write': {},
222+
},
223+
],
224+
};
225+
226+
await sendRpcMethodWithResponse(ctrl, domain, req);
227+
228+
// Now let's call that restricted method:
229+
req = {
230+
method: 'write',
231+
params: [],
232+
};
233+
234+
const result = await sendRpcMethodWithResponse(ctrl, domain, req);
235+
236+
t.ok(result, 'should succeed');
237+
t.deepEqual(result, [{ foo: 'bar' }, { baz: 'boo' }, 3], 'returned the correct subset');
238+
239+
} catch (err) {
240+
t.notOk(err, 'should not throw');
241+
}
242+
t.end();
243+
});
244+
189245
test('filterResponse caveat passes through subset portion of response', async (t) => {
190246
const domain = { origin: 'www.metamask.io' };
191247

0 commit comments

Comments
 (0)