You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the Exception Filter (using exactly the one documented here: https://tsed.io/docs/exceptions.html#exception-filter), in the catch methode, when we try to use the ctx.request.route parameter, we get undefined in the special situation where we catch an unauthorized exception.
So it happens with a basic authentication (using passport), and I observe 2 cases:
When the Authorization header is not set, the ctx.request.route is OK
When the Authorization header is set with wrong credentials, the ctx.request.route is undefined (but the ctx.request.url is OK)
Example
Exception filter
@Catch(Exception)exportclassHttpExceptionFilterimplementsExceptionFilterMethods{
@Inject()metricsService: MetricsService// The catch does not change the TS.ED default behavior. It is intended to calculate metrics only.catch(exception: Exception,ctx: PlatformContext){consterror=this.mapError(exception);constheaders=this.getHeaders(exception);// /!\ undefined when 401 error due to wrong credentialsconsole.log(ctx.request.route?.toString());ctx.response.setHeaders(headers).status(error.status).body(error);}// Default mapErrormapError(error: any){return{name: error.origin?.name||error.name,message: error.message,status: error.status||500,errors: this.getErrors(error)};}// Default getErrorsprotectedgetErrors(error: any){return[error,error.origin].filter(Boolean).reduce((errs,{ errors }: ResponseErrorObject)=>{return[...errs, ...(errors||[])];},[]);}// Default getHeadersprotectedgetHeaders(error: any){return[error,error.origin].filter(Boolean).reduce((obj,{ headers }: ResponseErrorObject)=>{return{
...obj,
...(headers||{})};},{});}}
Basic Auth protocol
@Protocol({name: "basic",useStrategy: BasicStrategy,settings: {}})exportclassBasicProtocolimplementsOnVerify,OnInstall{async$onVerify(@Req()request: Req, @Arg(0)username: string, @Arg(1)password: string){if(password!=="okpassword"){returnfalse;}returntrue;}$onInstall(strategy: Strategy): void{// intercept the strategy instance to adding extra configuration}}
It's probably because the Auth check is processed by Passport and the middleware are executed before the endpoint itself. I'm not really a bug and I don't want to create an enhancement for that, because it will probably cause many refactor on the middleware orchestration.
And because you haven't create a repository to reproduce the bug, I won't take a time to do it myself due to the complexity of your exemple. In this specific case, a repo is helpful with an integration test (+ expectation), to not waste time to try to reproduce correctly your issue.
See you ;)
Romain
cazeaux
changed the title
[BUG] Title
[BUG] Undefined ctx.request.route when using Exception Filter with basic authorization handled by passport
Aug 30, 2023
Information
In the Exception Filter (using exactly the one documented here: https://tsed.io/docs/exceptions.html#exception-filter), in the catch methode, when we try to use the
ctx.request.route
parameter, we getundefined
in the special situation where we catch an unauthorized exception.So it happens with a basic authentication (using passport), and I observe 2 cases:
ctx.request.route
is OKctx.request.route
is undefined (but thectx.request.url
is OK)Example
Exception filter
Basic Auth protocol
Simple controller with auth
Acceptance criteria
ctx.request.route
should not be defined in case of authentication failure in the exception filterThe text was updated successfully, but these errors were encountered: