-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from igorhrcek/issue-3
Issue 3
- Loading branch information
Showing
8 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.env | ||
vendor/ | ||
.idea/ | ||
.phpunit.result.cache | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
|
||
class RuleContent { | ||
|
||
|
||
public array $content; | ||
public array $templateVars; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace WP_CLI_Secure\SubCommands; | ||
|
||
class AddSecurityHeaders extends SubCommand { | ||
public string $ruleTemplate = 'add_security_headers'; | ||
public string $ruleName = 'SECURITY HEADERS'; | ||
public string $successMessage = 'Add Security Headers rule has been deployed.'; | ||
public string $removalMessage= 'Add Security Headers rule has been removed.'; | ||
|
||
public function getTemplateVars() { | ||
|
||
$default_headers = [ | ||
'Strict-Transport-Security' => '"max-age=63072000; includeSubDomains; preload"', | ||
'Referrer-Policy' => 'strict-origin-when-cross-origin', | ||
'X-Content-Type-Options' => 'nosniff', | ||
'X-Frame-Options' => 'SAMEORIGIN', | ||
'X-XSS-Protection' => '"1; mode=block"' | ||
]; | ||
|
||
$headers = isset( $this->commandArguments['headers'] ) ? $this->commandArguments['headers'] : array_keys( $default_headers ); | ||
if ( ! empty( $headers ) ) { | ||
if ( is_string( $headers ) ) { | ||
$headers = explode( ',', $headers ); | ||
} | ||
$headers = array_map( 'trim', $headers ); | ||
|
||
foreach ( $headers as $h ) { | ||
$header = ''; | ||
$value = ''; | ||
foreach ( $default_headers as $key => $v ) { | ||
if ( strtolower( $key ) === strtolower( $h ) ) { | ||
$header = $key; | ||
$value = $v; | ||
} | ||
} | ||
if ( empty( $header ) ) { | ||
continue; | ||
} | ||
$headers_array[] = | ||
[ | ||
'header' => $header, | ||
'value' => $value | ||
]; | ||
} | ||
return $headers_array; | ||
} | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Header always set {{header}} {{value}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_header {{header}} {{value}} always; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use Tests\BaseTestCase; | ||
use WP_CLI_Secure\SubCommands\AddSecurityHeaders; | ||
|
||
class AddSecurityHeadersTest extends BaseTestCase { | ||
public function setUp(): void { | ||
parent::setUp(); | ||
|
||
$command = new AddSecurityHeaders($this->nginxAssocArgs); | ||
$command->output(); | ||
|
||
$command = new AddSecurityHeaders($this->apacheAssocArgs); | ||
$command->output(); | ||
|
||
exec('cd ' . dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . $_ENV['WORDPRESS_NGINX_PATH'] . '&& ddev exec nginx -s reload'); | ||
} | ||
|
||
public function testItWillContainAllHeadersOnNginx() : void { | ||
$response = $this->nginxHttpClient->get('', ['http_errors' => false]); | ||
|
||
$this->assertNotEmpty($response->getHeaderLine( 'Strict-Transport-Security' )); | ||
$this->assertNotEmpty($response->getHeaderLine( 'Referrer-Policy' )); | ||
$this->assertNotEmpty($response->getHeaderLine( 'x-content-type-options' )); | ||
$this->assertNotEmpty($response->getHeaderLine( 'X-Frame-Options' )); | ||
$this->assertNotEmpty($response->getHeaderLine( 'X-XSS-Protection' )); | ||
} | ||
|
||
public function testItWillContainAllHeadersOnApache() : void { | ||
$response = $this->apacheHttpClient->get('', ['http_errors' => false]); | ||
|
||
$this->assertNotEmpty($response->getHeaderLine( 'Strict-Transport-Security' )); | ||
$this->assertNotEmpty($response->getHeaderLine( 'Referrer-Policy' )); | ||
$this->assertNotEmpty($response->getHeaderLine( 'x-content-type-options' )); | ||
$this->assertNotEmpty($response->getHeaderLine( 'X-Frame-Options' )); | ||
$this->assertNotEmpty($response->getHeaderLine( 'X-XSS-Protection' )); | ||
} | ||
} |