From 21430bcd069f48ece279f1cb83aeeb252e201717 Mon Sep 17 00:00:00 2001 From: joerger Date: Thu, 16 Jan 2025 11:09:28 -0800 Subject: [PATCH] Return an empty challenge and challenge response instead of undefined. --- .../ChangePasswordWizard/ChangePasswordWizard.tsx | 2 +- web/packages/teleport/src/lib/term/tty.ts | 6 +++--- web/packages/teleport/src/services/auth/auth.ts | 13 +++++++------ web/packages/teleport/src/services/mfa/makeMfa.ts | 10 +--------- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/web/packages/teleport/src/Account/ChangePasswordWizard/ChangePasswordWizard.tsx b/web/packages/teleport/src/Account/ChangePasswordWizard/ChangePasswordWizard.tsx index 738b88890b2dd..ce1bfdfb4b5f9 100644 --- a/web/packages/teleport/src/Account/ChangePasswordWizard/ChangePasswordWizard.tsx +++ b/web/packages/teleport/src/Account/ChangePasswordWizard/ChangePasswordWizard.tsx @@ -64,7 +64,7 @@ export function ChangePasswordWizard({ const reauthState = useReAuthenticate({ challengeScope: MfaChallengeScope.CHANGE_PASSWORD, onMfaResponse: async mfaResponse => - setWebauthnResponse(mfaResponse?.webauthn_response), + setWebauthnResponse(mfaResponse.webauthn_response), }); const [reauthMethod, setReauthMethod] = useState(); diff --git a/web/packages/teleport/src/lib/term/tty.ts b/web/packages/teleport/src/lib/term/tty.ts index de5f79a4da624..55aa52823411e 100644 --- a/web/packages/teleport/src/lib/term/tty.ts +++ b/web/packages/teleport/src/lib/term/tty.ts @@ -80,7 +80,7 @@ class Tty extends EventEmitterMfaSender { this.socket.send(bytearray.buffer); } - sendChallengeResponse(resp: MfaChallengeResponse) { + sendChallengeResponse(data: MfaChallengeResponse) { // we want to have the backend listen on a single message type // for any responses. so our data will look like data.webauthn, data.sso, etc // but to be backward compatible, we need to still spread the existing webauthn only fields @@ -88,8 +88,8 @@ class Tty extends EventEmitterMfaSender { // in 19, we can just pass "data" without this extra step // TODO (avatus): DELETE IN 19.0.0 const backwardCompatibleData = { - ...resp?.webauthn_response, - ...resp, + ...data.webauthn_response, + ...data, }; const encoded = this._proto.encodeChallengeResponse( JSON.stringify(backwardCompatibleData) diff --git a/web/packages/teleport/src/services/auth/auth.ts b/web/packages/teleport/src/services/auth/auth.ts index 100259d6dfc20..a846e34a623d0 100644 --- a/web/packages/teleport/src/services/auth/auth.ts +++ b/web/packages/teleport/src/services/auth/auth.ts @@ -238,7 +238,7 @@ const auth = { .then(res => { const request = { action: 'accept', - webauthnAssertionResponse: res?.webauthn_response, + webauthnAssertionResponse: res.webauthn_response, }; return api.put(cfg.getHeadlessSsoPath(transactionId), request); @@ -274,13 +274,14 @@ const auth = { }, // getChallengeResponse gets an MFA challenge response for the provided parameters. - // If is_mfa_required_req is provided and it is found that MFA is not required, returns null instead. + // If challenge is undefined or has no viable challenge options, returns empty. async getMfaChallengeResponse( challenge: MfaAuthenticateChallenge, mfaType?: DeviceType, totpCode?: string - ): Promise { - if (!challenge) return; + ): Promise { + // No challenge, return empty response. + if (!challenge) return {}; // TODO(Joerger): If mfaType is not provided by a parent component, use some global context // to display a component, similar to the one used in useMfa. For now we just default to @@ -310,7 +311,7 @@ const auth = { } // No viable challenge, return empty response. - return; + return {}; }, async getWebAuthnChallengeResponse( @@ -439,7 +440,7 @@ const auth = { return auth .getMfaChallenge({ scope, allowReuse, isMfaRequiredRequest }, abortSignal) .then(challenge => auth.getMfaChallengeResponse(challenge, 'webauthn')) - .then(res => res?.webauthn_response); + .then(res => res.webauthn_response); }, getMfaChallengeResponseForAdminAction(allowReuse?: boolean) { diff --git a/web/packages/teleport/src/services/mfa/makeMfa.ts b/web/packages/teleport/src/services/mfa/makeMfa.ts index 4d98503dafa87..0ec7c28f88071 100644 --- a/web/packages/teleport/src/services/mfa/makeMfa.ts +++ b/web/packages/teleport/src/services/mfa/makeMfa.ts @@ -63,15 +63,7 @@ export function parseMfaRegistrationChallengeJson( // parseMfaChallengeJson formats fetched authenticate challenge JSON. export function parseMfaChallengeJson( challenge: MfaAuthenticateChallengeJson -): MfaAuthenticateChallenge | undefined { - if ( - !challenge.sso_challenge && - !challenge.webauthn_challenge && - !challenge.totp_challenge - ) { - return; - } - +): MfaAuthenticateChallenge { // WebAuthn challenge contains Base64URL(byte) fields that needs to // be converted to ArrayBuffer expected by navigator.credentials.get: // - challenge