File tree 10 files changed +101
-24
lines changed
10 files changed +101
-24
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace bicf \securityheaders \behavior ;
4
+ use yii \base \Behavior ;
5
+
6
+ /**
7
+ * Class HeaderContentSecurityPolicyAcl
8
+ * @package bicf\securityheaders\modules
9
+ */
10
+ abstract class ContentSecurityPolicy extends Behavior
11
+ {
12
+ protected static $ token ;
13
+
14
+ /**
15
+ * @param string $token
16
+ */
17
+ public static function setContentSecurityPolicyToken ($ token )
18
+ {
19
+ if (self ::$ token === null ){
20
+ self ::$ token = $ token ;
21
+ } else {
22
+ throw new \UnexpectedValueException ("Token already set! " );
23
+ }
24
+ }
25
+
26
+ public static function getContentSecurityPolicyToken ()
27
+ {
28
+ if (self ::$ token === null ){
29
+ self ::$ token = \Yii::$ app ->security ->generateRandomString ();
30
+ }
31
+ return self ::$ token ;
32
+ }
33
+
34
+ abstract public function getContentSecurityPolicyTokenValue ();
35
+ abstract public function getContentSecurityPolicyTokenAttribute ();
36
+ abstract public function getContentSecurityPolicyTokenHeader ();
37
+ abstract public function getContentSecurityPolicyTokenArray ();
38
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace bicf \securityheaders \behavior ;
4
+
5
+ /**
6
+ * Class HeaderContentSecurityPolicyAcl
7
+ * @package bicf\securityheaders\modules
8
+ */
9
+ class ContentSecurityPolicyDummyBehavior extends ContentSecurityPolicy
10
+ {
11
+ public function getContentSecurityPolicyTokenValue ()
12
+ {
13
+ return "" ;
14
+ }
15
+
16
+
17
+ public function getContentSecurityPolicyTokenAttribute ()
18
+ {
19
+ return "" ;
20
+
21
+ }
22
+
23
+ public function getContentSecurityPolicyTokenHeader ()
24
+ {
25
+ return "" ;
26
+
27
+ }
28
+
29
+ public function getContentSecurityPolicyTokenArray ()
30
+ {
31
+ return [];
32
+ }
33
+
34
+
35
+ }
Original file line number Diff line number Diff line change 7
7
* Class HeaderContentSecurityPolicyAcl
8
8
* @package bicf\securityheaders\modules
9
9
*/
10
- class ContentSecurityPolicyNonceBehavior extends Behavior
10
+ class ContentSecurityPolicyNonceBehavior extends ContentSecurityPolicy
11
11
{
12
- private static $ token ;
13
-
14
- public static function setContentSecurityPolicyToken ($ token )
12
+ public function getContentSecurityPolicyTokenValue ()
15
13
{
16
- if (self ::$ token === null ){
17
- self ::$ token = $ token ;
18
- } else {
19
- throw new \UnexpectedValueException ("Token already setted! " );
20
- }
21
- }
22
-
23
- public static function getContentSecurityPolicyToken ()
24
- {
25
- if (self ::$ token === null ){
26
- self ::$ token = \Yii::$ app ->security ->generateRandomString ();
27
- }
28
- return self ::$ token ;
14
+ return self ::getContentSecurityPolicyToken ();
29
15
}
30
16
31
17
public function getContentSecurityPolicyTokenAttribute ()
@@ -43,7 +29,6 @@ public function getContentSecurityPolicyTokenHeader()
43
29
public function getContentSecurityPolicyTokenArray ()
44
30
{
45
31
return array ('nonce ' =>self ::getContentSecurityPolicyToken ());
46
-
47
32
}
48
33
49
34
Original file line number Diff line number Diff line change 1
1
<?php
2
2
namespace bicf \securityheaders \components ;
3
3
use bicf \securityheaders \modules \HeaderModuleBase ;
4
+ use bicf \securityheaders \modules \HeaderModuleInterface ;
4
5
use Yii ;
5
6
6
7
/**
@@ -91,12 +92,13 @@ public function init()
91
92
public $ modules =array ();
92
93
93
94
/**
94
- *
95
+ * @param $event
95
96
*/
96
97
public static function addSecurityHeaders ($ event )
97
98
{
98
99
/** @var $event->sender \bicf\securityheaders\components\Response */
99
100
foreach ($ event ->sender ->modules as $ module ){
101
+ /** @var HeaderModuleInterface $module */
100
102
$ module ->run ();
101
103
}
102
104
}
Original file line number Diff line number Diff line change @@ -10,6 +10,10 @@ class HeaderAccessControlAllowOrigin extends HeaderModuleBase
10
10
{
11
11
public $ value ;
12
12
13
+ public function init ()
14
+ {
15
+ }
16
+
13
17
public function run ()
14
18
{
15
19
if (!$ this ->enabled ){
Original file line number Diff line number Diff line change 9
9
class HeaderContentSecurityPolicyAcl extends HeaderContentSecurityPolicyBase
10
10
{
11
11
protected $ headerName ='Content-Security-Policy ' ;
12
+ public function init ()
13
+ {
14
+ }
12
15
13
16
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace bicf \securityheaders \modules ;
4
4
use bicf \securityheaders \behavior \ContentSecurityPolicyNonceBehavior ;
5
+ use bicf \securityheaders \behavior \ContentSecurityPolicyDummyBehavior ;
5
6
use bicf \securityheaders \components \SecureRequestInterface ;
6
- use yii \ web \Response ;
7
+ use bicf \ securityheaders \ components \Response ;
7
8
8
9
/**
9
10
* Class HeaderContentSecurityPolicyBase
@@ -29,10 +30,13 @@ abstract class HeaderContentSecurityPolicyBase extends HeaderModuleBase
29
30
public $ policies = array ();
30
31
31
32
/**
32
- * @var bool
33
+ * @var bool create a beahvior that handle the nonce hash
33
34
*/
34
35
public $ nonceEnabled = true ;
35
36
37
+ /** @var bool nonceFallback create a dummy behavior when $nonceEnabled is not enabled */
38
+ public $ nonceFallback = false ;
39
+
36
40
/**
37
41
* add the security header
38
42
*/
@@ -57,7 +61,10 @@ public function injectBehavior(Response $response)
57
61
{
58
62
// Avoid double attach
59
63
if ($ this ->nonceEnabled && $ response ->getBehavior (SecureRequestInterface::CSP_NONCE_BEHAVIOR ) === null ){
60
- $ rv = $ response ->attachBehavior (SecureRequestInterface::CSP_NONCE_BEHAVIOR ,new ContentSecurityPolicyNonceBehavior () );
64
+ $ response ->attachBehavior (SecureRequestInterface::CSP_NONCE_BEHAVIOR ,new ContentSecurityPolicyNonceBehavior () );
65
+ } elseif ($ this ->nonceFallback ) {
66
+ $ response ->attachBehavior (SecureRequestInterface::CSP_NONCE_BEHAVIOR ,new ContentSecurityPolicyDummyBehavior () );
67
+
61
68
}
62
69
}
63
70
Original file line number Diff line number Diff line change @@ -10,6 +10,9 @@ class HeaderContentSecurityPolicyMonitor extends HeaderContentSecurityPolicyBase
10
10
{
11
11
protected $ headerName ='Content-Security-Policy-Report-Only ' ;
12
12
13
+ public function init ()
14
+ {
15
+ }
13
16
14
17
15
18
}
Original file line number Diff line number Diff line change 9
9
namespace bicf \securityheaders \modules ;
10
10
11
11
12
+ use bicf \securityheaders \components \Response ;
12
13
use yii \base \BaseObject ;
13
- use yii \web \Response ;
14
14
15
15
/**
16
16
* Class HeaderModuleBase
Original file line number Diff line number Diff line change 9
9
namespace bicf \securityheaders \modules ;
10
10
11
11
12
- use yii \ web \Response ;
12
+ use bicf \ securityheaders \ components \Response ;
13
13
14
14
interface HeaderModuleInterface
15
15
{
You can’t perform that action at this time.
0 commit comments