|
| 1 | +import { HandlerType } from '@metamask/snaps-utils'; |
| 2 | +import { |
| 3 | + DEFAULT_SNAP_BUNDLE, |
| 4 | + MOCK_LOCAL_SNAP_ID, |
| 5 | + MOCK_ORIGIN, |
| 6 | + MOCK_SNAP_ID, |
| 7 | + spy, |
| 8 | +} from '@metamask/snaps-utils/test-utils'; |
| 9 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 10 | + |
| 11 | +import { |
| 12 | + WebWorkerExecutionService, |
| 13 | + WORKER_POOL_ID, |
| 14 | +} from './WebWorkerExecutionService'; |
| 15 | +import { MOCK_BLOCK_NUMBER } from '../../test-utils/constants'; |
| 16 | +import { createService } from '../../test-utils/service'; |
| 17 | + |
| 18 | +const WORKER_POOL_URL = 'http://localhost:63315/worker/pool/index.html'; |
| 19 | + |
| 20 | +describe('WebWorkerExecutionService', () => { |
| 21 | + afterEach(() => { |
| 22 | + document.getElementById(WORKER_POOL_ID)?.remove(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('can boot', async () => { |
| 26 | + const { service } = createService(WebWorkerExecutionService, { |
| 27 | + documentUrl: new URL(WORKER_POOL_URL), |
| 28 | + }); |
| 29 | + |
| 30 | + expect(service).toBeDefined(); |
| 31 | + await service.terminateAllSnaps(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('only creates a single iframe', async () => { |
| 35 | + const { service } = createService(WebWorkerExecutionService, { |
| 36 | + documentUrl: new URL(WORKER_POOL_URL), |
| 37 | + }); |
| 38 | + |
| 39 | + await service.executeSnap({ |
| 40 | + snapId: MOCK_SNAP_ID, |
| 41 | + sourceCode: ` |
| 42 | + module.exports.onRpcRequest = () => null; |
| 43 | + `, |
| 44 | + endowments: [], |
| 45 | + }); |
| 46 | + |
| 47 | + await service.executeSnap({ |
| 48 | + snapId: MOCK_LOCAL_SNAP_ID, |
| 49 | + sourceCode: ` |
| 50 | + module.exports.onRpcRequest = () => null; |
| 51 | + `, |
| 52 | + endowments: [], |
| 53 | + }); |
| 54 | + |
| 55 | + expect(document.getElementById(WORKER_POOL_ID)).not.toBeNull(); |
| 56 | + expect(document.getElementsByTagName('iframe')).toHaveLength(1); |
| 57 | + |
| 58 | + await service.terminateAllSnaps(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('can create a snap worker and start the snap', async () => { |
| 62 | + const { service } = createService(WebWorkerExecutionService, { |
| 63 | + documentUrl: new URL(WORKER_POOL_URL), |
| 64 | + }); |
| 65 | + |
| 66 | + const response = await service.executeSnap({ |
| 67 | + snapId: 'TestSnap', |
| 68 | + sourceCode: ` |
| 69 | + module.exports.onRpcRequest = () => null; |
| 70 | + `, |
| 71 | + endowments: [], |
| 72 | + }); |
| 73 | + |
| 74 | + expect(response).toBe('OK'); |
| 75 | + await service.terminateAllSnaps(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('executes a snap', async () => { |
| 79 | + const { service } = createService(WebWorkerExecutionService, { |
| 80 | + documentUrl: new URL(WORKER_POOL_URL), |
| 81 | + }); |
| 82 | + |
| 83 | + await service.executeSnap({ |
| 84 | + snapId: MOCK_SNAP_ID, |
| 85 | + sourceCode: DEFAULT_SNAP_BUNDLE, |
| 86 | + endowments: ['console'], |
| 87 | + }); |
| 88 | + |
| 89 | + const result = await service.handleRpcRequest(MOCK_SNAP_ID, { |
| 90 | + origin: MOCK_ORIGIN, |
| 91 | + handler: HandlerType.OnRpcRequest, |
| 92 | + request: { |
| 93 | + jsonrpc: '2.0', |
| 94 | + id: 1, |
| 95 | + method: 'foo', |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + expect(result).toBe('foo1'); |
| 100 | + |
| 101 | + await service.terminateAllSnaps(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('can handle a crashed snap', async () => { |
| 105 | + expect.assertions(1); |
| 106 | + const { service } = createService(WebWorkerExecutionService, { |
| 107 | + documentUrl: new URL(WORKER_POOL_URL), |
| 108 | + }); |
| 109 | + |
| 110 | + const action = async () => { |
| 111 | + await service.executeSnap({ |
| 112 | + snapId: MOCK_SNAP_ID, |
| 113 | + sourceCode: ` |
| 114 | + throw new Error("Crashed."); |
| 115 | + `, |
| 116 | + endowments: [], |
| 117 | + }); |
| 118 | + }; |
| 119 | + |
| 120 | + await expect(action()).rejects.toThrow( |
| 121 | + `Error while running snap '${MOCK_SNAP_ID}': Crashed.`, |
| 122 | + ); |
| 123 | + await service.terminateAllSnaps(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('can detect outbound requests', async () => { |
| 127 | + expect.assertions(5); |
| 128 | + |
| 129 | + const { service, messenger } = createService(WebWorkerExecutionService, { |
| 130 | + documentUrl: new URL(WORKER_POOL_URL), |
| 131 | + }); |
| 132 | + |
| 133 | + const publishSpy = spy(messenger, 'publish'); |
| 134 | + |
| 135 | + const executeResult = await service.executeSnap({ |
| 136 | + snapId: MOCK_SNAP_ID, |
| 137 | + sourceCode: ` |
| 138 | + module.exports.onRpcRequest = () => ethereum.request({ method: 'eth_blockNumber', params: [] }); |
| 139 | + `, |
| 140 | + endowments: ['ethereum'], |
| 141 | + }); |
| 142 | + |
| 143 | + expect(executeResult).toBe('OK'); |
| 144 | + |
| 145 | + const result = await service.handleRpcRequest(MOCK_SNAP_ID, { |
| 146 | + origin: 'foo', |
| 147 | + handler: HandlerType.OnRpcRequest, |
| 148 | + request: { |
| 149 | + jsonrpc: '2.0', |
| 150 | + id: 1, |
| 151 | + method: 'foobar', |
| 152 | + params: [], |
| 153 | + }, |
| 154 | + }); |
| 155 | + |
| 156 | + expect(result).toBe(MOCK_BLOCK_NUMBER); |
| 157 | + |
| 158 | + expect(publishSpy.calls).toHaveLength(2); |
| 159 | + expect(publishSpy.calls[0]).toStrictEqual({ |
| 160 | + args: ['ExecutionService:outboundRequest', MOCK_SNAP_ID], |
| 161 | + result: undefined, |
| 162 | + }); |
| 163 | + |
| 164 | + expect(publishSpy.calls[1]).toStrictEqual({ |
| 165 | + args: ['ExecutionService:outboundResponse', MOCK_SNAP_ID], |
| 166 | + result: undefined, |
| 167 | + }); |
| 168 | + |
| 169 | + await service.terminateAllSnaps(); |
| 170 | + publishSpy.reset(); |
| 171 | + }); |
| 172 | + |
| 173 | + it('confirms that events are secured', async () => { |
| 174 | + // Check if the security critical properties of the Event object |
| 175 | + // are unavailable. This will confirm that executeLockdownEvents works |
| 176 | + // inside snaps-execution-environments |
| 177 | + const { service } = createService(WebWorkerExecutionService, { |
| 178 | + documentUrl: new URL(WORKER_POOL_URL), |
| 179 | + }); |
| 180 | + |
| 181 | + await service.executeSnap({ |
| 182 | + snapId: MOCK_SNAP_ID, |
| 183 | + sourceCode: ` |
| 184 | + module.exports.onRpcRequest = async ({ request }) => { |
| 185 | + let result; |
| 186 | + const promise = new Promise((resolve) => { |
| 187 | + const xhr = new XMLHttpRequest(); |
| 188 | + xhr.open('GET', 'https://metamask.io/'); |
| 189 | + xhr.send(); |
| 190 | + xhr.onreadystatechange = (ev) => { |
| 191 | + result = ev; |
| 192 | + resolve(); |
| 193 | + }; |
| 194 | + }); |
| 195 | + await promise; |
| 196 | +
|
| 197 | + return { |
| 198 | + targetIsUndefined: result.target === undefined, |
| 199 | + currentTargetIsUndefined: result.target === undefined, |
| 200 | + srcElementIsUndefined: result.target === undefined, |
| 201 | + composedPathIsUndefined: result.target === undefined |
| 202 | + }; |
| 203 | + }; |
| 204 | + `, |
| 205 | + endowments: ['console', 'XMLHttpRequest'], |
| 206 | + }); |
| 207 | + |
| 208 | + const result = await service.handleRpcRequest(MOCK_SNAP_ID, { |
| 209 | + origin: 'foo', |
| 210 | + handler: HandlerType.OnRpcRequest, |
| 211 | + request: { |
| 212 | + jsonrpc: '2.0', |
| 213 | + id: 1, |
| 214 | + method: 'foobar', |
| 215 | + params: [], |
| 216 | + }, |
| 217 | + }); |
| 218 | + |
| 219 | + expect(result).toStrictEqual({ |
| 220 | + targetIsUndefined: true, |
| 221 | + currentTargetIsUndefined: true, |
| 222 | + srcElementIsUndefined: true, |
| 223 | + composedPathIsUndefined: true, |
| 224 | + }); |
| 225 | + }); |
| 226 | +}); |
0 commit comments