Conversation
1a5c8ba to
df474fa
Compare
internal/requestclient/http.go
Outdated
There was a problem hiding this comment.
I got rid of external boolean here since we only ever took action if both it and useCloudflareHeaders were true. We simplify the logic here by just moving this logic to the Internal/External middleware constructors.
internal/requestclient/http_test.go
Outdated
There was a problem hiding this comment.
I assume CF prevents anyone from adding this header, or it makes sure that the header they set is always first?
There was a problem hiding this comment.
https://developers.cloudflare.com/fundamentals/reference/http-request-headers/#x-forwarded-for
I haven't tested this personally, but the way I interpret their docs is that they assume responsibility for preventing anyone from being able to set this header:
CF-Connecting-IP provides the client IP address connecting to Cloudflare to the origin web server. This header will only be sent on the traffic from Cloudflare’s edge to your origin web server.
internal/requestclient/http_test.go
Outdated
9e26623 to
62d9ad8
Compare
df474fa to
415e9b2
Compare
Merge activity
|
…der, not just the first
415e9b2 to
5499193
Compare
|
Caution License checking failed, please read: how to deal with third parties licensing. |

Closes https://linear.app/sourcegraph/issue/SRC-454/extract-and-propagate-user-ip-address-throughout-the-request-lifecycle
According to HTTP1.1/RFC 2616: Headers may be repeated, and any comma-separated list-headers (like
X-Forwarded-For) should be treated as a single value.In section 4.2:
For Example:
For the following HTTP request, it's valid to have multiple instances of x-forwarded-for:
That must be interpret-able as
X-Forwarded-For: 203.0.113.195, 70.41.3.18, 150.172.238.178, 123.45.67.89Previously, our code used http.Header.Get():
https://github.com/sourcegraph/sourcegraph/blob/9e26623d90461bf7df2e0f3e35561fecc0c35f3f/internal/requestclient/http.go#L81-L95
However, func (Header) Get only returns the first value of the header:
In our example, this means that our code would only get
X-Forwarded-For: 203.0.113.195, 70.41.3.18, which is invalid according to RFC 2616.Test plan
There were no unit tests, so I added some.
Changelog