Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/snaps-execution-environments/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 90.74,
"functions": 94.96,
"lines": 90.85,
"statements": 90.27
"lines": 90.84,
"statements": 90.26
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export class BaseSnapExecutor {
});
}

async #respond(id: JsonRpcId, response: Record<string, unknown>) {
async #respond(id: JsonRpcId, response: Record<string, Json>) {
if (!isValidResponse(response)) {
// Instead of throwing, we directly respond with an error.
// This prevents an issue where we wouldn't respond when errors were non-serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ describe('isValidResponse', () => {
expect(isValidResponse('foo')).toBe(false);
});

it('returns false if the value is not JSON serializable', () => {
expect(isValidResponse({ foo: BigInt(0) })).toBe(false);
});

it('returns false if the value is too large', () => {
expect(isValidResponse({ foo: '1'.repeat(100_000_000) })).toBe(false);
});
Expand Down
17 changes: 8 additions & 9 deletions packages/snaps-execution-environments/src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { RequestArguments } from '@metamask/providers';
import { rpcErrors } from '@metamask/rpc-errors';
import { assert, getJsonSize, getSafeJson, isObject } from '@metamask/utils';
import { getJsonSizeUnsafe } from '@metamask/snaps-utils';
import type { Json } from '@metamask/utils';
import { assert, getSafeJson, isObject } from '@metamask/utils';

import { log } from '../logging';

Expand Down Expand Up @@ -128,16 +130,13 @@ export function sanitizeRequestArguments(value: unknown): RequestArguments {
* @param response - The response.
* @returns True if the response is valid, otherwise false.
*/
export function isValidResponse(response: Record<string, unknown>) {
export function isValidResponse(response: Record<string, Json>) {
if (!isObject(response)) {
return false;
}

try {
// If the JSON is invalid this will throw and we should return false.
const size = getJsonSize(response);
return size < MAX_RESPONSE_JSON_SIZE;
} catch {
return false;
}
// We know that the response is valid JSON already and we don't
// need the size to be exact.
const size = getJsonSizeUnsafe(response);
return size < MAX_RESPONSE_JSON_SIZE;
}
1 change: 1 addition & 0 deletions packages/snaps-utils/src/index.executionenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from './errors';
export * from './handlers/exports';
export * from './handlers/types';
export * from './iframe';
export * from './json';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't be necessary anymore after #3322 😄

export * from './logging';
export * from './types';
Loading