Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/default src tests #11

Closed
Closed
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
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .travis.yml
100644 → 100755
Empty file.
Empty file modified CODE_OF_CONDUCT.md
100644 → 100755
Empty file.
Empty file modified CONTRIBUTING.md
100644 → 100755
Empty file.
Empty file modified LICENSE.md
100644 → 100755
Empty file.
1 change: 0 additions & 1 deletion README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ to the require section of your composer.json.
'geolocation' => "'self'",
'gyroscope' => "'self'",
'layout-animations' => "'self'",
'legacy-image-format' => "'self'",
'magnetometer' => "'self'",
'microphone' => "'self'",
'midi' => "'self'",
Expand Down
Empty file modified composer.json
100644 → 100755
Empty file.
1,028 changes: 467 additions & 561 deletions composer.lock
100644 → 100755

Large diffs are not rendered by default.

29 changes: 12 additions & 17 deletions phpunit.xml.dist
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./tests/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
4 changes: 2 additions & 2 deletions src/Headers.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Headers extends Component implements BootstrapInterface
* @access public
* @var string
*/
public $xPoweredBy = 'Hyperia';
public $xPoweredBy = '';

/**
* Report URI
Expand Down Expand Up @@ -156,7 +156,7 @@ public function bootstrap($app)
];

foreach ($headerPolicy as $policy) {
if ($policy->isValid()) {
if ($policy->isValid() && !$headers->has($policy->getName())) {
$headers->set($policy->getName(), $policy->getValue());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/headers/ContentSecurityPolicy.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getValue(): string

public function isValid(): bool
{
$allowedDirectives = array_keys($this->defaultDirectives);
$allowedDirectives = array_keys(array_merge($this->defaultDirectives, $this->defaultCsp));
foreach ($this->directives as $directive => $value) {
if (!in_array($directive, $allowedDirectives) && !empty($value)) {
return false;
Expand Down
1 change: 0 additions & 1 deletion src/headers/FeaturePolicy.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class FeaturePolicy implements PolicyInterface
'geolocation' => "'self'",
'gyroscope' => "'self'",
'layout-animations' => "'self'",
'legacy-image-format' => "'self'",
'magnetometer' => "'self'",
'microphone' => "'self'",
'midi' => "'self'",
Expand Down
Empty file modified src/headers/PolicyInterface.php
100644 → 100755
Empty file.
Empty file modified src/headers/ReferrerPolicy.php
100644 → 100755
Empty file.
Empty file modified src/headers/StrictTransportSecurity.php
100644 → 100755
Empty file.
Empty file modified src/headers/XContentTypeOptions.php
100644 → 100755
Empty file.
Empty file modified src/headers/XFrameOptions.php
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion src/headers/XPoweredBy.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public function getValue(): string

public function isValid(): bool
{
return true;
return !empty($this->value);
}
}
Empty file modified src/headers/XssProtection.php
100644 → 100755
Empty file.
Empty file modified tests/HeadersTest.php
100644 → 100755
Empty file.
Empty file modified tests/TestCase.php
100644 → 100755
Empty file.
Empty file modified tests/bootstrap.php
100644 → 100755
Empty file.
Empty file modified tests/config/config.php
100644 → 100755
Empty file.
64 changes: 43 additions & 21 deletions tests/headers/ContentSecurityPolicyTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@

class ContentSecurityPolicyTest extends TestCase
{
/**
* @var ContentSecurityPolicy
*/
private $header;

public function setUp(): void
public function testCommon(): void
{
$this->header = new ContentSecurityPolicy([
$policy = new ContentSecurityPolicy([
'object-src' => "'self'",
'media-src' => "'self'",
'form-action' => "'self'",
Expand All @@ -23,21 +18,10 @@ public function setUp(): void
'upgradeInsecureRequests' => false,
'blockAllMixedContent' => true
], 'https://www.example.com');
}

public function testGetValue(): void
{
$this->assertSame("default-src 'none'; connect-src 'self'; font-src 'self'; frame-src 'self'; img-src 'self' data:; manifest-src 'self'; object-src 'self'; prefetch-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src 'self'; form-action 'self'; worker-src 'self'; report-uri https://www.example.com/r/d/csp/enforce; block-all-mixed-content", $this->header->getValue());
}

public function testGetName(): void
{
$this->assertSame('Content-Security-Policy', $this->header->getName());
}

public function testIsValid(): void
{
$this->assertTrue($this->header->isValid());
$this->assertSame("default-src 'none'; connect-src 'self'; font-src 'self'; frame-src 'self'; img-src 'self' data:; manifest-src 'self'; object-src 'self'; prefetch-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src 'self'; form-action 'self'; worker-src 'self'; report-uri https://www.example.com/r/d/csp/enforce; block-all-mixed-content", $policy->getValue());
$this->assertSame('Content-Security-Policy', $policy->getName());
$this->assertTrue($policy->isValid());
}

public function testInvalid(): void
Expand Down Expand Up @@ -66,6 +50,44 @@ public function testWithSubresourceIntegrity()
'requireSriForStyle' => true
], 'https://www.example.com');

$this->assertTrue($policy->isValid());
$this->assertSame("default-src 'none'; connect-src 'self'; font-src 'self'; frame-src 'self'; img-src 'self' data:; manifest-src 'self'; object-src 'self'; prefetch-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src 'self'; form-action 'self'; worker-src 'self'; require-sri-for script style; report-uri https://www.example.com/r/d/csp/enforce", $policy->getValue());
}

public function testDefaultSrc(): void
{
$policy = new ContentSecurityPolicy([
'default-src' => "*",
], [
'upgradeInsecureRequests' => false,
'blockAllMixedContent' => true
], 'https://www.example.com');

$this->assertSame("default-src *; connect-src 'self'; font-src 'self'; frame-src 'self'; img-src 'self' data:; manifest-src 'self'; object-src 'self'; prefetch-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src 'self'; form-action 'self'; worker-src 'self'; report-uri https://www.example.com/r/d/csp/enforce; block-all-mixed-content", $policy->getValue());
$this->assertTrue($policy->isValid());

// 'self'
$policy = new ContentSecurityPolicy([
'default-src' => "'self'",
], [
'upgradeInsecureRequests' => false,
'blockAllMixedContent' => true
], 'https://www.example.com');

$this->assertSame("default-src 'self'; connect-src 'self'; font-src 'self'; frame-src 'self'; img-src 'self' data:; manifest-src 'self'; object-src 'self'; prefetch-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src 'self'; form-action 'self'; worker-src 'self'; report-uri https://www.example.com/r/d/csp/enforce; block-all-mixed-content", $policy->getValue());
$this->assertTrue($policy->isValid());
}

public function testWithChildSrc(): void
{
$policy = new ContentSecurityPolicy([
'child-src' => "'self'"
], [
'upgradeInsecureRequests' => false,
'blockAllMixedContent' => true
], 'https://www.example.com');

$this->assertNotTrue($policy->isValid());
$this->assertSame("default-src 'none'; connect-src 'self'; font-src 'self'; frame-src 'self'; img-src 'self' data:; manifest-src 'self'; object-src 'self'; prefetch-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src 'self'; form-action 'self'; worker-src 'self'; child-src 'self'; report-uri https://www.example.com/r/d/csp/enforce; block-all-mixed-content", $policy->getValue());
}
}
29 changes: 29 additions & 0 deletions tests/headers/EmptyXPoweredByTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace hyperia\security\tests\headers;

use hyperia\security\headers\XPoweredBy;
use hyperia\security\tests\TestCase;

class EmptyXPoweredByTest extends TestCase
{
/**
* @var XPoweredBy
*/
private $header;

public function setUp(): void
{
$this->header = new XPoweredBy('');
}

public function testGetValue(): void
{
$this->assertSame('', $this->header->getValue());
}

public function testIsValid(): void
{
$this->assertNotTrue($this->header->isValid());
}
}
2 changes: 1 addition & 1 deletion tests/headers/FeaturePolicyTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function setUp(): void

public function testGetValue(): void
{
$this->assertSame("accelerometer 'self'; ambient-light-sensor 'self'; autoplay 'self'; battery 'self'; camera 'self'; display-capture 'self'; document-domain 'self'; encrypted-media 'self'; fullscreen 'self'; geolocation 'self'; gyroscope 'self'; layout-animations 'self'; legacy-image-format 'self'; magnetometer 'self'; microphone 'self'; midi 'self'; oversized-images 'self'; payment *; picture-in-picture 'none'; publickey-credentials-get 'self'; sync-xhr 'self'; usb 'self'; wake-lock 'self'; xr-spatial-tracking 'self'", $this->header->getValue());
$this->assertSame("accelerometer 'self'; ambient-light-sensor 'self'; autoplay 'self'; battery 'self'; camera 'self'; display-capture 'self'; document-domain 'self'; encrypted-media 'self'; fullscreen 'self'; geolocation 'self'; gyroscope 'self'; layout-animations 'self'; magnetometer 'self'; microphone 'self'; midi 'self'; oversized-images 'self'; payment *; picture-in-picture 'none'; publickey-credentials-get 'self'; sync-xhr 'self'; usb 'self'; wake-lock 'self'; xr-spatial-tracking 'self'", $this->header->getValue());
}

public function testGetName(): void
Expand Down
Empty file modified tests/headers/ReferrerPolicyTest.php
100644 → 100755
Empty file.
Empty file modified tests/headers/StrictTransportSecurityTest.php
100644 → 100755
Empty file.
Empty file modified tests/headers/XContentTypeOptionsTest.php
100644 → 100755
Empty file.
Empty file modified tests/headers/XFrameOptionsTest.php
100644 → 100755
Empty file.
Empty file modified tests/headers/XPoweredByTest.php
100644 → 100755
Empty file.
Empty file modified tests/headers/XssProtectionTest.php
100644 → 100755
Empty file.