-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send valid JSON-RPC notifications from executors (#639)
`BaseSnapExecutor.notify()` was sending invalid [JSON-RPC notifications](https://www.jsonrpc.org/specification#notification) to the execution service over the command stream. This PR ensures that only valid notifications are sent.
- Loading branch information
Showing
8 changed files
with
203 additions
and
53 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
112 changes: 112 additions & 0 deletions
112
packages/controllers/src/services/AbstractExecutionService.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,112 @@ | ||
import { ControllerMessenger } from '@metamask/controllers'; | ||
import { | ||
ErrorMessageEvent, | ||
ExecutionServiceMessenger, | ||
} from './ExecutionService'; | ||
import { NodeThreadExecutionService } from './node'; | ||
|
||
class MockExecutionService extends NodeThreadExecutionService { | ||
constructor(messenger: ExecutionServiceMessenger) { | ||
super({ | ||
messenger, | ||
setupSnapProvider: () => undefined, | ||
}); | ||
} | ||
|
||
getJobs() { | ||
return this.jobs; | ||
} | ||
} | ||
|
||
describe('AbstractExecutionService', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('logs error for unrecognized notifications', async () => { | ||
const consoleErrorSpy = jest.spyOn(console, 'error'); | ||
|
||
const controllerMessenger = new ControllerMessenger< | ||
never, | ||
ErrorMessageEvent | ||
>(); | ||
const service = new MockExecutionService( | ||
controllerMessenger.getRestricted< | ||
'ExecutionService', | ||
never, | ||
ErrorMessageEvent['type'] | ||
>({ | ||
name: 'ExecutionService', | ||
}), | ||
); | ||
|
||
await service.executeSnap({ | ||
snapId: 'TestSnap', | ||
sourceCode: ` | ||
console.log('foo'); | ||
`, | ||
endowments: ['console'], | ||
}); | ||
|
||
const { streams } = service.getJobs().values().next().value; | ||
streams.command.emit('data', { | ||
jsonrpc: '2.0', | ||
method: 'foo', | ||
}); | ||
|
||
await service.terminateAllSnaps(); | ||
|
||
expect(consoleErrorSpy).toHaveBeenCalledTimes(1); | ||
expect(consoleErrorSpy).toHaveBeenCalledWith( | ||
new Error(`Received unexpected command stream notification "foo".`), | ||
); | ||
}); | ||
|
||
it('logs error for malformed UnhandledError notification', async () => { | ||
const consoleErrorSpy = jest.spyOn(console, 'error'); | ||
|
||
const controllerMessenger = new ControllerMessenger< | ||
never, | ||
ErrorMessageEvent | ||
>(); | ||
const service = new MockExecutionService( | ||
controllerMessenger.getRestricted< | ||
'ExecutionService', | ||
never, | ||
ErrorMessageEvent['type'] | ||
>({ | ||
name: 'ExecutionService', | ||
}), | ||
); | ||
|
||
await service.executeSnap({ | ||
snapId: 'TestSnap', | ||
sourceCode: ` | ||
console.log('foo'); | ||
`, | ||
endowments: ['console'], | ||
}); | ||
|
||
const { streams } = service.getJobs().values().next().value; | ||
streams.command.emit('data', { | ||
jsonrpc: '2.0', | ||
method: 'UnhandledError', | ||
params: 2, | ||
}); | ||
|
||
streams.command.emit('data', { | ||
jsonrpc: '2.0', | ||
method: 'UnhandledError', | ||
params: { error: null }, | ||
}); | ||
|
||
await service.terminateAllSnaps(); | ||
|
||
const expectedError = new Error( | ||
`Received malformed "UnhandledError" command stream notification.`, | ||
); | ||
expect(consoleErrorSpy).toHaveBeenCalledTimes(2); | ||
expect(consoleErrorSpy).toHaveBeenNthCalledWith(1, expectedError); | ||
expect(consoleErrorSpy).toHaveBeenNthCalledWith(2, expectedError); | ||
}); | ||
}); |
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
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
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
Oops, something went wrong.