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 support to PwaRuntime #221

Merged
merged 1 commit into from
Jun 17, 2024
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
32 changes: 31 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,21 @@ parameters:
count: 1
path: src/ImageProcessor/GDImageProcessor.php

-
message: "#^Parameter \\#2 \\$red of function imagecolorallocate expects int\\<0, 255\\>, int given\\.$#"
count: 1
path: src/ImageProcessor/GDImageProcessor.php

-
message: "#^Parameter \\#3 \\$green of function imagecolorallocate expects int\\<0, 255\\>, int given\\.$#"
count: 1
path: src/ImageProcessor/GDImageProcessor.php

-
message: "#^Parameter \\#4 \\$blue of function imagecolorallocate expects int\\<0, 255\\>, int given\\.$#"
count: 1
path: src/ImageProcessor/GDImageProcessor.php

-
message: "#^Should not use node with type \"Stmt_Echo\", please change the code\\.$#"
count: 3
Expand Down Expand Up @@ -638,4 +653,19 @@ parameters:
-
message: "#^Attribute class Symfony\\\\Component\\\\DependencyInjection\\\\Attribute\\\\TaggedIterator is deprecated\\: since Symfony 7\\.1, use \\{@see AutowireIterator\\} instead\\.$#"
count: 1
path: src/Subscriber/PwaDevServerSubscriber.php
path: src/Subscriber/PwaDevServerSubscriber.php

-
message: "#^Call to method getNonce\\(\\) on an unknown class Nelmio\\\\SecurityBundle\\\\EventListener\\\\ContentSecurityPolicyListener\\.$#"
count: 1
path: src/Twig/PwaRuntime.php

-
message: "#^Parameter \\$cspListener of method SpomkyLabs\\\\PwaBundle\\\\Twig\\\\PwaRuntime\\:\\:__construct\\(\\) has invalid type Nelmio\\\\SecurityBundle\\\\EventListener\\\\ContentSecurityPolicyListener\\.$#"
count: 1
path: src/Twig/PwaRuntime.php

-
message: "#^Property SpomkyLabs\\\\PwaBundle\\\\Twig\\\\PwaRuntime\\:\\:\\$cspListener has unknown class Nelmio\\\\SecurityBundle\\\\EventListener\\\\ContentSecurityPolicyListener as its type\\.$#"
count: 1
path: src/Twig/PwaRuntime.php
13 changes: 12 additions & 1 deletion src/Twig/PwaRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SpomkyLabs\PwaBundle\Twig;

use InvalidArgumentException;
use Nelmio\SecurityBundle\EventListener\ContentSecurityPolicyListener;
use SpomkyLabs\PwaBundle\Dto\Favicons;
use SpomkyLabs\PwaBundle\Dto\Icon;
use SpomkyLabs\PwaBundle\Dto\Manifest;
Expand All @@ -13,6 +14,7 @@
use Symfony\Component\AssetMapper\MappedAsset;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Mime\MimeTypes;
use function array_key_exists;
use const ENT_COMPAT;
use const ENT_SUBSTITUTE;
use const PHP_EOL;
Expand All @@ -28,6 +30,8 @@ public function __construct(
private FaviconsCompiler $faviconsCompiler,
#[Autowire('%spomky_labs_pwa.manifest.public_url%')]
string $manifestPublicUrl,
#[Autowire(service: 'nelmio_security.csp_listener')]
private ?ContentSecurityPolicyListener $cspListener = null,
) {
$this->manifestPublicUrl = '/' . trim($manifestPublicUrl, '/');
}
Expand Down Expand Up @@ -122,7 +126,7 @@ private function injectServiceWorker(string $output, bool $injectSW, array $swAt
if ($serviceWorker->workbox->enabled === true) {
$workboxUrl = sprintf('%s%s', $serviceWorker->workbox->workboxPublicUrl, '/workbox-window.prod.mjs');
$declaration = <<<SERVICE_WORKER
<script type="module" {$scriptAttributes}>
<script type="module"{$scriptAttributes}>
import {Workbox} from '{$workboxUrl}';
if ('serviceWorker' in navigator) {
const wb = new Workbox('{$url}'{$registerOptions});
Expand Down Expand Up @@ -218,6 +222,13 @@ private function createAttributesString(array $attributes): string
self::class
));
}
if (! array_key_exists('nonce', $attributes) && $this->cspListener !== null) {
$nonce = $this->cspListener->getNonce('script');
$attributes['nonce'] = $nonce;
} elseif (array_key_exists('nonce', $attributes) && $attributes['nonce'] === false) {
unset($attributes['nonce']);
}

foreach ($attributes as $name => $value) {
$attributeString .= ' ';
if ($value === true) {
Expand Down