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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

munch-lax
Copy link

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

github-actions bot commented Dec 12, 2024

Welcome!

Hello there, congrats on your first PR! We're excited to have you contributing to this project.
By submitting your Pull Request, you acknowledge that you agree with the terms of our Contributor License Agreement.

Generated by 🚫 dangerJS against de0c95b

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
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

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.

2 participants