Skip to content

Commit

Permalink
Introduce class ipl\Web\Compat\StyleWithNonce (#202)
Browse files Browse the repository at this point in the history
nilmerg authored Sep 8, 2023
2 parents e3d0c02 + f1400c8 commit c8825cb
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/Compat/StyleWithNonce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace ipl\Web\Compat;

use Icinga\Application\Version;
use Icinga\Util\Csp;
use ipl\Web\Style;

/**
* Use this class to define inline style which is compatible
* with Icinga Web &lt; 2.12 and with CSP support in &gt;= 2.12
*/
class StyleWithNonce extends Style
{
public function getNonce(): ?string
{
if ($this->nonce === null) {
$this->nonce = version_compare(Version::VERSION, '2.12.0', '>=')
? Csp::getStyleNonce() ?? ''
: '';
}

return parent::getNonce();
}
}
2 changes: 1 addition & 1 deletion src/Style.php
Original file line number Diff line number Diff line change
@@ -102,7 +102,7 @@ public function render(): string

return (new HtmlElement(
'style',
(new Attributes())->addAttribute(new Attribute('nonce', $this->nonce)),
(new Attributes())->addAttribute(new Attribute('nonce', $this->getNonce())),
HtmlString::create($ruleset->renderCss())
))->render();
}

0 comments on commit c8825cb

Please sign in to comment.