Skip to content

Commit

Permalink
TemplateFactory: "nonce" is Presenter agnostic (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
hranicka authored and dg committed Feb 2, 2017
1 parent 25a1874 commit ca1baaa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Bridges/ApplicationLatte/TemplateFactory.php
Expand Up @@ -105,7 +105,7 @@ public function createTemplate(UI\Control $control = NULL)
$latte->addProvider('uiControl', $control);
$latte->addProvider('uiPresenter', $presenter);
$latte->addProvider('snippetBridge', new Nette\Bridges\ApplicationLatte\SnippetBridge($control));
$nonce = preg_match('#\s\'nonce-([\w+/]+=*)\'#', $presenter->getHttpResponse()->getHeader('Content-Security-Policy'), $m) ? $m[1] : NULL;
$nonce = $presenter && preg_match('#\s\'nonce-([\w+/]+=*)\'#', $presenter->getHttpResponse()->getHeader('Content-Security-Policy'), $m) ? $m[1] : NULL;
$latte->addProvider('uiNonce', $nonce);
}
$latte->addProvider('cacheStorage', $this->cacheStorage);
Expand Down
34 changes: 34 additions & 0 deletions tests/Bridges.Latte/TemplateFactory.nonce.control.phpt
@@ -0,0 +1,34 @@
<?php

/**
* Test: TemplateFactory nonce
*/

use Nette\Application\UI;
use Nette\Bridges\ApplicationLatte;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';

$latte = new Latte\Engine;

$latteFactory = Mockery::mock(ApplicationLatte\ILatteFactory::class);
$latteFactory->shouldReceive('create')->andReturn($latte);

$response = Mockery::mock(Nette\Http\IResponse::class);
$response->shouldReceive('getHeader')->with('Content-Security-Policy')->andReturn("hello 'nonce-abcd123==' world");

$control = Mockery::mock(UI\Control::class);
$control->shouldReceive('getPresenter')->andReturn(NULL);
$control->shouldIgnoreMissing();

$factory = new ApplicationLatte\TemplateFactory($latteFactory);
$factory->createTemplate($control);

$latte->setLoader(new Latte\Loaders\StringLoader);

Assert::match(
'<script></script>',
$latte->renderToString('<script n:nonce></script>')
);

0 comments on commit ca1baaa

Please sign in to comment.