Added Laravel project

This commit is contained in:
2017-09-17 00:35:10 +02:00
parent a3c19304d5
commit ecf605b8f5
6246 changed files with 682270 additions and 2 deletions

View File

@@ -0,0 +1,13 @@
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

38
Laravel/vendor/fideloper/proxy/composer.json vendored Executable file
View File

@@ -0,0 +1,38 @@
{
"name": "fideloper/proxy",
"description": "Set trusted proxies for Laravel",
"keywords": ["proxy", "trusted proxy", "load balancing"],
"license": "MIT",
"authors": [
{
"name": "Chris Fidao",
"email": "fideloper@gmail.com"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/contracts": "~5.0"
},
"require-dev": {
"illuminate/http": "~5.0",
"mockery/mockery": "~0.9.3",
"phpunit/phpunit": "^5.7"
},
"autoload": {
"psr-4": {
"Fideloper\\Proxy\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "3.3-dev"
},
"laravel": {
"providers": [
"Fideloper\\Proxy\\TrustedProxyServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}

View File

@@ -0,0 +1,71 @@
<?php
return [
/*
* Set trusted proxy IP addresses.
*
* Both IPv4 and IPv6 addresses are
* supported, along with CIDR notation.
*
* The "*" character is syntactic sugar
* within TrustedProxy to trust any proxy
* that connects directly to your server,
* a requirement when you cannot know the address
* of your proxy (e.g. if using Rackspace balancers).
*
* The "**" character is syntactic sugar within
* TrustedProxy to trust not just any proxy that
* connects directly to your server, but also
* proxies that connect to those proxies, and all
* the way back until you reach the original source
* IP. It will mean that $request->getClientIp()
* always gets the originating client IP, no matter
* how many proxies that client's request has
* subsequently passed through.
*/
'proxies' => [
'192.168.1.10',
],
/*
* Or, to trust all proxies that connect
* directly to your server, uncomment this:
*/
# 'proxies' => '*',
/*
* Or, to trust ALL proxies, including those that
* are in a chain of forwarding, uncomment this:
*/
# 'proxies' => '**',
/*
* Default Header Names
*
* Change these if the proxy does
* not send the default header names.
*
* Note that headers such as X-Forwarded-For
* are transformed to HTTP_X_FORWARDED_FOR format.
*
* The following are Symfony defaults, found in
* \Symfony\Component\HttpFoundation\Request::$trustedHeaders
*
* You may optionally set headers to 'null' here if you'd like
* for them to be considered untrusted instead. Ex:
*
* Illuminate\Http\Request::HEADER_CLIENT_HOST => null,
*
* WARNING: If you're using AWS Elastic Load Balancing or Heroku,
* the FORWARDED and X_FORWARDED_HOST headers should be set to null
* as they are currently unsupported there.
*/
'headers' => [
(defined('Illuminate\Http\Request::HEADER_FORWARDED') ? Illuminate\Http\Request::HEADER_FORWARDED : 'forwarded') => 'FORWARDED',
Illuminate\Http\Request::HEADER_CLIENT_IP => 'X_FORWARDED_FOR',
Illuminate\Http\Request::HEADER_CLIENT_HOST => 'X_FORWARDED_HOST',
Illuminate\Http\Request::HEADER_CLIENT_PROTO => 'X_FORWARDED_PROTO',
Illuminate\Http\Request::HEADER_CLIENT_PORT => 'X_FORWARDED_PORT',
]
];

View File

@@ -0,0 +1,181 @@
<?php
namespace Fideloper\Proxy;
use Closure;
use Illuminate\Contracts\Config\Repository;
class TrustProxies
{
/**
* The config repository instance.
*
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;
/**
* The trusted proxies for the application.
*
* @var array
*/
protected $proxies;
/**
* The proxy header mappings.
*
* @var array
*/
protected $headers;
/**
* Create a new trusted proxies middleware instance.
*
* @param \Illuminate\Contracts\Config\Repository $config
*/
public function __construct(Repository $config)
{
$this->config = $config;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*
* @return mixed
*/
public function handle($request, Closure $next)
{
$this->setTrustedProxyHeaderNames($request);
$this->setTrustedProxyIpAddresses($request);
return $next($request);
}
/**
* Sets the trusted proxies on the request to the value of trustedproxy.proxies
*
* @param \Illuminate\Http\Request $request
*/
protected function setTrustedProxyIpAddresses($request)
{
$trustedIps = $this->proxies ?: $this->config->get('trustedproxy.proxies');
// We only trust specific IP addresses
if (is_array($trustedIps)) {
return $this->setTrustedProxyIpAddressesToSpecificIps($request, $trustedIps);
}
// We trust any IP address that calls us, but not proxies further
// up the forwarding chain.
// TODO: Determine if this should only trust the first IP address
// Currently it trusts the entire chain (array of IPs),
// potentially making the "**" convention redundant.
if ($trustedIps === '*') {
return $this->setTrustedProxyIpAddressesToTheCallingIp($request);
}
// We trust all proxies. Those that call us, and those that are
// further up the calling chain (e.g., where the X-FORWARDED-FOR
// header has multiple IP addresses listed);
if ($trustedIps === '**') {
return $this->setTrustedProxyIpAddressesToAllIps($request);
}
}
/**
* We specify the IP addresses to trust explicitly.
*
* @param \Illuminate\Http\Request $request
* @param array $trustedIps
*/
private function setTrustedProxyIpAddressesToSpecificIps($request, $trustedIps)
{
$request->setTrustedProxies((array) $trustedIps, $this->getTrustedHeaderSet());
}
/**
* We set the trusted proxy to be the first IP addresses received.
*
* @param \Illuminate\Http\Request $request
*/
private function setTrustedProxyIpAddressesToTheCallingIp($request)
{
$request->setTrustedProxies($request->getClientIps(), $this->getTrustedHeaderSet());
}
/**
* Trust all IP Addresses.
*
* @param \Illuminate\Http\Request $request
*/
private function setTrustedProxyIpAddressesToAllIps($request)
{
// 0.0.0.0/0 is the CIDR for all ipv4 addresses
// 2000:0:0:0:0:0:0:0/3 is the CIDR for all ipv6 addresses currently
// allocated http://www.iana.org/assignments/ipv6-unicast-address-assignments/ipv6-unicast-address-assignments.xhtml
$request->setTrustedProxies(['0.0.0.0/0', '2000:0:0:0:0:0:0:0/3'], $this->getTrustedHeaderSet());
}
/**
* Set the trusted header names based on the content of trustedproxy.headers.
*
* Note: Depreciated in Symfony 3.3+, but available for backwards compatibility.
*
* @depreciated
*
* @param \Illuminate\Http\Request $request
*/
protected function setTrustedProxyHeaderNames($request)
{
$trustedHeaderNames = $this->getTrustedHeaderNames();
if(!is_array($trustedHeaderNames)) { return; } // Leave the defaults
foreach ($trustedHeaderNames as $headerKey => $headerName) {
$request->setTrustedHeaderName($headerKey, $headerName);
}
}
/**
* Retrieve trusted header names, falling back to defaults if config not set.
*
* @return array
*/
protected function getTrustedHeaderNames()
{
return $this->headers ?: $this->config->get('trustedproxy.headers');
}
/**
* Construct bit field integer of the header set that setTrustedProxies() expects.
*
* @return int
*/
protected function getTrustedHeaderSet()
{
$trustedHeaderNames = $this->getTrustedHeaderNames();
$headerKeys = array_keys($this->getTrustedHeaderNames());
return array_reduce($headerKeys, function ($set, $key) use ($trustedHeaderNames) {
// PHP 7+ gives a warning if non-numeric value is used
// resulting in a thrown ErrorException within Laravel
// This error occurs with Symfony < 3.3, PHP7+
if(! is_numeric($key)) {
return $set;
}
// If the header value is null, it is a distrusted header,
// so we will ignore it and move on.
if (is_null($trustedHeaderNames[$key])) {
return $set;
}
return $set | $key;
}, 0);
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Fideloper\Proxy;
use Illuminate\Foundation\Application as LaravelApplication;
use Illuminate\Support\ServiceProvider;
use Laravel\Lumen\Application as LumenApplication;
class TrustedProxyServiceProvider extends ServiceProvider
{
/**
* Boot the service provider.
*
* @return void
*/
public function boot()
{
$source = realpath(__DIR__.'/../config/trustedproxy.php');
if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
$this->publishes([$source => config_path('trustedproxy.php')]);
} elseif ($this->app instanceof LumenApplication) {
$this->app->configure('trustedproxy');
}
$this->mergeConfigFrom($source, 'trustedproxy');
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
//
}
}