Skip to content

Commit a2faf6d

Browse files
committed
~ apply patch
1 parent 6dddd1b commit a2faf6d

File tree

4 files changed

+86
-33
lines changed

4 files changed

+86
-33
lines changed
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* OpenMage
57
*
6-
* This source file is subject to the Academic Free License (AFL 3.0)
7-
* that is bundled with this package in the file LICENSE_AFL.txt.
8-
* It is also available at https://opensource.org/license/afl-3-0-php
8+
* This source file is subject to the Open Software License (OSL 3.0)
9+
* that is bundled with this package in the file LICENSE.txt.
10+
* It is also available at https://opensource.org/license/osl-3-0-php
911
*
1012
* @category Mage
1113
* @package Mage_Csp
1214
* @copyright Copyright (c) 2025 The OpenMage Contributors (https://www.openmage.org)
13-
* @license https://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
15+
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
1416
*/
1517

1618
class Mage_Csp_Block_Adminhtml_Csp extends Mage_Csp_Block_Csp
1719
{
1820
protected string $section = 'admin';
19-
2021
}

app/code/core/Mage/Csp/Block/Csp.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
class Mage_Csp_Block_Csp extends Mage_Core_Block_Abstract
1717
{
18+
/** @var array<string, array<int, string>> */
1819
protected array $items = [];
1920
protected string $section = 'system';
2021

2122
public function addItem(string $type, string $data): self
2223
{
23-
$this->items[$type] [] = $data;
24+
$this->items[$type][] = $data;
2425
return $this;
2526
}
2627

@@ -34,17 +35,14 @@ protected function _toHtml(): string
3435
return '';
3536
}
3637

37-
/**
38-
* @var Mage_Csp_Helper_Data $helper
39-
*/
38+
/** @var Mage_Csp_Helper_Data $helper */
4039
$helper = Mage::helper('csp');
4140

42-
if (!Mage::getStoreConfigFlag("$this->section/csp/enabled")) {
41+
if (!$helper->isCspEnabled($this->section)) {
4342
return '';
4443
}
45-
/**
46-
* @var Mage_Csp_Model_Config $config
47-
*/
44+
45+
/** @var Mage_Csp_Model_Config $config */
4846
$config = Mage::getSingleton('csp/config');
4947
$directives = array_merge_recursive(
5048
$helper->getPolicies($this->section),
@@ -56,8 +54,7 @@ protected function _toHtml(): string
5654
$cspHeader[] = $directive . ' ' . (is_array($value) ? implode(' ', $value) : (string) $value);
5755
}
5856

59-
$header = Mage::getStoreConfigFlag("$this->section/csp/report_only") ?
60-
'Content-Security-Policy-Report-Only' : 'Content-Security-Policy';
57+
$header = $helper->getCspHeader($this->section);
6158
$response->setHeader($header, implode('; ', $cspHeader));
6259
return '';
6360
}
Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* OpenMage
57
*
6-
* This source file is subject to the Academic Free License (AFL 3.0)
7-
* that is bundled with this package in the file LICENSE_AFL.txt.
8-
* It is also available at https://opensource.org/license/afl-3-0-php
8+
* This source file is subject to the Open Software License (OSL 3.0)
9+
* that is bundled with this package in the file LICENSE.txt.
10+
* It is also available at https://opensource.org/license/osl-3-0-php
911
*
1012
* @category Mage
1113
* @package Mage_Csp
1214
* @copyright Copyright (c) 2025 The OpenMage Contributors (https://www.openmage.org)
13-
* @license https://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
15+
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
1416
*/
17+
1518
class Mage_Csp_Helper_Data extends Mage_Core_Helper_Abstract
1619
{
20+
protected $_moduleName = 'Mage_Csp';
21+
1722
public const CONFIG_MAPPING = [
1823
'default-src',
1924
'script-src',
@@ -27,15 +32,41 @@ class Mage_Csp_Helper_Data extends Mage_Core_Helper_Abstract
2732
'form-action',
2833
];
2934

35+
/**
36+
* @return array<string, string>
37+
*/
3038
public function getPolicies(string $section): array
3139
{
32-
if (!Mage::getStoreConfigFlag("$section/csp/enabled")) {
33-
return [];
34-
}
3540
$result = [];
41+
42+
if (!$this->isCspEnabled($section)) {
43+
return $result;
44+
}
45+
3646
foreach (self::CONFIG_MAPPING as $key) {
37-
$result [$key] = Mage::getStoreConfig("$section/csp/$key");
47+
$result[$key] = $this->getCspConfigByKey($section, $key);
3848
}
3949
return $result;
4050
}
51+
52+
public function isCspEnabled(string $section): bool
53+
{
54+
return Mage::getStoreConfigFlag("$section/csp/enabled");
55+
}
56+
57+
public function isCspReportOnly(string $section): bool
58+
{
59+
return Mage::getStoreConfigFlag("$section/csp/report_only");
60+
}
61+
62+
public function getCspConfigByKey(string $section, string $key): string
63+
{
64+
return Mage::getStoreConfig("$section/csp/$key");
65+
}
66+
67+
public function getCspHeader(string $section): string
68+
{
69+
return $this->isCspReportOnly($section) ?
70+
'Content-Security-Policy-Report-Only' : 'Content-Security-Policy';
71+
}
4172
}

app/code/core/Mage/Csp/Model/Config.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* OpenMage
57
*
6-
* This source file is subject to the Academic Free License (AFL 3.0)
7-
* that is bundled with this package in the file LICENSE_AFL.txt.
8-
* It is also available at https://opensource.org/license/afl-3-0-php
8+
* This source file is subject to the Open Software License (OSL 3.0)
9+
* that is bundled with this package in the file LICENSE.txt.
10+
* It is also available at https://opensource.org/license/osl-3-0-php
911
*
1012
* @category Mage
1113
* @package Mage_Csp
1214
* @copyright Copyright (c) 2025 The OpenMage Contributors (https://www.openmage.org)
13-
* @license https://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
15+
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
1416
*/
17+
1518
class Mage_Csp_Model_Config extends Varien_Simplexml_Config
1619
{
1720
public const CACHE_TYPE = 'config';
@@ -39,28 +42,38 @@ public function __construct($sourceData = null)
3942
*/
4043
protected function _construct(): self
4144
{
42-
if (Mage::app()->useCache(self::CACHE_TYPE) && $this->loadCache()) {
45+
if ($this->hasUseCache() && $this->loadCache()) {
4346
return $this;
4447
}
4548

4649
$this->loadString('<config/>');
4750
$config = Mage::getConfig()->loadModulesConfiguration('csp.xml', $this);
4851

49-
$this->setXml($config->getNode());
52+
$node = $config->getNode();
53+
if ($node) {
54+
$this->setXml($node);
55+
}
5056

51-
if (Mage::app()->useCache(self::CACHE_TYPE)) {
57+
if ($this->hasUseCache()) {
5258
$this->saveCache();
5359
}
5460
return $this;
5561
}
5662

5763
/**
5864
* Retrieve all adapters
65+
* @return array<string, array<int, string>>
5966
*/
6067
public function getPolicies(): array
6168
{
6269
$policies = [];
63-
foreach ($this->getXpath('csp') as $config) {
70+
71+
$xpaths = $this->getXpath('csp/policy');
72+
if (!$xpaths) {
73+
return $policies;
74+
}
75+
76+
foreach ($xpaths as $config) {
6477
foreach ($config as $policy => $rules) {
6578
foreach ($rules as $host) {
6679
$policies[$policy][] = (string) $host;
@@ -92,7 +105,7 @@ protected function _loadCache($id): bool
92105
/**
93106
* @param string $data
94107
* @param string $id
95-
* @param array $tags
108+
* @param array<int, string> $tags
96109
* @param false|int $lifetime
97110
*/
98111
protected function _saveCache($data, $id, $tags = [], $lifetime = false): bool
@@ -121,7 +134,10 @@ public function extend(Varien_Simplexml_Config $config, $overwrite = false): sel
121134
return $this;
122135
}
123136

124-
$this->_extendNode($this->getNode(), $config, $overwrite);
137+
$node = $this->getNode();
138+
if ($node) {
139+
$this->_extendNode($node, $config, $overwrite);
140+
}
125141

126142
return $this;
127143
}
@@ -139,4 +155,12 @@ protected function _extendNode(Varien_Simplexml_Element $baseNode, Varien_Simple
139155
$this->_extendNode($newChild, $child, $overwrite);
140156
}
141157
}
158+
159+
/**
160+
* @return array<mixed>|false
161+
*/
162+
protected function hasUseCache(): array|false
163+
{
164+
return Mage::app()->useCache(self::CACHE_TYPE);
165+
}
142166
}

0 commit comments

Comments
 (0)