Skip to content

Commit 3e30a1e

Browse files
committed
UIMacros: added n:nonce
1 parent feeeba0 commit 3e30a1e

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

src/Application/UI/Presenter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ public function getContext()
13651365
/**
13661366
* @return Nette\Http\IRequest
13671367
*/
1368-
protected function getHttpRequest()
1368+
public function getHttpRequest()
13691369
{
13701370
return $this->httpRequest;
13711371
}
@@ -1374,7 +1374,7 @@ protected function getHttpRequest()
13741374
/**
13751375
* @return Nette\Http\IResponse
13761376
*/
1377-
protected function getHttpResponse()
1377+
public function getHttpResponse()
13781378
{
13791379
return $this->httpResponse;
13801380
}

src/Bridges/ApplicationLatte/TemplateFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public function createTemplate(UI\Control $control = NULL)
103103
$latte->addProvider('uiControl', $control);
104104
$latte->addProvider('uiPresenter', $presenter);
105105
$latte->addProvider('snippetBridge', new Nette\Bridges\ApplicationLatte\SnippetBridge($control));
106+
$nonce = preg_match('#\s\'nonce-([\w+/]+=*)\'#', $presenter->getHttpResponse()->getHeader('Content-Security-Policy'), $m) ? $m[1] : NULL;
107+
$latte->addProvider('uiNonce', $nonce);
106108
}
107109
$latte->addProvider('cacheStorage', $this->cacheStorage);
108110

src/Bridges/ApplicationLatte/UIMacros.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* - {link destination ...} control link
2222
* - {plink destination ...} presenter link
2323
* - {snippet ?} ... {/snippet ?} control snippet
24+
* - n:once
2425
*/
2526
class UIMacros extends Latte\Macros\MacroSet
2627
{
@@ -41,6 +42,7 @@ public static function install(Latte\Compiler $compiler)
4142
$me->addMacro('ifCurrent', [$me, 'macroIfCurrent'], '}'); // deprecated; use n:class="$presenter->linkCurrent ? ..."
4243
$me->addMacro('extends', [$me, 'macroExtends']);
4344
$me->addMacro('layout', [$me, 'macroExtends']);
45+
$me->addMacro('nonce', NULL, NULL, 'echo $this->global->uiNonce ? " nonce=\"{$this->global->uiNonce}\"" : "";');
4446
}
4547

4648

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* Test: TemplateFactory nonce
5+
*/
6+
7+
use Nette\Application\UI;
8+
use Nette\Bridges\ApplicationLatte;
9+
use Tester\Assert;
10+
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
15+
$latte = new Latte\Engine;
16+
17+
$latteFactory = Mockery::mock(ApplicationLatte\ILatteFactory::class);
18+
$latteFactory->shouldReceive('create')->andReturn($latte);
19+
20+
$response = Mockery::mock(Nette\Http\Response::class);
21+
$response->shouldReceive('getHeader')->with('Content-Security-Policy')->andReturn("hello 'nonce-abcd123==' world");
22+
23+
$presenter = Mockery::mock(UI\Presenter::class);
24+
$presenter->shouldReceive('getPresenter')->andReturn($presenter);
25+
$presenter->shouldReceive('getHttpResponse')->andReturn($response);
26+
$presenter->shouldIgnoreMissing();
27+
28+
$factory = new ApplicationLatte\TemplateFactory($latteFactory);
29+
$factory->createTemplate($presenter);
30+
31+
$latte->setLoader(new Latte\Loaders\StringLoader);
32+
33+
Assert::match(
34+
'<script nonce="abcd123=="></script>',
35+
$latte->renderToString('<script n:nonce></script>')
36+
);

0 commit comments

Comments
 (0)