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

Add nonce attribute to script & style tags when csp_nonce() is available #462

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 7 additions & 6 deletions resources/views/errorPage.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<!DOCTYPE html>
<?php /** @var \Spatie\Ignition\ErrorPage\ErrorPageViewModel $viewModel */ ?>
<?php /** @var string $cspAttrs */ ?>
<html lang="en" class="<?= $viewModel->theme() ?>">
<!--
<?= $viewModel->throwableString() ?>
-->
<head>
<!-- Hide dumps asap -->
<style>
<style<?= $cspAttrs ?>>
pre.sf-dump {
display: none !important;
}
Expand All @@ -19,7 +20,7 @@

<title><?= $viewModel->title() ?></title>

<script>
<script<?= $cspAttrs ?>>
// Livewire modals remove CSS classes on the `html` element so we re-add
// the theme class again using JavaScript.
document.documentElement.classList.add('<?= $viewModel->theme() ?>');
Expand All @@ -30,14 +31,14 @@
}
</script>

<style><?= $viewModel->getAssetContents('ignition.css') ?></style>
<style<?= $cspAttrs ?>><?= $viewModel->getAssetContents('ignition.css') ?></style>

<?= $viewModel->customHtmlHead() ?>

</head>
<body class="scrollbar-lg antialiased bg-center bg-dots-darker dark:bg-dots-lighter">

<script>
<script<?= $cspAttrs ?>>
window.data = <?=
$viewModel->jsonEncode([
'report' => $viewModel->report(),
Expand All @@ -56,13 +57,13 @@

<div id="app"></div>

<script>
<script<?= $cspAttrs ?>>
<!--
<?= $viewModel->getAssetContents('ignition.js') ?>
-->
</script>

<script>
<script<?= $cspAttrs ?>>
window.ignite(window.data);
</script>

Expand Down
10 changes: 9 additions & 1 deletion src/Ignition.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,15 @@ public function renderException(Throwable $throwable, ?Report $report = null): v
$this->customHtmlBody,
);

(new Renderer())->render(['viewModel' => $viewModel], self::viewPath('errorPage'));
(new Renderer())->render(
[
'viewModel' => $viewModel,
'cspAttrs' => \function_exists('csp_nonce')
? ' nonce="'.\csp_nonce().'"'
: '',
],
self::viewPath('errorPage'),
);
}

public static function viewPath(string $viewName): string
Expand Down
Loading