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 : #8825 If attachment token expires, it throws a 500 error instead of Unauthenticated #9043

Merged
merged 10 commits into from
Jan 8, 2025

Conversation

munch-lax
Copy link
Contributor

@munch-lax munch-lax commented Dec 12, 2024

Fixes #8825

FilePathGuard implements token verification via verifyWorkspaceToken function which throws AuthException error ,
since CanActivate expects a boolean value , we add a try catch while verifying the token

if token is invalid/expired
Screenshot 2024-12-12 at 9 44 58 PM
else
Screenshot 2024-12-12 at 9 47 10 PM

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR adds error handling in FilePathGuard to properly handle expired or invalid attachment tokens, preventing 500 errors by returning appropriate authentication responses.

  • Added try-catch block in packages/twenty-server/src/engine/core-modules/file/guards/file-path-guard.ts to handle AuthException during token verification
  • Returns false instead of throwing 500 error when token is unauthenticated
  • Throws AuthException with INTERNAL_SERVER_ERROR code for non-authentication token verification failures
  • Maintains workspaceId validation and expiration date checks after successful verification

💡 (1/5) You can manually trigger the bot by mentioning @greptileai in a comment!

1 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings | Greptile

if (!query || !query['token']) {
return false;
}

const payload = await this.jwtWrapperService.verifyWorkspaceToken(
let payload:any ={}
Copy link
Contributor

Choose a reason for hiding this comment

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

syntax: Missing space after colon in type declaration. Should be 'payload: any = {}'

Copy link
Member

Choose a reason for hiding this comment

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

Please check run your linter: npx nx run twenty-server:lint

Comment on lines 30 to 31
if(error instanceof AuthException && error.code === AuthExceptionCode.UNAUTHENTICATED ){
return false
Copy link
Contributor

Choose a reason for hiding this comment

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

syntax: Missing semicolons at line endings

if(error instanceof AuthException && error.code === AuthExceptionCode.UNAUTHENTICATED ){
return false
}
else{
Copy link
Member

Choose a reason for hiding this comment

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

no need for else as you are already returning

if (error instance of...) {
return
}

throw ...

Copy link
Member

Choose a reason for hiding this comment

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

actually forget about this comment as I think we should have another strategy :)

Copy link
Member

@charlesBochet charlesBochet left a comment

Choose a reason for hiding this comment

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

Hi @munch-lax thanks for the PR. We would like to follow another strategy here:

  1. Create a FileAPIExceptionFilter (similar to AuthRestApiExceptionFilter) this filter should be able to handle the AuthException (with AuthExceptionCode.UNAUTHENTICATED)
  2. use it on top of the file.controller.ts (see GoogleAPIsAuthController as an example)

Now the exception that is thrown in the file-path-guard will be properly handled.

@munch-lax
Copy link
Contributor Author

Hey @charlesBochet do we need to return the error in the same format that is by using handleExceptionService.handleError ?

@charlesBochet
Copy link
Member

It should be very similar to AuthRestApiExceptionFilter :) so same output format

@FelixMalfait
Copy link
Member

FelixMalfait commented Jan 4, 2025

Thanks! Let's keep unauthenticated as a 403 (as you did) for now to stay consistent with the rest of the codebase. But I created a followup issue here #9347

@FelixMalfait
Copy link
Member

There's another issue with our current code which I'll fix (not linked to your PR). We've implemented a second notion of "expirationDate" just for the file service. The expiry is something JWT handle natively well and we should rely on the JWT expiry instead of introducing a second expirationDate. With the current code the file expiration date would be the lowest of the default token expiration date and the file token expiration date which doesn't make sense.

Copy link
Member

@FelixMalfait FelixMalfait left a comment

Choose a reason for hiding this comment

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

Good for me! But I'll let you check again before @charlesBochet since it can affect security

@charlesBochet
Copy link
Member

I'm back from holidays, I'm taking a look

@charlesBochet
Copy link
Member

Thank you @munch-lax :) I've moved your code to a different location (file.exception.ts instead of auth.exception.ts and same for the filter)

@charlesBochet charlesBochet merged commit d324cac into twentyhq:main Jan 8, 2025
16 checks passed
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.

If attachment token expires, it throws a 500 error instead of Unauthenticated
4 participants