-
Notifications
You must be signed in to change notification settings - Fork 89
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
chore: token invalid throw 401 instead of return public incoming payment #3062
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for brilliant-pasca-3e80ec canceled.
|
@@ -178,6 +178,18 @@ You can either trigger the debugger by adding `debugger` statements in the code | |||
#### Debugging with VS Code: | |||
|
|||
To debug with VS Code, add this configuration to your `.vscode/launch.json`: | |||
```json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also adding the json from the localenv README file here as it's missing here and I find it useful as it helped me debug the code.
if out of scope PR, can make another PR
return | ||
} | ||
|
||
const authSplit = ctx.request.headers.authorization?.split(' ') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const authSplit = ctx.request.headers.authorization?.split(' ') | |
const authSplit = ctx.request.headers.authorization.split(' ') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
authorization
can still be undefined here, when the canSkipAuthValidation
is false it won't enter the early return statement and will return 401 because of undefined authorization;
this is the reason i left the ?
there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just need to run pnpm format
to fix the check.
We also have a line in packages/backend/src/open_payments/payment/incoming_remote/service.ts
which we can remove
("// TODO: remove after #2889 is completed").
@@ -145,19 +151,16 @@ export function createTokenIntrospectionMiddleware({ | |||
: undefined | |||
} | |||
} | |||
|
|||
await next() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep this in the outside of try-catch. Otherwise, the catch block in this middleware will apply to all of the following middlewares called in await next()
which might lead to unexpected behaviour, e.g., adding WWW-Authenticate
when we don't want to.
if (!(err instanceof OpenPaymentsServerRouteError)) { | ||
throw err | ||
} | ||
|
||
ctx.set('WWW-Authenticate', `GNAP as_uri=${config.authServerGrantUrl}`) | ||
|
||
if (!bypassError) { | ||
throw err | ||
} | ||
throw err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!(err instanceof OpenPaymentsServerRouteError)) { | |
throw err | |
} | |
ctx.set('WWW-Authenticate', `GNAP as_uri=${config.authServerGrantUrl}`) | |
if (!bypassError) { | |
throw err | |
} | |
throw err | |
if (err instanceof OpenPaymentsServerRouteError) { | |
ctx.set('WWW-Authenticate', `GNAP as_uri=${config.authServerGrantUrl}`) | |
} | |
throw err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are here, this seemed more straightforward
@@ -577,7 +577,7 @@ export class App { | |||
createTokenIntrospectionMiddleware({ | |||
requestType: AccessType.IncomingPayment, | |||
requestAction: RequestAction.Read, | |||
bypassError: true | |||
canSkipAuthValidation: true | |||
}), | |||
authenticatedStatusMiddleware, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In authenticatedStatusMiddleware
we should return early if we know that we ended up skipping auth validation in createTokenIntrospectionMiddleware
(since it doesn't make sense to do a lot of work for something we already know shouldn't succeed). One way we can do this is to make client
optional in HttpSigWithAuthenticatedStatusContext
, and only proceed to call throwIfSignatureInvalid
if ctx.client
is defined (setting ctx.client
is the main function of createTokenIntrospectionMiddleware
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you think about checking for
if (!ctx.request.headers.authorization) await next() return }
instead of making the client
optional (string | undefined) for the rest of the code? I also see that authenticatedStatusMiddleware
is only used for open payments
`authorization` can still be undefined here, when the `canSkipAuthValidation` is false it won't enter the early return statement and will return 401 because of undefined authorization Co-authored-by: Max Kurapov <max@interledger.org>
Changes proposed in this pull request
Moving the
await next()
inside the try catch as I believe this is a good practice.Letting next() call outside the try-catch creates potential issues:
Renaming
bypassError
tocanSkipAuthValidation
to not introspect an access token and respond with "public" incoming payment that does not require an access token at allContext
If an
accessToken
is provided with the request to get an incoming payment, and the token is invalid, it fails with a 401 error instead of returning a public incoming paymentLink issues here - fixes #2889
Checklist
fixes #number
user-docs
label (if necessary)