Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce class ipl\Web\Compat\StyleWithNonce #202

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() ?? ''
: '';
nilmerg marked this conversation as resolved.
Show resolved Hide resolved
}

return parent::getNonce();
}
}
2 changes: 1 addition & 1 deletion src/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down