-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Throw when encountering unexpected RPC method hooks #24357
Merged
Merged
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4a98173
refactor: Remove extraneous RPC handler hooks
rekmarks b1e96b2
feat: Throw error for extraneous method middleware hooks
rekmarks 6080f37
fix: Mock method middleware in MetaMaskController test
rekmarks fdd89d9
refactor: Move hook assertions into own function
rekmarks aa342d9
refactor: Use getOwnPropertyNames() instead of keys()
rekmarks 20cee7c
refactor: Convert createMethodMiddleware to TypeScript
rekmarks 7012f92
test: Add createMethodMiddleware tests
rekmarks 2d7d8f0
refactor: Improve method middleware types
rekmarks 350a322
refactor: Revert TypeScript conversion
rekmarks 3a118aa
Merge branch 'develop' into remove-unused-rpc-handler-hook
legobeat de5d01f
test: Add more error test cases
rekmarks 9d38291
Merge branch 'develop' into remove-unused-rpc-handler-hook
rekmarks 62bbc83
Merge branch 'develop' into remove-unused-rpc-handler-hook
rekmarks d13efd6
Merge branch 'develop' into remove-unused-rpc-handler-hook
rekmarks b05090a
Merge branch 'develop' into remove-unused-rpc-handler-hook
rekmarks 38cf3c7
Merge branch 'develop' into remove-unused-rpc-handler-hook
rekmarks 2c1d1a3
Merge branch 'develop' into remove-unused-rpc-handler-hook
rekmarks 5edfc0d
Merge branch 'develop' into remove-unused-rpc-handler-hook
adonesky1 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
150 changes: 150 additions & 0 deletions
150
app/scripts/lib/rpc-method-middleware/createMethodMiddleware.test.js
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,150 @@ | ||
import { JsonRpcEngine } from 'json-rpc-engine'; | ||
import { | ||
assertIsJsonRpcFailure, | ||
assertIsJsonRpcSuccess, | ||
} from '@metamask/utils'; | ||
import { createMethodMiddleware } from '.'; | ||
|
||
jest.mock('@metamask/permission-controller', () => ({ | ||
permissionRpcMethods: { handlers: [] }, | ||
})); | ||
|
||
jest.mock('./handlers', () => [ | ||
{ | ||
implementation: (req, res, _next, end, hooks) => { | ||
if (Array.isArray(req.params)) { | ||
switch (req.params[0]) { | ||
case 1: | ||
res.result = hooks.hook1(); | ||
break; | ||
case 2: | ||
res.result = hooks.hook2(); | ||
break; | ||
case 3: | ||
return end(new Error('test error')); | ||
default: | ||
throw new Error(`unexpected param "${req.params[0]}"`); | ||
} | ||
} | ||
return end(); | ||
}, | ||
hookNames: { hook1: true, hook2: true }, | ||
methodNames: ['method1', 'method2'], | ||
}, | ||
]); | ||
|
||
describe('createMethodMiddleware', () => { | ||
const method1 = 'method1'; | ||
|
||
const getDefaultHooks = () => ({ | ||
hook1: () => 42, | ||
hook2: () => 99, | ||
}); | ||
|
||
it('should return a function', () => { | ||
const middleware = createMethodMiddleware(getDefaultHooks()); | ||
expect(typeof middleware).toBe('function'); | ||
}); | ||
|
||
it('should throw an error if a required hook is missing', () => { | ||
const hooks = { hook1: () => 42 }; | ||
|
||
// @ts-expect-error Intentional destructive testing | ||
expect(() => createMethodMiddleware(hooks)).toThrow( | ||
'Missing expected hooks', | ||
); | ||
}); | ||
|
||
it('should throw an error if an extraneous hook is provided', () => { | ||
const hooks = { | ||
...getDefaultHooks(), | ||
extraneousHook: () => 100, | ||
}; | ||
|
||
expect(() => createMethodMiddleware(hooks)).toThrow( | ||
'Received unexpected hooks', | ||
); | ||
}); | ||
|
||
it('should call the handler for the matching method (uses hook1)', async () => { | ||
const middleware = createMethodMiddleware(getDefaultHooks()); | ||
const engine = new JsonRpcEngine(); | ||
engine.push(middleware); | ||
|
||
const response = await engine.handle({ | ||
jsonrpc: '2.0', | ||
id: 1, | ||
method: method1, | ||
params: [1], | ||
}); | ||
assertIsJsonRpcSuccess(response); | ||
|
||
expect(response.result).toBe(42); | ||
}); | ||
|
||
it('should call the handler for the matching method (uses hook2)', async () => { | ||
const middleware = createMethodMiddleware(getDefaultHooks()); | ||
const engine = new JsonRpcEngine(); | ||
engine.push(middleware); | ||
|
||
const response = await engine.handle({ | ||
jsonrpc: '2.0', | ||
id: 1, | ||
method: method1, | ||
params: [2], | ||
}); | ||
assertIsJsonRpcSuccess(response); | ||
|
||
expect(response.result).toBe(99); | ||
}); | ||
|
||
it('should not call the handler for a non-matching method', async () => { | ||
const middleware = createMethodMiddleware(getDefaultHooks()); | ||
const engine = new JsonRpcEngine(); | ||
engine.push(middleware); | ||
|
||
const response = await engine.handle({ | ||
jsonrpc: '2.0', | ||
id: 1, | ||
method: 'nonMatchingMethod', | ||
}); | ||
assertIsJsonRpcFailure(response); | ||
|
||
expect(response.error).toMatchObject({ | ||
message: expect.stringMatching( | ||
/Response has no error or result for request/u, | ||
), | ||
}); | ||
}); | ||
|
||
it('should reject unsupported methods', async () => { | ||
const middleware = createMethodMiddleware(getDefaultHooks()); | ||
const engine = new JsonRpcEngine(); | ||
engine.push(middleware); | ||
|
||
const response = await engine.handle({ | ||
jsonrpc: '2.0', | ||
id: 1, | ||
method: 'eth_signTransaction', | ||
}); | ||
assertIsJsonRpcFailure(response); | ||
|
||
expect(response.error.message).toBe('Method not supported.'); | ||
}); | ||
|
||
it('should handle errors thrown by the implementation', async () => { | ||
const middleware = createMethodMiddleware(getDefaultHooks()); | ||
const engine = new JsonRpcEngine(); | ||
engine.push(middleware); | ||
|
||
const response = await engine.handle({ | ||
jsonrpc: '2.0', | ||
id: 1, | ||
method: method1, | ||
params: [3], | ||
}); | ||
assertIsJsonRpcFailure(response); | ||
|
||
expect(response.error.message).toBe('test error'); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5005,16 +5005,6 @@ export default class MetamaskController extends EventEmitter { | |
endApprovalFlow: this.approvalController.endFlow.bind( | ||
this.approvalController, | ||
), | ||
setApprovalFlowLoadingText: | ||
this.approvalController.setFlowLoadingText.bind( | ||
this.approvalController, | ||
), | ||
showApprovalSuccess: this.approvalController.success.bind( | ||
this.approvalController, | ||
), | ||
showApprovalError: this.approvalController.error.bind( | ||
this.approvalController, | ||
), | ||
sendMetrics: this.metaMetricsController.trackEvent.bind( | ||
this.metaMetricsController, | ||
), | ||
|
@@ -5076,18 +5066,11 @@ export default class MetamaskController extends EventEmitter { | |
this.networkController, | ||
), | ||
findNetworkConfigurationBy: this.findNetworkConfigurationBy.bind(this), | ||
getNetworkClientIdForDomain: | ||
this.selectedNetworkController.getNetworkClientIdForDomain.bind( | ||
this.selectedNetworkController, | ||
), | ||
setNetworkClientIdForDomain: | ||
this.selectedNetworkController.setNetworkClientIdForDomain.bind( | ||
this.selectedNetworkController, | ||
), | ||
|
||
getUseRequestQueue: this.preferencesController.getUseRequestQueue.bind( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whoops 😅 |
||
this.preferencesController, | ||
), | ||
getProviderConfig: () => this.networkController.state.providerConfig, | ||
setProviderType: (type) => { | ||
return this.networkController.setProviderType(type); | ||
|
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 |
---|---|---|
|
@@ -83,8 +83,15 @@ const createLoggerMiddlewareMock = () => (req, res, next) => { | |
} | ||
next(); | ||
}; | ||
|
||
jest.mock('./lib/createLoggerMiddleware', () => createLoggerMiddlewareMock); | ||
|
||
const rpcMethodMiddlewareMock = { | ||
createMethodMiddleware: () => (_req, _res, next, _end) => { | ||
next(); | ||
}, | ||
}; | ||
jest.mock('./lib/rpc-method-middleware', () => rpcMethodMiddlewareMock); | ||
|
||
Comment on lines
+87
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests now blow up without this mock. |
||
jest.mock( | ||
'./controllers/preferences', | ||
() => | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handlers
is self-declaring.