Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Make Request::isFromTrustedProxy() public. #18197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2016
Merged

Conversation

sjamaan
Copy link

@sjamaan sjamaan commented Mar 16, 2016

Q A
Branch master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

When adding custom headers to your proxy (in our particular case, a X-Base-Url header, but it could be anything), it is necessary to be able to tell whether the request came from a trusted proxy or not. Unfortunately, currently the isFromTrustedProxy() method is private, which means you'll need to subclass Request and add your own differently named method just to be able to access this information.

This functionality is pretty straightforward, and I don't see a reason to keep this method private so this a trivial pull request to make it public (and it adds a comment so it'll show up in the API docs with proper documentation).

Also add documentation so we know what it can be used for.
@xabbuh
Copy link
Member

xabbuh commented Mar 16, 2016

Can you explain the use case where it is important to determine if the request came from a trusted proxy? This method is already used in several places in the Request class. So maybe it would be more reasonable if you called one the other methods of the Request class.

@sjamaan
Copy link
Author

sjamaan commented Mar 16, 2016

I'm using this to determine whether to trust a custom X-Base-Url header that's added by our Vagrant reverse proxy setup. It's a nonstandard header, so I think none of the existing methods are sufficient to extract it.

More concretely, we have a small middleware plugin for Laravel which sets the root URL for the application:

class ProxiedBaseUrlMiddleware
{
    public function handle($request, Closure $next)
    {
        $request = CustomRequest::createFromBase($request);
        $baseUrl = $request->maybeGetProxiedBaseUrl();
        if ($baseUrl) \URL::forceRootUrl($baseUrl);
        return $next($request);
    }
}

And CustomRequest looks like this:

class CustomRequest extends IlluminateRequest
{
    public function maybeGetProxiedBaseUrl()
    {
        $isFromTrustedProxy = self::$trustedProxies && IpUtils::checkIp($this->server->get('REMOTE_ADDR'), self::$trustedProxies);
        /* $this->isFromTrustedProxy() is private... */
        if ($isFromTrustedProxy) return $this->headers->get('X-Base-Url');
        else return null;
    }
}

Here, IlluminateRequest is Laravel's Request class which extends from Symfony's. If the Request class would expose isFromTrustedProxy(), we could move the check to our middleware and get rid of the custom subclass as well as the creation of another Request object with the createFromBase() method (though this latter part is a shortcoming of Laravel's dependency injection, not a problem with Symfony).

@sjamaan
Copy link
Author

sjamaan commented Mar 17, 2016

A slightly longer explanation can be found in my blog post about how we're using this plugin.

@nicolas-grekas
Copy link
Member

👍

@xabbuh
Copy link
Member

xabbuh commented Mar 23, 2016

👍

Status: Reviewed

@fabpot
Copy link
Member

fabpot commented Mar 25, 2016

Thank you @sjamaan.

@fabpot fabpot merged commit 286f64f into symfony:master Mar 25, 2016
fabpot added a commit that referenced this pull request Mar 25, 2016
This PR was merged into the 3.1-dev branch.

Discussion
----------

Make Request::isFromTrustedProxy() public.

| Q             | A
| ------------- | ---
| Branch        | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

When adding custom headers to your proxy (in our particular case, a `X-Base-Url` header, but it could be anything), it is necessary to be able to tell whether the request came from a trusted proxy or not. Unfortunately, currently the `isFromTrustedProxy()` method is private, which means you'll need to subclass `Request` and add your own *differently named* method just to be able to access this information.

This functionality is pretty straightforward, and I don't see a reason to keep this method private so this a trivial pull request to make it public (and it adds a comment so it'll show up in the API docs with proper documentation).

Commits
-------

286f64f Make Request::isFromTrustedProxy() public.
@fabpot fabpot mentioned this pull request May 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants