Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/rector-cs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Rector + PHP CS Fixer

on:
pull_request:
paths:
- '.github/workflows/rector-cs.yml'
- 'src/**'
- 'tests/**'
- 'rector.php'
- '.php-cs-fixer.dist.php'
- 'composer.json'

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
rector-cs:
permissions:
contents: write # Required to commit formatting fixes back to the PR
uses: yiisoft/actions/.github/workflows/rector-cs.yml@master
with:
php: '8.0'
24 changes: 0 additions & 24 deletions .github/workflows/rector.yml

This file was deleted.

22 changes: 22 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Finder;
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
use Yiisoft\CodeStyle\ConfigBuilder;

$finder = (new Finder())->in([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

return ConfigBuilder::build()
->setCacheFile(__DIR__ . '/runtime/.php-cs-fixer.cache')
->setRiskyAllowed(true)
->setParallelConfig(ParallelConfigFactory::detect())
->setRules([
'@Yiisoft/Core' => true,
'@Yiisoft/Core:risky' => true,
])
->setFinder($finder);
85 changes: 0 additions & 85 deletions .styleci.yml

This file was deleted.

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.0.3 under development

- Chg #44: Change PHP constraint in `composer.json` to `8.0 - 8.4` (@vjik)
- Enh #55: Explicitly import constants in "use" section (@vjik)

## 1.0.2 August 06, 2024

Expand Down
18 changes: 15 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
"yiisoft/network-utilities": "^1.1"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.9.1",
"friendsofphp/php-cs-fixer": "^3.95",
"httpsoft/http-message": "^1.1.6",
"maglnet/composer-require-checker": "^4.4",
"phpunit/phpunit": "^9.6.22",
"rector/rector": "^2.0.10",
"rector/rector": "^2.5.8",
"roave/infection-static-analysis-plugin": "^1.25",
"spatie/phpunit-watcher": "^1.23.6",
"vimeo/psalm": "^4.30 || ^5.26.1 || ^6.9.1",
"yiisoft/code-style": "^1.1",
"yiisoft/test-support": "^3.0.2"
},
"autoload": {
Expand All @@ -61,10 +63,20 @@
"sort-packages": true,
"allow-plugins": {
"infection/extension-installer": true,
"composer/package-versions-deprecated": true
"composer/package-versions-deprecated": true,
"bamarni/composer-bin-plugin": true
}
},
"extra": {
"bamarni-bin": {
"bin-links": true,
"target-directory": "tools",
"forward-command": true
}
},
"scripts": {
"cs-fix": "php-cs-fixer fix",
"rector": "rector",
"test": "phpunit --testdox --no-interaction",
"test-watch": "phpunit-watcher watch"
}
Expand Down
6 changes: 3 additions & 3 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Yiisoft\CodeStyle\Rector\SetList;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withPhpSets(php80: true)
->withRules([
InlineConstructorDefaultToPropertyRector::class,
->withSets([
SetList::YII_CORE,
])
->withSkip([
ClosureToArrowFunctionRector::class,
Expand Down
2 changes: 2 additions & 0 deletions runtime/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
4 changes: 1 addition & 3 deletions src/Exception/InvalidConnectionChainItemException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@
* Used by {@see TrustedHostsNetworkResolver}. Thrown when resolved data of connection chain item - either IP, protocol,
* host or port is invalid.
*/
final class InvalidConnectionChainItemException extends Exception
{
}
final class InvalidConnectionChainItemException extends Exception {}
4 changes: 1 addition & 3 deletions src/Exception/RfcProxyParseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@
*
* @link https://datatracker.ietf.org/doc/html/rfc7239
*/
final class RfcProxyParseException extends Exception
{
}
final class RfcProxyParseException extends Exception {}
23 changes: 12 additions & 11 deletions src/TrustedHostsNetworkResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
use function is_callable;
use function is_string;

use const FILTER_FLAG_HOSTNAME;
use const FILTER_VALIDATE_DOMAIN;

/**
* Scans the entire connection chain and resolves the data from forwarded headers taking into account trusted IPs.
* Additionally, all items' structure is thoroughly validated because headers' data can't be trusted. The following data
Expand Down Expand Up @@ -146,9 +149,7 @@ class TrustedHostsNetworkResolver implements MiddlewareInterface
*/
private ?string $connectionChainItemsAttribute = null;

public function __construct()
{
}
public function __construct() {}

/**
* Returns a new instance with changed list of connection chain trusted IPs
Expand Down Expand Up @@ -229,8 +230,8 @@ public function withForwardedHeaderGroups(array $headerGroups): self
$this->assertIsAllowedProtocol($value, 'Value in mapping for protocol header');
}
} elseif (!is_callable($headerGroup['protocol'][1])) {
$message = 'Protocol header resolving must be specified either via an associative array or a ' .
'callable.';
$message = 'Protocol header resolving must be specified either via an associative array or a '
. 'callable.';

throw new InvalidArgumentException($message);
}
Expand All @@ -244,8 +245,8 @@ public function withForwardedHeaderGroups(array $headerGroups): self

$validatedHeaderGroups[] = $validatedHeaderGroup;
} else {
$message = 'Forwarded header group must be either an associative array or ' .
'TrustedHostsNetworkResolver::FORWARDED_HEADER_GROUP_RFC constant.';
$message = 'Forwarded header group must be either an associative array or '
. 'TrustedHostsNetworkResolver::FORWARDED_HEADER_GROUP_RFC constant.';

throw new InvalidArgumentException($message);
}
Expand Down Expand Up @@ -641,8 +642,8 @@ private function parseProxiesFromRfcHeader(array $proxyItems): array
foreach ($directiveMap as $name => $value) {
if (!in_array($name, self::ALLOWED_RFC_HEADER_DIRECTIVES)) {
$allowedDirectivesStr = implode('", "', self::ALLOWED_RFC_HEADER_DIRECTIVES);
$message = "\"$name\" is not a valid directive. Allowed values are: \"$allowedDirectivesStr\" " .
'(case-insensitive).';
$message = "\"$name\" is not a valid directive. Allowed values are: \"$allowedDirectivesStr\" "
. '(case-insensitive).';

throw new RfcProxyParseException($message);
}
Expand Down Expand Up @@ -731,8 +732,8 @@ private function getConnectionChainItem(

if ($protocol !== null && $validateProtocol && !$this->isProtocol($protocol)) {
$allowedProtocolsStr = implode('", "', self::ALLOWED_PROTOCOLS);
$message = "\"$protocol\" protocol is not allowed. Allowed values are: \"$allowedProtocolsStr\" " .
'(case-sensitive).';
$message = "\"$protocol\" protocol is not allowed. Allowed values are: \"$allowedProtocolsStr\" "
. '(case-sensitive).';

throw new InvalidConnectionChainItemException($message);
}
Expand Down
4 changes: 1 addition & 3 deletions tests/Support/MockRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ final class MockRequestHandler implements RequestHandlerInterface
public ?ServerRequestInterface $processedRequest = null;
private ?Throwable $handleException = null;

public function __construct(private int $responseStatusCode = Status::OK)
{
}
public function __construct(private int $responseStatusCode = Status::OK) {}

public function handle(ServerRequestInterface $request): ResponseInterface
{
Expand Down
6 changes: 3 additions & 3 deletions tests/TrustedHeaderProtocolResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function schemeDataProvider(): array
],
[
'x-forwarded-proto-1' => ['http' => 'http'],
'x-forwarded-proto-2' => static fn () => null,
'x-forwarded-proto-2' => static fn() => null,
'x-forwarded-proto-3' => ['https' => 'https'],
'x-forwarded-proto-4' => ['http' => 'http'],
],
Expand Down Expand Up @@ -200,7 +200,7 @@ public function schemeCallableFailureDataProvider(): array
'empty-array' => [[]],
'empty-string' => [['']],
'object' => [[new stdClass()]],
'callable' => [[static fn () => 'https']],
'callable' => [[static fn() => 'https']],
];
}

Expand All @@ -214,7 +214,7 @@ public function testCallableSchemeFailure(array $scheme): void
$request->method('getHeader')->willReturn($scheme);

$middleware = (new TrustedHeaderProtocolResolver())
->withAddedProtocolHeader('x-forwarded-proto', static fn () => $scheme)
->withAddedProtocolHeader('x-forwarded-proto', static fn() => $scheme)
;

$this->expectException(RuntimeException::class);
Expand Down
Loading