passbolt-browser-extension 5.16.1 → 5.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +105 -1
- package/RELEASE_NOTES.md +94 -4
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager/AppDelegate.swift +3 -3
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager/ViewController.swift +10 -10
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Extension/SafariWebExtensionHandler.swift +5 -5
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Extension/controllers/AbstractController.swift +5 -5
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Extension/controllers/ErrorController.swift +2 -2
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Extension/controllers/FetchController.swift +3 -3
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Extension/controllers/OpenSafariSettingsController.swift +1 -1
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Extension/controllers/SaveFileController.swift +2 -2
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Extension/controllers/WriteClipboardController.swift +3 -3
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Extension/services/fetch/SecureProfileSessionManager.swift +36 -30
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Extension/services/fetch/fetchService.swift +24 -24
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Extension/services/file/SaveFileService.swift +15 -15
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Extension/utils/ErrorSerializer.swift +13 -13
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Extension/utils/LocatedError.swift +3 -3
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Tests/SafariWebExtensionHandlerTests.swift +3 -3
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Tests/controllers/AbstractControllerTests.swift +2 -2
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Tests/controllers/ErrorControllerTests.swift +1 -1
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Tests/controllers/FetchControllerTests.swift +1 -1
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Tests/controllers/OpenSafariSettingsControllerTests.swift +1 -1
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Tests/controllers/SaveFileControllerTests.swift +1 -1
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Tests/controllers/WriteClipboardControllerTests.swift +1 -1
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Tests/services/fetch/FetchServiceTests.swift +1 -1
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Tests/services/fetch/SecureProfileSessionManagerTests.swift +7 -7
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Tests/utils/ErrorSerializerTests.swift +17 -17
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager Tests/utils/LocatedErrorTests.swift +12 -12
- package/build-safari-extension/Passbolt-Safari-Extension/Passbolt - password manager.xcodeproj/project.pbxproj +2 -4
- package/package.json +10 -7
- package/src/all/background_page/controller/accountRecovery/recoverAccountController.js +4 -0
- package/src/all/background_page/controller/accountRecovery/recoverAccountController.test.js +5 -1
- package/src/all/background_page/controller/informCallToActionController/informCallToActionController.js +4 -2
- package/src/all/background_page/controller/informCallToActionController/informCallToActionController.test.js +4 -2
- package/src/all/background_page/controller/mfaPolicy/mfaGetMfaSettingsController.test.js +13 -0
- package/src/all/background_page/controller/mfaSetup/MfaSetupAbortWebauthnCeremonyController.js +52 -0
- package/src/all/background_page/controller/mfaSetup/MfaSetupAbortWebauthnCeremonyController.test.js +54 -0
- package/src/all/background_page/controller/mfaSetup/MfaSetupBeginWebauthnCeremonyController.js +52 -0
- package/src/all/background_page/controller/mfaSetup/MfaSetupBeginWebauthnCeremonyController.test.js +54 -0
- package/src/all/background_page/controller/mfaSetup/MfaSetupDeleteWebauthnCredentialController.js +55 -0
- package/src/all/background_page/controller/mfaSetup/MfaSetupDeleteWebauthnCredentialController.test.js +55 -0
- package/src/all/background_page/controller/mfaSetup/MfaSetupFinishWebauthnCeremonyController.js +54 -0
- package/src/all/background_page/controller/mfaSetup/MfaSetupFinishWebauthnCeremonyController.test.js +54 -0
- package/src/all/background_page/controller/mfaSetup/MfaSetupGetWebauthnCredentialsController.js +54 -0
- package/src/all/background_page/controller/mfaSetup/MfaSetupGetWebauthnCredentialsController.test.js +61 -0
- package/src/all/background_page/controller/mfaSetup/MfaSetupGetWebauthnSettingsController.js +53 -0
- package/src/all/background_page/controller/mfaSetup/MfaSetupGetWebauthnSettingsController.test.js +57 -0
- package/src/all/background_page/controller/mfaSetup/MfaSetupHandleWebauthnCeremonyResultController.js +55 -0
- package/src/all/background_page/controller/mfaSetup/MfaSetupHandleWebauthnCeremonyResultController.test.js +52 -0
- package/src/all/background_page/controller/move/moveFolderController.js +12 -12
- package/src/all/background_page/controller/move/moveFolderController.test.js +31 -1
- package/src/all/background_page/controller/move/moveResourcesController.js +18 -5
- package/src/all/background_page/controller/move/moveResourcesController.test.js +68 -7
- package/src/all/background_page/controller/recover/completeRecoverController.js +4 -0
- package/src/all/background_page/controller/recover/completeRecoverController.test.js +5 -1
- package/src/all/background_page/controller/setup/completeSetupController.js +4 -0
- package/src/all/background_page/controller/setup/completeSetupController.test.js +5 -1
- package/src/all/background_page/error/webauthnAlreadyRunningCeremonyError.js +24 -0
- package/src/all/background_page/error/webauthnAuthenticatorAlreadyRegisteredError.js +26 -0
- package/src/all/background_page/error/webauthnCeremonyAbortedError.js +26 -0
- package/src/all/background_page/error/webauthnCeremonyCancelledError.js +26 -0
- package/src/all/background_page/error/webauthnCeremonyError.js +26 -0
- package/src/all/background_page/error/webauthnCeremonyNotFoundError.js +26 -0
- package/src/all/background_page/error/webauthnTLSCertificateError.js +24 -0
- package/src/all/background_page/event/appBootstrapEvents.js +32 -0
- package/src/all/background_page/event/appBootstrapEvents.test.js +31 -0
- package/src/all/background_page/event/folderEvents.js +5 -4
- package/src/all/background_page/event/informCallToActionEvents.js +3 -2
- package/src/all/background_page/event/mfaEvents.js +38 -1
- package/src/all/background_page/event/resourceEvents.js +9 -5
- package/src/all/background_page/index.js +16 -0
- package/src/all/background_page/model/entity/folder/foldersCollection.js +13 -9
- package/src/all/background_page/model/entity/folder/foldersCollection.test.data.js +35 -0
- package/src/all/background_page/model/entity/folder/foldersCollection.test.js +60 -0
- package/src/all/background_page/model/entity/mfa/mfaEnabledProviderEntity.js +11 -0
- package/src/all/background_page/model/entity/mfa/mfaEnabledProviderEntity.test.js +13 -0
- package/src/all/background_page/model/entity/mfa/mfaWebauthnCredentialEntity.js +126 -0
- package/src/all/background_page/model/entity/mfa/mfaWebauthnCredentialEntity.test.data.js +26 -0
- package/src/all/background_page/model/entity/mfa/mfaWebauthnCredentialEntity.test.js +105 -0
- package/src/all/background_page/model/entity/mfa/mfaWebauthnCredentialsCollection.js +52 -0
- package/src/all/background_page/model/entity/mfa/mfaWebauthnCredentialsCollection.test.data.js +22 -0
- package/src/all/background_page/model/entity/mfa/mfaWebauthnCredentialsCollection.test.js +60 -0
- package/src/all/background_page/model/entity/mfa/mfaWebauthnSettingsEntity.js +45 -0
- package/src/all/background_page/model/entity/mfa/mfaWebauthnSettingsEntity.test.data.js +17 -0
- package/src/all/background_page/model/entity/mfa/mfaWebauthnSettingsEntity.test.js +48 -0
- package/src/all/background_page/model/entity/mfa/webauthnCredentialCreationOptionsEntity.js +235 -0
- package/src/all/background_page/model/entity/mfa/webauthnCredentialCreationOptionsEntity.test.data.js +42 -0
- package/src/all/background_page/model/entity/mfa/webauthnCredentialCreationOptionsEntity.test.js +152 -0
- package/src/all/background_page/model/entity/mfa/webauthnRegistrationResponseEntity.js +72 -0
- package/src/all/background_page/model/entity/mfa/webauthnRegistrationResponseEntity.test.data.js +26 -0
- package/src/all/background_page/model/entity/mfa/webauthnRegistrationResponseEntity.test.js +83 -0
- package/src/all/background_page/model/entity/mfa/webauthnSetupCeremonyEntity.js +121 -0
- package/src/all/background_page/model/entity/mfa/webauthnSetupCeremonyEntity.test.data.js +36 -0
- package/src/all/background_page/model/entity/mfa/webauthnSetupCeremonyEntity.test.js +100 -0
- package/src/all/background_page/model/entity/permission/change/permissionChangesCollection.js +0 -59
- package/src/all/background_page/model/entity/permission/change/permissionChangesCollection.test.js +0 -333
- package/src/all/background_page/model/entity/userAndGroupSearchResultEntity/userAndGroupSearchResultEntity.js +1 -1
- package/src/all/background_page/model/entity/userAndGroupSearchResultEntity/userAndGroupSearchResultEntity.test.js +19 -1
- package/src/all/background_page/pagemod/accountRecoveryBootstrapPagemod.test.js +1 -1
- package/src/all/background_page/pagemod/appBootstrapPagemod.js +2 -1
- package/src/all/background_page/pagemod/appBootstrapPagemod.test.js +8 -8
- package/src/all/background_page/pagemod/authBootstrapPagemod.test.js +1 -1
- package/src/all/background_page/pagemod/pagemod.js +3 -1
- package/src/all/background_page/pagemod/pagemod.test.js +1 -1
- package/src/all/background_page/pagemod/publicWebsiteSignInPagemod.test.js +1 -1
- package/src/all/background_page/pagemod/recoverBootstrapPagemod.test.js +1 -1
- package/src/all/background_page/pagemod/setupBootstrapPagemod.test.js +1 -1
- package/src/all/background_page/pagemod/webIntegrationPagemod.test.js +1 -1
- package/src/all/background_page/sdk/port/portManager.js +13 -1
- package/src/all/background_page/sdk/port/portManager.test.js +98 -0
- package/src/all/background_page/service/api/mfa/webauthnApiService.js +76 -0
- package/src/all/background_page/service/api/mfa/webauthnApiService.test.js +119 -0
- package/src/all/background_page/service/api/mfa/webauthnSettingsApiService.js +44 -0
- package/src/all/background_page/service/api/mfa/webauthnSettingsApiService.test.js +56 -0
- package/src/all/background_page/service/auth/postLoginService.js +2 -1
- package/src/all/background_page/service/auth/postLoginService.test.js +1 -1
- package/src/all/background_page/service/auth/postLogoutService.js +2 -1
- package/src/all/background_page/service/auth/postLogoutService.test.js +24 -1
- package/src/all/background_page/service/extension/onExtensionUpdateAvailableService.test.js +2 -6
- package/src/all/background_page/service/group/groupUpdateSecretsCryptoService.js +1 -1
- package/src/all/background_page/service/group/groupUpdateService.js +1 -1
- package/src/all/background_page/service/group/groupUpdateService.test.js +3 -3
- package/src/all/background_page/service/localStorage/localStorageService.js +2 -0
- package/src/all/background_page/service/localStorage/localStorageService.test.js +3 -2
- package/src/all/background_page/service/mfa/validateWebauthnCreationOptionsService.js +41 -0
- package/src/all/background_page/service/mfa/validateWebauthnCreationOptionsService.test.js +52 -0
- package/src/all/background_page/service/mfa/webauthnSetupCeremonyService.js +266 -0
- package/src/all/background_page/service/mfa/webauthnSetupCeremonyService.test.js +437 -0
- package/src/all/background_page/service/move/deserializeConfirmedMovePermissions.js +41 -0
- package/src/all/background_page/service/move/deserializeConfirmedMovePermissions.test.js +80 -0
- package/src/all/background_page/service/move/moveOneFolderService.js +88 -135
- package/src/all/background_page/service/move/moveOneFolderService.test.js +774 -5237
- package/src/all/background_page/service/move/moveResourcesService.js +68 -40
- package/src/all/background_page/service/move/moveResourcesService.test.data.js +53 -0
- package/src/all/background_page/service/move/moveResourcesService.test.js +559 -2090
- package/src/all/background_page/service/offline/getOrFindOfflineSettingsService.js +2 -1
- package/src/all/background_page/service/offline/getOrFindOfflineSettingsService.test.js +23 -0
- package/src/all/background_page/service/permission/calculatePermissionsChangesService.js +172 -0
- package/src/all/background_page/service/permission/calculatePermissionsChangesService.test.js +370 -0
- package/src/all/background_page/service/rbac/getOrFindRbacService.js +2 -1
- package/src/all/background_page/service/resourceType/getOrFindResourceTypesService.js +2 -1
- package/src/all/background_page/service/resourceType/getOrFindResourceTypesService.test.js +19 -0
- package/src/all/background_page/service/sessionStorage/webauthnSetupCeremonySessionStorageService.js +62 -0
- package/src/all/background_page/service/sessionStorage/webauthnSetupCeremonySessionStorageService.test.js +60 -0
- package/src/all/background_page/service/sessionStorage/workersSessionStorage.js +30 -0
- package/src/all/background_page/service/sessionStorage/workersSessionStorage.test.js +55 -0
- package/src/all/background_page/service/share/shareFoldersService.js +20 -36
- package/src/all/background_page/service/share/shareFoldersService.test.js +236 -0
- package/src/all/background_page/service/tab/tabService.js +3 -2
- package/src/all/background_page/service/toolbar/toolbarService.js +13 -5
- package/src/all/background_page/service/ui/browserTab.service.js +6 -4
- package/src/all/background_page/service/ui/browserTab.service.test.js +19 -6
- package/src/all/background_page/service/user/getOrFindMeService.js +2 -1
- package/src/all/background_page/service/user/getOrFindMeService.test.js +17 -0
- package/src/all/background_page/service/worker/workerService.js +42 -11
- package/src/all/background_page/service/worker/workerService.test.js +70 -2
- package/src/all/contentScripts/js/app/App.js +2 -0
- package/src/all/contentScripts/js/controller/abortWebauthnSetupCeremonyContentScriptController.js +62 -0
- package/src/all/contentScripts/js/controller/abortWebauthnSetupCeremonyContentScriptController.test.js +44 -0
- package/src/all/contentScripts/js/controller/runWebauthnSetupCeremonyContentScriptController.js +65 -0
- package/src/all/contentScripts/js/controller/runWebauthnSetupCeremonyContentScriptController.test.js +70 -0
- package/src/all/contentScripts/js/event/webauthnSetupCeremonyEvents.js +33 -0
- package/src/all/contentScripts/js/service/webauthnSetupCeremonyContentScriptService.js +154 -0
- package/src/all/contentScripts/js/service/webauthnSetupCeremonyContentScriptService.test.js +252 -0
- package/src/all/locales/en-UK/common.json +0 -1
- package/src/all/webAccessibleResources/js/lib/port.js +106 -10
- package/src/all/webAccessibleResources/js/lib/port.test.js +174 -1
- package/src/chrome/manifest.json +2 -3
- package/src/chrome-mv3/index.js +6 -0
- package/src/chrome-mv3/manifest.json +3 -4
- package/src/firefox/manifest.json +2 -3
- package/src/safari/background_page/index.js +2 -1
- package/src/safari/background_page/service/cookies/cookiesService.js +9 -3
- package/src/safari/background_page/service/cookies/cookiesService.test.js +28 -1
- package/src/safari/manifest.json +2 -3
- package/test/mocks/mockWebExtensionPolyfill.js +2 -4
- package/src/all/background_page/service/move/calculatePermissionsChangesForMoveService.js +0 -113
- package/src/all/background_page/service/move/calculatePermissionsChangesForMoveService.test.js +0 -158
- package/src/all/background_page/service/move/confirmMoveStrategyService.js +0 -42
- package/src/all/background_page/service/move/confirmMoveStrategyService.test.js +0 -67
- /package/src/all/background_page/service/{move/calculatePermissionsChangesForMoveService.test.data.js → permission/calculatePermissionsChangesService.test.data.js} +0 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Passbolt ~ Open source password manager for teams
|
|
3
|
+
* Copyright (c) Passbolt SA (https://www.passbolt.com)
|
|
4
|
+
*
|
|
5
|
+
* Licensed under GNU Affero General Public License version 3 of the or any later version.
|
|
6
|
+
* For full copyright and license information, please see the LICENSE.txt
|
|
7
|
+
* Redistributions of files must retain the above copyright notice.
|
|
8
|
+
*
|
|
9
|
+
* @copyright Copyright (c) Passbolt SA (https://www.passbolt.com)
|
|
10
|
+
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
|
|
11
|
+
* @link https://www.passbolt.com Passbolt(tm)
|
|
12
|
+
* @since 5.17.0
|
|
13
|
+
*/
|
|
14
|
+
import Validator from "validator";
|
|
15
|
+
import WebauthnAuthenticatorAlreadyRegisteredError from "../../../background_page/error/webauthnAuthenticatorAlreadyRegisteredError";
|
|
16
|
+
import WebauthnCeremonyCancelledError from "../../../background_page/error/webauthnCeremonyCancelledError";
|
|
17
|
+
import WebauthnCeremonyError from "../../../background_page/error/webauthnCeremonyError";
|
|
18
|
+
import WebauthnTLSCertificateError from "../../../background_page/error/webauthnTLSCertificateError";
|
|
19
|
+
import WebauthnAlreadyRunningCeremonyError from "../../../background_page/error/webauthnAlreadyRunningCeremonyError";
|
|
20
|
+
import WebauthnRelyingPartyIpAddressError from "passbolt-styleguide/src/shared/lib/Error/WebauthnRelyingPartyIpAddressError";
|
|
21
|
+
|
|
22
|
+
// Authenticator data: rpIdHash (32) + flags (1) + signCount (4) + aaguid (16), https://www.w3.org/TR/webauthn-3/#sctn-attested-credential-data
|
|
23
|
+
const AAGUID_OFFSET = 37;
|
|
24
|
+
const AAGUID_LENGTH = 16;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Handles navigator.credentials.create() on the relying party origin on behalf of the background page.
|
|
28
|
+
*/
|
|
29
|
+
class WebauthnSetupCeremonyContentScriptService {
|
|
30
|
+
/**
|
|
31
|
+
* @constructor
|
|
32
|
+
*/
|
|
33
|
+
constructor() {
|
|
34
|
+
this.pendingId = null;
|
|
35
|
+
this.abortController = null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Run a ceremony and emit its result. A ceremony still pending is aborted first.
|
|
40
|
+
* @param {string} id creation options in PublicKeyCredentialCreationOptionsJSON form
|
|
41
|
+
* @param {WebauthnCredentialCreationOptionsEntity} options creation options in PublicKeyCredentialCreationOptionsJSON form
|
|
42
|
+
* @returns {Promise<object>}
|
|
43
|
+
* @throws {WebauthnRelyingPartyIpAddressError} if the page is reached through an IP address
|
|
44
|
+
* @throws {WebauthnCeremonyError} if the relying party id does not match the current page origin
|
|
45
|
+
* @throws {Error} if something goes wrong and that the abort did already drop the ceremony
|
|
46
|
+
*/
|
|
47
|
+
async run(id, options) {
|
|
48
|
+
this.abortController?.abort();
|
|
49
|
+
|
|
50
|
+
const abortController = new AbortController();
|
|
51
|
+
this.abortController = abortController;
|
|
52
|
+
|
|
53
|
+
this.pendingId = id;
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
if (Validator.isIP(location.hostname.replace(/^\[(.*)\]$/, "$1"))) {
|
|
57
|
+
throw new WebauthnRelyingPartyIpAddressError();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (location.hostname !== options?.rp?.id) {
|
|
61
|
+
throw new WebauthnCeremonyError(`The relying party id "${options?.rp?.id}" does not match the page origin.`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const publicKey = PublicKeyCredential.parseCreationOptionsFromJSON(options.toDto());
|
|
65
|
+
const credential = await navigator.credentials.create({ publicKey, signal: abortController.signal });
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
id,
|
|
69
|
+
credential: credential.toJSON(),
|
|
70
|
+
aaguid: this.getAaguid(credential),
|
|
71
|
+
};
|
|
72
|
+
} catch (error) {
|
|
73
|
+
// the background requested the abort and has not already dropped the ceremony
|
|
74
|
+
if (!abortController.signal.aborted) {
|
|
75
|
+
throw this.qualifyWebauthnCeremonyError(id, error);
|
|
76
|
+
}
|
|
77
|
+
} finally {
|
|
78
|
+
if (this.abortController === abortController) {
|
|
79
|
+
this.pendingId = null;
|
|
80
|
+
this.abortController = null;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Abort the pending ceremony if it is the one requested.
|
|
87
|
+
* @param {string} id the webauthn ceremony id to abort
|
|
88
|
+
*/
|
|
89
|
+
abort(id) {
|
|
90
|
+
if (this.pendingId === id) {
|
|
91
|
+
this.abortController?.abort();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Read the authenticator aaguid from the attestation, null when the authenticator data is truncated.
|
|
97
|
+
* @param {PublicKeyCredential} credential
|
|
98
|
+
* @returns {string|null} dashed hex, uuid-like
|
|
99
|
+
* @private
|
|
100
|
+
*/
|
|
101
|
+
getAaguid(credential) {
|
|
102
|
+
const data = new Uint8Array(credential.response.getAuthenticatorData());
|
|
103
|
+
if (data.length < AAGUID_OFFSET + AAGUID_LENGTH) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
const hex = Array.from(data.subarray(AAGUID_OFFSET, AAGUID_OFFSET + AAGUID_LENGTH), (b) =>
|
|
107
|
+
b.toString(16).padStart(2, "0"),
|
|
108
|
+
).join("");
|
|
109
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Map a ceremony exception to a typed error dto that crosses the port.
|
|
114
|
+
* Exception names per https://www.w3.org/TR/webauthn-3/#sctn-createCredential
|
|
115
|
+
* @param {string} ceremonyId
|
|
116
|
+
* @param {Error} error
|
|
117
|
+
* @returns {Error}
|
|
118
|
+
* @private
|
|
119
|
+
*/
|
|
120
|
+
qualifyWebauthnCeremonyError(ceremonyId, error) {
|
|
121
|
+
let typedError;
|
|
122
|
+
|
|
123
|
+
switch (error.name) {
|
|
124
|
+
case "NotAllowedError":
|
|
125
|
+
typedError = new WebauthnCeremonyCancelledError();
|
|
126
|
+
break;
|
|
127
|
+
case "InvalidStateError":
|
|
128
|
+
typedError = new WebauthnAuthenticatorAlreadyRegisteredError();
|
|
129
|
+
break;
|
|
130
|
+
case "WebauthnCeremonyError":
|
|
131
|
+
case "WebauthnRelyingPartyIpAddressError":
|
|
132
|
+
typedError = error;
|
|
133
|
+
break;
|
|
134
|
+
case "SecurityError":
|
|
135
|
+
typedError = new WebauthnTLSCertificateError();
|
|
136
|
+
break;
|
|
137
|
+
case "ConstraintError":
|
|
138
|
+
typedError = new WebauthnAlreadyRunningCeremonyError();
|
|
139
|
+
break;
|
|
140
|
+
default:
|
|
141
|
+
typedError = new WebauthnCeremonyError(`${error.name}: ${error.message}`);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
typedError.ceremonyId = ceremonyId;
|
|
145
|
+
// A passed-through error must not reference itself: the port serializes it with JSON.stringify.
|
|
146
|
+
if (typedError !== error) {
|
|
147
|
+
typedError.cause = error;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return typedError;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export default new WebauthnSetupCeremonyContentScriptService();
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Passbolt ~ Open source password manager for teams
|
|
3
|
+
* Copyright (c) Passbolt SA (https://www.passbolt.com)
|
|
4
|
+
*
|
|
5
|
+
* Licensed under GNU Affero General Public License version 3 of the or any later version.
|
|
6
|
+
* For full copyright and license information, please see the LICENSE.txt
|
|
7
|
+
* Redistributions of files must retain the above copyright notice.
|
|
8
|
+
*
|
|
9
|
+
* @copyright Copyright (c) Passbolt SA (https://www.passbolt.com)
|
|
10
|
+
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
|
|
11
|
+
* @link https://www.passbolt.com Passbolt(tm)
|
|
12
|
+
* @since 5.17.0
|
|
13
|
+
*/
|
|
14
|
+
import WebauthnSetupCeremonyContentScriptService from "./webauthnSetupCeremonyContentScriptService";
|
|
15
|
+
import WebauthnCredentialCreationOptionsEntity from "../../../background_page/model/entity/mfa/webauthnCredentialCreationOptionsEntity";
|
|
16
|
+
import { defaultWebauthnCredentialCreationOptionsDto } from "../../../background_page/model/entity/mfa/webauthnCredentialCreationOptionsEntity.test.data";
|
|
17
|
+
import { defaultWebauthnRegistrationResponseDto } from "../../../background_page/model/entity/mfa/webauthnRegistrationResponseEntity.test.data";
|
|
18
|
+
import WebauthnCeremonyError from "../../../background_page/error/webauthnCeremonyError";
|
|
19
|
+
import WebauthnCeremonyCancelledError from "../../../background_page/error/webauthnCeremonyCancelledError";
|
|
20
|
+
import WebauthnAuthenticatorAlreadyRegisteredError from "../../../background_page/error/webauthnAuthenticatorAlreadyRegisteredError";
|
|
21
|
+
import WebauthnTLSCertificateError from "../../../background_page/error/webauthnTLSCertificateError";
|
|
22
|
+
import WebauthnAlreadyRunningCeremonyError from "../../../background_page/error/webauthnAlreadyRunningCeremonyError";
|
|
23
|
+
import WebauthnRelyingPartyIpAddressError from "passbolt-styleguide/src/shared/lib/Error/WebauthnRelyingPartyIpAddressError";
|
|
24
|
+
|
|
25
|
+
const CEREMONY_ID = "8e3874ae-4b40-590b-968a-418f704b9d9a";
|
|
26
|
+
const OTHER_CEREMONY_ID = "1cbfca6f-ab36-4e13-a5b8-7c0a1b2c3d4e";
|
|
27
|
+
const AAGUID = "fbfc3007-154e-4ecc-8c0b-6e020557d7bd";
|
|
28
|
+
|
|
29
|
+
// rpIdHash (32) + flags (1) + signCount (4) + aaguid (16)
|
|
30
|
+
const authenticatorData = () => {
|
|
31
|
+
const bytes = new Uint8Array(53);
|
|
32
|
+
const aaguidBytes = AAGUID.replace(/-/g, "")
|
|
33
|
+
.match(/.{2}/g)
|
|
34
|
+
.map((hex) => parseInt(hex, 16));
|
|
35
|
+
bytes.set(aaguidBytes, 37);
|
|
36
|
+
return bytes.buffer;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const mockCredential = (data = {}) => ({
|
|
40
|
+
toJSON: () => defaultWebauthnRegistrationResponseDto(),
|
|
41
|
+
response: { getAuthenticatorData: () => authenticatorData() },
|
|
42
|
+
...data,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const domException = (name) => {
|
|
46
|
+
const error = new Error(`${name} message`);
|
|
47
|
+
error.name = name;
|
|
48
|
+
return error;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const defineGlobal = (name, value) => Object.defineProperty(globalThis, name, { value, configurable: true });
|
|
52
|
+
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
jest.clearAllMocks();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("WebauthnSetupCeremonyContentScriptService", () => {
|
|
58
|
+
let create, parseCreationOptionsFromJSON;
|
|
59
|
+
const options = new WebauthnCredentialCreationOptionsEntity(defaultWebauthnCredentialCreationOptionsDto());
|
|
60
|
+
|
|
61
|
+
beforeEach(() => {
|
|
62
|
+
// The service is a singleton, reset its state between tests.
|
|
63
|
+
WebauthnSetupCeremonyContentScriptService.pendingId = null;
|
|
64
|
+
WebauthnSetupCeremonyContentScriptService.abortController = null;
|
|
65
|
+
create = jest.fn().mockResolvedValue(mockCredential());
|
|
66
|
+
parseCreationOptionsFromJSON = jest.fn((json) => ({ parsed: json }));
|
|
67
|
+
defineGlobal("location", { hostname: "passbolt.local" });
|
|
68
|
+
defineGlobal("PublicKeyCredential", { parseCreationOptionsFromJSON });
|
|
69
|
+
Object.defineProperty(globalThis.navigator, "credentials", { value: { create }, configurable: true });
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe("::run", () => {
|
|
73
|
+
it("creates the credential and returns it with the aaguid.", async () => {
|
|
74
|
+
expect.assertions(5);
|
|
75
|
+
|
|
76
|
+
const result = await WebauthnSetupCeremonyContentScriptService.run(CEREMONY_ID, options);
|
|
77
|
+
|
|
78
|
+
expect(parseCreationOptionsFromJSON).toHaveBeenCalledWith(options.toDto());
|
|
79
|
+
expect(create).toHaveBeenCalledWith({ publicKey: { parsed: options.toDto() }, signal: expect.any(AbortSignal) });
|
|
80
|
+
expect(result).toEqual({
|
|
81
|
+
id: CEREMONY_ID,
|
|
82
|
+
credential: defaultWebauthnRegistrationResponseDto(),
|
|
83
|
+
aaguid: AAGUID,
|
|
84
|
+
});
|
|
85
|
+
expect(WebauthnSetupCeremonyContentScriptService.pendingId).toBeNull();
|
|
86
|
+
expect(WebauthnSetupCeremonyContentScriptService.abortController).toBeNull();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("throws a WebauthnCeremonyError if the page hostname differs from the relying party id.", async () => {
|
|
90
|
+
expect.assertions(4);
|
|
91
|
+
defineGlobal("location", { hostname: "evil.local" });
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
await WebauthnSetupCeremonyContentScriptService.run(CEREMONY_ID, options);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
expect(error).toBeInstanceOf(WebauthnCeremonyError);
|
|
97
|
+
expect(error.message).toEqual('The relying party id "passbolt.local" does not match the page origin.');
|
|
98
|
+
expect(error.ceremonyId).toEqual(CEREMONY_ID);
|
|
99
|
+
}
|
|
100
|
+
expect(create).not.toHaveBeenCalled();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it.each(["192.168.1.10", "[::1]"])(
|
|
104
|
+
"throws a WebauthnRelyingPartyIpAddressError if the page is reached through the IP address %s.",
|
|
105
|
+
async (hostname) => {
|
|
106
|
+
expect.assertions(3);
|
|
107
|
+
defineGlobal("location", { hostname });
|
|
108
|
+
|
|
109
|
+
const promise = WebauthnSetupCeremonyContentScriptService.run(CEREMONY_ID, options);
|
|
110
|
+
|
|
111
|
+
await expect(promise).rejects.toThrow(WebauthnRelyingPartyIpAddressError);
|
|
112
|
+
await expect(promise).rejects.toHaveProperty("ceremonyId", CEREMONY_ID);
|
|
113
|
+
expect(create).not.toHaveBeenCalled();
|
|
114
|
+
},
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
it("throws the qualified error if the credential creation fails.", async () => {
|
|
118
|
+
expect.assertions(3);
|
|
119
|
+
const error = domException("NotAllowedError");
|
|
120
|
+
create.mockRejectedValue(error);
|
|
121
|
+
|
|
122
|
+
const promise = WebauthnSetupCeremonyContentScriptService.run(CEREMONY_ID, options);
|
|
123
|
+
|
|
124
|
+
await expect(promise).rejects.toThrow(WebauthnCeremonyCancelledError);
|
|
125
|
+
await expect(promise).rejects.toHaveProperty("ceremonyId", CEREMONY_ID);
|
|
126
|
+
await expect(promise).rejects.toHaveProperty("cause", error);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("aborts a pending ceremony when a new one is run.", async () => {
|
|
130
|
+
expect.assertions(3);
|
|
131
|
+
const signals = [];
|
|
132
|
+
create.mockImplementation(({ signal }) => {
|
|
133
|
+
signals.push(signal);
|
|
134
|
+
return new Promise((resolve, reject) => {
|
|
135
|
+
signal.addEventListener("abort", () => reject(domException("AbortError")));
|
|
136
|
+
if (signals.length === 2) {
|
|
137
|
+
resolve(mockCredential());
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const first = WebauthnSetupCeremonyContentScriptService.run(CEREMONY_ID, options);
|
|
143
|
+
const second = WebauthnSetupCeremonyContentScriptService.run(OTHER_CEREMONY_ID, options);
|
|
144
|
+
const [firstResult, secondResult] = await Promise.all([first, second]);
|
|
145
|
+
|
|
146
|
+
expect(signals[0].aborted).toBe(true);
|
|
147
|
+
expect(firstResult).toBeUndefined();
|
|
148
|
+
expect(secondResult).toEqual(expect.objectContaining({ id: OTHER_CEREMONY_ID }));
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe("::abort", () => {
|
|
153
|
+
it("aborts the pending ceremony with a matching id.", async () => {
|
|
154
|
+
expect.assertions(3);
|
|
155
|
+
create.mockImplementation(
|
|
156
|
+
({ signal }) =>
|
|
157
|
+
new Promise((_resolve, reject) => signal.addEventListener("abort", () => reject(domException("AbortError")))),
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
const run = WebauthnSetupCeremonyContentScriptService.run(CEREMONY_ID, options);
|
|
161
|
+
const { signal } = WebauthnSetupCeremonyContentScriptService.abortController;
|
|
162
|
+
WebauthnSetupCeremonyContentScriptService.abort(CEREMONY_ID);
|
|
163
|
+
|
|
164
|
+
expect(signal.aborted).toBe(true);
|
|
165
|
+
await expect(run).resolves.toBeUndefined();
|
|
166
|
+
expect(WebauthnSetupCeremonyContentScriptService.abortController).toBeNull();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("ignores an abort for another ceremony id.", async () => {
|
|
170
|
+
expect.assertions(1);
|
|
171
|
+
let resolveCreate;
|
|
172
|
+
create.mockImplementation(() => new Promise((resolve) => (resolveCreate = resolve)));
|
|
173
|
+
|
|
174
|
+
const run = WebauthnSetupCeremonyContentScriptService.run(CEREMONY_ID, options);
|
|
175
|
+
WebauthnSetupCeremonyContentScriptService.abort(OTHER_CEREMONY_ID);
|
|
176
|
+
|
|
177
|
+
expect(WebauthnSetupCeremonyContentScriptService.abortController.signal.aborted).toBe(false);
|
|
178
|
+
resolveCreate(mockCredential());
|
|
179
|
+
await run;
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("does nothing if no ceremony is pending.", () => {
|
|
183
|
+
expect.assertions(1);
|
|
184
|
+
expect(() => WebauthnSetupCeremonyContentScriptService.abort(CEREMONY_ID)).not.toThrow();
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
describe("::getAaguid", () => {
|
|
189
|
+
it("returns the aaguid read from the authenticator data.", () => {
|
|
190
|
+
expect.assertions(1);
|
|
191
|
+
expect(WebauthnSetupCeremonyContentScriptService.getAaguid(mockCredential())).toEqual(AAGUID);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("returns null if the authenticator data is truncated.", () => {
|
|
195
|
+
expect.assertions(1);
|
|
196
|
+
const credential = mockCredential({ response: { getAuthenticatorData: () => new ArrayBuffer(37) } });
|
|
197
|
+
expect(WebauthnSetupCeremonyContentScriptService.getAaguid(credential)).toBeNull();
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe("::qualifyWebauthnCeremonyError", () => {
|
|
202
|
+
it.each([
|
|
203
|
+
["NotAllowedError", WebauthnCeremonyCancelledError],
|
|
204
|
+
["InvalidStateError", WebauthnAuthenticatorAlreadyRegisteredError],
|
|
205
|
+
["SecurityError", WebauthnTLSCertificateError],
|
|
206
|
+
["ConstraintError", WebauthnAlreadyRunningCeremonyError],
|
|
207
|
+
])("maps a %s exception to %p.", (domName, expectedErrorClass) => {
|
|
208
|
+
expect.assertions(3);
|
|
209
|
+
const error = domException(domName);
|
|
210
|
+
|
|
211
|
+
const result = WebauthnSetupCeremonyContentScriptService.qualifyWebauthnCeremonyError(CEREMONY_ID, error);
|
|
212
|
+
|
|
213
|
+
expect(result).toBeInstanceOf(expectedErrorClass);
|
|
214
|
+
expect(result.ceremonyId).toEqual(CEREMONY_ID);
|
|
215
|
+
expect(result.cause).toBe(error);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("keeps a WebauthnCeremonyError as is.", () => {
|
|
219
|
+
expect.assertions(3);
|
|
220
|
+
const error = new WebauthnCeremonyError("Something went wrong.");
|
|
221
|
+
|
|
222
|
+
const result = WebauthnSetupCeremonyContentScriptService.qualifyWebauthnCeremonyError(CEREMONY_ID, error);
|
|
223
|
+
|
|
224
|
+
expect(result).toBe(error);
|
|
225
|
+
expect(result.ceremonyId).toEqual(CEREMONY_ID);
|
|
226
|
+
expect(result.cause).toBeUndefined();
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("keeps a WebauthnRelyingPartyIpAddressError as is.", () => {
|
|
230
|
+
expect.assertions(3);
|
|
231
|
+
const error = new WebauthnRelyingPartyIpAddressError();
|
|
232
|
+
|
|
233
|
+
const result = WebauthnSetupCeremonyContentScriptService.qualifyWebauthnCeremonyError(CEREMONY_ID, error);
|
|
234
|
+
|
|
235
|
+
expect(result).toBe(error);
|
|
236
|
+
expect(result.ceremonyId).toEqual(CEREMONY_ID);
|
|
237
|
+
expect(result.cause).toBeUndefined();
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("maps any other exception to a WebauthnCeremonyError carrying the original name and message.", () => {
|
|
241
|
+
expect.assertions(4);
|
|
242
|
+
const error = domException("Error");
|
|
243
|
+
|
|
244
|
+
const result = WebauthnSetupCeremonyContentScriptService.qualifyWebauthnCeremonyError(CEREMONY_ID, error);
|
|
245
|
+
|
|
246
|
+
expect(result).toBeInstanceOf(WebauthnCeremonyError);
|
|
247
|
+
expect(result.message).toEqual("Error: Error message");
|
|
248
|
+
expect(result.ceremonyId).toEqual(CEREMONY_ID);
|
|
249
|
+
expect(result.cause).toBe(error);
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
});
|
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
"Calculating resources permissions changes": "Calculating resources permissions changes",
|
|
10
10
|
"Calculating secrets": "Calculating secrets",
|
|
11
11
|
"Calculating secrets {{counter}}/{{total}}": "Calculating secrets {{counter}}/{{total}}",
|
|
12
|
-
"Confirming share operation": "Confirming share operation",
|
|
13
12
|
"Could not verify the server key.": "Could not verify the server key.",
|
|
14
13
|
"Creating folder...": "Creating folder...",
|
|
15
14
|
"Creating metadata key": "Creating metadata key",
|
|
@@ -14,6 +14,14 @@
|
|
|
14
14
|
import { v4 as uuidv4 } from "uuid";
|
|
15
15
|
import Lock from "../../../background_page/utils/lock";
|
|
16
16
|
|
|
17
|
+
export const PORT_READY_TIMEOUT = 60000;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Maximum number of attempts to post a message, reconnecting the port between attempts.
|
|
21
|
+
* @type {number}
|
|
22
|
+
*/
|
|
23
|
+
export const EMIT_MAX_ATTEMPTS = 3;
|
|
24
|
+
|
|
17
25
|
class Port {
|
|
18
26
|
/**
|
|
19
27
|
*
|
|
@@ -37,10 +45,12 @@ class Port {
|
|
|
37
45
|
* @returns {Promise<>}
|
|
38
46
|
*/
|
|
39
47
|
connect() {
|
|
40
|
-
let resolver;
|
|
41
|
-
const promise = new Promise((resolve) => {
|
|
48
|
+
let resolver, rejecter;
|
|
49
|
+
const promise = new Promise((resolve, reject) => {
|
|
42
50
|
resolver = resolve;
|
|
51
|
+
rejecter = reject;
|
|
43
52
|
});
|
|
53
|
+
const isReconnection = Boolean(this._port);
|
|
44
54
|
try {
|
|
45
55
|
this._port = browser.runtime.connect({ name: this._name });
|
|
46
56
|
} catch (error) {
|
|
@@ -55,21 +65,70 @@ class Port {
|
|
|
55
65
|
}
|
|
56
66
|
this._connected = true;
|
|
57
67
|
this.initListener();
|
|
58
|
-
this.
|
|
68
|
+
this.handleReadyOrFailure(this._port, resolver, rejecter, isReconnection);
|
|
59
69
|
return promise;
|
|
60
70
|
}
|
|
61
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Settle a connection: resolve once the addon code acknowledges it, reject if the port is disconnected before or if
|
|
74
|
+
* a reconnection is not acknowledged in time. Without it, a connection denied by the addon code would wait forever
|
|
75
|
+
* while holding the connection lock, blocking any further message on this port.
|
|
76
|
+
* @param {object} port The runtime port being connected
|
|
77
|
+
* @param {function} resolve The connection resolver
|
|
78
|
+
* @param {function} reject The connection rejecter
|
|
79
|
+
* @param {boolean} isReconnection Whether the port was connected before, only a reconnection has a maximum delay
|
|
80
|
+
* @private
|
|
81
|
+
*/
|
|
82
|
+
handleReadyOrFailure(port, resolve, reject, isReconnection) {
|
|
83
|
+
let isSettled = false;
|
|
84
|
+
const onReady = () => {
|
|
85
|
+
if (isSettled) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
isSettled = true;
|
|
89
|
+
clearTimeout(timeout);
|
|
90
|
+
resolve();
|
|
91
|
+
};
|
|
92
|
+
const onFailure = (message) => {
|
|
93
|
+
if (isSettled) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
isSettled = true;
|
|
97
|
+
clearTimeout(timeout);
|
|
98
|
+
this.removeListener("passbolt.port.ready", onReady);
|
|
99
|
+
if (this._port === port) {
|
|
100
|
+
this._connected = false;
|
|
101
|
+
}
|
|
102
|
+
port.disconnect();
|
|
103
|
+
reject(new Error(message));
|
|
104
|
+
};
|
|
105
|
+
const timeout = isReconnection
|
|
106
|
+
? setTimeout(
|
|
107
|
+
() => onFailure(`The port ${this._name} has not been acknowledged by the addon code in time.`),
|
|
108
|
+
PORT_READY_TIMEOUT,
|
|
109
|
+
)
|
|
110
|
+
: null;
|
|
111
|
+
port.onDisconnect.addListener(() =>
|
|
112
|
+
onFailure(`The port ${this._name} has been disconnected before being acknowledged by the addon code.`),
|
|
113
|
+
);
|
|
114
|
+
this.once("passbolt.port.ready", onReady);
|
|
115
|
+
}
|
|
116
|
+
|
|
62
117
|
/**
|
|
63
118
|
* Init listener
|
|
64
119
|
* @private
|
|
65
120
|
*/
|
|
66
121
|
initListener() {
|
|
67
|
-
this._port
|
|
122
|
+
const port = this._port;
|
|
123
|
+
port.onDisconnect.addListener(() => {
|
|
124
|
+
if (this._port !== port) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
68
127
|
console.warn(`port disconnected from addon code: ${this._name}`);
|
|
69
128
|
this._connected = false;
|
|
70
129
|
});
|
|
71
130
|
|
|
72
|
-
|
|
131
|
+
port.onMessage.addListener((msg) => {
|
|
73
132
|
this._onMessage(msg);
|
|
74
133
|
});
|
|
75
134
|
}
|
|
@@ -146,6 +205,9 @@ class Port {
|
|
|
146
205
|
if (index > -1) {
|
|
147
206
|
this._listeners[name].splice(index, 1);
|
|
148
207
|
}
|
|
208
|
+
if (this._listeners[name].length === 0) {
|
|
209
|
+
delete this._listeners[name];
|
|
210
|
+
}
|
|
149
211
|
}
|
|
150
212
|
|
|
151
213
|
/**
|
|
@@ -164,9 +226,39 @@ class Port {
|
|
|
164
226
|
* @param requestArgs the arguments
|
|
165
227
|
*/
|
|
166
228
|
async emit(...requestArgs) {
|
|
167
|
-
|
|
229
|
+
await this.postMessageWithRetry(JSON.stringify(requestArgs));
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Post a message, connecting the port first if needed.
|
|
234
|
+
* The port can be dead without its disconnection being received (i.e. the service worker was terminated while the
|
|
235
|
+
* disconnection is not delivered to the page on Edge): reconnect and try again, up to a maximum number of attempts.
|
|
236
|
+
* @param {string} message The serialized message
|
|
237
|
+
* @param {number} [attemptsLeft] The remaining number of attempts
|
|
238
|
+
* @returns {Promise<void>}
|
|
239
|
+
* @throws {Error} If the message cannot be posted after the last attempt
|
|
240
|
+
* @private
|
|
241
|
+
*/
|
|
242
|
+
async postMessageWithRetry(message, attemptsLeft = EMIT_MAX_ATTEMPTS) {
|
|
168
243
|
await this.connectIfDisconnected();
|
|
169
|
-
|
|
244
|
+
try {
|
|
245
|
+
this._port.postMessage(message);
|
|
246
|
+
} catch (error) {
|
|
247
|
+
if (attemptsLeft <= 1) {
|
|
248
|
+
throw error;
|
|
249
|
+
}
|
|
250
|
+
console.warn(`port found disconnected while emitting: ${this._name}`, error);
|
|
251
|
+
this.markAsDisconnected();
|
|
252
|
+
await this.postMessageWithRetry(message, attemptsLeft - 1);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Flag the current port as disconnected, so the next emit reconnects it.
|
|
258
|
+
* @private
|
|
259
|
+
*/
|
|
260
|
+
markAsDisconnected() {
|
|
261
|
+
this._connected = false;
|
|
170
262
|
}
|
|
171
263
|
|
|
172
264
|
/**
|
|
@@ -187,15 +279,19 @@ class Port {
|
|
|
187
279
|
* Observe when the request has been completed.
|
|
188
280
|
* Or if a progress notification is sent.
|
|
189
281
|
*/
|
|
190
|
-
|
|
282
|
+
const onResponse = (status, ...callbackArgs) => {
|
|
191
283
|
if (status === "SUCCESS") {
|
|
192
284
|
resolve.apply(null, callbackArgs);
|
|
193
285
|
} else if (status === "ERROR") {
|
|
194
286
|
reject.apply(null, callbackArgs);
|
|
195
287
|
}
|
|
288
|
+
};
|
|
289
|
+
this.once(requestId, onResponse);
|
|
290
|
+
// Emit the message to the addon-code, the request fails if it cannot be sent (i.e. the connection is denied).
|
|
291
|
+
this.emit.apply(this, requestArgs).catch((error) => {
|
|
292
|
+
this.removeListener(requestId, onResponse);
|
|
293
|
+
reject(error);
|
|
196
294
|
});
|
|
197
|
-
// Emit the message to the addon-code.
|
|
198
|
-
this.emit.apply(this, requestArgs);
|
|
199
295
|
});
|
|
200
296
|
}
|
|
201
297
|
|