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

Configure headers in response setup #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 8 additions & 11 deletions src/Bridges/HttpDI/HttpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Nette\Bridges\HttpDI;

use Nette;
use Nette\PhpGenerator\Helpers;
use Nette\Schema\Expect;


Expand Down Expand Up @@ -76,13 +75,13 @@ public function loadConfiguration()
}


public function afterCompile(Nette\PhpGenerator\ClassType $class)
public function beforeCompile()
{
if ($this->cliMode) {
return;
}

$initialize = $class->getMethod('initialize');
$builder = $this->getContainerBuilder();
$config = $this->config;
$headers = array_map('strval', $config->headers);

Expand All @@ -96,16 +95,14 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
$headers['X-Frame-Options'] = $frames;
}

$code = [];
foreach (['csp', 'cspReportOnly'] as $key) {
if (empty($config->$key)) {
continue;
}
$value = self::buildPolicy($config->$key);
if (strpos($value, "'nonce'")) {
$code[0] = '$cspNonce = base64_encode(random_bytes(16));';
$value = Nette\DI\ContainerBuilder::literal(
'str_replace(?, ? . $cspNonce, ?)',
'str_replace(?, ? . (isset($cspNonce) \? $cspNonce : $cspNonce = base64_encode(random_bytes(16))), ?)',
["'nonce", "'nonce-", $value]
);
}
Expand All @@ -116,16 +113,16 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
$headers['Feature-Policy'] = self::buildPolicy($config->featurePolicy);
}

$code[] = Helpers::formatArgs('$response = $this->getService(?);', [$this->prefix('response')]);
$response = $builder->getDefinition($this->prefix('response'));
assert($response instanceof Nette\DI\Definitions\ServiceDefinition);

foreach ($headers as $key => $value) {
if ($value !== '') {
$code[] = Helpers::formatArgs('$response->setHeader(?, ?);', [$key, $value]);
$response->addSetup('?->setHeader(?, ?);', ['@self', $key, $value]);
}
}

$code[] = Helpers::formatArgs('$response->setCookie(...?);', [['nette-samesite', '1', 0, '/', null, null, true, 'Strict']]);

$initialize->addBody("(function () {\n\t" . implode("\n\t", $code) . "\n})();");
$response->addSetup('?->setCookie(...?)', ['@self', ['nette-samesite', '1', 0, '/', null, null, true, 'Strict']]);
}


Expand Down
4 changes: 2 additions & 2 deletions tests/Http.DI/HttpExtension.csp.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ EOD
eval($compiler->addConfig($config)->compile());

$container = new Container;
$container->initialize();
$container->getService('http.response');

$headers = headers_list();

Expand All @@ -59,5 +59,5 @@ echo ' '; @ob_flush(); flush();
Assert::true(headers_sent());

Assert::exception(function () use ($container) {
$container->initialize();
$container->createService('http.response');
}, Nette\InvalidStateException::class, 'Cannot send header after %a%');
2 changes: 1 addition & 1 deletion tests/Http.DI/HttpExtension.defaultHeaders.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ $compiler->addExtension('http', new HttpExtension);
eval($compiler->compile());

$container = new Container;
$container->initialize();
$container->getService('http.response');

$headers = headers_list();
Assert::contains('X-Frame-Options: SAMEORIGIN', $headers);
Expand Down
4 changes: 2 additions & 2 deletions tests/Http.DI/HttpExtension.featurePolicy.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ EOD
eval($compiler->addConfig($config)->compile());

$container = new Container;
$container->initialize();
$container->getService('http.response');

$headers = headers_list();
var_dump($headers);
Expand All @@ -48,5 +48,5 @@ echo ' '; @ob_flush(); flush();
Assert::true(headers_sent());

Assert::exception(function () use ($container) {
$container->initialize();
$container->createService('http.response');
}, Nette\InvalidStateException::class, 'Cannot send header after %a%');
4 changes: 2 additions & 2 deletions tests/Http.DI/HttpExtension.headers.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ EOD
eval($compiler->addConfig($config)->compile());

$container = new Container;
$container->initialize();
$container->getService('http.response');

$headers = headers_list();
Assert::contains('X-Frame-Options: SAMEORIGIN', $headers);
Expand All @@ -49,5 +49,5 @@ echo ' '; @ob_flush(); flush();
Assert::true(headers_sent());

Assert::exception(function () use ($container) {
$container->initialize();
$container->createService('http.response');
}, Nette\InvalidStateException::class, 'Cannot send header after %a%');
2 changes: 1 addition & 1 deletion tests/Http.DI/HttpExtension.sameSiteProtection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $compiler->addExtension('http', new HttpExtension);
eval($compiler->compile());

$container = new Container;
$container->initialize();
$container->getService('http.response');

$headers = headers_list();
Assert::contains(
Expand Down