Skip to content

Commit

Permalink
Merge pull request #19 from gscscnd/employ-phpcs
Browse files Browse the repository at this point in the history
Employ PHP_CodeSniffer
  • Loading branch information
paragonie-scott authored Nov 8, 2017
2 parents 2bf754f + a2f2303 commit 146fd5e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ install:
script:
- vendor/bin/phpunit
- vendor/bin/psalm
- vendor/bin/phpcs
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"require-dev": {
"psr/http-message": "^1",
"phpunit/phpunit": "4.*|5.*",
"squizlabs/php_codesniffer": "^3",
"vimeo/psalm": "^0.3"
},
"suggest": {
Expand Down
10 changes: 10 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<ruleset>
<rule ref="PSR2"/>

<arg name="colors"/>

<file>bin</file>
<file>src</file>
<file>test</file>
</ruleset>
12 changes: 7 additions & 5 deletions src/CSPBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function compile(): string

$compiled = [];

foreach(self::$directives as $dir) {
foreach (self::$directives as $dir) {
if (\in_array($dir, $ruleKeys)) {
if (empty($ruleKeys)) {
if ($dir === 'base-uri') {
Expand Down Expand Up @@ -278,7 +278,7 @@ public static function fromData($data = ''): self
{
$array = \json_decode($data, true);

if(!\is_array($array)) {
if (!\is_array($array)) {
throw new \Exception('Is not array valid');
}

Expand Down Expand Up @@ -369,7 +369,7 @@ public function preHash(
* @param bool $legacy
* @return \Psr\Http\Message\MessageInterface
*/
function injectCSPHeader(MessageInterface $message, bool $legacy = false): MessageInterface
public function injectCSPHeader(MessageInterface $message, bool $legacy = false): MessageInterface
{
if ($this->needsCompile) {
$this->compile();
Expand Down Expand Up @@ -596,15 +596,17 @@ protected function compileSubgroup(string $directive, $policies = []): string
if ($url !== false) {
if ($this->supportOldBrowsers) {
if (\strpos($url, '://') === false) {
if (($this->isHTTPSConnection() && $this->httpsTransformOnHttpsConnections) || !empty($this->policies['upgrade-insecure-requests'])) {
if (($this->isHTTPSConnection() && $this->httpsTransformOnHttpsConnections)
|| !empty($this->policies['upgrade-insecure-requests'])) {
// We only want HTTPS connections here.
$ret .= 'https://'.$url.' ';
} else {
$ret .= 'https://'.$url.' http://'.$url.' ';
}
}
}
if (($this->isHTTPSConnection() && $this->httpsTransformOnHttpsConnections) || !empty($this->policies['upgrade-insecure-requests'])) {
if (($this->isHTTPSConnection() && $this->httpsTransformOnHttpsConnections)
|| !empty($this->policies['upgrade-insecure-requests'])) {
$ret .= \str_replace('http://', 'https://', $url).' ';
} else {
$ret .= $url.' ';
Expand Down
27 changes: 15 additions & 12 deletions test/BasicTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace ParagonIE\CSPBuilderTest;

use ParagonIE\CSPBuilder\CSPBuilder;
use PHPUnit_Framework_TestCase;

/**
*
*/
class BasicTest extends PHPUnit_Framework_TestCase
{
public function testBasicFromFile()
Expand All @@ -14,7 +15,7 @@ public function testBasicFromFile()
file_get_contents(__DIR__.'/vectors/basic-csp.out'),
$basic->getCompiledHeader()
);

$noOld = file_get_contents(__DIR__.'/vectors/basic-csp-no-old.out');
// We expect different output for ytimg.com when we disable legacy
// browser support (i.e. Safari):
Expand All @@ -24,7 +25,7 @@ public function testBasicFromFile()
->disableOldBrowserSupport()
->getCompiledHeader()
);

$array = $basic->getHeaderArray();
$this->assertEquals(
$array,
Expand All @@ -34,8 +35,8 @@ public function testBasicFromFile()
'X-Webkit-CSP' => $noOld
]
);


$array2 = $basic->getHeaderArray(false);
$this->assertEquals(
$array2,
Expand All @@ -48,7 +49,7 @@ public function testBasicFromFile()
public function testBasicFromData()
{
$data = file_get_contents(__DIR__.'/vectors/basic-csp.json');

$basic = CSPBuilder::fromData($data);
$basic->addSource('img-src', 'ytimg.com');

Expand All @@ -57,7 +58,7 @@ public function testBasicFromData()
$basic->getCompiledHeader()
);
}

public function testHash()
{
$basic = CSPBuilder::fromFile(__DIR__.'/vectors/basic-csp.json');
Expand All @@ -67,7 +68,7 @@ public function testHash()
$basic->getCompiledHeader()
);
}

public function testPreHash()
{
$basic = CSPBuilder::fromFile(__DIR__.'/vectors/basic-csp.json');
Expand All @@ -87,7 +88,8 @@ public function testPreHash()
public function testSourceHttpsConversion()
{
/** @var CSPBuilder|\PHPUnit_Framework_MockObject_MockObject $cspHttp */
$cspHttp = $this->getMockBuilder(CSPBuilder::class)->setMethods(['isHTTPSConnection'])->disableOriginalConstructor()->getMock();
$cspHttp = $this->getMockBuilder(CSPBuilder::class)->setMethods(['isHTTPSConnection'])
->disableOriginalConstructor()->getMock();
$cspHttp->method('isHTTPSConnection')->willReturn(false);

$cspHttp->addSource('form', 'http://example.com');
Expand All @@ -98,7 +100,8 @@ public function testSourceHttpsConversion()
$this->assertContains('http://another.com', $compiledCspHttp);

/** @var CSPBuilder|\PHPUnit_Framework_MockObject_MockObject $cspHttps */
$cspHttps = $this->getMockBuilder(CSPBuilder::class)->setMethods(['isHTTPSConnection'])->disableOriginalConstructor()->getMock();
$cspHttps = $this->getMockBuilder(CSPBuilder::class)->setMethods(['isHTTPSConnection'])
->disableOriginalConstructor()->getMock();
$cspHttps->method('isHTTPSConnection')->willReturn(true);

$cspHttps->addSource('form', 'http://example.com');
Expand Down

0 comments on commit 146fd5e

Please sign in to comment.