From b37b44826a812a21210fdda2be74ab11f96672ac Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Wed, 8 Jan 2020 13:10:15 +0800 Subject: [PATCH 1/5] Prevent case that login search string is empty Might prevent a situation like https://github.com/solid/solid-cli/issues/8 --- src/SolidClient.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/SolidClient.js b/src/SolidClient.js index 0ffd2ae..5a0c8e3 100644 --- a/src/SolidClient.js +++ b/src/SolidClient.js @@ -181,7 +181,8 @@ class SolidClient { if (this.isAboveVersion511(loginResponse.headers['x-powered-by'])) { const consentUrl = new URL(authUrl); const search = consentUrl.search.substring(1); - let consPostData = JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/\=/g, '":"') + '"}'); + const searchJson = decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/\=/g, '":"'); + let consPostData = searchJson ? JSON.parse(`{"${searchJson}"}`) : {}; consPostData.consent = true; consPostData.access_mode = ['Read', 'Write', 'Append', 'Control']; consPostData = querystring.stringify(consPostData); From cef68409124c17e1b4b33e5c21f0df3b00dd1ad1 Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Fri, 10 Jan 2020 14:27:54 +0800 Subject: [PATCH 2/5] refactor: throw if search string is incorrect in login response --- src/SolidClient.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/SolidClient.js b/src/SolidClient.js index 5a0c8e3..4206893 100644 --- a/src/SolidClient.js +++ b/src/SolidClient.js @@ -181,8 +181,16 @@ class SolidClient { if (this.isAboveVersion511(loginResponse.headers['x-powered-by'])) { const consentUrl = new URL(authUrl); const search = consentUrl.search.substring(1); + if (!search) { + throw new Error(`Login response doesn't contain a search string: ${authUrl}`); + } const searchJson = decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/\=/g, '":"'); - let consPostData = searchJson ? JSON.parse(`{"${searchJson}"}`) : {}; + let consPostData = {}; + try { + consPostData = JSON.parse(`{"${searchJson}"}`); + } catch (error) { + throw new Error(`Login response doesn't contain a search string: ${authUrl} , cause JSON parsing error ${error}`); + } consPostData.consent = true; consPostData.access_mode = ['Read', 'Write', 'Append', 'Control']; consPostData = querystring.stringify(consPostData); From 8a6bae0d7fcf3bdb07e7ebc7f91168ad7715de16 Mon Sep 17 00:00:00 2001 From: linonetwo Date: Fri, 10 Jan 2020 14:35:18 +0800 Subject: [PATCH 3/5] lint: run prettier --- src/SolidClient.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/SolidClient.js b/src/SolidClient.js index 4206893..e09576f 100644 --- a/src/SolidClient.js +++ b/src/SolidClient.js @@ -136,8 +136,7 @@ class SolidClient { // Extract the password form's hidden fields const loginParams = { loginUrl }; let match, inputRegex = / { response.body = ''; - response.on('data', data => response.body += data); + response.on('data', data => (response.body += data)); response.on('end', () => resolve(response)); }); request.on('error', reject); @@ -249,7 +256,7 @@ class SolidClient { */ isExpired(session) { const now = Date.now() / 1000; - const expiry = session.idClaims && session.idClaims.exp || 0; + const expiry = (session.idClaims && session.idClaims.exp) || 0; return expiry < now; } } From 04649fdde58225f7c90adf00a60cf8e0bb9e0877 Mon Sep 17 00:00:00 2001 From: linonetwo Date: Sat, 11 Jan 2020 11:18:50 +0800 Subject: [PATCH 4/5] fix: typo --- src/SolidClient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SolidClient.js b/src/SolidClient.js index e09576f..e43f7a0 100644 --- a/src/SolidClient.js +++ b/src/SolidClient.js @@ -193,7 +193,7 @@ class SolidClient { } catch (error) { throw new Error( - `Login response doesn't contain a search string: ${authUrl} , cause JSON parsing error ${error}` + `Login response doesn't contain a search string: ${authUrl}, causing a JSON parsing error: ${error}` ); } consPostData.consent = true; From e3e72f9b0ab724d903e2949c6321d33ff67bf22c Mon Sep 17 00:00:00 2001 From: linonetwo Date: Sat, 11 Jan 2020 11:19:45 +0800 Subject: [PATCH 5/5] lint: format lone line --- src/SolidClient.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/SolidClient.js b/src/SolidClient.js index e43f7a0..71c8c45 100644 --- a/src/SolidClient.js +++ b/src/SolidClient.js @@ -136,7 +136,8 @@ class SolidClient { // Extract the password form's hidden fields const loginParams = { loginUrl }; let match, inputRegex = /