Skip to content

Support RFC 7239: HTTP Forwarded header#1017

Closed
mattbostock wants to merge 2 commits into
rack:masterfrom
alphagov:support_rfc_7239
Closed

Support RFC 7239: HTTP Forwarded header#1017
mattbostock wants to merge 2 commits into
rack:masterfrom
alphagov:support_rfc_7239

Conversation

@mattbostock

Copy link
Copy Markdown
Contributor

RFC 7239 is an IETF proposed standard for a HTTP Forwarded request
header that aims to replace non-standard X-Forwarded-* headers.

This commit supports all relevant tokens that may be set in the
Forwarded header:

  • proto
  • host
  • for

I have prioritised the Forwarded header over the non-standard headers
as I think it's preferable to use the standard header when specified.

Multiple values for each token are allowed by the standard. When
multiple values are supplied, I've followed these rules:

  • for the proto token, use the first value specified as it's most
    likely to be the protocol used by the original client
  • for the host token, use the last value specified following the
    convention already used by Rack for multiple X-Forwarded-For
    headers

When both X-Forwarded-For and Forwarded headers are supplied and the
latter includes for values, I've concatenated both sets of values with
the Forwarded values coming last.

I've been careful to support mixed case in the header and optional
double quotes encapsulating the token value as specified in the RFC.

`standartd` should read `standard`.
[RFC 7239][] is an IETF proposed standard for a HTTP `Forwarded` request
header that aims to replace non-standard `X-Forwarded-*` headers.

This commit supports all relevant tokens that may be set in the
Forwarded header:
  - proto
  - host
  - for

I have prioritised the Forwarded header over the non-standard headers
as I think it's preferable to use the standard header when specified.

Multiple values for each token are allowed by the standard. When
multiple values are supplied, I've followed these rules:
  - for the `proto` token, use the first value specified as it's most
    likely to be the protocol used by the original client
  - for the `host` token, use the last value specified following the
    convention already used by Rack for multiple `X-Forwarded-For`
    headers

When both `X-Forwarded-For` and `Forwarded` headers are supplied and the
latter includes `for` values, I've concatenated both sets of values with
the `Forwarded` values coming last.

I've been careful to support mixed case in the header and optional
double quotes encapsulating the token value as specified in the RFC.

[RFC 7239]: https://tools.ietf.org/html/rfc7239
@mattbostock

Copy link
Copy Markdown
Contributor Author

Is this a useful addition? I'd be really grateful for any feedback. 😎

Comment thread lib/rack/common_logger.rb

msg = FORMAT % [
env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
forwarded_for || env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems this is kinda-implementing Rack::Request#ip, maybe nicer to reuse that.

@raggi

raggi commented Jun 5, 2016

Copy link
Copy Markdown
Member

I think this will need to be made configurable now.

The problem here is that proxies that have yet to be updated will not strip these headers, and also will not know to overwrite them.

A failure to protect against outside spoofing can have important security implications. Users must manage the transition with care.

@mattbostock

Copy link
Copy Markdown
Contributor Author

Sorry for the lack of activity and thanks for the feedback. Hopefully I'll get time to work some more on this in the next couple of weeks.

@raggi: Would you suggest disabling this functionality by default?

@raggi

raggi commented Jul 17, 2016

Copy link
Copy Markdown
Member

I would not recommend making major API changes without changing major versions, ideally. This situation is unfortunate and tricky, but I don't think we can safely just break this.

@ioquatix

Copy link
Copy Markdown
Member

What's the status of this?

@ioquatix

Copy link
Copy Markdown
Member

Can we rebase this and land it in Rack 3.x?

@fatkodima

Copy link
Copy Markdown
Contributor

@ioquatix I will move this forward if @mattbostock don't want (have time) to.
How we should address @raggi 's latest two comments?

@ioquatix

Copy link
Copy Markdown
Member

I haven't reviewed this PR for a while, but feel free to rebase it and we can go from there.

@jeremyevans

Copy link
Copy Markdown
Contributor

It seems more likely we will use the updated version of this (#1423) than this, so I'm going to close this now.

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.

6 participants