Skip to content

Commit

Permalink
MSI 100%, Fix typo, Remove .gitkeep, Add functions to "use" (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jun 30, 2023
1 parent 5a23c43 commit de79472
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
Empty file removed src/.gitkeep
Empty file.
30 changes: 22 additions & 8 deletions src/TrustedHostsNetworkResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
use Yiisoft\Validator\Rule\Ip;
use Yiisoft\Validator\ValidatorInterface;

use function count;
use function in_array;
use function is_array;
use function is_callable;
use function is_string;

/**
* 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 @@ -399,9 +405,9 @@ private function assertNonEmpty(string|array $value, string $name, bool $inRunti
return;
}

$expeptionClassName = $inRuntime ? RuntimeException::class : InvalidArgumentException::class;
$exceptionClassName = $inRuntime ? RuntimeException::class : InvalidArgumentException::class;

throw new $expeptionClassName("$name can't be empty.");
throw new $exceptionClassName("$name can't be empty.");
}

/**
Expand All @@ -421,9 +427,9 @@ private function assertExactKeysForArray(

$allowedKeysStr = implode('", "', $allowedKeys);
$message = "Invalid array keys for $name. The allowed and required keys are: \"$allowedKeysStr\".";
$expeptionClassName = $inRuntime ? RuntimeException::class : InvalidArgumentException::class;
$exceptionClassName = $inRuntime ? RuntimeException::class : InvalidArgumentException::class;

throw new $expeptionClassName($message);
throw new $exceptionClassName($message);
}

/**
Expand All @@ -436,9 +442,9 @@ private function assertIsNonEmptyString(mixed $value, string $name, bool $inRunt
return;
}

$expeptionClassName = $inRuntime ? RuntimeException::class : InvalidArgumentException::class;
$exceptionClassName = $inRuntime ? RuntimeException::class : InvalidArgumentException::class;

throw new $expeptionClassName("$name must be non-empty string.");
throw new $exceptionClassName("$name must be non-empty string.");
}

/**
Expand All @@ -455,9 +461,9 @@ private function assertIsAllowedProtocol(mixed $value, string $name, bool $inRun

$allowedProtocolsStr = implode('", "', self::ALLOWED_PROTOCOLS);
$message = "$name must be a valid protocol. Allowed values are: \"$allowedProtocolsStr\" (case-sensitive).";
$expeptionClassName = $inRuntime ? RuntimeException::class : InvalidArgumentException::class;
$exceptionClassName = $inRuntime ? RuntimeException::class : InvalidArgumentException::class;

throw new $expeptionClassName($message);
throw new $exceptionClassName($message);
}

private function handleNotTrusted(
Expand Down Expand Up @@ -521,6 +527,9 @@ private function normalizeHeaderName(string $headerName): string
* @psalm-param non-empty-string $remoteAddr
*
* @psalm-return array{0: ForwardedHeaderGroup, 1: list<RawConnectionChainItem>}
*
* @throws InvalidConnectionChainItemException
* @throws RfcProxyParseException
*/
private function getConnectionChainItems(string $remoteAddr, ServerRequestInterface $request): array
{
Expand Down Expand Up @@ -609,6 +618,9 @@ private function getProtocolFromSeparateHeader(
* @psalm-return list<RawConnectionChainItem>
*
* @link https://tools.ietf.org/html/rfc7239
*
* @throws RfcProxyParseException
* @throws InvalidConnectionChainItemException
*/
private function parseProxiesFromRfcHeader(array $proxyItems): array
{
Expand Down Expand Up @@ -701,6 +713,8 @@ private function parseProxiesFromRfcHeader(array $proxyItems): array

/**
* @psalm-return RawConnectionChainItem
*
* @throws InvalidConnectionChainItemException
*/
private function getConnectionChainItem(
?string $ip = null,
Expand Down
Empty file removed tests/.gitkeep
Empty file.
33 changes: 33 additions & 0 deletions tests/TrustedHostsNetworkResolver/ConfigurationExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ public function dataWithForwardedHeaderGroupsException(): array
],
'Header name for "host" must be non-empty string.',
],
'ip: value is empty string' => [
[
TrustedHostsNetworkResolver::FORWARDED_HEADER_RFC,
[
'ip' => '',
'protocol' => 'y-forwarded-proto',
'host' => 'y-forwarded-host',
'port' => 'y-forwarded-port',
],
TrustedHostsNetworkResolver::FORWARDED_HEADER_GROUP_X_PREFIX,
],
'Header name for "ip" must be non-empty string.',
],
'protocol: value is neither a string nor an array' => [
[
TrustedHostsNetworkResolver::FORWARDED_HEADER_RFC,
Expand Down Expand Up @@ -243,6 +256,22 @@ public function dataWithForwardedHeaderGroupsException(): array
],
'Protocol header resolving must be specified either via an associative array or a callable.',
],
'protocol, array, resolving: name equal to exists function' => [
[
TrustedHostsNetworkResolver::FORWARDED_HEADER_RFC,
[
'ip' => 'y-forwarded-for',
'protocol' => [
'\Yiisoft\ProxyMiddleware\Tests\TrustedHostsNetworkResolver\testCallableFunction',
'test',
],
'host' => 'y-forwarded-host',
'port' => 'y-forwarded-port',
],
TrustedHostsNetworkResolver::FORWARDED_HEADER_GROUP_X_PREFIX,
],
'Protocol header resolving must be specified either via an associative array or a callable.',
],
'protocol, array, resolving: array, empty' => [
[
TrustedHostsNetworkResolver::FORWARDED_HEADER_RFC,
Expand Down Expand Up @@ -388,3 +417,7 @@ public function testWithConnectionChainItemsAttributeException(): void
$middleware->withConnectionChainItemsAttribute('');
}
}

function testCallableFunction(): void
{
}

0 comments on commit de79472

Please sign in to comment.