Skip to content

Commit 3996c99

Browse files
author
Lars Schröder
committed
bugfix custom headers: add support for urls; code cleanup
1 parent 98caec6 commit 3996c99

File tree

6 files changed

+45
-42
lines changed

6 files changed

+45
-42
lines changed

Diff for: KielCodingSecurityHeaders.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,20 @@ public function deactivate(DeactivateContext $context)
3030
*/
3131
public function build(ContainerBuilder $container)
3232
{
33-
$container->setParameter($this->getContainerPrefix() . '.plugin_dir', $this->getPath());
34-
$container->setParameter($this->getContainerPrefix() . '.plugin_name', $this->getName());
33+
$container->setParameter('kiel_coding_security_headers.plugin_dir', $this->getPluginPath());
3534

3635
parent::build($container);
3736
}
37+
38+
/**
39+
* Gets the Plugin directory path.
40+
*
41+
* @return string The Plugin absolute path
42+
*/
43+
public function getPluginPath()
44+
{
45+
$reflected = new \ReflectionObject($this);
46+
47+
return dirname($reflected->getFileName());
48+
}
3849
}

Diff for: Resources/config.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@
8080
<name>customHeaders</name>
8181
<label>Custom Headers</label>
8282
<label lang="de">Eigene Headers</label>
83-
<description>E.g. X-Powered-By: My Company Name</description>
84-
<description lang="de">Z.B. X-Powered-By: Mein Firmenname</description>
83+
<description>Split header name and value with ":"; split multiple headers with line break.E.g. X-Powered-By: My Company Name.</description>
84+
<description lang="de">Header-Name und -Wert mittels ":" trennen; mehrere Header mittels Zeilenumbruch trennen. Z.B. X-Powered-By: Mein Firmenname.</description>
8585
</element>
8686
</elements>
8787
</config>

Diff for: Resources/services.xml

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22
<container xmlns="http://symfony.com/schema/dic/services"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<parameters>
7+
<parameter key="kiel_coding_security_headers.plugin_name">KielCodingSecurityHeaders</parameter>
8+
</parameters>
9+
510
<services>
6-
<service id="kiel_coding_trusted_shops_reviews.subscriber.frontend" class="KielCodingSecurityHeaders\Subscriber\Frontend">
7-
<argument type="service" id="service_container" />
11+
<service id="kiel_coding_security_headers.subscriber.frontend" class="KielCodingSecurityHeaders\Subscriber\Frontend">
12+
<argument type="service" id="shopware.plugin.cached_config_reader" />
13+
<argument type="string">%kiel_coding_security_headers.plugin_name%</argument>
14+
815
<tag name="shopware.event_subscriber" />
916
</service>
1017
</services>

Diff for: Subscriber/Frontend.php

+12-32
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,22 @@
33
namespace KielCodingSecurityHeaders\Subscriber;
44

55
use Enlight\Event\SubscriberInterface;
6-
use Shopware\Models\Shop\DetachedShop;
7-
use Symfony\Component\DependencyInjection\ContainerInterface;
6+
use Shopware\Components\Plugin\CachedConfigReader;
87

98
class Frontend implements SubscriberInterface
109
{
1110
/**
12-
* @var ContainerInterface
13-
*/
14-
private $container;
15-
16-
/**
17-
* @var
11+
* @var array
1812
*/
1913
private $config;
2014

2115
/**
22-
* @param ContainerInterface $container
16+
* @param CachedConfigReader $configReader
17+
* @param string $pluginName
2318
*/
24-
public function __construct(ContainerInterface $container)
19+
public function __construct(CachedConfigReader $configReader, $pluginName)
2520
{
26-
$this->container = $container;
27-
$this->config = $this->getPluginConfig();
21+
$this->config = $configReader->getByPluginName($pluginName, Shopware()->Shop());
2822
}
2923

3024
/**
@@ -42,11 +36,12 @@ public static function getSubscribedEvents()
4236
*/
4337
public function onPostDispatch(\Enlight_Controller_ActionEventArgs $args)
4438
{
39+
/** @var \Enlight_Controller_Response_ResponseHttp $response */
4540
$response = $args->getResponse();
4641

4742
$this->setSecurityHeaders($response);
4843
$this->setCustomHeaders($response);
49-
$this->removeInsecureHeaders($response);
44+
$this->removeInsecureHeaders();
5045
}
5146

5247
/**
@@ -88,10 +83,7 @@ private function setCustomHeaders(\Enlight_Controller_Response_ResponseHttp $res
8883
}
8984
}
9085

91-
/**
92-
* @param \Enlight_Controller_Response_ResponseHttp $response
93-
*/
94-
private function removeInsecureHeaders(\Enlight_Controller_Response_ResponseHttp $response)
86+
private function removeInsecureHeaders()
9587
{
9688
if ($this->config['xPoweredByDisabled']) {
9789
@ini_set('expose_php', 'off');
@@ -111,31 +103,19 @@ private function getCustomHeaders()
111103

112104
$headersFormatted = [];
113105
foreach ($headers as $header) {
114-
$headerParts = explode(':', $header);
106+
// Use preg_split with limit to prevent url splitting caused by ":" inside.
107+
$headerParts = preg_split('/[\\s+:\\s+]/', $header, 2);
115108
$headersFormatted[$headerParts[0]] = $headerParts[1];
116109
}
117110

118111
return $headersFormatted;
119112
}
120113

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-
131114
/**
132115
* @return bool
133116
*/
134117
private function isSecure()
135118
{
136-
/** @var DetachedShop $shop */
137-
$shop = $this->container->get('shop');
138-
139-
return $shop->getSecure();
119+
return Shopware()->Shop()->getSecure();
140120
}
141121
}

Diff for: plugin.png

-262 Bytes
Loading

Diff for: plugin.xml

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
<plugin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="../../../engine/Shopware/Components/Plugin/schema/plugin.xsd">
44
<label>Security Headers</label>
5-
<version>1.1.0</version>
5+
<version>1.2.0</version>
66

7-
<author>Kiel Coding</author>
8-
<copyright>(c) by Kiel Coding</copyright>
9-
<link>https://kielcoding.de</link>
7+
<author>KielCoding</author>
8+
<copyright>(c) KielCoding</copyright>
9+
<link>https://www.kielcoding.de</link>
1010
<license>proprietary</license>
1111
<compatibility minVersion="5.2.0" />
1212

13+
<changelog version="1.2.0">
14+
<changes>Custom header url bugfix and code cleanup.</changes>
15+
<changes lang="de">Eigene Header URL Bugfix und Code-Bereinigung.</changes>
16+
</changelog>
17+
1318
<changelog version="1.1.0">
1419
<changes>Fix event</changes>
1520
<changes lang="de">Fix Event</changes>

0 commit comments

Comments
 (0)