Skip to content

Commit 8bb7416

Browse files
author
Greg Bowler
committed
feature: remove default csrf protection while default cors behaviour is evaluated
1 parent 1123773 commit 8bb7416

File tree

2 files changed

+42
-84
lines changed

2 files changed

+42
-84
lines changed

composer.lock

Lines changed: 30 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Middleware/RequestHandler.php

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
use Gt\Config\Config;
55
use Gt\Config\ConfigFactory;
66
use Gt\Config\ConfigSection;
7-
use Gt\Csrf\HTMLDocumentProtector;
8-
use Gt\Csrf\SessionTokenStore;
97
use Gt\Dom\HTMLDocument;
108
use Gt\DomTemplate\ComponentExpander;
119
use Gt\DomTemplate\DocumentBinder;
@@ -149,6 +147,9 @@ public function handle(
149147
$serviceContainer->set($viewModel);
150148
}
151149

150+
$input = new Input($_GET, $_POST, $_FILES);
151+
$serviceContainer->set($input);
152+
152153
if($viewModel instanceof HTMLDocument) {
153154
try {
154155
$partial = new PartialContent(implode(DIRECTORY_SEPARATOR, [
@@ -195,67 +196,19 @@ public function handle(
195196
$sessionId
196197
);
197198
$serviceContainer->set($session);
198-
199-
$session = $serviceContainer->get(Session::class);
200-
201-
$shouldVerifyCsrf = true;
202-
$ignoredPathArray = explode(",", $this->config->getString("security.csrf_ignore_path") ?? "");
203-
foreach($ignoredPathArray as $ignoredPath) {
204-
if(empty($ignoredPath)) {
205-
continue;
206-
}
207-
208-
if(str_contains($ignoredPath, "*")) {
209-
$pattern = strtr(rtrim($ignoredPath, "/"), [
210-
"*" => ".*",
211-
]);
212-
if(preg_match("|$pattern|", rtrim($uriPath, "/"))) {
213-
$shouldVerifyCsrf = false;
214-
}
215-
}
216-
else {
217-
if(rtrim($uriPath, "/") === rtrim($ignoredPath, "/")) {
218-
$shouldVerifyCsrf = false;
219-
}
220-
}
221-
}
222-
223-
if($shouldVerifyCsrf) {
224-
$csrfTokenStore = new SessionTokenStore(
225-
$session->getStore("webengine.csrf", true),
226-
$this->config->getInt("security.csrf_max_tokens")
227-
);
228-
$csrfTokenStore->setTokenLength(
229-
$this->config->getInt("security.csrf_token_length")
230-
);
231-
232-
if($request->getMethod() === "POST") {
233-
$csrfTokenStore->verify($_POST);
234-
}
235-
236-
$sharing = match($this->config->getString("security.csrf_token_sharing")) {
237-
"per-page" => HTMLDocumentProtector::ONE_TOKEN_PER_PAGE,
238-
default => HTMLDocumentProtector::ONE_TOKEN_PER_FORM,
239-
};
240-
$protector = new HTMLDocumentProtector($viewModel, $csrfTokenStore);
241-
$tokens = $protector->protect($sharing);
242-
$response = $response->withHeader($this->config->getString("security.csrf_header"), $tokens);
243-
}
244199
}
245200

246-
$input = new Input($_GET, $_POST, $_FILES);
247-
$serviceContainer->set($input);
248-
249201
Protection::overrideInternals(
250202
Protection::removeGlobals($GLOBALS, [
251-
"_ENV" => explode(",", $this->config->getString("app.globals_whitelist_env") ?? ""),
252-
"_SERVER" => explode(",", $this->config->getString("app.globals_whitelist_server") ?? ""),
253-
"_GET" => explode(",", $this->config->getString("app.globals_whitelist_get") ?? ""),
254-
"_POST" => explode(",", $this->config->getString("app.globals_whitelist_post") ?? ""),
255-
"_FILES" => explode(",", $this->config->getString("app.globals_whitelist_files") ?? ""),
256-
"_COOKIES" => explode(",", $this->config->getString("app.globals_whitelist_cookies") ?? ""),
257-
]
258-
));
203+
"_ENV" => explode(",", $this->config->getString("app.globals_whitelist_env") ?? ""),
204+
"_SERVER" => explode(",", $this->config->getString("app.globals_whitelist_server") ?? ""),
205+
"_GET" => explode(",", $this->config->getString("app.globals_whitelist_get") ?? ""),
206+
"_POST" => explode(",", $this->config->getString("app.globals_whitelist_post") ?? ""),
207+
"_FILES" => explode(",", $this->config->getString("app.globals_whitelist_files") ?? ""),
208+
"_COOKIES" => explode(",", $this->config->getString("app.globals_whitelist_cookies") ?? ""),
209+
]
210+
)
211+
);
259212

260213
$injector = new Injector($serviceContainer);
261214

0 commit comments

Comments
 (0)