Skip to content

Commit

Permalink
UIMacros: added n:nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 19, 2017
1 parent feeeba0 commit 3e30a1e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ public function getContext()
/**
* @return Nette\Http\IRequest
*/
protected function getHttpRequest()
public function getHttpRequest()
{
return $this->httpRequest;
}
Expand All @@ -1374,7 +1374,7 @@ protected function getHttpRequest()
/**
* @return Nette\Http\IResponse
*/
protected function getHttpResponse()
public function getHttpResponse()
{
return $this->httpResponse;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Bridges/ApplicationLatte/TemplateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ 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;

This comment has been minimized.

Copy link
@hranicka

hranicka Jan 25, 2017

Contributor

@dg $presenter can be nullable here. This is a BC-break then, isn't it?

I'll push a PR for this.

This comment has been minimized.

Copy link
@dg

dg Jan 25, 2017

Author Member

Right, it should be $nonce = $presenter && preg_match(...

$latte->addProvider('uiNonce', $nonce);
}
$latte->addProvider('cacheStorage', $this->cacheStorage);

Expand Down
2 changes: 2 additions & 0 deletions src/Bridges/ApplicationLatte/UIMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* - {link destination ...} control link
* - {plink destination ...} presenter link
* - {snippet ?} ... {/snippet ?} control snippet
* - n:once

This comment has been minimized.

Copy link
@mrtnzlml

mrtnzlml Jan 19, 2017

Contributor

It should be n:nonce... :) But it's nice shortcut... :))

This comment has been minimized.

Copy link
@dg

dg Jan 19, 2017

Author Member

fixed

*/
class UIMacros extends Latte\Macros\MacroSet
{
Expand All @@ -41,6 +42,7 @@ public static function install(Latte\Compiler $compiler)
$me->addMacro('ifCurrent', [$me, 'macroIfCurrent'], '}'); // deprecated; use n:class="$presenter->linkCurrent ? ..."
$me->addMacro('extends', [$me, 'macroExtends']);
$me->addMacro('layout', [$me, 'macroExtends']);
$me->addMacro('nonce', NULL, NULL, 'echo $this->global->uiNonce ? " nonce=\"{$this->global->uiNonce}\"" : "";');
}


Expand Down
36 changes: 36 additions & 0 deletions tests/Bridges.Latte/TemplateFactory.nonce.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\Response::class);
$response->shouldReceive('getHeader')->with('Content-Security-Policy')->andReturn("hello 'nonce-abcd123==' world");

$presenter = Mockery::mock(UI\Presenter::class);
$presenter->shouldReceive('getPresenter')->andReturn($presenter);
$presenter->shouldReceive('getHttpResponse')->andReturn($response);
$presenter->shouldIgnoreMissing();

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

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

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

0 comments on commit 3e30a1e

Please sign in to comment.