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: use session if exists when doing initial redirect for user #2810

Merged
merged 5 commits into from
Apr 29, 2024

Conversation

ajay-sentry
Copy link
Contributor

@ajay-sentry ajay-sentry commented Apr 25, 2024

Description

Previously, when a user would come to app.codecov.io or similar, we would route them to the login page more often than not. This was due to us having "naive" redirect logic that was dependent on the existence of a provider to fetch the current user, using the useCurrentUser hook.

More often than not, this would not be the case, and a user would frustratingly end up on the login page even if we already had a session cookie set for them!

My solution then, was to use the useInternalUser() hook (thanks @JerrySentry for the pointer), which will pull the user's session if it exists first and use that to redirect the user. If we have a more specific route, or are able to derive the user's info via the provider and useCurrentUser hook, then we still do that.

There are also a couple other small fixes I baked in here:

  • Converted to TS
  • The test case that compared "search" values wasn't actually testing for that (Fixed now)
  • We use the HomePageRedirect component instead of '/login' for the '/' path
  • We have validation around the provider param being used now (similar to the fix in sec: 390 - Add validation for potential XSS vuln #2797)

Below you can see the behavior before and after. In the before video, we see the sessionId cookie exists, but when we navigate to app.codecov.io we are directed back to the login page. Only when adding 'gh' since I am logged in using my github account am I redirected properly. In the after video there is so such issue :)

Screenshots

BEFORE

Screen.Recording.2024-04-25.at.3.35.08.PM.mov

AFTER

Screen.Recording.2024-04-25.at.3.34.14.PM.mov

Link to Sample Entry

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. In 2022 this entity acquired Codecov and as result Sentry is going to need some rights from me in order to utilize my contributions in this PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

src/App.tsx Outdated
let redirectURL = '/login'

if (internalUser) {
redirectURL = `/${internalUser.owners![0]?.service}/${
Copy link
Contributor Author

Choose a reason for hiding this comment

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

if internalUser exists, my assumption here is owners will at least be an empty array

Copy link
Contributor

Choose a reason for hiding this comment

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

I can't find any reason why that assumption would be bad. but just in case you didn't know how to do optional chaining with array indexing (like I didn't until recently):

Suggested change
redirectURL = `/${internalUser.owners![0]?.service}/${
redirectURL = `/${internalUser.owners?.[0]?.service}/${

I think that would be a safer play. Generally try to steer clear of ! myself - it's scary 😰

@codecov-staging
Copy link

codecov-staging bot commented Apr 25, 2024

Bundle Report

Changes will increase total bundle size by 593 bytes ⬆️

Bundle name Size Change
gazebo-staging-array-push 6.48MB 593 bytes ⬆️

Copy link

codecov bot commented Apr 25, 2024

Bundle Report

Changes will increase total bundle size by 593 bytes ⬆️

Bundle name Size Change
gazebo-production-array-push 6.48MB 593 bytes ⬆️

@codecov-releaser
Copy link
Contributor

codecov-releaser commented Apr 25, 2024

✅ Deploy preview for gazebo ready!

Previews expire after 1 month automatically.

Commit Created Cloud Enterprise
90bf65b Thu, 25 Apr 2024 22:24:00 GMT Expired Expired
288b6f3 Thu, 25 Apr 2024 23:13:58 GMT Expired Expired
9ce17e2 Thu, 25 Apr 2024 23:28:49 GMT Expired Expired
9ce17e2 Thu, 25 Apr 2024 23:29:03 GMT Expired Expired
8cafe90 Mon, 29 Apr 2024 17:03:28 GMT Cloud Enterprise

@codecov-qa
Copy link

codecov-qa bot commented Apr 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.46%. Comparing base (a662e79) to head (8cafe90).

✅ All tests successful. No failed tests found ☺️

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2810   +/-   ##
=======================================
  Coverage   98.46%   98.46%           
=======================================
  Files         871      871           
  Lines       12672    12676    +4     
  Branches     3389     3373   -16     
=======================================
+ Hits        12478    12482    +4     
  Misses        190      190           
  Partials        4        4           
Files Coverage Δ
src/App.tsx 100.00% <100.00%> (ø)
src/services/user/useInternalUser.ts 100.00% <100.00%> (ø)
Components Coverage Δ
Assets 55.55% <ø> (ø)
Layouts 97.25% <ø> (ø)
Pages 99.26% <ø> (ø)
Services 99.56% <100.00%> (+<0.01%) ⬆️
Shared 99.84% <ø> (ø)
UI 94.39% <ø> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a662e79...8cafe90. Read the comment docs.

Copy link

codecov-public-qa bot commented Apr 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.46%. Comparing base (a662e79) to head (8cafe90).

✅ All tests successful. No failed tests found ☺️

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2810   +/-   ##
=======================================
  Coverage   98.46%   98.46%           
=======================================
  Files         871      871           
  Lines       12672    12676    +4     
  Branches     3372     3373    +1     
=======================================
+ Hits        12478    12482    +4     
  Misses        190      190           
  Partials        4        4           
Files Coverage Δ
src/App.tsx 100.00% <100.00%> (ø)
src/services/user/useInternalUser.ts 100.00% <100.00%> (ø)
Components Coverage Δ
Assets 55.55% <ø> (ø)
Layouts 97.25% <ø> (ø)
Pages 99.26% <ø> (ø)
Services 99.56% <100.00%> (+<0.01%) ⬆️
Shared 99.84% <ø> (ø)
UI 94.39% <ø> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a662e79...8cafe90. Read the comment docs.

Copy link

codecov bot commented Apr 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.47%. Comparing base (a662e79) to head (8cafe90).

✅ All tests successful. No failed tests found ☺️

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #2810   +/-   ##
=====================================
  Coverage   98.47   98.47           
=====================================
  Files        871     871           
  Lines      12672   12676    +4     
  Branches    3389    3332   -57     
=====================================
+ Hits       12478   12482    +4     
  Misses       190     190           
  Partials       4       4           
Files Coverage Δ
src/App.tsx 100.00% <100.00%> (ø)
src/services/user/useInternalUser.ts 100.00% <100.00%> (ø)
Components Coverage Δ
Assets 55.55% <ø> (ø)
Layouts 97.25% <ø> (ø)
Pages 99.26% <ø> (ø)
Services 99.56% <100.00%> (+<0.01%) ⬆️
Shared 99.84% <ø> (ø)
UI 94.39% <ø> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a662e79...8cafe90. Read the comment docs.

@codecov-notifications
Copy link

codecov-notifications bot commented Apr 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

✅ All tests successful. No failed tests found ☺️

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2810   +/-   ##
=======================================
  Coverage        ?   98.45%           
=======================================
  Files           ?      870           
  Lines           ?    12636           
  Branches        ?     3380           
=======================================
  Hits            ?    12441           
  Misses          ?      191           
  Partials        ?        4           
Files Coverage Δ
src/App.tsx 100.00% <100.00%> (ø)
src/services/user/useInternalUser.ts 100.00% <100.00%> (ø)
Components Coverage Δ
Assets 55.55% <ø> (?)
Layouts 97.25% <ø> (?)
Pages 99.28% <ø> (?)
Services 99.56% <100.00%> (?)
Shared 99.84% <ø> (?)
UI 94.15% <ø> (?)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a662e79...8cafe90. Read the comment docs.

src/App.tsx Outdated

if (internalUser) {
redirectURL = `/${internalUser.owners![0]?.service}/${
internalUser.owners![0]?.username
Copy link
Contributor

Choose a reason for hiding this comment

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

Same suggestion here

Suggested change
internalUser.owners![0]?.username
internalUser.owners?.[0]?.username

Copy link
Contributor

Choose a reason for hiding this comment

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

You could also toss a lodash isArray in the if condition, if that's more your style

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, my IDE didn't like that one unfortunately 😭

What do you think about just changing the if statement to

 if (internalUser && internalUser.owners) {
    redirectURL = `/${internalUser.owners[0]?.service}/${internalUser.owners[0]?.username}`
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Think its a litttttttle cleaner that way?

Copy link
Contributor

Choose a reason for hiding this comment

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

oh yea that's great too!

Copy link
Contributor

@spalmurray-codecov spalmurray-codecov left a comment

Choose a reason for hiding this comment

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

Ran into this when testing locally:

Recording 2024-04-25 at 18 43 21

@ajay-sentry ajay-sentry merged commit 782e9b4 into main Apr 29, 2024
51 checks passed
@ajay-sentry ajay-sentry deleted the Ajay/1219-pricing-page-redirect branch April 29, 2024 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants