From 892a39813baf371dce4db9d2101ef1cfda1581d7 Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:00 +0000 Subject: [PATCH 01/16] Introduce object proxy_for_requestID to track proxy decisions --- src/scripts/background.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/scripts/background.js b/src/scripts/background.js index 4527196..a1f34d2 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -219,6 +219,7 @@ function setDisabled(isError) { // ----- session global let authData = {}; let authPending = {}; +let proxy_for_requestID = {}; async function sendAuth(request) { // Do nothing if this not proxy auth request: From b4d1dc5eb0321979cb42b0dca5d735dcff7af789 Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:01 +0000 Subject: [PATCH 02/16] Introduce constant proxy_matched in function proxyRequest --- src/scripts/background.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/scripts/background.js b/src/scripts/background.js index a1f34d2..4f767e5 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -138,6 +138,7 @@ function storageOnChanged(changes, area) { } function proxyRequest(requestInfo) { + const proxy_matched = findProxyMatch(requestInfo.url, activeSettings); return findProxyMatch(requestInfo.url, activeSettings); } From 61ed1aa1523a24edbe5251c7daa3256a17968421 Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:02 +0000 Subject: [PATCH 03/16] Return proxy_matched from function proxyRequest --- src/scripts/background.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scripts/background.js b/src/scripts/background.js index 4f767e5..ffb387e 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -139,7 +139,7 @@ function storageOnChanged(changes, area) { function proxyRequest(requestInfo) { const proxy_matched = findProxyMatch(requestInfo.url, activeSettings); - return findProxyMatch(requestInfo.url, activeSettings); + return proxy_matched; } function setActiveSettings(settings) { From e6f18f5a0ca750e6539b872e962cb3c04cd681eb Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:03 +0000 Subject: [PATCH 04/16] Assign proxy_matched result to global tracking object proxy_for_requestID --- src/scripts/background.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/scripts/background.js b/src/scripts/background.js index ffb387e..fe1f86c 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -139,6 +139,7 @@ function storageOnChanged(changes, area) { function proxyRequest(requestInfo) { const proxy_matched = findProxyMatch(requestInfo.url, activeSettings); + proxy_for_requestID[requestInfo.requestId] = proxy_matched; return proxy_matched; } From e6c0e7bba278e99f83f68cc0636cd2af3709f62a Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:04 +0000 Subject: [PATCH 05/16] Limit previous Assignment to non speculative requests --- src/scripts/background.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/scripts/background.js b/src/scripts/background.js index fe1f86c..bb45132 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -139,7 +139,12 @@ function storageOnChanged(changes, area) { function proxyRequest(requestInfo) { const proxy_matched = findProxyMatch(requestInfo.url, activeSettings); - proxy_for_requestID[requestInfo.requestId] = proxy_matched; + // better to filter out: requestInfo.type == "speculative" + // They don't seem to fire in "onCompleted" or "onErrorOccurred" + // and will fill "proxy_for_requestID" without deletion. + if (requestInfo.type != "speculative") { + proxy_for_requestID[requestInfo.requestId] = proxy_matched; + } return proxy_matched; } From f79ade4515777a9d141b06335aeedb5ab0b0da87 Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:05 +0000 Subject: [PATCH 06/16] Introduce local variable proxy_matched to function sendAuth --- src/scripts/background.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/scripts/background.js b/src/scripts/background.js index bb45132..fab4748 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -251,6 +251,8 @@ async function sendAuth(request) { authPending[request.requestId] = 1; // prevent bad authentication loop return {authCredentials: authData[request.challenger.host]}; } + + let proxy_matched = {}; // --- no user/pass set for the challenger.host, leave the authentication to the browser } From 597a9b28adc1f129712b9215f0ff5519f647bc22 Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:06 +0000 Subject: [PATCH 07/16] Assign proxy info to proxy_matched based on the previous proxy selection. If such a previous decision is not found, repeat the selection process. --- src/scripts/background.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/scripts/background.js b/src/scripts/background.js index fab4748..9faf655 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -253,6 +253,11 @@ async function sendAuth(request) { } let proxy_matched = {}; + if (proxy_for_requestID[request.requestId]) { + proxy_matched = proxy_for_requestID[request.requestId]; + } else { + proxy_matched = proxyRequest(request); + }; // --- no user/pass set for the challenger.host, leave the authentication to the browser } From cf008641389dbbc47aa66930c0b9823d1e1aa3c9 Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:07 +0000 Subject: [PATCH 08/16] Add comments to developer thoughts about availability of host/proxy address. --- src/scripts/background.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/scripts/background.js b/src/scripts/background.js index 9faf655..5266180 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -252,6 +252,9 @@ async function sendAuth(request) { return {authCredentials: authData[request.challenger.host]}; } + // Can confirm (Fx 68.2 ESR), but requested host is available in "request.url": + // Use result from function "proxyRequest" via "proxy_for_requestID" if + // available, otherwise repeat call to "proxyRequest": let proxy_matched = {}; if (proxy_for_requestID[request.requestId]) { proxy_matched = proxy_for_requestID[request.requestId]; From 395571855cac4304946c5b20ec00f7d55182d8f5 Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:08 +0000 Subject: [PATCH 09/16] Construct and return authentication credentials to proxy, if - we know what proxy should be used, - this corresponds with the proxy used (host and port) and - we have a username or password to give away. Keep the "authPending" logic. --- src/scripts/background.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/scripts/background.js b/src/scripts/background.js index 5266180..f585594 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -261,6 +261,15 @@ async function sendAuth(request) { } else { proxy_matched = proxyRequest(request); }; + if (proxy_matched) { + if (request.challenger.host == proxy_matched.host && request.challenger.port == proxy_matched.port) { + authPending[request.requestId] = 1; // prevent bad authentication loop + if (proxy_matched.username != "" || proxy_matched.password != "") { + const response = {authCredentials: {username: proxy_matched.username, password: proxy_matched.password}}; + return response; + } + } + } // --- no user/pass set for the challenger.host, leave the authentication to the browser } From 527e4a72b52a282da24496bce96753ce1d68d99f Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:09 +0000 Subject: [PATCH 10/16] Introduce function clearProxyrRequestID to delete proxy info from global tracking object proxy_for_requestID --- src/scripts/background.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/scripts/background.js b/src/scripts/background.js index f585594..3bb81a8 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -286,6 +286,13 @@ async function getAuth(request) { }); } +function clearProxyrRequestID(request) { + + if (proxy_for_requestID[request.requestId]) { + delete proxy_for_requestID[request.requestId]; + } +} + function clearPending(request) { if(!authPending[request.requestId]) { return; } From 22835eca86ab4634835eb025b735f3c5439f54c8 Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:10 +0000 Subject: [PATCH 11/16] Make use of function clearProxyrRequestID to clean up tracking object. --- src/scripts/background.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/scripts/background.js b/src/scripts/background.js index 3bb81a8..6883013 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -295,6 +295,7 @@ function clearProxyrRequestID(request) { function clearPending(request) { + clearProxyrRequestID(request); if(!authPending[request.requestId]) { return; } if (request.error) { From ce3eb9e5c5acf4f06647e87e5f46e5d178229f45 Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:11 +0000 Subject: [PATCH 12/16] Stop using authData cache, based on the proxy address to choose credentials used for authentication. We get this info from proxy_for_requestID, based on the url/address from the actual host via function proxyRequest (from cache or directly). --- src/scripts/background.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/scripts/background.js b/src/scripts/background.js index 6883013..e3902e1 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -247,10 +247,6 @@ async function sendAuth(request) { // But in my tests (Fx 69.0.1 MacOS), it is indeed the proxy requesting the authentication // TODO: test in future Fx releases to see if that changes. // console.log(request.challenger.host, "challenger host"); - if (authData[request.challenger.host]) { - authPending[request.requestId] = 1; // prevent bad authentication loop - return {authCredentials: authData[request.challenger.host]}; - } // Can confirm (Fx 68.2 ESR), but requested host is available in "request.url": // Use result from function "proxyRequest" via "proxy_for_requestID" if From 94b69b528e56257f392f7571ce7acec4b6802bf6 Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:12 +0000 Subject: [PATCH 13/16] Auth credentials are provided from findProxyMatch and cached in proxy_for_requestID . No need for this special cache anymore. --- src/scripts/background.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/scripts/background.js b/src/scripts/background.js index e3902e1..9389c2b 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -238,9 +238,6 @@ async function sendAuth(request) { // --- already sent once and pending if (authPending[request.requestId]) { return {cancel: true}; } - // --- authData credentials not yet populated from storage - if(!Object.keys(authData)[0]) { await getAuth(request); } - // --- first authentication // According to https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onAuthRequired : // "request.challenger.host is the requested host instead of the proxy requesting the authentication" From c949e53ecf0ba6392090956117d79c6ca7f4d40e Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:13 +0000 Subject: [PATCH 14/16] Function getAuth has no user in code base anymore. --- src/scripts/background.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/scripts/background.js b/src/scripts/background.js index 9389c2b..cf5acd2 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -266,19 +266,6 @@ async function sendAuth(request) { // --- no user/pass set for the challenger.host, leave the authentication to the browser } -async function getAuth(request) { - - await new Promise(resolve => { - chrome.storage.local.get(null, result => { - const host = result.hostData[request.challenger.host]; - if (host && host.username) { // cache credentials in authData - authData[host] = {username: host.username, password: host.password}; - } - resolve(); - }); - }); -} - function clearProxyrRequestID(request) { if (proxy_for_requestID[request.requestId]) { From e9c600b8cd0c2758a4d20ec4fa7e012b88aad481 Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:14 +0000 Subject: [PATCH 15/16] authData has no consumers anymore. To stop computing content of authData here, was the goal of this patch series. It stored credentials only for the last proxy configuration with the same proxy address and "forgot" earlier configurations. Disabled proxies were not sorted out, different ports were ignored In case of different proxy configurations with the same address, always the same username and password (from the last configuration) was used. --- src/scripts/background.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/scripts/background.js b/src/scripts/background.js index cf5acd2..546fb4d 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -154,10 +154,6 @@ function setActiveSettings(settings) { const pref = settings; const prefKeys = Object.keys(pref).filter(item => !['mode', 'logging', 'sync'].includes(item)); // not for these - // --- cache credentials in authData (only those with user/pass) - prefKeys.forEach(id => pref[id].username && pref[id].password && - (authData[pref[id].address] = {username: pref[id].username, password: pref[id].password}) ); - const mode = settings.mode; activeSettings = { // global mode, From 54971def4a0367ca289820d751176d95c283d3c8 Mon Sep 17 00:00:00 2001 From: pubkeypin Date: Fri, 20 Dec 2019 12:00:15 +0000 Subject: [PATCH 16/16] authData is not used anymore. --- src/scripts/background.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/scripts/background.js b/src/scripts/background.js index 546fb4d..0924e24 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -220,7 +220,6 @@ function setDisabled(isError) { // ----------------- Proxy Authentication ------------------ // ----- session global -let authData = {}; let authPending = {}; let proxy_for_requestID = {};