Skip to content

Commit

Permalink
Upgrade to Laravel 9
Browse files Browse the repository at this point in the history
  • Loading branch information
gtjamesa committed Feb 9, 2022
1 parent 5c819bb commit 163d4e5
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 98 deletions.
14 changes: 4 additions & 10 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ jobs:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [ 7.4, 8.0 ]
laravel: [ 6.*, 7.*, 8.* ]
php: [ 8.0, 8.1 ]
laravel: [ 9.* ]
include:
- laravel: 6.*
testbench: 4.*
stability: prefer-stable
- laravel: 7.*
testbench: 5.*
stability: prefer-stable
- laravel: 8.*
testbench: 6.*
- laravel: 9.*
testbench: 7.*
stability: prefer-stable

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.os }}
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ build
composer.lock
docs
vendor
coverage
coverage
.php_cs.cache
.php-cs-fixer.cache
16 changes: 16 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use PhpCsFixer\Finder;

$project_path = getcwd();
$finder = Finder::create()
->in([
$project_path . '/src',
$project_path . '/tests',
])
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

return JamesAusten\styles($finder);
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ This package was created due to `request()->ip()` always returning `127.0.0.1` o

Due to the dynamic nature of Laravel Vapor, it becomes a challenge to set the [trusted proxies](https://laravel.com/docs/7.x/requests#configuring-trusted-proxies) for your Laravel application. If you rely on the IP address of the user being valid, then this package is for you!

## Version Compatibility

[Laravel 9.0](https://laravel.com/docs/9.x/upgrade) introduced changes to the default `TrustedProxies` middleware, and the [fideloper/proxy](https://packagist.org/packages/fideloper/proxy) package is no longer required, as the [functionality is included with Laravel](https://github.com/illuminate/http/blob/9.x/Middleware/TrustProxies.php). I'm not actively using Vapor and am not sure whether this package is still required, but I have gone ahead and updated the requirements and pushed the `v2.0` release, which drops support for earlier Laravel versions.

| Laravel | Package Version |
| -------- | --------------- |
| `^9.0` | `^2.0` |
| `<= 9.0` | `^1.0` |

## Installation

You can install the package via composer:
Expand All @@ -27,6 +36,7 @@ use Illuminate\Http\Request;

class TrustProxies extends Middleware
{
...
```

The package will then work out-of-the-box to dynamically fetch the proxy servers used by your current Vapor's deployment.
Expand Down
33 changes: 14 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"description": "Build a dynamic list of trusted proxies for use with Laravel Vapor",
"keywords": [
"ge-tracker",
"laravel-vapor-trusted-proxies"
"laravel-vapor-trusted-proxies",
"laravel vapor",
"laravel vapor trusted proxies",
"trusted proxy"
],
"homepage": "https://github.com/ge-tracker/laravel-vapor-trusted-proxies",
"license": "MIT",
Expand All @@ -16,13 +19,15 @@
}
],
"require": {
"php": "^7.4|^8.0",
"fideloper/proxy": "^4.2",
"illuminate/support": "^6.0|^7.0|^8.0"
"php": "^8.0",
"illuminate/http": "^9.0",
"illuminate/support": "^9.0"
},
"require-dev": {
"orchestra/testbench": "^4.0|^5.0|^6.0",
"phpunit/phpunit": "^8.0|^9.3"
"friendsofphp/php-cs-fixer": "^3.6",
"gtjamesa/php-standards": "^2.0",
"orchestra/testbench": "^7.0",
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
Expand All @@ -36,22 +41,12 @@
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"

"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
"fix": "vendor/bin/php-cs-fixer fix"
},
"config": {
"sort-packages": true
},
"prefer-stable": true,
"minimum-stability": "dev",
"extra": {
"laravel": {
"providers": [
"GeTracker\\LaravelVaporTrustedProxies\\LaravelVaporTrustedProxiesServiceProvider"
],
"aliases": {
"LaravelVaporTrustedProxies": "GeTracker\\LaravelVaporTrustedProxies\\LaravelVaporTrustedProxiesFacade"
}
}
}
"minimum-stability": "dev"
}
18 changes: 6 additions & 12 deletions src/Http/LaravelVaporTrustedProxies.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@

namespace GeTracker\LaravelVaporTrustedProxies\Http;

use Fideloper\Proxy\TrustProxies as Middleware;
use GeTracker\LaravelVaporTrustedProxies\VaporProxyResolver;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Http\Middleware\TrustProxies as Middleware;
use Illuminate\Http\Request;

class LaravelVaporTrustedProxies extends Middleware
{
private VaporProxyResolver $proxyResolver;

public function __construct(Repository $config, VaporProxyResolver $proxyResolver)
{
parent::__construct($config);

$this->proxyResolver = $proxyResolver;
public function __construct(
private VaporProxyResolver $proxyResolver
) {
}

/**
Expand All @@ -24,14 +19,13 @@ public function __construct(Repository $config, VaporProxyResolver $proxyResolve
* @param Request $request
*
* @noinspection PhpVoidFunctionResultUsedInspection
* @noinspection ReturnTypeCanBeDeclaredInspection
*/
protected function setTrustedProxyIpAddresses(Request $request)
{
$trustedIps = $this->proxies ?: $this->config->get('trustedproxy.proxies');

$this->proxies = $this->proxyResolver->resolve(
$request->headers->all(),
$trustedIps
$this->proxies
);

return parent::setTrustedProxyIpAddresses($request);
Expand Down
14 changes: 3 additions & 11 deletions src/LaravelVaporTrustedProxiesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,21 @@

namespace GeTracker\LaravelVaporTrustedProxies;

use GeTracker\LaravelVaporTrustedProxies\Http\LaravelVaporTrustedProxies;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class LaravelVaporTrustedProxiesServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{

}

/**
* Register the application services.
*/
public function register()
{
// Register a test route to return the requesting IP address
if ($this->app->runningUnitTests()) {
Route::get('/test-ip', function () {
return request()->ips();
});
Route::get('/test-ip', static fn () => request()->ips())
->middleware([LaravelVaporTrustedProxies::class]);
}
}
}
17 changes: 6 additions & 11 deletions src/VaporProxyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ class VaporProxyResolver
private const FORWARDED_FOR = 'x-forwarded-for';

private array $headers;
private ?array $proxies;
private ?array $proxies = [];

/**
* Resolve a new set of trusted proxies
*
* @param array $headers Array of HTTP server headers
* @param array|null $proxies Array of proxies defined in TrustProxies middleware
* @param array $headers Array of HTTP server headers
* @param array|string|null $proxies Array of proxies defined in TrustProxies middleware
*
* @return array|null
*/
public function resolve(array $headers, $proxies = null): ?array
public function resolve(array $headers, array|string|null $proxies = null): ?array
{
$this->headers = $headers;
$this->setProxies($proxies);
Expand Down Expand Up @@ -110,13 +110,8 @@ private function runningInVapor(): bool
/**
* @param array|string|null $proxies
*/
public function setProxies($proxies): void
public function setProxies(array|string|null $proxies): void
{
if ($proxies && is_string($proxies)) {
$this->proxies = [$proxies];
return;
}

$this->proxies = $proxies;
$this->proxies = Arr::wrap($proxies);
}
}
24 changes: 0 additions & 24 deletions tests/Http/Kernel.php

This file was deleted.

9 changes: 0 additions & 9 deletions tests/TrustProxiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace GeTracker\LaravelVaporTrustedProxies\Tests;

use GeTracker\LaravelVaporTrustedProxies\LaravelVaporTrustedProxiesServiceProvider;
use GeTracker\LaravelVaporTrustedProxies\Tests\Http\Kernel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Orchestra\Testbench\TestCase;

class TrustProxiesTest extends TestCase
Expand All @@ -15,17 +12,11 @@ protected function getPackageProviders($app)
return [LaravelVaporTrustedProxiesServiceProvider::class];
}

protected function resolveApplicationHttpKernel($app)
{
$app->singleton('Illuminate\Contracts\Http\Kernel', Kernel::class);
}

protected function setUp(): void
{
parent::setUp();

$_SERVER['VAPOR_ARTIFACT_NAME'] = 'laravel-vapor';
Config::set('trustedproxy.headers', Request::HEADER_X_FORWARDED_ALL);
}

/** @test */
Expand Down
3 changes: 2 additions & 1 deletion tests/VaporProxyResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public function stop_basic_spoof_with_no_proxies_configured(): void
'x-forwarded-for' => '1.2.3.4, 2.2.2.2, 3.3.3.3',
]);

$this->assertNull($proxies);
$this->assertIsArray($proxies);
$this->assertEmpty($proxies);
}

/** @test */
Expand Down

0 comments on commit 163d4e5

Please sign in to comment.