1+ <?php
2+
3+ namespace KielCodingSecurityHeaders \Subscriber ;
4+
5+ use Enlight \Event \SubscriberInterface ;
6+ use Shopware \Models \Shop \DetachedShop ;
7+ use Symfony \Component \DependencyInjection \ContainerInterface ;
8+
9+ class Frontend implements SubscriberInterface
10+ {
11+ /**
12+ * @var ContainerInterface
13+ */
14+ private $ container ;
15+
16+ /**
17+ * @var
18+ */
19+ private $ config ;
20+
21+ /**
22+ * @param ContainerInterface $container
23+ */
24+ public function __construct (ContainerInterface $ container )
25+ {
26+ $ this ->container = $ container ;
27+ $ this ->config = $ this ->getPluginConfig ();
28+ }
29+
30+ /**
31+ * {@inheritdoc}
32+ */
33+ public static function getSubscribedEvents ()
34+ {
35+ return [
36+ 'Enlight_Controller_Action_PostDispatchSecure_Frontend_Index ' => 'onPostDispatch ' ,
37+ ];
38+ }
39+
40+ /**
41+ * @param \Enlight_Controller_ActionEventArgs $args
42+ */
43+ public function onPostDispatch (\Enlight_Controller_ActionEventArgs $ args )
44+ {
45+ $ response = $ args ->getResponse ();
46+
47+ $ this ->setSecurityHeaders ($ response );
48+ $ this ->setCustomHeaders ($ response );
49+ $ this ->removeInsecureHeaders ($ response );
50+ }
51+
52+ /**
53+ * @param \Enlight_Controller_Response_ResponseHttp $response
54+ */
55+ private function setSecurityHeaders (\Enlight_Controller_Response_ResponseHttp $ response )
56+ {
57+ if ($ this ->config ['strictTransportSecurityEnabled ' ]) {
58+ $ response ->setHeader ('Strict-Transport-Security ' , $ this ->config ['strictTransportSecurity ' ]);
59+ }
60+ if ($ this ->config ['xFrameOptionsEnabled ' ]) {
61+ $ response ->setHeader ('X-Frame-Options ' , $ this ->config ['xFrameOptions ' ]);
62+ }
63+ if ($ this ->config ['xXssProtectionEnabled ' ]) {
64+ $ response ->setHeader ('X-XSS-Protection ' , $ this ->config ['xXssProtection ' ]);
65+ }
66+ if ($ this ->config ['xContentTypeOptionsEnabled ' ]) {
67+ $ response ->setHeader ('X-Content-Type-Options ' , $ this ->config ['xContentTypeOptions ' ]);
68+ }
69+ if ($ this ->config ['referrerPolicyEnabled ' ]) {
70+ $ response ->setHeader ('Referrer-Policy ' , $ this ->config ['referrerPolicy ' ]);
71+ }
72+ if ($ this ->config ['contentSecurityPolicyEnabled ' ] && $ this ->isSecure ()) {
73+ if ($ this ->config ['contentSecurityPolicyDebug ' ]) {
74+ $ response ->setHeader ('Content-Security-Policy ' , $ this ->config ['contentSecurityPolicy ' ]);
75+ } else {
76+ $ response ->setHeader ('Content-Security-Policy-Report-Only ' , $ this ->config ['contentSecurityPolicy ' ]);
77+ }
78+ }
79+ }
80+
81+ /**
82+ * @param \Enlight_Controller_Response_ResponseHttp $response
83+ */
84+ private function setCustomHeaders (\Enlight_Controller_Response_ResponseHttp $ response )
85+ {
86+ foreach ($ this ->getCustomHeaders () as $ header => $ value ) {
87+ $ response ->setHeader ($ header , $ value );
88+ }
89+ }
90+
91+ /**
92+ * @param \Enlight_Controller_Response_ResponseHttp $response
93+ */
94+ private function removeInsecureHeaders (\Enlight_Controller_Response_ResponseHttp $ response )
95+ {
96+ if ($ this ->config ['xPoweredByDisabled ' ]) {
97+ @ini_set ('expose_php ' , 'off ' );
98+ }
99+ }
100+
101+ /**
102+ * @return array
103+ */
104+ private function getCustomHeaders ()
105+ {
106+ if (empty ($ this ->config ['customHeaders ' ])) {
107+ return [];
108+ }
109+
110+ $ headers = explode ('\n ' , $ this ->config ['customHeaders ' ]);
111+
112+ $ headersFormatted = [];
113+ foreach ($ headers as $ header ) {
114+ $ headerParts = explode (': ' , $ header );
115+ $ headersFormatted [$ headerParts [0 ]] = $ headerParts [1 ];
116+ }
117+
118+ return $ headersFormatted ;
119+ }
120+
121+ /**
122+ * @return array
123+ */
124+ private function getPluginConfig ()
125+ {
126+ $ pluginName = $ this ->container ->getParameter ('kiel_coding_security_headers.plugin_name ' );
127+
128+ return $ this ->container ->get ('shopware.plugin.cached_config_reader ' )->getByPluginName ($ pluginName );
129+ }
130+
131+ /**
132+ * @return bool
133+ */
134+ private function isSecure ()
135+ {
136+ /** @var DetachedShop $shop */
137+ $ shop = $ this ->container ->get ('shop ' );
138+
139+ return $ shop ->getSecure ();
140+ }
141+ }
0 commit comments