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

Use csp()->getNonce() instead of cspNonce() - VUFIND-1510 #3392

Merged
merged 3 commits into from Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion module/VuFind/src/VuFind/Security/NonceGenerator.php
Expand Up @@ -48,7 +48,7 @@ class NonceGenerator
*
* @var string
*/
protected $nonce;
protected string $nonce = '';

/**
* Generates a random nonce parameter.
Expand Down
27 changes: 16 additions & 11 deletions module/VuFind/src/VuFind/View/Helper/Root/Csp.php
Expand Up @@ -42,29 +42,22 @@
*/
class Csp extends \Laminas\View\Helper\AbstractHelper
{
/**
* Response object
*
* @var Response
*/
protected $response;

/**
* Constructor
*
* @param Response $response HTTP Response, if any
* @param ?Response $response HTTP Response, if any
* @param string $nonce CSP nonce
*/
public function __construct(?Response $response)
public function __construct(protected ?Response $response, protected string $nonce)
{
$this->response = $response;
}

/**
* Disable Content Security Policy by removing the headers
*
* @return void
*/
public function disablePolicy()
public function disablePolicy(): void
{
if (null === $this->response) {
return;
Expand All @@ -86,4 +79,16 @@ public function disablePolicy()
}
}
}

/**
* Return the current nonce
*
* Result is a base64 encoded string that does not need escaping.
*
* @return string
*/
public function getNonce(): string
{
return $this->nonce;
}
}
3 changes: 2 additions & 1 deletion module/VuFind/src/VuFind/View/Helper/Root/CspFactory.php
Expand Up @@ -68,6 +68,7 @@ public function __invoke(
if (!empty($options)) {
throw new \Exception('Unexpected options sent to factory.');
}
return new $requestedName($container->get('Response'));
$nonceGenerator = $container->get(\VuFind\Security\NonceGenerator::class);
return new $requestedName($container->get('Response'), $nonceGenerator->getNonce());
}
}
2 changes: 2 additions & 0 deletions module/VuFind/src/VuFind/View/Helper/Root/CspNonce.php
Expand Up @@ -62,6 +62,8 @@ public function __construct($nonce)
*
* Result is a base64 encoded string that does not need escaping.
*
* @deprecated Use Csp::getNonce() instead
*
* @return string
*/
public function __invoke()
Expand Down
Expand Up @@ -77,7 +77,7 @@ public function testDisablePolicyWithCspEnabled(): void
$added = $headers->get('Content-Security-Policy');
$this->assertEquals(1, $added->count());

$csp = new \VuFind\View\Helper\Root\Csp($response);
$csp = new \VuFind\View\Helper\Root\Csp($response, $nonceGenerator->getNonce());
$csp->disablePolicy();
$this->assertFalse($headers->get('Content-Security-Policy'));
}
Expand Down Expand Up @@ -119,7 +119,7 @@ public function testDisablePolicyWithCspReportOnly(): void
$added = $headers->get('Content-Security-Policy-Report-Only');
$this->assertFalse(is_iterable($added));

$csp = new \VuFind\View\Helper\Root\Csp($response);
$csp = new \VuFind\View\Helper\Root\Csp($response, $nonceGenerator->getNonce());
$csp->disablePolicy();
$this->assertFalse($headers->get('Content-Security-Policy-Report-Only'));
}
Expand Down Expand Up @@ -155,7 +155,7 @@ public function testDisablePolicyWithCspDisabled(): void
$header = $cspHeaderGenerator->getHeader();
$this->assertNull($header);

$csp = new \VuFind\View\Helper\Root\Csp($response);
$csp = new \VuFind\View\Helper\Root\Csp($response, $nonceGenerator->getNonce());
$csp->disablePolicy();
}
}
2 changes: 1 addition & 1 deletion themes/bootstrap3/templates/layout/layout.phtml
Expand Up @@ -101,7 +101,7 @@
$root = rtrim($this->url('home'), '/');
$translations = !isset($this->renderingError) ? $this->render('js-translations.phtml') : '{}';
$dsb = DEFAULT_SEARCH_BACKEND;
$cspNonce = $this->cspNonce();
$cspNonce = $this->csp()->getNonce();
$searchId = json_encode($this->layout()->searchId);
$appendScripts[] = <<<JS
VuFind.path = '{$root}';
Expand Down
1 change: 0 additions & 1 deletion themes/root/theme.config.php
Expand Up @@ -22,7 +22,6 @@
'VuFind\View\Helper\Root\ContentBlock' => 'Laminas\ServiceManager\Factory\InvokableFactory',
'VuFind\View\Helper\Root\Context' => 'Laminas\ServiceManager\Factory\InvokableFactory',
'VuFind\View\Helper\Root\Csp' => 'VuFind\View\Helper\Root\CspFactory',
'VuFind\View\Helper\Root\CspNonce' => 'VuFind\View\Helper\Root\CspNonceFactory',
demiankatz marked this conversation as resolved.
Show resolved Hide resolved
'VuFind\View\Helper\Root\CurrentPath' => 'Laminas\ServiceManager\Factory\InvokableFactory',
'VuFind\View\Helper\Root\DateTime' => 'VuFind\View\Helper\Root\DateTimeFactory',
'VuFind\View\Helper\Root\DisplayLanguageOption' => 'VuFind\View\Helper\Root\DisplayLanguageOptionFactory',
Expand Down