Skip to content

Commit df15a3f

Browse files
authored
Merge pull request #5228 from kenjis/csrf-put-patch-delete
Add CSRF Protection for PUT/PATCH/DELETE
2 parents a087518 + 070cdb4 commit df15a3f

8 files changed

Lines changed: 177 additions & 99 deletions

File tree

system/Filters/Filters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ protected function processMethods()
443443
}
444444

445445
// Request method won't be set for CLI-based requests
446-
$method = strtolower($_SERVER['REQUEST_METHOD'] ?? 'cli');
446+
$method = strtolower($this->request->getMethod()) ?? 'cli';
447447

448448
if (array_key_exists($method, $this->config->methods)) {
449449
$this->filters['before'] = array_merge($this->filters['before'], $this->config->methods[$method]);

system/Security/Security.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,20 @@ public function getCSRFTokenName(): string
259259
*
260260
* @throws SecurityException
261261
*
262-
* @return $this|false
262+
* @return $this
263263
*/
264264
public function verify(RequestInterface $request)
265265
{
266-
// If it's not a POST request we will set the CSRF cookie.
267-
if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST') {
268-
return $this->sendCookie($request);
266+
// Protects POST, PUT, DELETE, PATCH
267+
$method = strtoupper($request->getMethod());
268+
$methodsToProtect = ['POST', 'PUT', 'DELETE', 'PATCH'];
269+
if (! in_array($method, $methodsToProtect, true)) {
270+
return $this;
269271
}
270272

271273
$token = $this->getPostedToken($request);
272274

273-
// Does the tokens exist in both the POST/POSTed JSON and COOKIE arrays and match?
275+
// Do the tokens match?
274276
if (! isset($token, $this->hash) || ! hash_equals($this->hash, $token)) {
275277
throw SecurityException::forDisallowedAction();
276278
}

0 commit comments

Comments
 (0)