Skip to content

Commit

Permalink
Merge pull request #359 from pscheit/remove-tag-from-nodes
Browse files Browse the repository at this point in the history
Fix that passing a tag to twig nodes is deprecated since twig 3.12
  • Loading branch information
Seldaek authored Sep 3, 2024
2 parents de34d69 + 0625e8f commit 00d275a
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 20 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
'static_lambda' => true,
'strict_param' => true,
'ternary_to_null_coalescing' => true,
'trailing_comma_in_multiline' => ['elements' => ['arrays']],
])
->setUsingCache(true)
->setRiskyAllowed(true)
Expand Down
9 changes: 9 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,14 @@ parameters:
- '#^Dynamic call to static method Symfony\\Bundle\\FrameworkBundle\\Test\\\S+\(\)\.$#'
# Ignore typing providers in tests
- '#^Method Nelmio\\SecurityBundle\\Tests\\[^:]+Test::(provide\w+|\w+Provider)\(\) return type has no value type specified in iterable type (array|iterable)\.#'

# TODO: twig/twig:>3.12 remove this ignore
-
message: "#^Class Twig\\\\Node\\\\CaptureNode constructor invoked with 3 parameters, 2 required\\.$#"
count: 1
path: src/Twig/Node/CSPNode.php
reportUnmatched: false

dynamicConstantNames:
- Symfony\Component\HttpKernel\Kernel::VERSION
- Twig\Environment::VERSION_ID
2 changes: 1 addition & 1 deletion src/ContentSecurityPolicy/ContentSecurityPolicyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function quoteKeywords(array $sourceList): array
return array_map(
static function (string $source) use ($keywords) {
if (\in_array($source, $keywords, true)) {
return sprintf("'%s'", $source);
return \sprintf("'%s'", $source);
}

return $source;
Expand Down
4 changes: 2 additions & 2 deletions src/ContentSecurityPolicy/DirectiveSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private function normalizeSignatures(?array $signatures): ?array
$normalizedSignatures['script-src'] = implode(
' ',
array_map(static function (string $value): string {
return sprintf('\'%s\'', $value);
return \sprintf('\'%s\'', $value);
}, $signatures['script-src'])
);
}
Expand All @@ -252,7 +252,7 @@ private function normalizeSignatures(?array $signatures): ?array
$normalizedSignatures['style-src'] = implode(
' ',
array_map(static function (string $value): string {
return sprintf('\'%s\'', $value);
return \sprintf('\'%s\'', $value);
}, $signatures['style-src'])
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ContentSecurityPolicy/ShaComputer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class ShaComputer implements ShaComputerInterface
public function __construct(string $type)
{
if (!\in_array($type, ['sha256', 'sha384', 'sha512'], true)) {
throw new \InvalidArgumentException(sprintf('Type "%s" is not supported', $type));
throw new \InvalidArgumentException(\sprintf('Type "%s" is not supported', $type));
}

$this->type = $type;
Expand Down Expand Up @@ -76,7 +76,7 @@ private function getFavorite(): string

private function compute(string $data): string
{
return sprintf('%s-%s', $this->type, base64_encode($this->computeHash($data)));
return \sprintf('%s-%s', $this->type, base64_encode($this->computeHash($data)));
}

private function computeHash(string $data): string
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private function addReferrerPolicyNode(): ArrayNodeDefinition
->always(function (array $values): array {
foreach ($values as $policy) {
if (!\in_array($policy, $this->referrerPolicies, true)) {
throw new \InvalidArgumentException(sprintf('Unknown referrer policy "%s". Possible referrer policies are "%s".', $policy, implode('", "', $this->referrerPolicies)));
throw new \InvalidArgumentException(\sprintf('Unknown referrer policy "%s". Possible referrer policies are "%s".', $policy, implode('", "', $this->referrerPolicies)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ExternalRedirect/AllowListBasedTargetValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function isTargetAllowed(string $targetUrl): bool
$host = parse_url($targetUrl, \PHP_URL_HOST);

if (!\is_string($host)) {
throw new \InvalidArgumentException(sprintf('Url "%s" does not contain a host name.', $targetUrl));
throw new \InvalidArgumentException(\sprintf('Url "%s" does not contain a host name.', $targetUrl));
}

return preg_match('{^'.$this->allowList.'$}i', $host) > 0;
Expand Down
6 changes: 3 additions & 3 deletions src/Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public function __construct(string $secret, string $algo, ?string $legacyAlgo =
$this->separator = $separator;

if (!\in_array($this->algo, hash_algos(), true)) {
throw new \InvalidArgumentException(sprintf("The supplied hashing algorithm '%s' is not supported by this system.", $this->algo));
throw new \InvalidArgumentException(\sprintf("The supplied hashing algorithm '%s' is not supported by this system.", $this->algo));
}

if (null !== $this->legacyAlgo && !\in_array($this->legacyAlgo, hash_algos(), true)) {
throw new \InvalidArgumentException(sprintf("The supplied legacy hashing algorithm '%s' is not supported by this system.", $this->legacyAlgo));
throw new \InvalidArgumentException(\sprintf("The supplied legacy hashing algorithm '%s' is not supported by this system.", $this->legacyAlgo));
}
}

Expand Down Expand Up @@ -78,7 +78,7 @@ public function verifySignedValue(string $signedValue): bool
public function getVerifiedRawValue(string $signedValue): string
{
if (!$this->verifySignedValue($signedValue)) {
throw new \InvalidArgumentException(sprintf("The signature for '%s' was invalid.", $signedValue));
throw new \InvalidArgumentException(\sprintf("The signature for '%s' was invalid.", $signedValue));
}

$valueSignatureTuple = $this->splitSignatureFromSignedValue($signedValue);
Expand Down
15 changes: 12 additions & 3 deletions src/Twig/Node/CSPNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* file that was distributed with this source code.
*/

use Nelmio\SecurityBundle\Twig\Version as TwigVersion;
use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Node\CaptureNode;
Expand All @@ -36,11 +37,19 @@ final class CSPNode extends Node
public function __construct(Node $body, int $lineno, string $tag, string $directive, ?string $sha = null)
{
if (class_exists(CaptureNode::class)) {
$body = new CaptureNode($body, $lineno, $tag);
if (TwigVersion::needsNodeTag()) {
$body = new CaptureNode($body, $lineno, $tag);
} else {
$body = new CaptureNode($body, $lineno);
}
$body->setAttribute('raw', true);
}

parent::__construct(['body' => $body], [], $lineno, $tag);
if (TwigVersion::needsNodeTag()) {
parent::__construct(['body' => $body], [], $lineno, $tag);
} else {
parent::__construct(['body' => $body], [], $lineno);
}
$this->sha = $sha;
$this->directive = $directive;
}
Expand Down Expand Up @@ -74,7 +83,7 @@ public function compile(Compiler $compiler): void
} elseif ('style-src' === $this->directive) {
$compiler->write("\$this->env->getRuntime('Nelmio\SecurityBundle\Twig\CSPRuntime')->getListener()->addStyle(\$content);\n");
} else {
throw new \InvalidArgumentException(sprintf('Unable to compile for directive "%s"', $this->directive));
throw new \InvalidArgumentException(\sprintf('Unable to compile for directive "%s"', $this->directive));
}

if (class_exists(CaptureNode::class)) {
Expand Down
25 changes: 25 additions & 0 deletions src/Twig/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Nelmio SecurityBundle.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\SecurityBundle\Twig;

/**
* @internal
*/
final class Version
{
public static function needsNodeTag(): bool
{
return \Twig\Environment::VERSION_ID < 301200;
}
}
10 changes: 5 additions & 5 deletions tests/App/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public function registerBundles(): iterable

public function getCacheDir(): string
{
return sprintf('%scache', $this->getBaseDir());
return \sprintf('%scache', $this->getBaseDir());
}

public function getLogDir(): string
{
return sprintf('%slog', $this->getBaseDir());
return \sprintf('%slog', $this->getBaseDir());
}

public function getProjectDir(): string
Expand All @@ -62,16 +62,16 @@ public function getProjectDir(): string
*/
protected function configureRoutes($routes): void
{
$routes->import(sprintf('%s/config/routes.yaml', $this->getProjectDir()));
$routes->import(\sprintf('%s/config/routes.yaml', $this->getProjectDir()));
}

protected function configureContainer(ContainerBuilder $containerBuilder, LoaderInterface $loader): void
{
$loader->load(sprintf('%s/config/config.yaml', $this->getProjectDir()));
$loader->load(\sprintf('%s/config/config.yaml', $this->getProjectDir()));
}

private function getBaseDir(): string
{
return sprintf('%s/nelmio-security-bundle/var/', sys_get_temp_dir());
return \sprintf('%s/nelmio-security-bundle/var/', sys_get_temp_dir());
}
}
4 changes: 2 additions & 2 deletions tests/Listener/ExternalRedirectListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ public function provideRedirectOverrides(): iterable
'/override',
'redirect_to',
$target,
sprintf('/override?redirect_to=%s', urlencode($target)),
\sprintf('/override?redirect_to=%s', urlencode($target)),
];

yield 'override with parameter and with forwardAs' => [
'/override?param=value',
'redirect_to',
$target,
sprintf('/override?param=value&redirect_to=%s', urlencode($target)),
\sprintf('/override?param=value&redirect_to=%s', urlencode($target)),
];
}

Expand Down

0 comments on commit 00d275a

Please sign in to comment.