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

fix: Check auth validity before setting isAuthenticated #8967

Merged
merged 2 commits into from
Mar 8, 2024

Conversation

julian-determined-ai
Copy link
Contributor

@julian-determined-ai julian-determined-ai commented Mar 6, 2024

Description

There is bug in GenAI currently where a user gets stuck in an infinite redirect loop, this fixes the issue.

You can see the bug in action below:
https://github.com/determined-ai/determined/assets/103522725/7375fff4-bdc6-4c78-a6f0-5717080cf21f

The ultimate cause of the bug is that if you look at the logic in the sign in page we:

  1. Check if the user `isAuthenticated"
  2. Check if there is a redirect, if there is we redirect the user.

However the issue is that in (1) the isAuthenticated can be set to true without the user actually being a valid and authenticated user. This causes an infinite login bug in GenAI since we use the MLDE login page for authentication also. The reason that this is not an issue in MLDE is because after sign-in:

  1. The user is redirected to the dashboard page
  2. As soon as an API call fails, we catch the auth error, and remove the bad token
  3. We instantly send the user back to the sign-in page.

However (2) cannot happen in GenAI since the invalid token lives and is controlled on the MLDE app.

In order to preserve all current log in and auth related behavior, this PR introduces an extra check to ensure the user is authenticated if an auth-token is present. If not, then we handle the error using out handleError function. This set up ensures that unauthenticated users will not be improperly redirected.

Test Plan

  1. Ensure that you are logged out of MLDE.
  2. Navigate to the login page.
  3. Using devtools set a value for the global/auth-token
  4. Add a ?redirect query to the login url such as login?redirect=/lore/
  5. Visit the URL in (4).
  6. Ensure that you are not redirected, and you instead immediately shown a "loading" message and then the login page again.

Commentary (optional)

Checklist

  • Changes have been manually QA'd
  • User-facing API changes need the "User-facing API Change" label.
  • Release notes should be added as a separate file under docs/release-notes/.
    See Release Note for details.
  • Licenses should be included for new code which was copied and/or modified from any external code.

Ticket

Copy link

netlify bot commented Mar 6, 2024

Deploy Preview for determined-ui ready!

Name Link
🔨 Latest commit a409040
🔍 Latest deploy log https://app.netlify.com/sites/determined-ui/deploys/65e8ee5fc4e216000869fc9f
😎 Deploy Preview https://deploy-preview-8967--determined-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codecov bot commented Mar 6, 2024

Codecov Report

Attention: Patch coverage is 16.66667% with 10 lines in your changes are missing coverage. Please review.

Project coverage is 42.60%. Comparing base (191a144) to head (a409040).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8967      +/-   ##
==========================================
- Coverage   47.37%   42.60%   -4.77%     
==========================================
  Files        1162      842     -320     
  Lines      176134   136791   -39343     
  Branches     2237     2236       -1     
==========================================
- Hits        83448    58286   -25162     
+ Misses      92528    78347   -14181     
  Partials      158      158              
Flag Coverage Δ
harness ?
web 42.54% <16.66%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
webui/react/src/hooks/useAuthCheck.ts 37.34% <16.66%> (-1.12%) ⬇️

... and 320 files with indirect coverage changes

@julian-determined-ai julian-determined-ai assigned hkang1 and unassigned gt2345 Mar 6, 2024
@julian-determined-ai julian-determined-ai changed the title feat: Check auth validity before setting isAuthenticated fix: Check auth validity before setting isAuthenticated Mar 6, 2024
Copy link
Contributor

@hkang1 hkang1 left a comment

Choose a reason for hiding this comment

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

Thanks for addressing this!

Had some questions on the changes.

authStore.setAuth({ isAuthenticated: true, token: authToken });
});
} catch (e) {
// If an invalid auth token is detected we need to properl handle the auth error
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: typo properly

Copy link
Contributor

Choose a reason for hiding this comment

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

question: if it fails, should the source of the token get cleared? Things like:

  • jwt token from searchParams
  • cookieToken not sure if anything can or needs to be done here
  • globalStorage.authToken clear this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Calling handleError ended up being the best approach here. I ran into unwanted beahvior/bugs otherwise. I am happy to delve into it though if need be.

The handleError method eventually calls paths.logout which properly entirely resets the application and removes the tokens.

Regarding removing the jwt from the search params. I am not sure that we want to remove that here, since we have logic that explicitly checks for it and I am not sure if removing it will cause unwanted side effects. But if we think it is okay to remove here then I am open to it.

webui/react/src/hooks/useAuthCheck.ts Outdated Show resolved Hide resolved
@hkang1 hkang1 removed their assignment Mar 6, 2024
@julian-determined-ai julian-determined-ai requested a review from a team March 7, 2024 18:41
Copy link
Contributor

@johnkim-det johnkim-det left a comment

Choose a reason for hiding this comment

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

LGTM

@julian-determined-ai julian-determined-ai merged commit 26c985c into main Mar 8, 2024
73 of 85 checks passed
@julian-determined-ai julian-determined-ai deleted the gas-424 branch March 8, 2024 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants