From 67daaa36a51ee2e709dc803afa3e21f46df4c960 Mon Sep 17 00:00:00 2001 From: Bruno Meilick Date: Fri, 29 Nov 2024 19:03:51 +0000 Subject: [PATCH] :recycle: reduce Cognitive Complexity in load() --- classes/SecurityHeaders.php | 50 +++++++++++++++++++++++------------ composer.json | 2 +- vendor/composer/installed.php | 8 +++--- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/classes/SecurityHeaders.php b/classes/SecurityHeaders.php index e2aa9da..f40b686 100644 --- a/classes/SecurityHeaders.php +++ b/classes/SecurityHeaders.php @@ -169,35 +169,53 @@ public function csp(): CSPBuilder public function load(array|string|null $data = null): CSPBuilder { + // load default if is null if (is_null($data)) { $data = $this->option('loader'); } - if (is_string($data) && F::exists($data)) { - $mime = F::mime($data); - $data = F::read($data); - if (in_array($mime, A::get(Mime::types(), 'json'))) { - $data = Json::decode($data); - } elseif (A::get(Mime::types(), 'yaml') && in_array($mime, A::get(Mime::types(), 'yaml'))) { - $data = Yaml::decode($data); - } + if (is_string($data)) { + $data = $this->loadFromFile($data); } - if (is_array($data)) { - $this->cspBuilder = CSPBuilder::fromArray($data); - } else { // aka null - $this->cspBuilder = new CSPBuilder; + $this->cspBuilder = is_array($data) ? CSPBuilder::fromArray($data) : new CSPBuilder; + + $this->addNonceForSelf(); + $this->addPanelNonces(); + + return $this->cspBuilder; + } + + public function loadFromFile(string $data): mixed + { + if (! F::exists($data)) { + return null; } - // add nonce for self + $mime = F::mime($data); + $data = F::read($data); + + if (in_array($mime, A::get(Mime::types(), 'json'))) { + $data = Json::decode($data); + } elseif (A::get(Mime::types(), 'yaml') && in_array($mime, A::get(Mime::types(), 'yaml'))) { + $data = Yaml::decode($data); + } + + return $data; + } + + public function addNonceForSelf(): void + { if ($seed = strval($this->option('seed'))) { $nonce = $this->setNonce($seed); $this->cspBuilder->nonce('script-src', $nonce); $this->cspBuilder->nonce('style-src', $nonce); } + } - // add panel nonces - if ($this->option('panel')) { + public function addPanelNonces(): void + { + if (! $this->option('panel')) { $panelnonces = (array) $this->option('panelnonces'); foreach ($panelnonces as $nonce) { if (! is_string($nonce)) { @@ -208,8 +226,6 @@ public function load(array|string|null $data = null): CSPBuilder $this->cspBuilder->nonce('style-src', $nonce); } } - - return $this->cspBuilder; } public function applySetter(): void diff --git a/composer.json b/composer.json index 751c92c..48b7c2e 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "bnomei/kirby3-security-headers", "type": "kirby-plugin", - "version": "5.0.0", + "version": "5.0.1", "license": "MIT", "description": "Kirby Plugin for easier Security Headers setup", "authors": [ diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index 92da514..3542609 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -1,8 +1,8 @@ array( 'name' => 'bnomei/kirby3-security-headers', - 'pretty_version' => '5.0.0', - 'version' => '5.0.0.0', + 'pretty_version' => '5.0.1', + 'version' => '5.0.1.0', 'reference' => null, 'type' => 'kirby-plugin', 'install_path' => __DIR__ . '/../../', @@ -11,8 +11,8 @@ ), 'versions' => array( 'bnomei/kirby3-security-headers' => array( - 'pretty_version' => '5.0.0', - 'version' => '5.0.0.0', + 'pretty_version' => '5.0.1', + 'version' => '5.0.1.0', 'reference' => null, 'type' => 'kirby-plugin', 'install_path' => __DIR__ . '/../../',