Skip to content

Commit

Permalink
Close #33
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Feb 14, 2019
1 parent 87e9243 commit 454e923
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Expand Up @@ -7,3 +7,4 @@
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/phpunit.xml export-ignore
/.php_cs.dist export-ignore
18 changes: 18 additions & 0 deletions .php_cs.dist
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

return PhpCsFixer\Config::create()
->setUsingCache(false)
->setRules([
'native_function_invocation' => [
'exclude' => []
],
])
->setRiskyAllowed(true)
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->append([__FILE__])
)
;
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -10,6 +10,11 @@ matrix:
- php vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
after_script:
- php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover
- php: 7.3
script:
- composer test-cs
- composer test-md
- composer test-cs-fixer
before_script:
- travis_retry composer self-update
- travis_retry composer install --no-interaction --prefer-dist
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Expand Up @@ -32,7 +32,8 @@
"mockery/mockery": "^1.0",
"scrutinizer/ocular": "^1.4",
"squizlabs/php_codesniffer": "^2.9",
"phpmd/phpmd": "^2.6"
"phpmd/phpmd": "^2.6",
"friendsofphp/php-cs-fixer": "^2.14"
},
"minimum-stability": "stable",
"autoload": {
Expand All @@ -46,10 +47,12 @@
}
},
"scripts": {
"test": ["@test-unit", "@test-cs", "@test-md"],
"test": ["@test-unit", "@test-cs", "@test-md", "@test-cs-fixer"],
"test-unit": "./vendor/phpunit/phpunit/phpunit --coverage-text",
"test-unit-phpdbg": "phpdbg -qrr ./vendor/bin/phpunit --coverage-text",
"test-cs": "./vendor/bin/phpcs -p -s --standard=PSR2 ./src ./tests",
"test-md": "./vendor/bin/phpmd ./src text codesize,controversial,cleancode,design,unusedcode,naming"
"test-md": "./vendor/bin/phpmd ./src text codesize,controversial,cleancode,design,unusedcode,naming",
"test-cs-fixer": "./vendor/bin/php-cs-fixer fix --diff --dry-run -v",
"cs-fixer": "./vendor/bin/php-cs-fixer fix --diff -v --ansi"
}
}
28 changes: 14 additions & 14 deletions src/Analyzer.php
Expand Up @@ -213,7 +213,7 @@ protected function analyzeAsPreFlight(RequestInterface $request, string $request

return $this->createResult(AnalysisResultInterface::TYPE_REQUEST_OUT_OF_CORS_SCOPE);
}
$requestMethod = reset($requestMethod);
$requestMethod = \reset($requestMethod);

// OK now we are sure it's a pre-flight request
$this->logDebug('Request is identified as a pre-flight CORS request.');
Expand Down Expand Up @@ -280,7 +280,7 @@ protected function createPreFlightResponseHeaders(

// #6.2.10
// Has only 'simple' headers excluding Content-Type
$isSimpleExclCT = empty(array_diff($lcRequestHeaders, static::SIMPLE_LC_HEADERS_EXCLUDING_CONTENT_TYPE));
$isSimpleExclCT = empty(\array_diff($lcRequestHeaders, static::SIMPLE_LC_HEADERS_EXCLUDING_CONTENT_TYPE));
if ($isSimpleExclCT === false || $this->strategy->isForceAddAllowedHeadersToPreFlightResponse() === true) {
$headers[CorsResponseHeaders::ALLOW_HEADERS] = $this->strategy->getRequestAllowedHeaders($request);
}
Expand All @@ -298,10 +298,10 @@ protected function getRequestedHeadersInLowerCase(RequestInterface $request): ar
$requestHeaders = [];

foreach ($request->getHeader(CorsRequestHeaders::HEADERS) as $headersList) {
$headersList = strtolower($headersList);
foreach (explode(CorsRequestHeaders::HEADERS_SEPARATOR, $headersList) as $header) {
$headersList = \strtolower($headersList);
foreach (\explode(CorsRequestHeaders::HEADERS_SEPARATOR, $headersList) as $header) {
// after explode header names might have spaces in the beginnings and ends so trim them
$header = trim($header);
$header = \trim($header);
if (empty($header) === false) {
$requestHeaders[] = $header;
}
Expand All @@ -321,7 +321,7 @@ protected function getOriginHeader(RequestInterface $request): string
if ($request->hasHeader(CorsRequestHeaders::ORIGIN) === true) {
$header = $request->getHeader(CorsRequestHeaders::ORIGIN);
if (empty($header) === false) {
return reset($header);
return \reset($header);
}
}

Expand All @@ -348,15 +348,15 @@ protected function checkIsSameHost(RequestInterface $request): bool
//
// `parse_url` function thinks the first value is `path` and the second is `host` with `port`
// which is a bit annoying so...
$portOrNull = parse_url($host, PHP_URL_PORT);
$hostUrl = $portOrNull === null ? $host : parse_url($host, PHP_URL_HOST);
$portOrNull = \parse_url($host, PHP_URL_PORT);
$hostUrl = $portOrNull === null ? $host : \parse_url($host, PHP_URL_HOST);

// Neither MDN, nor RFC tell anything definitive about Host header comparison.
// Browsers such as Firefox and Chrome do not add the optional port for
// HTTP (80) and HTTPS (443).
// So we require port match only if it specified in settings.

$isHostUrlMatch = strcasecmp($serverOriginHost, $hostUrl) === 0;
$isHostUrlMatch = \strcasecmp($serverOriginHost, $hostUrl) === 0;
$isSameHost =
$isHostUrlMatch === true &&
($serverOriginPort === null || $serverOriginPort === $portOrNull);
Expand All @@ -381,7 +381,7 @@ protected function checkIsSameHost(RequestInterface $request): bool
*/
protected function checkIsCrossOrigin(string $requestOrigin): bool
{
$parsedUrl = parse_url($requestOrigin);
$parsedUrl = \parse_url($requestOrigin);
if ($parsedUrl === false) {
$this->logWarning('Request origin header URL cannot be parsed.', ['url' => $requestOrigin]);

Expand All @@ -391,12 +391,12 @@ protected function checkIsCrossOrigin(string $requestOrigin): bool
// check `host` parts
$requestOriginHost = $parsedUrl['host'] ?? '';
$serverOriginHost = $this->strategy->getServerOriginHost();
if (strcasecmp($requestOriginHost, $serverOriginHost) !== 0) {
if (\strcasecmp($requestOriginHost, $serverOriginHost) !== 0) {
return true;
}

// check `port` parts
$requestOriginPort = array_key_exists('port', $parsedUrl) === true ? (int)$parsedUrl['port'] : null;
$requestOriginPort = \array_key_exists('port', $parsedUrl) === true ? (int)$parsedUrl['port'] : null;
$serverOriginPort = $this->strategy->getServerOriginPort();
if ($requestOriginPort !== $serverOriginPort) {
return true;
Expand All @@ -405,7 +405,7 @@ protected function checkIsCrossOrigin(string $requestOrigin): bool
// check `scheme` parts
$requestOriginScheme = $parsedUrl['scheme'] ?? '';
$serverOriginScheme = $this->strategy->getServerOriginScheme();
if (strcasecmp($requestOriginScheme, $serverOriginScheme) !== 0) {
if (\strcasecmp($requestOriginScheme, $serverOriginScheme) !== 0) {
return true;
}

Expand Down Expand Up @@ -450,7 +450,7 @@ protected static function getFactory(): FactoryInterface
private function getRequestHostHeader(RequestInterface $request): ?string
{
$hostHeaderValue = $request->getHeader(CorsRequestHeaders::HOST);
$host = empty($hostHeaderValue) === true ? null : reset($hostHeaderValue);
$host = empty($hostHeaderValue) === true ? null : \reset($hostHeaderValue);

return $host;
}
Expand Down
32 changes: 16 additions & 16 deletions src/Strategies/Settings.php
Expand Up @@ -263,16 +263,16 @@ public function getServerOriginPort(): ?int
*/
public function setServerOrigin(string $scheme, string $host, int $port): self
{
assert(empty($scheme) === false);
assert(empty($host) === false);
assert(0 < $port && $port <= 0xFFFF);
\assert(empty($scheme) === false);
\assert(empty($host) === false);
\assert(0 < $port && $port <= 0xFFFF);

$this->serverOriginScheme = $scheme;
$this->serverOriginHost = $host;

if (strcasecmp($scheme, 'http') === 0 && $port === 80) {
if (\strcasecmp($scheme, 'http') === 0 && $port === 80) {
$port = null;
} elseif (strcasecmp($scheme, 'https') === 0 && $port === 443) {
} elseif (\strcasecmp($scheme, 'https') === 0 && $port === 443) {
$port = null;
}
$this->serverOriginPort = $port;
Expand Down Expand Up @@ -305,7 +305,7 @@ public function getPreFlightCacheMaxAge(RequestInterface $request): int
*/
public function setPreFlightCacheMaxAge(int $cacheMaxAge): self
{
assert($cacheMaxAge >= 0);
\assert($cacheMaxAge >= 0);

$this->preFlightCacheMaxAge = $cacheMaxAge;
$this->isPreFlightCanBeCached = $cacheMaxAge > 0;
Expand Down Expand Up @@ -428,7 +428,7 @@ public function isRequestOriginAllowed(string $requestOrigin): bool
{
return
$this->areAllOriginsAllowed === true ||
isset($this->allowedOrigins[strtolower($requestOrigin)]) === true;
isset($this->allowedOrigins[\strtolower($requestOrigin)]) === true;
}

/**
Expand All @@ -455,7 +455,7 @@ public function setAllowedOrigins(array $origins): self
$this->allowedOrigins = [];

foreach ($origins as $origin) {
$this->allowedOrigins[strtolower($origin)] = true;
$this->allowedOrigins[\strtolower($origin)] = true;
}

$this->areAllOriginsAllowed = false;
Expand All @@ -468,7 +468,7 @@ public function setAllowedOrigins(array $origins): self
*/
public function isRequestMethodSupported(string $method): bool
{
return $this->areAllMethodsAllowed === true || isset($this->allowedLcMethods[strtolower($method)]) === true;
return $this->areAllMethodsAllowed === true || isset($this->allowedLcMethods[\strtolower($method)]) === true;
}

/**
Expand Down Expand Up @@ -500,11 +500,11 @@ public function enableAllMethodsAllowed(): self
*/
public function setAllowedMethods(array $methods): self
{
$this->allowedMethodsList = implode(', ', $methods);
$this->allowedMethodsList = \implode(', ', $methods);

$this->allowedLcMethods = [];
foreach ($methods as $method) {
$this->allowedLcMethods[strtolower($method)] = true;
$this->allowedLcMethods[\strtolower($method)] = true;
}

$this->areAllMethodsAllowed = false;
Expand All @@ -518,7 +518,7 @@ public function setAllowedMethods(array $methods): self
public function isRequestAllHeadersSupported(array $lcHeaders): bool
{
return $this->areAllHeadersAllowed === true ||
count(array_intersect($this->allowedLcHeaders, $lcHeaders)) === count($lcHeaders);
\count(\array_intersect($this->allowedLcHeaders, $lcHeaders)) === \count($lcHeaders);
}

/**
Expand Down Expand Up @@ -550,11 +550,11 @@ public function enableAllHeadersAllowed(): self
*/
public function setAllowedHeaders(array $headers): self
{
$this->allowedHeadersList = implode(', ', $headers);
$this->allowedHeadersList = \implode(', ', $headers);

$this->allowedLcHeaders = [];
foreach ($headers as $header) {
$this->allowedLcHeaders[] = strtolower($header);
$this->allowedLcHeaders[] = \strtolower($header);
}

$this->areAllHeadersAllowed = false;
Expand Down Expand Up @@ -599,12 +599,12 @@ public function setExposedHeaders(array $headers): self
// make sense to include those headers to exposed.
$filtered = [];
foreach ($headers as $header) {
if (in_array(strtolower($header), static::SIMPLE_LC_RESPONSE_HEADERS) === false) {
if (\in_array(\strtolower($header), static::SIMPLE_LC_RESPONSE_HEADERS) === false) {
$filtered[] = $header;
}
}

$this->exposedHeadersList = implode(', ', $filtered);
$this->exposedHeadersList = \implode(', ', $filtered);

return $this;
}
Expand Down

0 comments on commit 454e923

Please sign in to comment.