Skip to content

Commit 60de6ce

Browse files
authored
Fix WebAuthn error checking (#36219)
Fixes: #36216 Now `detectWebAuthnSupport` returns the error type and lets the caller decide whether they call `webAuthnError` and show the error. It no longer shows the error during page load when the user has not even interacted with the feature. The bug affects all users on HTTP, so I think a quick fix release for this might be good.
1 parent 5151e30 commit 60de6ce

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

web_src/js/features/user-auth-webauthn.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import {GET, POST} from '../modules/fetch.ts';
44

55
const {appSubUrl} = window.config;
66

7+
/** One of the possible values for the `data-webauthn-error-msg` attribute on the webauthn error message element */
8+
type ErrorType = 'general' | 'insecure' | 'browser' | 'unable-to-process' | 'duplicated' | 'unknown';
9+
710
export async function initUserAuthWebAuthn() {
811
const elPrompt = document.querySelector('.user.signin.webauthn-prompt');
912
const elSignInPasskeyBtn = document.querySelector('.signin-passkey');
1013
if (!elPrompt && !elSignInPasskeyBtn) {
1114
return;
1215
}
1316

14-
if (!detectWebAuthnSupport()) {
17+
const errorType = detectWebAuthnSupport();
18+
if (errorType) {
1519
if (elSignInPasskeyBtn) hideElem(elSignInPasskeyBtn);
1620
return;
1721
}
@@ -177,7 +181,7 @@ async function webauthnRegistered(newCredential: any) { // TODO: Credential type
177181
window.location.reload();
178182
}
179183

180-
function webAuthnError(errorType: string, message:string = '') {
184+
function webAuthnError(errorType: ErrorType, message:string = '') {
181185
const elErrorMsg = document.querySelector(`#webauthn-error-msg`)!;
182186

183187
if (errorType === 'general') {
@@ -194,25 +198,26 @@ function webAuthnError(errorType: string, message:string = '') {
194198
showElem('#webauthn-error');
195199
}
196200

197-
function detectWebAuthnSupport() {
201+
/** Returns the error type or `null` when there was no error. */
202+
function detectWebAuthnSupport(): ErrorType | null {
198203
if (!window.isSecureContext) {
199-
webAuthnError('insecure');
200-
return false;
204+
return 'insecure';
201205
}
202206

203207
if (typeof window.PublicKeyCredential !== 'function') {
204-
webAuthnError('browser');
205-
return false;
208+
return 'browser';
206209
}
207210

208-
return true;
211+
return null;
209212
}
210213

211214
export function initUserAuthWebAuthnRegister() {
212215
const elRegister = document.querySelector<HTMLInputElement>('#register-webauthn');
213216
if (!elRegister) return;
214217

215-
if (!detectWebAuthnSupport()) {
218+
const errorType = detectWebAuthnSupport();
219+
if (errorType) {
220+
webAuthnError(errorType);
216221
elRegister.disabled = true;
217222
return;
218223
}

0 commit comments

Comments
 (0)