http-headerscloudflare

How to ensure that a request is really proxied by CloudFlare?


In one of projects we use CloudFlare and its cf-connecting-ip header to get a visitor IP address. But I'm not sure that the info can be fully trusted. For example, a bad user somehow had obtained real IP of our server, and connecting directly to it, putting fake IP address to the cf-connecting-ip header, and pretending that it was a legitimate CF-proxied request.

I examined headers for CF-proxied requests, and saw the cf-request-id header. But it's unique per a request, and I cannot see there any fixed secure key which I can use to verify that the request was legitimate.

How to ensure that a request was really proxied by CF?


Solution

  • You may want to use the Authenticated Origin Pulls feature from Cloudflare:

    Authenticated Origin Pulls let origin web servers strongly validate that a web request is coming from Cloudflare.
    We use TLS client certificate authentication, a feature supported by most web servers, and present a Cloudflare certificate when establishing a connection between Cloudflare and the origin server.
    By validating this certificate in origin server configuration, access can be limited to Cloudflare connections.

    You will need to download the client certificate from Cloudflare and configure your webserver to enforce the certificate check.

    The referenced page has instructions on how to set up client certificate in Apache and Nginx.