Skip to content

Added custom headers forwarding support - #258

Merged
Dynom merged 6 commits into
h2non:masterfrom
r-antonio:master
Apr 8, 2019
Merged

Dynom merged 6 commits into
h2non:masterfrom
r-antonio:master

Conversation

@r-antonio

Copy link
Copy Markdown
Contributor

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.

@Dynom

Dynom commented Apr 2, 2019

Copy link
Copy Markdown
Collaborator

Hi,

Thanks for your contribution!

Before I start a review a question up front: What problem do you solve with header forwarding?

@r-antonio

Copy link
Copy Markdown
Contributor Author

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 Dynom left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

Comment thread README.md Outdated
-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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread imaginary.go Outdated
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.")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread imaginary.go Outdated
-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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same thing about the white-space. I think replacing the tabs with spaces cleans it up nicely.

Comment thread source_http.go Outdated
}

if len(s.Config.CustomHeaders) != 0 {
s.setCustomHeaders(req, ireq)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread source_http_test.go Outdated
}
}

func TestHttpImageSourceCustomHeaders(t *testing.T) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@r-antonio

Copy link
Copy Markdown
Contributor Author

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 Dynom left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests look great, well done! Just a few dots on the i's and it's good to merge.

Comment thread source_http.go Outdated
headers := s.Config.ForwardHeaders
for _, header := range headers {
if header != "" {
if header != "" && ireq.Header.Get(header) != "" && req.Header.Get(header) == "" {

@Dynom Dynom Apr 6, 2019 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread imaginary.go
return headers
}
return strings.Split(headers, ",")
for _, header := range strings.Split(forwardHeaders, ",") {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 💪

Comment thread source_http.go Outdated
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 {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Dynom Dynom Apr 8, 2019 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Suggested change
if header != "" && len(ireq.Header[header]) > 0 {
if _, ok := h[header]; ok {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh great, I didn't know that I could check for existence that way in a map. I will submit the changes.

@r-antonio

Copy link
Copy Markdown
Contributor Author

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 Dynom left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks for your contribution!

@Dynom Dynom added this to the 1.1.1 milestone Apr 8, 2019
@Dynom
Dynom merged commit 519c5ff into h2non:master Apr 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

⌂ 换网址