From 1741381df92b58d23ce6beb9e805eecb40811800 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 7 Aug 2024 03:10:00 +0200 Subject: [PATCH] Helpers::getNonce() -> getNonceAttr() --- src/Tracy/Bar/Bar.php | 4 ++-- src/Tracy/Bar/assets/loader.phtml | 3 +-- src/Tracy/BlueScreen/BlueScreen.php | 2 +- src/Tracy/BlueScreen/assets/page.phtml | 3 +-- src/Tracy/Debugger/assets/error.500.phtml | 2 +- src/Tracy/Dumper/Dumper.php | 3 +-- src/Tracy/Helpers.php | 6 +++--- 7 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Tracy/Bar/Bar.php b/src/Tracy/Bar/Bar.php index 037bc3cee..00d85b5c4 100644 --- a/src/Tracy/Bar/Bar.php +++ b/src/Tracy/Bar/Bar.php @@ -59,7 +59,7 @@ public function renderLoader(DeferredContent $defer): void $this->loaderRendered = true; $requestId = $defer->getRequestId(); - $nonce = Helpers::getNonce(); + $nonceAttr = Helpers::getNonceAttr(); $async = true; require __DIR__ . '/assets/loader.phtml'; } @@ -101,7 +101,7 @@ public function render(DeferredContent $defer): void $defer->addSetup('Tracy.Debug.init', $content); } else { - $nonce = Helpers::getNonce(); + $nonceAttr = Helpers::getNonceAttr(); $async = false; Debugger::removeOutputBuffers(false); require __DIR__ . '/assets/loader.phtml'; diff --git a/src/Tracy/Bar/assets/loader.phtml b/src/Tracy/Bar/assets/loader.phtml index 9471d54a2..11247c664 100644 --- a/src/Tracy/Bar/assets/loader.phtml +++ b/src/Tracy/Bar/assets/loader.phtml @@ -5,14 +5,13 @@ declare(strict_types=1); namespace Tracy; /** - * @var ?string $nonce + * @var string $nonceAttr * @var bool $async * @var string $requestId */ $baseUrl = $_SERVER['REQUEST_URI'] ?? ''; $baseUrl .= strpos($baseUrl, '?') === false ? '?' : '&'; -$nonceAttr = $nonce ? ' nonce="' . Helpers::escapeHtml($nonce) . '"' : ''; $asyncAttr = $async ? ' async' : ''; ?> diff --git a/src/Tracy/BlueScreen/BlueScreen.php b/src/Tracy/BlueScreen/BlueScreen.php index 187d711ca..0a30592bf 100644 --- a/src/Tracy/BlueScreen/BlueScreen.php +++ b/src/Tracy/BlueScreen/BlueScreen.php @@ -179,7 +179,7 @@ private function renderTemplate(\Throwable $exception, string $template, bool $t ], Debugger::$customCssFiles)); $css = Helpers::minifyCss(implode('', $css)); - $nonce = $toScreen ? Helpers::getNonce() : null; + $nonceAttr = $toScreen ? Helpers::getNonceAttr() : null; $actions = $toScreen ? $this->renderActions($exception) : []; require $template; diff --git a/src/Tracy/BlueScreen/assets/page.phtml b/src/Tracy/BlueScreen/assets/page.phtml index 0dff01fbb..9eb54f124 100644 --- a/src/Tracy/BlueScreen/assets/page.phtml +++ b/src/Tracy/BlueScreen/assets/page.phtml @@ -7,13 +7,12 @@ namespace Tracy; /** * @var \Throwable $exception * @var string $title - * @var ?string $nonce + * @var string $nonceAttr * @var string $css * @var string $source */ $code = $exception->getCode() ? ' #' . $exception->getCode() : ''; -$nonceAttr = $nonce ? ' nonce="' . Helpers::escapeHtml($nonce) . '"' : ''; $chain = Helpers::getExceptionChain($exception); ?>

diff --git a/src/Tracy/Debugger/assets/error.500.phtml b/src/Tracy/Debugger/assets/error.500.phtml index 54db23eb6..cbcb71583 100644 --- a/src/Tracy/Debugger/assets/error.500.phtml +++ b/src/Tracy/Debugger/assets/error.500.phtml @@ -38,6 +38,6 @@ namespace Tracy; - diff --git a/src/Tracy/Dumper/Dumper.php b/src/Tracy/Dumper/Dumper.php index 27da44dc0..4f4fea6af 100644 --- a/src/Tracy/Dumper/Dumper.php +++ b/src/Tracy/Dumper/Dumper.php @@ -155,8 +155,7 @@ public static function renderAssets(): void $sent = true; - $nonce = Helpers::getNonce(); - $nonceAttr = $nonce ? ' nonce="' . Helpers::escapeHtml($nonce) . '"' : ''; + $nonceAttr = Helpers::getNonceAttr(); $s = file_get_contents(__DIR__ . '/../assets/toggle.css') . file_get_contents(__DIR__ . '/assets/dumper-light.css') . file_get_contents(__DIR__ . '/assets/dumper-dark.css'); diff --git a/src/Tracy/Helpers.php b/src/Tracy/Helpers.php index 923050184..168e375d7 100644 --- a/src/Tracy/Helpers.php +++ b/src/Tracy/Helpers.php @@ -329,11 +329,11 @@ public static function isCli(): bool /** @internal */ - public static function getNonce(): ?string + public static function getNonceAttr(): string { return preg_match('#^Content-Security-Policy(?:-Report-Only)?:.*\sscript-src\s+(?:[^;]+\s)?\'nonce-([\w+/]+=*)\'#mi', implode("\n", headers_list()), $m) - ? $m[1] - : null; + ? ' nonce="' . Helpers::escapeHtml($m[1]) . '"' + : ''; }