Repository navigation
Added custom headers forwarding support - #258
Conversation
|
Hi, Thanks for your contribution! Before I start a review a question up front: What problem do you solve with header forwarding? |
|
We have a legacy image server that checks for mandatory headers before retrieving an image, a sort of API key functionality with some validations. Also I would like to use an Openstack Swift server that needs an X-Auth-Token header which was not supported in imaginary. I hope that maybe more people need something like this but I understand if it's not the proper way to do it. |
Dynom
left a comment
There was a problem hiding this comment.
Thanks for your effort, I think it's a very useful addition to the project. I've marked a couple of things that I'd like to see changed, would you mind addressing them ?
| -enable-url-source Enable remote HTTP URL image source processing (?url=http://..) | ||
| -enable-placeholder Enable image response placeholder to be used in case of error [default: false] | ||
| -enable-auth-forwarding Forwards X-Forward-Authorization or Authorization header to the image source server. -enable-url-source flag must be defined. Tip: secure your server from public access to prevent attack vectors | ||
| -custom-headers Forwards custom headers to the image source server. -enable-url-source flag must be defined. |
There was a problem hiding this comment.
The spacing is a bit off here compared to the rest. Even though the Code uses tabs for indentation, the Documentation uses spaces to keep it nicely consistent.
| aCertFile = flag.String("certfile", "", "TLS certificate file path") | ||
| aKeyFile = flag.String("keyfile", "", "TLS private key file path") | ||
| aAuthorization = flag.String("authorization", "", "Defines a constant Authorization header value passed to all the image source servers. -enable-url-source flag must be defined. This overwrites authorization headers forwarding behavior via X-Forward-Authorization") | ||
| aCustomHeaders = flag.String("custom-headers", "", "Forwards custom headers to the image source server. -enable-url-source flag must be defined.") |
There was a problem hiding this comment.
I'm not a fan of the wording here "Custom Headers" doesn't directly convey the intent of what the flag does. Personally I would prefer the flag be named "forward-headers" instead. It also matches your description better.
| -enable-url-source Enable remote HTTP URL image source processing | ||
| -enable-placeholder Enable image response placeholder to be used in case of error [default: false] | ||
| -enable-auth-forwarding Forwards X-Forward-Authorization or Authorization header to the image source server. -enable-url-source flag must be defined. Tip: secure your server from public access to prevent attack vectors | ||
| -custom-headers Forwards custom headers to the image source server. -enable-url-source flag must be defined. |
There was a problem hiding this comment.
The same thing about the white-space. I think replacing the tabs with spaces cleans it up nicely.
| } | ||
|
|
||
| if len(s.Config.CustomHeaders) != 0 { | ||
| s.setCustomHeaders(req, ireq) |
There was a problem hiding this comment.
This sequence opens up a vulnerability regarding the Authorization Header. As effectively this allows the client to override the Authorization Header.
I'm not sure what's wise here:
- Change the sequence, so that adding Imaginary's own headers is always last
- Change the implementation to include a
if ! ireq.Header.Has(header) {
I think my preference would be to do both.
| } | ||
| } | ||
|
|
||
| func TestHttpImageSourceCustomHeaders(t *testing.T) { |
There was a problem hiding this comment.
Great start! However please include test cases for:
- Proof that the client can't override headers set by Imaginary
- Proof that empty HTTP headers also work
- Proof that case sensitivity when setting headers isn't a problem (headers set by the client are correctly set in the upstream request)
- Proof that custom headers that aren't white-listed, but who are defined by the client, aren't forwarded.
|
Thanks for taking the time to review the code. I updated the PR and I think that I covered most of the advices. If you have any more suggestions or there is any bug please tell me. |
Dynom
left a comment
There was a problem hiding this comment.
The tests look great, well done! Just a few dots on the i's and it's good to merge.
| headers := s.Config.ForwardHeaders | ||
| for _, header := range headers { | ||
| if header != "" { | ||
| if header != "" && ireq.Header.Get(header) != "" && req.Header.Get(header) == "" { |
There was a problem hiding this comment.
This change breaks HTTP expectations. Headers can actually have an empty value, in fact HTTP/2 uses it as part of its upgrade process.
I'm not sure if this is actually a problem in this use-case, but I also think it's fine. Fewer code is better in this situation I think. So if you stick to the if header != "" { here, it should be good.
| return headers | ||
| } | ||
| return strings.Split(headers, ",") | ||
| for _, header := range strings.Split(forwardHeaders, ",") { |
There was a problem hiding this comment.
This is a great improvement over your previous implementation. However there is still a gap that allows for miss-configuration, namely if the flag has a space between commas. Which might not be that far fetched if you take newline characters into account: https://play.golang.org/p/WUh_upVSy2F
So if you trim white-space before the empty check, I think we've nailed it 💪
| func (s *HTTPImageSource) setForwardHeaders(req *http.Request, ireq *http.Request) { | ||
| headers := s.Config.ForwardHeaders | ||
| for _, header := range headers { | ||
| if header != "" && len(ireq.Header[header]) > 0 { |
There was a problem hiding this comment.
Here I used this approach to check if a header is defined or if it's empty. Leaving the header != "" condition alone brings the scenario where all the forwarded headers are set although the original request hasn't defined them.
There was a problem hiding this comment.
Oh wow, of course you're completely right! What I meant to say was that we only need to check for existence, rather than that it has a value. So basically:
if ireq.Header.Has(header) && req.Header.Has(header) {Since you've cleaned up s.Config.ForwardHeaders, I suppose header != "" became effectively a noop.
I just noticed that Header.has is private, what a shame! So I guess we can't use that. However, since http.Header is a map, how about we use the typical exists notation?
| if header != "" && len(ireq.Header[header]) > 0 { | |
| if _, ok := h[header]; ok { |
There was a problem hiding this comment.
Oh great, I didn't know that I could check for existence that way in a map. I will submit the changes.
|
I made a few modifications considering your review and the cases I missed to cover. Please comment if you find something else to check. Thanks! |
Dynom
left a comment
There was a problem hiding this comment.
Looks great, thanks for your contribution!
Hi, I added support to forward any header to the image source server. Maybe can help if the source server checks those headers for track information of API consumption and if uses another custom header for authorization.
I'm a beginner of Go so any modification or advice are welcome.