• Stars
    star
    247
  • Rank 164,117 (Top 4 %)
  • Language
    Elixir
  • License
    MIT License
  • Created about 8 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A plug to rewrite the Plug.Conn's remote_ip based on forwarding headers.

RemoteIp

build status coverage status hex.pm version

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.

Installation

Add :remote_ip to your list of dependencies in mix.exs:

def deps do
  [{:remote_ip, "~> 1.1"}]
end

Usage

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.

Motivation

Problem: Your app is behind a proxy and you want to know the original client's IP address.

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.

Problem: Plug does not derive remote_ip from headers such as X-Forwarded-For.

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. the X-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.

Problem: Ain't nobody got time for that.

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.

Problem: Existing solutions are incomplete and have subtle bugs.

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 and X-Forwarded-For (X-Forwarded-For takes precedence over Forwarded), 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.

Contributing

See CONTRIBUTING.md for instructions on how to open issues or pull requests.

Prior Art

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: