-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Ensure unsupported method rejection occurs before permission mi…
…ddleware
- Loading branch information
Showing
7 changed files
with
68 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/scripts/lib/rpc-method-middleware/createUnsupportedMethodMiddleware.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { jsonrpc2 } from '@metamask/utils'; | ||
import { UNSUPPORTED_RPC_METHODS } from '../../../../shared/constants/network'; | ||
import { createUnsupportedMethodMiddleware } from '.'; | ||
|
||
describe('createDupeReqFilterMiddleware', () => { | ||
const getMockRequest = (method: string) => ({ | ||
jsonrpc: jsonrpc2, | ||
id: 1, | ||
method, | ||
}); | ||
const getMockResponse = () => ({ jsonrpc: jsonrpc2, id: 'foo' }); | ||
|
||
it('forwards requests whose methods are not on the list of unsupported methods', () => { | ||
const middleware = createUnsupportedMethodMiddleware(); | ||
const nextMock = jest.fn(); | ||
const endMock = jest.fn(); | ||
|
||
middleware(getMockRequest('foo'), getMockResponse(), nextMock, endMock); | ||
|
||
expect(nextMock).toHaveBeenCalledTimes(1); | ||
expect(endMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it.each([...UNSUPPORTED_RPC_METHODS.keys()])( | ||
'ends requests for methods that are on the list of unsupported methods: %s', | ||
(method) => { | ||
const middleware = createUnsupportedMethodMiddleware(); | ||
const nextMock = jest.fn(); | ||
const endMock = jest.fn(); | ||
|
||
const response = getMockResponse(); | ||
middleware(getMockRequest(method), response, nextMock, endMock); | ||
|
||
expect('result' in response).toBe(false); | ||
expect(nextMock).not.toHaveBeenCalled(); | ||
expect(endMock).toHaveBeenCalledTimes(1); | ||
}, | ||
); | ||
}); |
19 changes: 19 additions & 0 deletions
19
app/scripts/lib/rpc-method-middleware/createUnsupportedMethodMiddleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ethErrors } from 'eth-rpc-errors'; | ||
import type { JsonRpcMiddleware } from 'json-rpc-engine'; | ||
import { UNSUPPORTED_RPC_METHODS } from '../../../../shared/constants/network'; | ||
|
||
/** | ||
* Creates a middleware that rejects explicitly unsupported RPC methods with the | ||
* appropriate error. | ||
*/ | ||
export function createUnsupportedMethodMiddleware(): JsonRpcMiddleware< | ||
unknown, | ||
void | ||
> { | ||
return async function unsupportedMethodMiddleware(req, _res, next, end) { | ||
if ((UNSUPPORTED_RPC_METHODS as Set<string>).has(req.method)) { | ||
return end(ethErrors.rpc.methodNotSupported()); | ||
} | ||
return next(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './createMethodMiddleware'; | ||
export * from './createUnsupportedMethodMiddleware'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters