Skip to content

Commit 72bff50

Browse files
committed
dispense AntiForgery from HttpContext
1 parent 655145d commit 72bff50

File tree

8 files changed

+21
-73
lines changed

8 files changed

+21
-73
lines changed

lib/Endpoint/Filters/ValidateAntiForgery.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ public function __invoke(ActionContext $context, ActionDelegate $next): Task
2626
throw new AntiForgeryException("Unable to get IAntiForgery service, make sure to register it as a service!");
2727
}
2828

29-
$result = $antiForgery->validateToken($context->HttpContext);
29+
$method = $context->HttpContext->Request->Method;
30+
if ($method == 'GET') {
31+
return next($context);
32+
}
33+
34+
$formToken = $context->HttpContext->Request->Form->getValue($antiForgery->options->FieldName);
35+
$headerToken = $context->HttpContext->Request->Headers->getValues($antiForgery->options->FieldName)[0] ?? null;
36+
$formResult = $antiForgery->validateToken($formToken);
37+
$headerResult = $antiForgery->validateToken($headerToken);
3038

31-
if (!$result) {
39+
if (!$formResult && !$headerResult) {
3240
throw new AntiForgeryException("Invalid AntiForgery Token!", 403);
3341
}
3442

lib/Security/Session.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ class Session
1414
private string $name;
1515
private array $options = [];
1616

17-
public function __construct(string $name, ?int $lifetime = null, ?string $path = null)
17+
public function __construct(string $name, string $path = '/', ?int $lifetime = null)
1818
{
1919
$this->name = $name;
2020
$this->options['name'] = $name;
2121

22-
if (isset($lifetime)) {
23-
$this->options['cookie_lifetime'] = $lifetime;
22+
if (!empty($path)) {
23+
$this->options['cookie_path'] = $path;
2424
}
2525

26-
if (isset($path)) {
27-
$this->options['cookie_path'] = $path;
26+
if (isset($lifetime)) {
27+
$this->options['cookie_lifetime'] = $lifetime;
2828
}
2929

3030
if (isset($_COOKIE[$name])) {

lib/Security/Tokens/Csrf/Antiforgery.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace DevNet\Web\Security\Tokens\Csrf;
1010

1111
use DevNet\System\PropertyTrait;
12-
use DevNet\Web\Http\Message\HttpContext;
1312

1413
class AntiForgery implements IAntiForgery
1514
{
@@ -21,10 +20,6 @@ class AntiForgery implements IAntiForgery
2120

2221
public function __construct(AntiForgeryOptions $options)
2322
{
24-
if ($options->Cookie->HttpOnly === null) {
25-
$options->Cookie->HttpOnly = true;
26-
}
27-
2823
$this->options = $options;
2924
$this->generator = new AntiForgeryTokenGenerator();
3025
$this->store = new AntiForgeryTokenStore($options);
@@ -47,22 +42,9 @@ public function getToken(): AntiForgeryToken
4742
return $token;
4843
}
4944

50-
public function validateToken(HttpContext $httpContext): bool
45+
public function validateToken(string $token): bool
5146
{
52-
$method = $httpContext->Request->Method;
53-
if ($method == "GET") {
54-
return true;
55-
}
56-
57-
$token = $this->getToken();
58-
59-
$formToken = $httpContext->Request->Form->getValue($this->options->FieldName);
60-
if ($formToken == $token) {
61-
return true;
62-
}
63-
64-
$headerToken = $httpContext->Request->Headers->getValues($this->options->FieldName)[0] ?? null;
65-
if ($headerToken == $token) {
47+
if ($this->getToken() == $token) {
6648
return true;
6749
}
6850

lib/Security/Tokens/Csrf/AntiforgeryOptions.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,15 @@
88

99
namespace DevNet\Web\Security\Tokens\Csrf;
1010

11-
use DevNet\System\Runtime\LauncherProperties;
12-
use DevNet\Web\Http\Message\CookieOptions;
13-
1411
class AntiForgeryOptions
1512
{
16-
public CookieOptions $Cookie;
1713
public string $CookieName = "AntiForgery";
14+
public string $CookiePath = "/";
1815
public string $FieldName = "X-CSRF-TOKEN";
1916
public string $HeaderName = "X-XSRF-TOKEN";
2017

2118
public function __construct()
2219
{
23-
$this->Cookie = new CookieOptions();
24-
$this->CookieName = $this->CookieName . "-" . md5($this->CookieName . LauncherProperties::getRootDirectory());;
20+
$this->CookieName = $this->CookieName . "-" . md5($this->CookieName . $_SERVER['DOCUMENT_ROOT']);;
2521
}
2622
}

lib/Security/Tokens/Csrf/AntiforgeryTokenGenerator.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
namespace DevNet\Web\Security\Tokens\Csrf;
1010

11-
use DevNet\Web\Http\Message\HttpContext;
12-
1311
class AntiForgeryTokenGenerator
1412
{
1513
public function generateCookieToken(): AntiForgeryToken
@@ -21,15 +19,4 @@ public function generateRequestToken(string $cookieToken): AntiForgeryToken
2119
{
2220
return new AntiForgeryToken($cookieToken);
2321
}
24-
25-
public function matchTokens(HttpContext $httpContext, $tokens): bool
26-
{
27-
$formToken = $httpContext->Request->Form->getValue($tokens->FormFieldName);
28-
29-
if ($tokens->RequestToken == $formToken) {
30-
return true;
31-
}
32-
33-
return false;
34-
}
3522
}

lib/Security/Tokens/Csrf/AntiforgeryTokenSet.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

lib/Security/Tokens/Csrf/AntiforgeryTokenStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AntiForgeryTokenStore
1616

1717
public function __construct(AntiForgeryOptions $options)
1818
{
19-
$this->session = new Session($options->CookieName);
19+
$this->session = new Session($options->CookieName, $options->CookieName);
2020
}
2121

2222
public function saveCookieToken(AntiForgeryToken $token): void

lib/Security/Tokens/Csrf/IAntiforgery.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88

99
namespace DevNet\Web\Security\Tokens\Csrf;
1010

11-
use DevNet\Web\Http\Message\HttpContext;
12-
1311
interface IAntiForgery
1412
{
1513
public function getToken(): AntiForgeryToken;
1614

17-
public function validateToken(HttpContext $httpContext): bool;
15+
public function validateToken(string $token): bool;
1816
}

0 commit comments

Comments
 (0)