-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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(key-auth): keep query params order if hide_credentials is true #12758
Conversation
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.
@battlebyte A few thoughts...
The RFC2616 (HTTP 1.1) Section 4.2 says:
The order in which header fields with differing field names are
received is not significant. However, it is "good practice" to send
general-header fields first, followed by request-header or response-
header fields, and ending with the entity-header fields.
Thus, Kong's behavior is not deviating from the RFC.
The draft you linked also reinforces the fact that there may be alterations:
A signed HTTP message needs to be tolerant of some trivial
alterations during transmission as it goes through gateways, proxies,
and other entities
In the same section, the draft also says:
In order to generate the string that is signed with a key, the client
MUST use the values of each HTTP header field in theheaders
Signature Parameter, in the order they appear in theheaders
Signature Parameter.
The verification also MUST
follow the same process to recreate the signature (https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures#section-2.5).
Therefore, I see this as a non-conforming implementation on the client signing or the server verifying the signature.
I agree with your points @gszr , although those are in relation to the headers. The order of the headers is not important because, as you point out, the client MUST use the values of each HTTP header field in the headers Signature Parameter, in the order they appear in the headers Signature Parameter. The point here is that one of those headers is the (request-target) pseudo-header. This header includes the path and the query string, that is the reason why the order of the query string arguments should not be altered. See an example in slide 6 here https://revenue-ie.github.io/paye-employers-documentation/PIT3/examples/REST_Request_Authentication_V1.pdf. I hope this clarifies. |
If you see the example in https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures#section-3.1.3, the headers that will be part of the signature are:
Those headers can be received in any order, that is not a problem because the upstream knows how they should be ordered to calculate the signature. The key point, is that the
With the current implementation of
And the signature calculated by the upstream will not match the signature provided by the client. |
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.
See my comments in the main conversation.
3675de1
to
bf78534
Compare
@battlebyte I have taken the liberty to rebase and make a couple of changes to the code:
I will also request @bungle to take a look at this one. Also, the oauth2 plugin removes the header in the same way; should this fix also be applied there? |
Thanks for the review and changes @gszr . |
Agreed; once this one is merged we can submit a separate PR. |
Such a small PR which on a surface looks good and perhaps better than what we have, just raises a lot of small questions, for example: Can querystring param names be urlencoded, e.g. |
kong/plugins/key-auth/handler.lua
Outdated
if conf.key_in_query then | ||
local raw_query = kong.request.get_raw_query() | ||
local new_query = remove_query_key(raw_query, name) | ||
kong.service.request.set_raw_query(new_query) |
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 an error occurs in the regex gsub, remove_query_key
will return nil
, and this call to set_raw_query
will now throw an error. Let's change remove_query_key
to return nil, err
and log the error from here instead.
Also, I wonder if now we should respond with a 500 in case the query key clearing cannot be done - it can also be seen as a security issue if the plugin config has hide_credentials
and it has no effect.
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.
Fixed in the latest commit. I think returning 500 makes sense but I'm waiting on the final decision you guys make.
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.
This is a new error condition; IMO it makes sense to respond with a 500 in that case. Otherwise, the token will be leaked to the upstream service, even though the expectation is it was removed.
### Summary Adds libada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
### Summary Adds libada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
### Summary Adds libada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
### Summary Adds libada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
### Summary Adds libada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
Thanks @bungle , that makes sense. Your proposal is a much better solution. Thanks for taking the time! |
### Summary Adds libada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
### Summary Adds libada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
### Summary Adds libada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
Hi @bungle , what are the next steps to close this topic? |
### Summary Adds libada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
### Summary Adds libada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
### Summary Adds libada and lua-resty-ada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
### Summary Adds libada and lua-resty-ada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
### Summary Adds libada and lua-resty-ada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
### Summary Adds libada and lua-resty-ada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
### Summary Adds libada and lua-resty-ada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
### Summary Adds libada and lua-resty-ada as a dependency. This is needed for: #12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
…tials Fixes #12758 reported by @battlebyte. Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
…tials Fixes #12758 reported by @battlebyte. Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
…tials Fixes #12758 reported by @battlebyte. Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
### Summary Adds libada and lua-resty-ada as a dependency. This is needed for: Kong#12758 But it may be great for many other uses too. The `lua-resty-ada` LuaJIT FFI bindings can be found here: https://github.com/bungle/lua-resty-ada Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
…tials Fixes #12758 reported by @battlebyte. Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
…tials Fixes #12758 reported by @battlebyte. Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
…tials Fixes #12758 reported by @battlebyte. Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
….inspect Fixes #12758 reported by @battlebyte. Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
…tials Fixes #12758 reported by @battlebyte. Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com> (cherry picked from commit b3e065e) Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
When hide_credentials=true, query parameters should not be sorted alphabetically. This alters the original request and can have unintended consequences for upstream services. One example is authentication with the pseudo header (request-target) as described in https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures#section-2.3. Since the order is not preserved, the signature does not match and the upstream rejects the request.
Checklist
changelog/unreleased/kong
orskip-changelog
label added on PR if changelog is unnecessary. README.md