Skip to content

Commit

Permalink
Kinda fixed/added automatic nonce support
Browse files Browse the repository at this point in the history
  • Loading branch information
Gappa committed May 6, 2019
1 parent ed21c13 commit 5a91a8b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
11 changes: 10 additions & 1 deletion WebLoader/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function enableDebugging(bool $allow = true): void

public function getNonce(): ?string
{
return $this->nonce;
return $this->nonce ?? $this->getGlobalNonce();
}


Expand Down Expand Up @@ -328,4 +328,13 @@ public function getFileFilters(): array
{
return $this->fileFilters;
}


/** Copy from \Tracy\Helpers::getNonce() */
private function getGlobalNonce(): ?string
{
return preg_match('#^Content-Security-Policy(?:-Report-Only)?:.*\sscript-src\s+(?:[^;]+\s)?\'nonce-([\w+/]+=*)\'#mi', implode("\n", headers_list()), $m)
? $m[1]
: null;
}
}
12 changes: 10 additions & 2 deletions WebLoader/Nette/CssLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @author Jan Marek
* @license MIT
*/
class CssLoader extends \WebLoader\Nette\WebLoader
class CssLoader extends WebLoader
{

/** @var string */
Expand Down Expand Up @@ -88,6 +88,14 @@ public function getElement(string $source): Html
$alternate = '';
}

return Html::el('link')->rel('stylesheet' . $alternate)->type($this->type)->media($this->media)->title($this->title)->href($source);
$el = Html::el('link');
$el->setAttribute('rel', 'stylesheet' . $alternate);
$el->setAttribute('type', $this->type);
$el->setAttribute('media', $this->media);
$el->setAttribute('title', $this->title);
$el->setAttribute('nonce', $this->getCompiler()->getNonce());
$el->setAttribute('href', $source);

return $el;
}
}
11 changes: 8 additions & 3 deletions WebLoader/Nette/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Nette\Utils\Finder;
use SplFileInfo;
use WebLoader\FileNotFoundException;

/**
Expand Down Expand Up @@ -219,6 +220,7 @@ private function findFiles(array $filesConfig, string $sourceDir): array
{
$normalizedFiles = [];

/** @var array|string $file */
foreach ($filesConfig as $file) {
// finder support
if (is_array($file) && isset($file['files']) && (isset($file['in']) || isset($file['from']))) {
Expand All @@ -236,19 +238,22 @@ private function findFiles(array $filesConfig, string $sourceDir): array

$foundFilesList = [];
foreach ($finder as $foundFile) {
/** @var \SplFileInfo $foundFile */
/** @var SplFileInfo $foundFile */
$foundFilesList[] = $foundFile->getPathname();
}

natsort($foundFilesList);

/** @var string $foundFilePathname */
foreach ($foundFilesList as $foundFilePathname) {
$normalizedFiles[] = $foundFilePathname;
}

} else {
$this->checkFileExists($file, $sourceDir);
$normalizedFiles[] = $file;
if (is_string($file)) {
$this->checkFileExists($file, $sourceDir);
$normalizedFiles[] = $file;
}
}
}

Expand Down
15 changes: 6 additions & 9 deletions WebLoader/Nette/JavaScriptLoader.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@
* @author Jan Marek
* @license MIT
*/
class JavaScriptLoader extends \WebLoader\Nette\WebLoader
class JavaScriptLoader extends WebLoader
{

/**
* Get script element
*/
public function getElement(string $source): Html
{
$el = Html::el('script');
$this->getCompiler()->isAsync() ? $el = $el->addAttributes(['async' => true]) : null;
$this->getCompiler()->isDefer() ? $el = $el->addAttributes(['defer' => true]) : null;
($nonce = $this->getCompiler()->getNonce()) ? $el = $el->addAttributes(['nonce' => $nonce]) : null;
$el->setAttribute('async', $this->getCompiler()->isAsync());
$el->setAttribute('defer', $this->getCompiler()->isDefer());
$el->setAttribute('nonce', $this->getCompiler()->getNonce());
$el->setAttribute('src', $source);

return $el->src($source);
return $el;
}
}
10 changes: 4 additions & 6 deletions WebLoader/Nette/LoaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Nette\DI\Container;
use Nette\Http\IRequest;
use WebLoader\Compiler;

class LoaderFactory
{
Expand Down Expand Up @@ -34,19 +35,16 @@ public function __construct(array $tempPaths, string $extensionName, IRequest $h

public function createCssLoader(string $name, bool $appendLastModified = false): CssLoader
{
/** @var \WebLoader\Compiler $compiler */
/** @var Compiler $compiler */
$compiler = $this->serviceLocator->getService($this->extensionName . '.css' . ucfirst($name) . 'Compiler');
return new CssLoader($compiler, $this->formatTempPath($name, $compiler->isAbsoluteUrl()), $appendLastModified);
}


public function createJavaScriptLoader(string $name, bool $appendLastModified = false, ?string $nonce = null): JavaScriptLoader
public function createJavaScriptLoader(string $name, bool $appendLastModified = false): JavaScriptLoader
{
/** @var \WebLoader\Compiler $compiler */
/** @var Compiler $compiler */
$compiler = $this->serviceLocator->getService($this->extensionName . '.js' . ucfirst($name) . 'Compiler');
if ($nonce) {
$compiler->setNonce($nonce);
}
return new JavaScriptLoader($compiler, $this->formatTempPath($name, $compiler->isAbsoluteUrl()), $appendLastModified);
}

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
"kylekatarnls/coffeescript": "1.3.*",
"mockery/mockery": "1.*",
"phpunit/phpunit": "7.*",
"jakub-onderka/php-parallel-lint": "~0.7"
"jakub-onderka/php-parallel-lint": "~0.7",
"phpstan/phpstan-shim": "^0.11.0",
"phpstan/phpstan-nette": "^0.11.0"
},
"extra": {
"branch-alias": {
Expand Down

0 comments on commit 5a91a8b

Please sign in to comment.