From 66526d1c6816433147adc29ceed377bd1a1001c1 Mon Sep 17 00:00:00 2001 From: luckyrat Date: Sat, 15 Jun 2024 11:22:50 +0100 Subject: [PATCH] Fix HTTP Auth not working in Chrome --- src/background/NetworkAuth.ts | 12 +++++++ src/background/main.ts | 61 ++++++++++++++++++++++++++++------- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/background/NetworkAuth.ts b/src/background/NetworkAuth.ts index 2966811..39af1e0 100644 --- a/src/background/NetworkAuth.ts +++ b/src/background/NetworkAuth.ts @@ -8,6 +8,18 @@ import { kee } from "./KF"; export class NetworkAuth { constructor() { } + public provideCredentialsAsyncBlockingCallback( + requestDetails: any, + callback: (response: chrome.webRequest.BlockingResponse) => void + ) { + this.provideCredentialsAsync(requestDetails) + .then(result => callback(result)) + .catch(reason => { + KeeLog.error("AsyncBlockingCallback promise failed", reason); + callback({ cancel: false }); + }); + } + public async provideCredentialsAsync( requestDetails: any ): Promise { diff --git a/src/background/main.ts b/src/background/main.ts index 57455d6..f725e68 100644 --- a/src/background/main.ts +++ b/src/background/main.ts @@ -272,18 +272,55 @@ chrome.runtime.onConnect.addListener(async port => { // With MV3 we must always listen to httpauth requests and decide whether to handle them // based on whether we have already initialised the pinia store, got connected to KPRPC, have open DBs, etc. -chrome.webRequest.onAuthRequired.addListener( - async (requestDetails): Promise => { - if (KeeLog?.debug) KeeLog.debug("onAuthRequired request started"); - // We may crash at startup / session restore if we're not initialised yet - await Promise.race([initialised, new Promise(resolve => self.setTimeout(resolve, 20000))]); - if (KeeLog?.debug) KeeLog.debug("onAuthRequired request ongoing"); - const result = await networkAuth.provideCredentialsAsync(requestDetails); - return result; - }, - { urls: [""] }, - [isFirefox() ? "blocking" : "asyncBlocking"] -); +if (isFirefox()) { + chrome.webRequest.onAuthRequired.addListener( + async (requestDetails): Promise => { + if (KeeLog?.debug) KeeLog.debug("onAuthRequired request started"); + // We may crash at startup / session restore if we're not initialised yet + await Promise.race([initialised, new Promise(resolve => self.setTimeout(resolve, 20000))]); + if (KeeLog?.debug) KeeLog.debug("onAuthRequired request ongoing"); + const result = await networkAuth.provideCredentialsAsync(requestDetails); + return result; + }, + { urls: [""] }, + ["blocking"] + ); +} else { + chrome.webRequest.onAuthRequired.addListener( + (requestDetails, callback) => { + if (KeeLog?.debug) KeeLog.debug("onAuthRequired request started"); + Promise.race([ + initialised, + new Promise(resolve => self.setTimeout(resolve, 20000)) + ]) + .then(() => { + if (KeeLog?.debug) KeeLog.debug("onAuthRequired request ongoing"); + networkAuth.provideCredentialsAsyncBlockingCallback(requestDetails, callback); + }); + }, + { urls: [""] }, + ["asyncBlocking"] + ); +} + +// This can be used instead once Chrome either supports Promises in "blocking" mode +// or, more likely, a new flag, which then Firefox would have to support too but we +// could still just use a conditional in the mean time to keep compatibility with a +// wider range of Firefox versions. +// https://github.com/w3c/webextensions/issues/490 +// https://issues.chromium.org/issues/41483002 +// chrome.webRequest.onAuthRequired.addListener( +// async (requestDetails): Promise => { +// if (KeeLog?.debug) KeeLog.debug("onAuthRequired request started"); +// // We may crash at startup / session restore if we're not initialised yet +// await Promise.race([initialised, new Promise(resolve => self.setTimeout(resolve, 20000))]); +// if (KeeLog?.debug) KeeLog.debug("onAuthRequired request ongoing"); +// const result = await networkAuth.provideCredentialsAsync(requestDetails); +// return result; +// }, +// { urls: [""] }, +// [isFirefox() ? "blocking" : "asyncBlockingPromise"] +// ); (async () => { await ensureStarted();