Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject passkeys scripts at document_start #2432

Merged
merged 2 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions dist/manifest_chromium.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"background": {
"service_worker": "background/background_service.js"
},
"content_scripts": [
"content_scripts": [
{
"matches": [
"<all_urls>"
Expand All @@ -45,7 +45,6 @@
"js": [
"common/browser-polyfill.min.js",
"common/global.js",
"common/global_ui.js",
"common/sites.js",
"content/ui.js",
"content/banner.js",
Expand All @@ -61,11 +60,25 @@
"content/pwgen.js",
"content/totp-autocomplete.js",
"content/totp-field.js",
"content/username-field.js",
"content/passkeys-utils.js"
"content/username-field.js"
],
"run_at": "document_idle",
"all_frames": true
},
{
"matches": [
"<all_urls>"
],
"exclude_matches": [
"*://*/*.xml*",
"file:///*.xml*"
],
"js": [
"content/passkeys-inject.js",
"content/passkeys-utils.js"
],
"run_at": "document_start",
"all_frames": true
}
],
"commands": {
Expand Down
18 changes: 16 additions & 2 deletions dist/manifest_firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,25 @@
"content/pwgen.js",
"content/totp-autocomplete.js",
"content/totp-field.js",
"content/username-field.js",
"content/passkeys-utils.js"
"content/username-field.js"
],
"run_at": "document_idle",
"all_frames": true
},
{
"matches": [
"<all_urls>"
],
"exclude_matches": [
"*://*/*.xml*",
"file:///*.xml*"
],
"js": [
"content/passkeys-inject.js",
"content/passkeys-utils.js"
],
"run_at": "document_start",
"all_frames": true
}
],
"commands": {
Expand Down
85 changes: 0 additions & 85 deletions keepassxc-browser/content/keepassxc-browser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
'use strict';

const PASSKEYS_NO_LOGINS_FOUND = 15;
const PASSKEYS_CREDENTIAL_IS_EXCLUDED = 21;
const PASSKEYS_WAIT_FOR_LIFETIMER = 30;

// Contains already called method names
const _called = {};
_called.automaticRedetectCompleted = false;
Expand Down Expand Up @@ -863,83 +859,6 @@ kpxc.usePredefinedSites = function(currentLocation) {
}
};

// Apply a script to the page for intercepting Passkeys (WebAuthn) requests
kpxc.enablePasskeys = function() {
if (document?.documentElement?.ownerDocument?.contentType !== 'text/html') {
return;
}

const passkeys = document.createElement('script');
passkeys.src = browser.runtime.getURL('content/passkeys.js');
document.documentElement.appendChild(passkeys);

const startTimer = function(timeout) {
return setTimeout(() => {
throw new DOMException('lifetimeTimer has expired', 'NotAllowedError');
}, timeout);
};

const stopTimer = function(lifetimeTimer) {
if (lifetimeTimer) {
clearTimeout(lifetimeTimer);
}
};

const letTimerRunOut = function (errorCode) {
return (
errorCode === PASSKEYS_WAIT_FOR_LIFETIMER ||
errorCode === PASSKEYS_CREDENTIAL_IS_EXCLUDED ||
errorCode === PASSKEYS_NO_LOGINS_FOUND
);
};

const sendResponse = async function(command, publicKey, callback) {
const lifetimeTimer = startTimer(publicKey?.timeout);

const ret = await sendMessage(command, [ publicKey, window.location.origin ]);
if (ret) {
let errorMessage;
if (ret.response && ret.response.errorCode) {
errorMessage = await sendMessage('get_error_message', ret.response.errorCode);
kpxcUI.createNotification('error', errorMessage);

if (kpxc.settings.passkeysFallback) {
kpxcPasskeysUtils.sendPasskeysResponse(undefined, ret.response?.errorCode, errorMessage);
} else if (letTimerRunOut(ret?.response?.errorCode)) {
return;
}
}

logDebug('Passkey response', ret.response);
kpxcPasskeysUtils.sendPasskeysResponse(ret.response, ret.response?.errorCode, errorMessage);
stopTimer(lifetimeTimer);
}
};

document.addEventListener('kpxc-passkeys-request', async (ev) => {
if (!window.isSecureContext) {
kpxcUI.createNotification('error', tr('errorMessagePasskeysContextIsNotSecure'));
return;
}

if (ev.detail.action === 'passkeys_create') {
const publicKey = kpxcPasskeysUtils.buildCredentialCreationOptions(
ev.detail.publicKey,
ev.detail.sameOriginWithAncestors,
);
logDebug('Passkey request', publicKey);
await sendResponse('passkeys_register', publicKey);
} else if (ev.detail.action === 'passkeys_get') {
const publicKey = kpxcPasskeysUtils.buildCredentialRequestOptions(
ev.detail.publicKey,
ev.detail.sameOriginWithAncestors,
);
logDebug('Passkey request', publicKey);
await sendResponse('passkeys_get', publicKey);
}
});
};

/**
* Content script initialization.
*/
Expand All @@ -964,10 +883,6 @@ const initContentScript = async function() {
return;
}

if (kpxc.settings.passkeys) {
kpxc.enablePasskeys();
}

await kpxc.updateDatabaseState();
await kpxc.initCredentialFields();

Expand Down
109 changes: 109 additions & 0 deletions keepassxc-browser/content/passkeys-inject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
'use strict';

const PASSKEYS_NO_LOGINS_FOUND = 15;
const PASSKEYS_CREDENTIAL_IS_EXCLUDED = 21;
const PASSKEYS_WAIT_FOR_LIFETIMER = 30;

// Apply a script to the page for intercepting Passkeys (WebAuthn) requests
const enablePasskeys = async function() {
const passkeysLogDebug = function(message, extra) {
if (kpxcPasskeysUtils.debugLogging) {
debugLogMessage(message, extra);
}
};

const passkeys = document.createElement('script');
passkeys.src = chrome.runtime.getURL('content/passkeys.js');
document.documentElement.appendChild(passkeys);

const startTimer = function(timeout) {
return setTimeout(() => {
throw new DOMException('lifetimeTimer has expired', 'NotAllowedError');
}, timeout);
};

const stopTimer = function(lifetimeTimer) {
if (lifetimeTimer) {
clearTimeout(lifetimeTimer);
}
};

const letTimerRunOut = function (errorCode) {
return (
errorCode === PASSKEYS_WAIT_FOR_LIFETIMER ||
errorCode === PASSKEYS_CREDENTIAL_IS_EXCLUDED ||
errorCode === PASSKEYS_NO_LOGINS_FOUND
);
};

const sendResponse = async function(command, publicKey, callback) {
const lifetimeTimer = startTimer(publicKey?.timeout);

const ret = await chrome.runtime.sendMessage({ action: command, args: [ publicKey, window.location.origin ] });
if (ret) {
let errorMessage;
if (ret.response && ret.response.errorCode) {
errorMessage = await chrome.runtime.sendMessage({
action: 'get_error_message',
args: ret.response.errorCode,
});
kpxcUI.createNotification('error', errorMessage);

if (kpxcPasskeysUtils.passkeysFallback) {
kpxcPasskeysUtils.sendPasskeysResponse(undefined, ret.response?.errorCode, errorMessage);
} else if (letTimerRunOut(ret?.response?.errorCode)) {
return;
}
}

passkeysLogDebug('Passkey response', ret.response);
kpxcPasskeysUtils.sendPasskeysResponse(ret.response, ret.response?.errorCode, errorMessage);
stopTimer(lifetimeTimer);
}
};

document.addEventListener('kpxc-passkeys-request', async (ev) => {
if (!window.isSecureContext) {
kpxcUI.createNotification('error', tr('errorMessagePasskeysContextIsNotSecure'));
return;
}

if (ev.detail.action === 'passkeys_create') {
const publicKey = kpxcPasskeysUtils.buildCredentialCreationOptions(
ev.detail.publicKey,
ev.detail.sameOriginWithAncestors,
);
passkeysLogDebug('Passkey request', publicKey);
await sendResponse('passkeys_register', publicKey);
} else if (ev.detail.action === 'passkeys_get') {
const publicKey = kpxcPasskeysUtils.buildCredentialRequestOptions(
ev.detail.publicKey,
ev.detail.sameOriginWithAncestors,
);
passkeysLogDebug('Passkey request', publicKey);
await sendResponse('passkeys_get', publicKey);
}
});
};

const initContent = async () => {
if (document?.documentElement?.ownerDocument?.contentType !== 'text/html'
&& document?.documentElement?.ownerDocument?.contentType !== 'application/xhtml+xml'
) {
return;
}

const settings = await chrome.runtime.sendMessage({ action: 'load_settings' });
if (!settings) {
console.log('Error: Cannot load extension settings');
return;
}

if (settings.passkeys) {
kpxcPasskeysUtils.debugLogging = settings?.debugLogging;
kpxcPasskeysUtils.passkeysFallback = settings?.passkeysFallback;
enablePasskeys();
}
};

initContent();
4 changes: 2 additions & 2 deletions keepassxc-browser/content/passkeys-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ const kpxcPasskeysUtils = {};
// Sends response from KeePassXC back to the injected script
kpxcPasskeysUtils.sendPasskeysResponse = function(publicKey, errorCode, errorMessage) {
const response = errorCode
? { errorCode: errorCode, errorMessage: errorMessage, fallback: kpxc.settings.passkeysFallback }
: { publicKey: publicKey, fallback: kpxc.settings.passkeysFallback };
? { errorCode: errorCode, errorMessage: errorMessage, fallback: kpxcPasskeysUtils?.passkeysFallback }
: { publicKey: publicKey, fallback: kpxcPasskeysUtils?.passkeysFallback };
const details = isFirefox() ? cloneInto(response, document.defaultView) : response;
document.dispatchEvent(new CustomEvent('kpxc-passkeys-response', { detail: details }));
};
Expand Down
Loading