Skip to content

Commit 4493f9b

Browse files
committed
HttpExtension: added option sameSiteProtection and Request::isSameSite()
1 parent 86d83ce commit 4493f9b

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/Bridges/HttpDI/HttpExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class HttpExtension extends Nette\DI\CompilerExtension
2828
'cspReportOnly' => [], // Content-Security-Policy-Report-Only
2929
'featurePolicy' => [], // Feature-Policy
3030
'secureCookie' => 'auto', // true|false|auto Whether the cookie is available only through HTTPS
31+
'sameSiteProtection' => null, // activate Response::isSameSite() protection
3132
];
3233

3334
/** @var bool */
@@ -127,6 +128,10 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
127128
$initialize->addBody('$this->getService(?)->setHeader(?, ?);', [$this->prefix('response'), $key, $value]);
128129
}
129130
}
131+
132+
if (!empty($config['sameSiteProtection'])) {
133+
$initialize->addBody('$this->getService(?)->setCookie(...?);', [$this->prefix('response'), ['nette-samesite', '1', 0, null, null, null, true, 'Strict']]);
134+
}
130135
}
131136

132137

src/Http/Request.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,15 @@ public function isSecured(): bool
227227
}
228228

229229

230+
/**
231+
* Is the request sent from the same origin?
232+
*/
233+
public function isSameSite(): bool
234+
{
235+
return !empty($this->cookies['nette-samesite']);
236+
}
237+
238+
230239
/**
231240
* Is AJAX request?
232241
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Bridges\HttpDI\HttpExtension;
6+
use Nette\Bridges\HttpDI\SessionExtension;
7+
use Nette\DI;
8+
use Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
if (PHP_SAPI === 'cli') {
14+
Tester\Environment::skip('Headers are not testable in CLI mode');
15+
}
16+
17+
18+
$compiler = new DI\Compiler;
19+
$compiler->addExtension('http', new HttpExtension);
20+
$compiler->addExtension('session', new SessionExtension(false, PHP_SAPI === 'cli'));
21+
22+
$loader = new DI\Config\Loader;
23+
$config = $loader->load(Tester\FileMock::create('
24+
http:
25+
sameSiteProtection: yes
26+
', 'neon'));
27+
28+
eval($compiler->addConfig($config)->compile());
29+
30+
$container = new Container;
31+
$container->initialize();
32+
33+
$headers = headers_list();
34+
Assert::contains(
35+
PHP_VERSION_ID >= 70300
36+
? 'Set-Cookie: nette-samesite=1; path=/; HttpOnly; SameSite=Strict'
37+
: 'Set-Cookie: nette-samesite=1; path=/; SameSite=Strict; HttpOnly',
38+
$headers
39+
);
40+
Assert::same('Lax', $container->getService('session.session')->getOptions()['cookie_samesite']);

0 commit comments

Comments
 (0)