Skip to content

Fix WebUI Admin Action infinite retry with no MFA devices#51134

Merged
Joerger merged 7 commits intomasterfrom
joerger/fix-webui-admin-action-retry
Jan 25, 2025
Merged

Fix WebUI Admin Action infinite retry with no MFA devices#51134
Joerger merged 7 commits intomasterfrom
joerger/fix-webui-admin-action-retry

Conversation

@Joerger
Copy link
Copy Markdown
Contributor

@Joerger Joerger commented Jan 16, 2025

Changelog: Fixed a bug where performing an admin action in the WebUI would hang indefinitely instead of getting an actionable error if the user has no MFA devices registered.

#49679 and subsequently #50570 introduced a change where getMfaChallengeResponse could return null | undefined when the user had no MFA challenges (no devices or not required). fetchJsonWithMfaAuthnRetry expects getMfaChallengeResponse to return {} in these cases, and will retry with MFA until it gets either an empty or non-empty object.

This PR fixes the issue by:

  1. addressing the recursive fetchJsonWithMfaAuthnRetry which could result in an infinite loop. It's no longer recursive so it will only retry once even if null or undefined is received.
  2. Reverting part of https://github.com/gravitational/teleport/pull/50570/files and instead making getMfaChallengeResponse return {} so we can properly determine at any point whether an mfa response is undefined or an empty response resulting from a no-op challenge attempt (no devices or not required).

Closes #51105

image

@Joerger Joerger requested a review from ravicious January 16, 2025 19:21
@github-actions github-actions Bot requested review from rudream and ryanclark January 16, 2025 19:21
@Joerger Joerger force-pushed the joerger/fix-webui-admin-action-retry branch from 21430bc to 61b4600 Compare January 16, 2025 19:36
@Joerger Joerger added backport/branch/v17 no-changelog Indicates that a PR does not require a changelog entry labels Jan 16, 2025
Copy link
Copy Markdown
Member

@ravicious ravicious left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but there are still a few failing tests, even after fixing those in api.test.ts.

Comment thread web/packages/teleport/src/services/auth/auth.ts Outdated
Comment thread web/packages/teleport/src/services/auth/auth.ts Outdated
Comment thread web/packages/teleport/src/services/api/api.ts
Comment thread web/packages/teleport/src/services/api/api.ts Outdated
Comment thread web/packages/teleport/src/services/api/api.ts
@Joerger Joerger force-pushed the joerger/fix-webui-admin-action-retry branch from 61b4600 to cbc4c2a Compare January 21, 2025 18:59
@Joerger Joerger requested a review from ravicious January 21, 2025 19:31
Comment thread web/packages/teleport/src/services/api/api.ts
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving a preemptive approval, but how do I even test it, bar what Xin described in the issue? If I set second factor to optional, I cannot even log in as a user with no MFA. The login form shows "t is undefined" error and I'm forced to select a multi-factor type anyway.

I managed to work around this by choosing an authenticator app as the MFA type and then providing a bogus code. But how is it supposed to work for normal users? 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to make an SSO user, which wouldn't have an MFA device until you add one

Comment on lines +146 to +151
return await api.fetch(url, customOptions, mfaResponse);
} catch (err) {
// error reading JSON
const message = response.ok
? err.message
: `${response.status} - ${response.url}`;
throw new ApiError({ message, response, opts: { cause: err } });
}

if (response.ok) {
return json;
}

/** This error can occur in the edge case where a role in the user's certificate was deleted during their session. */
const isRoleNotFoundErr = isRoleNotFoundError(parseError(json));
if (isRoleNotFoundErr) {
websession.logoutWithoutSlo({
/* Don't remember location after login, since they may no longer have access to the page they were on. */
rememberLocation: false,
/* Show "access changed" notice on login page. */
withAccessChangedMessage: true,
});
return;
// Retry with MFA if we get an admin action MFA error.
if (!mfaResponse && isAdminActionRequiresMfaError(err)) {
mfaResponse = await api.getAdminActionMfaResponse();
return await api.fetch(url, customOptions, mfaResponse);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the awaits here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's not a big deal, but if you have a promise, you can return it from an async function without awaiting.

I'm not sure if there's a lint rule which would automatically take care of this. await-thenable and no-misused-promises seem to be about different cases entirely. typescript-eslint/typescript-eslint#8517

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I wasn't sure how the try/catch would work without the first await

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @Joerger, you're absolutely correct

image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, reverted that one.

@Joerger Joerger requested a review from ryanclark January 22, 2025 19:55
@Joerger Joerger added this pull request to the merge queue Jan 25, 2025
Merged via the queue into master with commit 6b6b0cd Jan 25, 2025
@Joerger Joerger deleted the joerger/fix-webui-admin-action-retry branch January 25, 2025 02:50
@public-teleport-github-review-bot
Copy link
Copy Markdown

@Joerger See the table below for backport results.

Branch Result
branch/v17 Failed

Joerger added a commit that referenced this pull request Jan 27, 2025
* Fix admin action retry method resulting in an infinite loop.

* Return an empty challenge and challenge response instead of undefined.

* Add api.getAdminActionMfaResponse and add new custom error message.

* Resolve comments.

* Don't swallow non-admin-action-required errors.

* Remove unnecessary awaits.

* Fix try/catch.
github-merge-queue Bot pushed a commit that referenced this pull request Jan 30, 2025
…51513)

* Fix admin action retry method resulting in an infinite loop.

* Return an empty challenge and challenge response instead of undefined.

* Add api.getAdminActionMfaResponse and add new custom error message.

* Resolve comments.

* Don't swallow non-admin-action-required errors.

* Remove unnecessary awaits.

* Fix try/catch.
carloscastrojumo pushed a commit to carloscastrojumo/teleport that referenced this pull request Feb 19, 2025
…nal#51134)

* Fix admin action retry method resulting in an infinite loop.

* Return an empty challenge and challenge response instead of undefined.

* Add api.getAdminActionMfaResponse and add new custom error message.

* Resolve comments.

* Don't swallow non-admin-action-required errors.

* Remove unnecessary awaits.

* Fix try/catch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/branch/v17 no-changelog Indicates that a PR does not require a changelog entry size/sm ui

Projects

None yet

Development

Successfully merging this pull request may close these issues.

User creation hangs in the web UI with no error message if no MFA method is added

3 participants