diff --git a/.gitattributes b/.gitattributes index 4a0e9a1..7c543b7 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,14 +1,9 @@ .editorconfig export-ignore .gitattributes export-ignore -.github/ export-ignore .gitignore export-ignore .php_cs export-ignore .scrutinizer.yml export-ignore -.styleci.yml export-ignore .travis.yml export-ignore -phpspec.yml.ci export-ignore -phpspec.yml.dist export-ignore -phpunit.xml.dist export-ignore -spec/ export-ignore tests/ export-ignore phpstan.neon export-ignore +phpunit.xml.dist export-ignore diff --git a/.gitignore b/.gitignore index aa5d9d7..227f2f5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /phpunit.xml /vendor/ .php_cs.cache +.phpunit.result.cache diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 0933af7..f68cd37 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -8,3 +8,10 @@ filter: tools: external_code_coverage: timeout: 600 + +build: + nodes: + analysis: + tests: + override: + - php-scrutinizer-run diff --git a/.travis.yml b/.travis.yml index 1a20f31..a50bda1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,18 @@ +dist: xenial +os: linux language: php + +# avoid double Travis build when the PR is created on upstream +if: | + type = pull_request OR \ + branch = master + cache: directories: - - $HOME/.composer/cache + - $HOME/.composer/cache/files php: - - 7.1 - 7.2 - 7.3 - 7.4 @@ -16,17 +23,17 @@ matrix: allow_failures: - php: nightly include: - - php: 7.1 + - php: 7.2 env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" - php: 7.2 env: CS_FIXER=run COVERAGE=run before_install: - if [ -n "$GH_TOKEN" ]; then composer config github-oauth.github.com ${GH_TOKEN}; fi; - - travis_retry composer self-update install: - - travis_retry composer update ${COMPOSER_FLAGS} --no-progress --no-interaction + - composer update ${COMPOSER_FLAGS} --no-progress --no-interaction -o --no-suggest + - php vendor/bin/simple-phpunit install before_script: - if [ "$COVERAGE" = "run" ]; then PHPUNIT_FLAGS="--coverage-clover coverage.clover"; fi; @@ -34,7 +41,7 @@ before_script: script: - php vendor/bin/simple-phpunit -v $PHPUNIT_FLAGS - if [ "$CS_FIXER" = "run" ]; then php vendor/bin/php-cs-fixer fix --verbose --dry-run ; fi; - - if [ "$CS_FIXER" = "run" ]; then php vendor/bin/phpstan analyse src tests --no-progress --level 7 ; fi; + - if [ "$CS_FIXER" = "run" ]; then php vendor/bin/phpstan analyse ; fi; after_success: diff --git a/composer.json b/composer.json index ec71425..a8cc648 100644 --- a/composer.json +++ b/composer.json @@ -1,52 +1,53 @@ { - "name": "j0k3r/httplug-ssrf-plugin", - "description": "Server-Side Request Forgery (SSRF) protection plugin for HTTPlug", - "type": "library", - "license": "MIT", - "keywords": ["ssrf", "security", "http", "httplug", "plugin"], - "homepage": "https://github.com/j0k3r/httplug-ssrf-plugin", - "authors": [ - { - "name": "Jeremy Benoist", - "email": "jeremy.benoist@gmail.com" + "name": "j0k3r/httplug-ssrf-plugin", + "description": "Server-Side Request Forgery (SSRF) protection plugin for HTTPlug", + "type": "library", + "license": "MIT", + "keywords": ["ssrf", "security", "http", "httplug", "plugin"], + "homepage": "https://github.com/j0k3r/httplug-ssrf-plugin", + "authors": [ + { + "name": "Jeremy Benoist", + "email": "jeremy.benoist@gmail.com" + }, + { + "name": "aaa2000", + "email": "adev2000@gmail.com" + }, + { + "name": "Jack W", + "email": "jack@fin1te.net", + "role": "Developer (SafeCurl original version)" + } + ], + "require": { + "php": ">=7.2.9", + "php-http/client-common": "^2.0", + "php-http/message": "^1.7", + "php-http/message-factory": "^1.0.2", + "php-http/discovery": "^1.5" }, - { - "name": "aaa2000", - "email": "adev2000@gmail.com" + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.14", + "guzzlehttp/psr7": "^1.0", + "php-http/guzzle6-adapter": "^2.0", + "php-http/mock-client": "^1.0", + "phpstan/extension-installer": "^1.0", + "phpstan/phpstan": "^0.12", + "phpstan/phpstan-phpunit": "^0.12", + "symfony/phpunit-bridge": "~5.0" }, - { - "name": "Jack W", - "email": "jack@fin1te.net", - "role": "Developer (SafeCurl original version)" - } - ], - "require": { - "php": "^7.1", - "php-http/client-common": "^2.0", - "php-http/message": "^1.7", - "php-http/message-factory": "^1.0.2", - "php-http/discovery": "^1.5" - }, - "require-dev": { - "symfony/phpunit-bridge": "~3.4.19|~4.0", - "friendsofphp/php-cs-fixer": "^2.14", - "php-http/mock-client": "^1.0", - "php-http/guzzle6-adapter": "^2.0", - "guzzlehttp/psr7": "^1.0", - "phpstan/phpstan": "^0.11", - "phpstan/phpstan-phpunit": "^0.11" - }, - "autoload": { - "psr-4": { - "Graby\\HttpClient\\Plugin\\ServerSideRequestForgeryProtection\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "Tests\\Graby\\HttpClient\\Plugin\\ServerSideRequestForgeryProtection\\": "tests/" + "autoload": { + "psr-4": { + "Graby\\HttpClient\\Plugin\\ServerSideRequestForgeryProtection\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\Graby\\HttpClient\\Plugin\\ServerSideRequestForgeryProtection\\": "tests/" + } + }, + "config": { + "sort-packages": true } - }, - "scripts": { - "test": "vendor/bin/simple-phpunit" - } } diff --git a/phpstan.neon b/phpstan.neon index 597d383..da04ffb 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,10 +1,16 @@ -includes: - - vendor/phpstan/phpstan-phpunit/extension.neon - - vendor/phpstan/phpstan-phpunit/rules.neon - parameters: + level: 7 + paths: + - src + - tests + # https://github.com/phpstan/phpstan/issues/694#issuecomment-350724288 autoload_files: - - vendor/bin/.phpunit/phpunit-7.5-0/vendor/autoload.php + - vendor/bin/.phpunit/phpunit-8.3-0/vendor/autoload.php inferPrivatePropertyTypeFromConstructor: true + checkMissingIterableValueType: false + + ignoreErrors: + # Fix PHPUnit\Framework\TestCase::expectException() custom typehint + - '#class\-string\#' diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 47e7f80..4372bdf 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,12 +9,8 @@ stopOnFailure="false" bootstrap="vendor/autoload.php" > - - - - - + ./tests diff --git a/src/Exception/InvalidOptionException.php b/src/Exception/InvalidOptionException.php index 0104261..2e8f5f1 100644 --- a/src/Exception/InvalidOptionException.php +++ b/src/Exception/InvalidOptionException.php @@ -2,14 +2,14 @@ namespace Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception; -class InvalidOptionException extends \Exception implements SsrfException +final class InvalidOptionException extends \Exception implements SsrfException { /** * @param string $type * * @return static */ - public static function invalidType($type, array $expectedTypes) + public static function invalidType($type, array $expectedTypes): self { $expectedTypesList = '"' . implode('", "', \array_slice($expectedTypes, 0, -1)) . '" or "' . $expectedTypes[\count($expectedTypes) - 1] . '"'; @@ -22,18 +22,13 @@ public static function invalidType($type, array $expectedTypes) * * @return static */ - public static function invalidListName($listName) + public static function invalidListName($listName): self { return new static(sprintf('Provided list "%s" must be "whitelist" or "blacklist"', $listName)); } - public static function emptyValues() + public static function emptyValues(): self { return new static('Provided values cannot be empty'); } - - public static function invalidValues($values) - { - return new static(sprintf('Provided values must be an array, "%s" given', \gettype($values))); - } } diff --git a/src/Options.php b/src/Options.php index 22db59e..0016d0d 100644 --- a/src/Options.php +++ b/src/Options.php @@ -6,6 +6,7 @@ class Options { + /** @var array */ private static $availableType = [ 'ip', 'port', @@ -64,10 +65,8 @@ class Options /** * Get send credentials option. - * - * @return bool */ - public function getSendCredentials() + public function getSendCredentials(): bool { return $this->sendCredentials; } @@ -75,10 +74,8 @@ public function getSendCredentials() /** * Enable sending of credenitals * This is potentially a security risk. - * - * @return Options */ - public function enableSendCredentials() + public function enableSendCredentials(): self { $this->sendCredentials = true; @@ -87,10 +84,8 @@ public function enableSendCredentials() /** * Disable sending of credentials. - * - * @return Options */ - public function disableSendCredentials() + public function disableSendCredentials(): self { $this->sendCredentials = false; @@ -99,20 +94,16 @@ public function disableSendCredentials() /** * Get pin DNS option. - * - * @return bool */ - public function getPinDns() + public function getPinDns(): bool { return $this->pinDns; } /** * Enable DNS pinning. - * - * @return Options */ - public function enablePinDns() + public function enablePinDns(): self { $this->pinDns = true; @@ -121,10 +112,8 @@ public function enablePinDns() /** * Disable DNS pinning. - * - * @return Options */ - public function disablePinDns() + public function disablePinDns(): self { $this->pinDns = false; @@ -135,14 +124,10 @@ public function disablePinDns() * Checks if a specific value is in a list. * * @param string $listName Accepts 'whitelist' or 'blacklist - * @param string $type - * @param string $value * * @throws InvalidOptionException - * - * @return bool */ - public function isInList($listName, $type, $value) + public function isInList(string $listName, string $type, string $value): bool { $this->checkListByName($listName); $value = (string) $value; @@ -172,14 +157,11 @@ public function isInList($listName, $type, $value) /** * Returns a specific list. * - * @param string $listName Accepts 'whitelist' or 'blacklist - * @param string|null $type + * @param string $listName Accepts 'whitelist' or 'blacklist * * @throws InvalidOptionException - * - * @return array */ - public function getList($listName, $type = null) + public function getList(string $listName, string $type = null): array { $this->checkListByName($listName); @@ -197,22 +179,14 @@ public function getList($listName, $type = null) /** * Sets a list, the values must be passed as an array. * - * @param string $listName Accepts 'whitelist' or 'blacklist - * @param array $values - * @param string|null $type + * @param string $listName Accepts 'whitelist' or 'blacklist * * @throws InvalidOptionException - * - * @return Options */ - public function setList($listName, $values, $type = null) + public function setList(string $listName, array $values, string $type = null): self { $this->checkListByName($listName); - if (!\is_array($values)) { - throw InvalidOptionException::invalidValues($values); - } - if (null !== $type) { if (!\array_key_exists($type, $this->lists[$listName])) { throw InvalidOptionException::invalidType($type, self::$availableType); @@ -238,14 +212,11 @@ public function setList($listName, $values, $type = null) * Adds a value/values to a specific list. * * @param string $listName Accepts 'whitelist' or 'blacklist - * @param string $type * @param array|string $values * * @throws InvalidOptionException - * - * @return Options */ - public function addToList($listName, $type, $values) + public function addToList(string $listName, string $type, $values = null): self { $this->checkListByName($listName); @@ -273,14 +244,11 @@ public function addToList($listName, $type, $values) * Removes a value/values from a specific list. * * @param string $listName Accepts 'whitelist' or 'blacklist - * @param string $type * @param array|string $values * * @throws InvalidOptionException - * - * @return Options */ - public function removeFromList($listName, $type, $values) + public function removeFromList(string $listName, string $type, $values = null): self { $this->checkListByName($listName); @@ -305,7 +273,7 @@ public function removeFromList($listName, $type, $values) * * @throws InvalidOptionException */ - private function checkListByName($listName) + private function checkListByName($listName): void { if (!isset($this->lists[$listName])) { throw InvalidOptionException::invalidListName($listName); diff --git a/tests/Exception/InvalidOptionExceptionTest.php b/tests/Exception/InvalidOptionExceptionTest.php index 7b612a3..a3280a3 100644 --- a/tests/Exception/InvalidOptionExceptionTest.php +++ b/tests/Exception/InvalidOptionExceptionTest.php @@ -6,7 +6,7 @@ class InvalidOptionExceptionTest extends \PHPUnit\Framework\TestCase { - public function testInvalidType() + public function testInvalidType(): void { $exception = InvalidOptionException::invalidType('ippp', ['ip', 'port', 'domain', 'scheme']); diff --git a/tests/OptionsTest.php b/tests/OptionsTest.php index a438386..6062bc7 100644 --- a/tests/OptionsTest.php +++ b/tests/OptionsTest.php @@ -2,18 +2,20 @@ namespace Tests\Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection; +use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException; use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Options; class OptionsTest extends \PHPUnit\Framework\TestCase { + /** @var Options */ private $options; - protected function setUp() + protected function setUp(): void { $this->options = new Options(); } - public function testSendCredentials() + public function testSendCredentials(): void { $this->assertFalse($this->options->getSendCredentials()); @@ -26,7 +28,7 @@ public function testSendCredentials() $this->assertFalse($this->options->getSendCredentials()); } - public function testPinDns() + public function testPinDns(): void { $this->assertFalse($this->options->getPinDns()); @@ -39,7 +41,7 @@ public function testPinDns() $this->assertFalse($this->options->getPinDns()); } - public function testInListEmptyValue() + public function testInListEmptyValue(): void { $this->assertTrue($this->options->isInList('whitelist', 'ip', '')); $this->assertFalse($this->options->isInList('whitelist', 'port', '')); @@ -52,7 +54,7 @@ public function testInListEmptyValue() $this->assertFalse($this->options->isInList('blacklist', 'scheme', '')); } - public function testInListDomainRegex() + public function testInListDomainRegex(): void { $this->options->addToList('whitelist', 'domain', '(.*)\.fin1te\.net'); @@ -62,25 +64,23 @@ public function testInListDomainRegex() $this->assertTrue($this->options->isInList('whitelist', 'domain', 'www.fin1te.net')); } - /** - * @expectedException \Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException - * @expectedExceptionMessage Provided list "noo" must be "whitelist" or "blacklist" - */ - public function testInListBadList() + public function testInListBadList(): void { + $this->expectException(InvalidOptionException::class); + $this->expectExceptionMessage('Provided list "noo" must be "whitelist" or "blacklist"'); + $this->options->isInList('noo', 'domain', ''); } - /** - * @expectedException \Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException - * @expectedExceptionMessage Provided type "noo" must be "ip", "port", "domain" or "scheme" - */ - public function testInListBadType() + public function testInListBadType(): void { + $this->expectException(InvalidOptionException::class); + $this->expectExceptionMessage('Provided type "noo" must be "ip", "port", "domain" or "scheme"'); + $this->options->isInList('whitelist', 'noo', ''); } - public function testGetListWithoutType() + public function testGetListWithoutType(): void { $list = $this->options->getList('whitelist'); @@ -99,7 +99,7 @@ public function testGetListWithoutType() $this->assertArrayHasKey('scheme', $list); } - public function testGetListWhitelistWithType() + public function testGetListWhitelistWithType(): void { $this->options->addToList('whitelist', 'ip', '0.0.0.0'); $list = $this->options->getList('whitelist', 'ip'); @@ -128,7 +128,7 @@ public function testGetListWhitelistWithType() $this->assertSame('https', $list[1]); } - public function testGetListBlacklistWithType() + public function testGetListBlacklistWithType(): void { $list = $this->options->getList('blacklist', 'ip'); @@ -154,25 +154,23 @@ public function testGetListBlacklistWithType() $this->assertSame('ftp', $list[0]); } - /** - * @expectedException \Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException - * @expectedExceptionMessage Provided list "noo" must be "whitelist" or "blacklist" - */ - public function testGetListBadList() + public function testGetListBadList(): void { + $this->expectException(InvalidOptionException::class); + $this->expectExceptionMessage('Provided list "noo" must be "whitelist" or "blacklist"'); + $this->options->getList('noo'); } - /** - * @expectedException \Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException - * @expectedExceptionMessage Provided type "noo" must be "ip", "port", "domain" or "scheme" - */ - public function testGetListBadType() + public function testGetListBadType(): void { + $this->expectException(InvalidOptionException::class); + $this->expectExceptionMessage('Provided type "noo" must be "ip", "port", "domain" or "scheme"'); + $this->options->getList('whitelist', 'noo'); } - public function testSetList() + public function testSetList(): void { $this->options->setList('whitelist', ['ip' => ['0.0.0.0']]); @@ -183,97 +181,79 @@ public function testSetList() $this->assertSame([22], $this->options->getList('blacklist', 'port')); } - /** - * @expectedException \Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException - * @expectedExceptionMessage Provided list "noo" must be "whitelist" or "blacklist" - */ - public function testSetListBadList() + public function testSetListBadList(): void { + $this->expectException(InvalidOptionException::class); + $this->expectExceptionMessage('Provided list "noo" must be "whitelist" or "blacklist"'); + $this->options->setList('noo', []); } - /** - * @expectedException \Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException - * @expectedExceptionMessage Provided values must be an array, "integer" given - */ - public function testSetListBadValue() + public function testSetListBadType(): void { - $this->options->setList('whitelist', 12); - } + $this->expectException(InvalidOptionException::class); + $this->expectExceptionMessage('Provided type "noo" must be "ip", "port", "domain" or "scheme"'); - /** - * @expectedException \Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException - * @expectedExceptionMessage Provided type "noo" must be "ip", "port", "domain" or "scheme" - */ - public function testSetListBadType() - { $this->options->setList('whitelist', [], 'noo'); } - /** - * @expectedException \Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException - * @expectedExceptionMessage Provided type "noo" must be "ip", "port", "domain" or "scheme" - */ - public function testSetListBadTypeValue() + public function testSetListBadTypeValue(): void { + $this->expectException(InvalidOptionException::class); + $this->expectExceptionMessage('Provided type "noo" must be "ip", "port", "domain" or "scheme"'); + $this->options->setList('whitelist', ['noo' => 'oops']); } - /** - * @expectedException \Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException - * @expectedExceptionMessage Provided list "noo" must be "whitelist" or "blacklist" - */ - public function testAddToListBadList() + public function testAddToListBadList(): void { + $this->expectException(InvalidOptionException::class); + $this->expectExceptionMessage('Provided list "noo" must be "whitelist" or "blacklist"'); + $this->options->addToList('noo', 'noo', 'noo'); } - /** - * @expectedException \Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException - * @expectedExceptionMessage Provided type "noo" must be "ip", "port", "domain" or "scheme" - */ - public function testAddToListBadType() + public function testAddToListBadType(): void { + $this->expectException(InvalidOptionException::class); + $this->expectExceptionMessage('Provided type "noo" must be "ip", "port", "domain" or "scheme"'); + $this->options->addToList('whitelist', 'noo', 'noo'); } - /** - * @expectedException \Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException - * @expectedExceptionMessage Provided values cannot be empty - */ - public function testAddToListBadValue() + public function testAddToListBadValue(): void { + $this->expectException(InvalidOptionException::class); + $this->expectExceptionMessage('Provided values cannot be empty'); + $this->options->addToList('whitelist', 'ip', null); } - /** - * @expectedException \Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException - * @expectedExceptionMessage Provided list "noo" must be "whitelist" or "blacklist" - */ - public function testRemoveFromListBadList() + public function testRemoveFromListBadList(): void { + $this->expectException(InvalidOptionException::class); + $this->expectExceptionMessage('Provided list "noo" must be "whitelist" or "blacklist"'); + $this->options->removeFromList('noo', 'noo', 'noo'); } - /** - * @expectedException \Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException - * @expectedExceptionMessage Provided type "noo" must be "ip", "port", "domain" or "scheme" - */ - public function testRemoveFromListBadType() + public function testRemoveFromListBadType(): void { + $this->expectException(InvalidOptionException::class); + $this->expectExceptionMessage('Provided type "noo" must be "ip", "port", "domain" or "scheme"'); + $this->options->removeFromList('whitelist', 'noo', 'noo'); } - /** - * @expectedException \Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException - * @expectedExceptionMessage Provided values cannot be empty - */ - public function testRemoveFromListBadValue() + public function testRemoveFromListBadValue(): void { + $this->expectException(InvalidOptionException::class); + $this->expectExceptionMessage('Provided values cannot be empty'); + $this->options->removeFromList('whitelist', 'ip', null); } - public function testRemoveFromList() + public function testRemoveFromList(): void { // remove not an array $this->options->addToList('blacklist', 'port', '8080'); diff --git a/tests/ServerSideRequestForgeryProtectionPluginTest.php b/tests/ServerSideRequestForgeryProtectionPluginTest.php index 949528a..a6c0216 100644 --- a/tests/ServerSideRequestForgeryProtectionPluginTest.php +++ b/tests/ServerSideRequestForgeryProtectionPluginTest.php @@ -13,7 +13,7 @@ class ServerSideRequestForgeryProtectionPluginTest extends \PHPUnit\Framework\TestCase { - public function testGET() + public function testGet(): void { $mockClient = new Client(); $mockClient->addResponse(new Response(200)); @@ -25,7 +25,7 @@ public function testGET() $this->assertSame(200, $response->getStatusCode()); } - public function dataForBlockedUrl() + public function dataForBlockedUrl(): array { return [ ['http://0.0.0.0:123', 'Provided port "123" doesn\'t match whitelisted values: 80, 443, 8080'], @@ -45,7 +45,7 @@ public function dataForBlockedUrl() /** * @dataProvider dataForBlockedUrl */ - public function testBlockedUrl($url, $message) + public function testBlockedUrl(string $url, string $message): void { $this->expectException(RequestException::class); $this->expectExceptionMessage($message); @@ -57,7 +57,7 @@ public function testBlockedUrl($url, $message) $client->sendRequest(new Request('GET', $url)); } - public function dataForBlockedUrlByOptions() + public function dataForBlockedUrlByOptions(): array { return [ ['http://login:password@google.fr', 'Credentials passed in but "sendCredentials" is set to false'], @@ -68,7 +68,7 @@ public function dataForBlockedUrlByOptions() /** * @dataProvider dataForBlockedUrlByOptions */ - public function testBlockedUrlByOptions($url, $message) + public function testBlockedUrlByOptions(string $url, string $message): void { $this->expectException(RequestException::class); $this->expectExceptionMessage($message); @@ -85,7 +85,7 @@ public function testBlockedUrlByOptions($url, $message) $client->sendRequest(new Request('GET', $url)); } - public function testWithPinDnsEnabled() + public function testWithPinDnsEnabled(): void { $options = new Options(); $options->enablePinDns(); @@ -99,7 +99,7 @@ public function testWithPinDnsEnabled() $this->assertNotEmpty($response); } - public function testWithFollowLocationLeadingToABlockedUrl() + public function testWithFollowLocationLeadingToABlockedUrl(): void { $this->expectException(RequestException::class); $this->expectExceptionMessage('Provided port "123" doesn\'t match whitelisted values: 80, 443, 8080'); diff --git a/tests/UrlTest.php b/tests/UrlTest.php index 61135b1..e419423 100644 --- a/tests/UrlTest.php +++ b/tests/UrlTest.php @@ -12,10 +12,10 @@ class UrlTest extends \PHPUnit\Framework\TestCase { - public function dataForValidate() + public function dataForValidate(): array { return [ - [null, InvalidURLException::class, 'Provided URL "" cannot be empty'], + ['', InvalidURLException::class, 'Provided URL "" cannot be empty'], ['http://user@:80', InvalidURLException::class, 'Error parsing URL "http://user@:80"'], ['http:///example.com/', InvalidURLException::class, 'Error parsing URL "http:///example.com/"'], ['http://:80', InvalidURLException::class, 'Error parsing URL "http://:80"'], @@ -30,7 +30,7 @@ public function dataForValidate() /** * @dataProvider dataForValidate */ - public function testValidateUrl($url, $exception, $message) + public function testValidateUrl(string $url, string $exception, string $message): void { $this->expectException($exception); $this->expectExceptionMessage($message); @@ -38,7 +38,7 @@ public function testValidateUrl($url, $exception, $message) Url::validateUrl($url, new Options()); } - public function testValidateScheme() + public function testValidateScheme(): void { $this->expectException(InvalidSchemeException::class); $this->expectExceptionMessage('Provided scheme "http" matches a blacklisted value'); @@ -49,7 +49,7 @@ public function testValidateScheme() Url::validateUrl('http://www.fin1te.net', $options); } - public function testValidatePort() + public function testValidatePort(): void { $this->expectException(InvalidPortException::class); $this->expectExceptionMessage('Provided port "8080" matches a blacklisted value'); @@ -60,7 +60,7 @@ public function testValidatePort() Url::validateUrl('http://www.fin1te.net:8080', $options); } - public function testValidateHostBlacklist() + public function testValidateHostBlacklist(): void { $this->expectException(InvalidDomainException::class); $this->expectExceptionMessage('Provided host "www.fin1te.net" matches a blacklisted value'); @@ -71,7 +71,7 @@ public function testValidateHostBlacklist() Url::validateUrl('http://www.fin1te.net', $options); } - public function testValidateHostWhitelist() + public function testValidateHostWhitelist(): void { $this->expectException(InvalidDomainException::class); $this->expectExceptionMessage('Provided host "www.google.fr" doesn\'t match whitelisted values: (.*)\.fin1te\.net'); @@ -82,7 +82,7 @@ public function testValidateHostWhitelist() Url::validateUrl('http://www.google.fr', $options); } - public function testValidateHostWithnoip() + public function testValidateHostWithnoip(): void { $this->expectException(InvalidDomainException::class); $this->expectExceptionMessage('Provided host "www.youpi.boom" doesn\'t resolve to an IP address'); @@ -92,7 +92,7 @@ public function testValidateHostWithnoip() Url::validateUrl('http://www.youpi.boom', $options); } - public function testValidateHostWithWhitelistIp() + public function testValidateHostWithWhitelistIp(): void { $this->expectException(InvalidIPException::class); $this->expectExceptionMessage('Provided host "2.2.2.2" resolves to "2.2.2.2", which doesn\'t match whitelisted values: 1.1.1.1'); @@ -103,7 +103,7 @@ public function testValidateHostWithWhitelistIp() Url::validateUrl('http://2.2.2.2', $options); } - public function testValidateHostWithWhitelistIpOk() + public function testValidateHostWithWhitelistIpOk(): void { $options = new Options(); $options->addToList('whitelist', 'ip', '1.1.1.1'); @@ -117,7 +117,7 @@ public function testValidateHostWithWhitelistIpOk() $this->assertArrayHasKey(0, $res['ips']); } - public function testValidateHostWithBlacklistIp() + public function testValidateHostWithBlacklistIp(): void { $this->expectException(InvalidIPException::class); $this->expectExceptionMessage('Provided host "1.1.1.1" resolves to "1.1.1.1", which matches a blacklisted value: 1.1.1.1'); @@ -128,20 +128,20 @@ public function testValidateHostWithBlacklistIp() Url::validateUrl('http://1.1.1.1', $options); } - public function testValidateUrlOk() + public function testValidateUrlOk(): void { $options = new Options(); $options->enablePinDns(); - $res = Url::validateUrl('http://www.fin1te.net:8080', $options); + $res = Url::validateUrl('https://bandito.re:8080', $options); $this->assertCount(3, $res); $this->assertArrayHasKey('url', $res); $this->assertArrayHasKey('host', $res); $this->assertArrayHasKey('ips', $res); $this->assertArrayHasKey(0, $res['ips']); - $this->assertSame('http://146.185.175.109:8080', $res['url']); - $this->assertSame('www.fin1te.net', $res['host']); + $this->assertSame('https://176.31.100.53:8080', $res['url']); + $this->assertSame('bandito.re', $res['host']); $res = Url::validateUrl('http://www.fin1te.net:8080', new Options());