From bbaa1509b83c7a56ccd3112d08aac709613dfb2f Mon Sep 17 00:00:00 2001 From: Hassan Malik Date: Fri, 30 Aug 2024 12:42:25 -0400 Subject: [PATCH 1/3] update char limits --- packages/snaps-rpc-methods/jest.config.js | 2 +- .../src/restricted/notify.test.ts | 16 ++++++++++++++-- .../snaps-rpc-methods/src/restricted/notify.ts | 16 +++++++++++++++- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/packages/snaps-rpc-methods/jest.config.js b/packages/snaps-rpc-methods/jest.config.js index e5ee2eb01e..015ebd2bcd 100644 --- a/packages/snaps-rpc-methods/jest.config.js +++ b/packages/snaps-rpc-methods/jest.config.js @@ -10,7 +10,7 @@ module.exports = deepmerge(baseConfig, { ], coverageThreshold: { global: { - branches: 91.81, + branches: 92.07, functions: 96.96, lines: 97.51, statements: 96.97, diff --git a/packages/snaps-rpc-methods/src/restricted/notify.test.ts b/packages/snaps-rpc-methods/src/restricted/notify.test.ts index 3c662c155e..5b4b3f85b1 100644 --- a/packages/snaps-rpc-methods/src/restricted/notify.test.ts +++ b/packages/snaps-rpc-methods/src/restricted/notify.test.ts @@ -241,10 +241,10 @@ describe('snap_notify', () => { ); }); - it('throws an error if the message is larger than 50 characters', () => { + it('throws an error if the message is larger than or equal to 50 characters for native notifications', () => { expect(() => getValidatedParams({ - type: NotificationType.InApp, + type: NotificationType.Native, message: 'test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg', }), @@ -253,6 +253,18 @@ describe('snap_notify', () => { ); }); + it('throws an error if the message is larger than or equal to 500 characters for in app notifications', () => { + expect(() => + getValidatedParams({ + type: NotificationType.InApp, + message: + 'test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg_test_msg', + }), + ).toThrow( + 'Must specify a non-empty string "message" less than 500 characters long.', + ); + }); + it('returns valid parameters', () => { expect(getValidatedParams(validParams)).toStrictEqual(validParams); }); diff --git a/packages/snaps-rpc-methods/src/restricted/notify.ts b/packages/snaps-rpc-methods/src/restricted/notify.ts index fdf3001be3..8a5732dddd 100644 --- a/packages/snaps-rpc-methods/src/restricted/notify.ts +++ b/packages/snaps-rpc-methods/src/restricted/notify.ts @@ -173,13 +173,27 @@ export function getValidatedParams(params: unknown): NotifyParams { }); } + const isNotString = !message || typeof message !== 'string'; // Set to the max message length on a Mac notification for now. - if (!message || typeof message !== 'string' || message.length >= 50) { + if ( + isNotString || + (type === NotificationType.Native && message.length >= 50) + ) { throw rpcErrors.invalidParams({ message: 'Must specify a non-empty string "message" less than 50 characters long.', }); } + if ( + isNotString || + (type === NotificationType.InApp && message.length >= 500) + ) { + throw rpcErrors.invalidParams({ + message: + 'Must specify a non-empty string "message" less than 500 characters long.', + }); + } + return params as NotificationArgs; } From 6e724dc7c3c6f25565e5bb55a549787ca9e5f9cb Mon Sep 17 00:00:00 2001 From: Hassan Malik Date: Tue, 3 Sep 2024 12:19:13 -0400 Subject: [PATCH 2/3] update condition --- packages/snaps-rpc-methods/src/restricted/notify.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/snaps-rpc-methods/src/restricted/notify.ts b/packages/snaps-rpc-methods/src/restricted/notify.ts index 8a5732dddd..8c4c11f882 100644 --- a/packages/snaps-rpc-methods/src/restricted/notify.ts +++ b/packages/snaps-rpc-methods/src/restricted/notify.ts @@ -176,8 +176,8 @@ export function getValidatedParams(params: unknown): NotifyParams { const isNotString = !message || typeof message !== 'string'; // Set to the max message length on a Mac notification for now. if ( - isNotString || - (type === NotificationType.Native && message.length >= 50) + type === NotificationType.Native && + (isNotString || message.length >= 50) ) { throw rpcErrors.invalidParams({ message: @@ -186,8 +186,8 @@ export function getValidatedParams(params: unknown): NotifyParams { } if ( - isNotString || - (type === NotificationType.InApp && message.length >= 500) + type === NotificationType.InApp && + (isNotString || message.length >= 500) ) { throw rpcErrors.invalidParams({ message: From 721602bf8a1275699b88a4c249aeff3908b56b5c Mon Sep 17 00:00:00 2001 From: Hassan Malik Date: Tue, 3 Sep 2024 12:26:45 -0400 Subject: [PATCH 3/3] fix test --- packages/snaps-rpc-methods/src/restricted/notify.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/snaps-rpc-methods/src/restricted/notify.test.ts b/packages/snaps-rpc-methods/src/restricted/notify.test.ts index 5b4b3f85b1..aeecba1a38 100644 --- a/packages/snaps-rpc-methods/src/restricted/notify.test.ts +++ b/packages/snaps-rpc-methods/src/restricted/notify.test.ts @@ -229,7 +229,7 @@ describe('snap_notify', () => { expect(() => getValidatedParams({ type: NotificationType.InApp, message: '' }), ).toThrow( - 'Must specify a non-empty string "message" less than 50 characters long.', + 'Must specify a non-empty string "message" less than 500 characters long.', ); }); @@ -237,7 +237,7 @@ describe('snap_notify', () => { expect(() => getValidatedParams({ type: NotificationType.InApp, message: 123 }), ).toThrow( - 'Must specify a non-empty string "message" less than 50 characters long.', + 'Must specify a non-empty string "message" less than 500 characters long.', ); });