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

Fixed server crash bug when tokens not found in db #230

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from

Conversation

andram11
Copy link
Contributor

@andram11 andram11 commented Dec 7, 2024

Description

Fix for bug: cannot read properties of undefined when user is logged in but database re-seeded, and user is still logged in (still has access and refresh tokens) and tries to access non public endpoints.

Issue link

Fixes bug

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Feature updates / changes
  • Tests
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Before fix, I log in with a user and have an access & refresh token.
If I shut down server and reseed the database, then I try to access a non public endpoint (like get forms by id) using the access & refresh token, I received an http 500 Internal Server Error. In logs there is a 'cannot read properties of undefined'(reading 'map')...After some time, when the token expires, I get the usual 403 error. This error occurs because when treating the JWT token, we were retrieving the roles of a user even though the user id no longer existed in the db (after reseed). As such, the roles were undefined which caused issues when calculating the abilities of user.
After fix, If i reseed and use old access & refresh token I just get a 403.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have updated the change log

@andram11 andram11 marked this pull request as ready for review December 7, 2024 17:59
… db check during access token validation, and added additional test for refactoring
@cherylli cherylli self-requested a review December 10, 2024 01:05
Copy link
Contributor

@cherylli cherylli left a comment

Choose a reason for hiding this comment

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

Tested manually and ran all the tests, all good.
I'm glad you figured out how to reproduce the error I've completed forgotten to put that in and I've also forgotten how to trigger the error to test it 😂

Just need a small change on the changelog, since this didn't get merged before the release.

CHANGELOG.md Outdated Show resolved Hide resolved
JoshuaHinman
JoshuaHinman previously approved these changes Dec 10, 2024
Copy link
Contributor

@JoshuaHinman JoshuaHinman left a comment

Choose a reason for hiding this comment

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

All tests passed and swagger tests worked as described - no 500s

cherylli
cherylli previously approved these changes Dec 14, 2024
Copy link
Contributor

@cherylli cherylli left a comment

Choose a reason for hiding this comment

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

thanks!

JoshuaHinman
JoshuaHinman previously approved these changes Dec 19, 2024
Copy link
Contributor

@JoshuaHinman JoshuaHinman left a comment

Choose a reason for hiding this comment

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

All good!

@cherylli
Copy link
Contributor

@andram11 could you please resolve merge conflicts when you have a chance? Thanks!

@andram11 andram11 dismissed stale reviews from JoshuaHinman and cherylli via ef500ac December 20, 2024 20:08
cherylli
cherylli previously approved these changes Dec 22, 2024
@cherylli
Copy link
Contributor

cherylli commented Dec 30, 2024

hey @andram11, I noticed from the last couple of PR the changelog entries were in the wrong place, when you resolve conflict could you please update it like below? Thanks

https://github.com/chingu-x/chingu-dashboard-be/pull/235/files#diff-06572a96a58dc510037d5efa622f9bec8519bc1beab13c9f251e97e657a9d4ed

JoshuaHinman
JoshuaHinman previously approved these changes Jan 4, 2025
Copy link
Contributor

@JoshuaHinman JoshuaHinman left a comment

Choose a reason for hiding this comment

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

Changes look good

cherylli
cherylli previously approved these changes Jan 7, 2025
Copy link
Contributor

@cherylli cherylli left a comment

Choose a reason for hiding this comment

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

re-approve after fixing changelog

@andram11 andram11 dismissed stale reviews from cherylli and JoshuaHinman via 2590a13 January 7, 2025 09:07
CHANGELOG.md Outdated Show resolved Hide resolved
@cherylli cherylli self-requested a review January 30, 2025 10:23
cherylli
cherylli previously approved these changes Jan 30, 2025
cherylli
cherylli previously approved these changes Feb 1, 2025
Copy link
Contributor

@cherylli cherylli left a comment

Choose a reason for hiding this comment

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

re-approve again after resolving conflicts

@cherylli
Copy link
Contributor

remove all changes to the changelog, since we've stopped manually updating changing

@@ -34,20 +34,24 @@ export class AtStrategy extends PassportStrategy(Strategy, "jwt-at") {
}

async validate(payload: any) {
const userInDb = await this.usersService.getUserRolesById(payload.sub);
//Get user roles

Choose a reason for hiding this comment

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

Would it be helpful to annotate the return type of this validate method? It looks like it would be Promise<UserReq | undefined>

Copy link
Contributor

@jenny-alexander jenny-alexander left a comment

Choose a reason for hiding this comment

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

I was able to replicate issue and get a 500 response.
After checking out these changes, I ran 'npm run seed' and accessed GET endpoint /api/v1/teams, received 401 error.

@cherylli - I get 401 (unauthorized) but the PR description explicitly states getting 403 (forbidden). I approved the PR anyway since this might be a typo in the PR text itself?

@cherylli
Copy link
Contributor

I was able to replicate issue and get a 500 response. After checking out these changes, I ran 'npm run seed' and accessed GET endpoint /api/v1/teams, received 401 error.

@cherylli - I get 401 (unauthorized) but the PR description explicitly states getting 403 (forbidden). I approved the PR anyway since this might be a typo in the PR text itself?

yeah 401 makes more sense since the user is not logged in. I think I got 401 too, then the user will be required to relogin in this case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants