A plug to rewrite the Plug.Conn
's remote_ip
based on forwarding headers.
Generic comma-separated headers like X-Forwarded-For
, X-Real-Ip
, and X-Client-Ip
are all recognized, as well as the RFC 7239 Forwarded
header. You can specify any number of forwarding headers to recognize and even configure your own parsers.
IPs are processed last-to-first to prevent IP spoofing. Loopback/private IPs are ignored by default, but known proxies & clients are configurable, so you have full control over which IPs are considered legitimate.
If your app is not behind at least one proxy, you should not use this plug. See the algorithm for more details.
Add :remote_ip
to your list of dependencies in mix.exs
:
def deps do
[{:remote_ip, "~> 1.2"}]
end
Add the RemoteIp
plug to your app's plug pipeline:
defmodule MyApp do
use Plug.Router
plug RemoteIp
plug :match
plug :dispatch
# get "/" do ...
end
You can also use RemoteIp.from/2
outside of a plug pipeline to extract the remote IP from a list of headers:
x_headers = [{"x-forwarded-for", "1.2.3.4"}]
RemoteIp.from(x_headers)
See the documentation for full details on usage, configuration options, and troubleshooting.
Proxies are pervasive for some purpose or another in modern HTTP infrastructure: encryption, load balancing, caching, compression, and more can be done via proxies. But a proxy makes HTTP requests appear to your app as if they came from the proxy's IP address. How is your app to know the "actual" requesting IP address (e.g., so you can geolocate a user)?
Solution: Many proxies prevent this information loss by adding HTTP headers to communicate the requesting client's IP address. There is no single, universal header. Though X-Forwarded-For
is common, options include X-Real-IP
, X-Client-IP
, and others. Due to this lack of standardization, RFC 7239 defines the Forwarded
header, fulfilling a relevant XKCD truism.
Per the Plug.Conn
docs:
remote_ip
- the IP of the client, example:{151, 236, 219, 228}
. This field is meant to be overwritten by plugs that understand e.g. theX-Forwarded-For
header or HAProxy's PROXY protocol. It defaults to peer's IP.
Note that the field is meant to be overwritten. Plug does not actually do any overwriting itself. The Cowboy changelog espouses a similar platform of non-involvement:
Because each server's proxy situation differs, it is better that this function is implemented by the application directly.
Solution: As definitively verified in elixir-lang/plug#319, users are intended to hand-roll their own header parsers.
Solution: There are a handful of plugs available on Hex. There are also the comments left in the elixir-lang/plug#319 thread that may give you some ideas, but I consider them to be non-starters - copying/pasting code from github comments isn't much better than hand-rolling an implementation.
None of the available solutions I have seen are ideal. In this sort of plug, you want:
- Configurable headers and parsers: With so many different headers being used, you should be able to configure the ones you need and how to parse them.
- Configurable proxies and clients: With multiple proxy hops, there may be several IPs in the forwarding headers. Without being able to tell the plug which of those IPs are actually known to be proxies, you may get one of them back as the
remote_ip
. - Security and correctness: Parsing forwarding headers can be surprisingly subtle, and it's easy to open yourself up to IP spoofing vulnerabilities.
Existing packages all fail on one or more of these fronts:
- plug_cloudflare - not a general purpose library, but is more secure & correct if you're specifically parsing the
CF-Connecting-IP
- plug_forwarded_peer - only parses
Forwarded
andX-Forwarded-For
(X-Forwarded-For
takes precedence overForwarded
), does not parse all of RFC 7239's supported syntax correctly, vulnerable to IP spoofing - plug_x_forwarded_for - only configurable for a single header, all headers parsed the same as
X-Forwarded-For
, vulnerable to IP spoofing - remote_ip_rewriter - can only configure one header, all headers parsed the same as
X-Forwarded-For
, cannot configure loopback/private IPs as known clients - trusted_proxy_rewriter - outdated fork of remote_ip_rewriter, has even more limited functionality
Solution: These are the sorts of things application developers should not have to worry about. RemoteIp
aims to be the proper solution to all of these problems.
See CONTRIBUTING.md for instructions on how to open issues or pull requests.
While RemoteIp
has morphed into something distinct from the Rails middleware of the same name, the Rails code was definitely where I started. So I'd like to explicitly acknowledge the inspiration: this plug would not have been possible without poring over the existing implementation, discussions, documentation, and commits that went into the Rails code. ❤️
Required reading for anyone who wants to think way too much about forwarding headers:
- @gingerlime's blog post explaining IP spoofing
- Rails'
RemoteIp
middleware - Rails' tests
- The extensive discussion on rails/rails#7980