From a630d8138492525821e8be391bad70d8f553b057 Mon Sep 17 00:00:00 2001 From: glasgio Date: Wed, 30 Aug 2023 18:12:28 -0300 Subject: [PATCH 1/4] Fix validation error of protocol not beeing returned Fixes #830 Fix the "https: or mailto:" error being overwritten by the error in the catch by passing upwards trough the cause property --- src/vapid-helper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vapid-helper.js b/src/vapid-helper.js index 0302593d..3d76afca 100644 --- a/src/vapid-helper.js +++ b/src/vapid-helper.js @@ -86,7 +86,7 @@ function validateSubject(subject) { + 'sending notifications.'); } } catch (err) { - throw new Error('Vapid subject is not a valid URL. ' + subject); + throw new Error('Vapid subject is not a valid URL. ' + subject, { cause: err }); } } From d11c30087824dcc4072ff0a7946decb986cabe07 Mon Sep 17 00:00:00 2001 From: glasgio Date: Thu, 31 Aug 2023 17:01:38 -0300 Subject: [PATCH 2/4] Change approach to fix validation error of protocol not beeing returned Fixes #830 Change the solution based on the review --- src/vapid-helper.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/vapid-helper.js b/src/vapid-helper.js index 3d76afca..bbe69c8b 100644 --- a/src/vapid-helper.js +++ b/src/vapid-helper.js @@ -77,17 +77,17 @@ function validateSubject(subject) { try { const subjectParseResult = new URL(subject); - if (!['https:', 'mailto:'].includes(subjectParseResult.protocol)) { - throw new Error('Vapid subject is not an https: or mailto: URL. ' + subject); - } - if (subjectParseResult.hostname === 'localhost') { - console.warn('Vapid subject points to a localhost web URI, which is unsupported by ' - + 'Apple\'s push notification server and will result in a BadJwtToken error when ' - + 'sending notifications.'); } } catch (err) { - throw new Error('Vapid subject is not a valid URL. ' + subject, { cause: err }); + throw new Error('Vapid subject is not a valid URL. ' + subject); + } + if (!['https:', 'mailto:'].includes(subjectParseResult.protocol)) { + throw new Error('Vapid subject is not an https: or mailto: URL. ' + subject); } + if (subjectParseResult.hostname === 'localhost') { + console.warn('Vapid subject points to a localhost web URI, which is unsupported by ' + + 'Apple\'s push notification server and will result in a BadJwtToken error when ' + + 'sending notifications.'); } function validatePublicKey(publicKey) { From 664b318bec1b9629e5de6ddbf31c5263351cb1c7 Mon Sep 17 00:00:00 2001 From: glasgio Date: Fri, 1 Sep 2023 13:24:21 -0300 Subject: [PATCH 3/4] Fix syntax error --- src/vapid-helper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vapid-helper.js b/src/vapid-helper.js index bbe69c8b..343d11e8 100644 --- a/src/vapid-helper.js +++ b/src/vapid-helper.js @@ -77,7 +77,6 @@ function validateSubject(subject) { try { const subjectParseResult = new URL(subject); - } } catch (err) { throw new Error('Vapid subject is not a valid URL. ' + subject); } @@ -88,6 +87,7 @@ function validateSubject(subject) { console.warn('Vapid subject points to a localhost web URI, which is unsupported by ' + 'Apple\'s push notification server and will result in a BadJwtToken error when ' + 'sending notifications.'); + } } function validatePublicKey(publicKey) { From 786ab38e5d2fca45d07ec5bbaca8cf940116f167 Mon Sep 17 00:00:00 2001 From: glasgio Date: Fri, 1 Sep 2023 14:11:55 -0300 Subject: [PATCH 4/4] Fix lint errors --- src/vapid-helper.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/vapid-helper.js b/src/vapid-helper.js index 343d11e8..c0305bdd 100644 --- a/src/vapid-helper.js +++ b/src/vapid-helper.js @@ -75,8 +75,9 @@ function validateSubject(subject) { + 'mailto: address. ' + subject); } + let subjectParseResult = null; try { - const subjectParseResult = new URL(subject); + subjectParseResult = new URL(subject); } catch (err) { throw new Error('Vapid subject is not a valid URL. ' + subject); }