Skip to content

Commit

Permalink
added option to control body parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbaturin committed May 15, 2024
1 parent 2e85441 commit b62935b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/CsrfMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class CsrfMiddleware implements MiddlewareInterface

private string $parameterName = self::PARAMETER_NAME;
private string $headerName = self::HEADER_NAME;
private bool $parseBody = true;

private ResponseFactoryInterface $responseFactory;
private CsrfTokenInterface $token;
Expand Down Expand Up @@ -73,6 +74,13 @@ public function withHeaderName(string $name): self
return $new;
}

public function withParseBody(bool $parseBody): self

Check warning on line 77 in src/CsrfMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/CsrfMiddleware.php#L77

Added line #L77 was not covered by tests
{
$new = clone $this;
$new->parseBody = $parseBody;
return $new;

Check warning on line 81 in src/CsrfMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/CsrfMiddleware.php#L79-L81

Added lines #L79 - L81 were not covered by tests
}

public function getParameterName(): string
{
return $this->parameterName;
Expand All @@ -83,6 +91,11 @@ public function getHeaderName(): string
return $this->headerName;
}

public function getParseBody(): bool

Check warning on line 94 in src/CsrfMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/CsrfMiddleware.php#L94

Added line #L94 was not covered by tests
{
return $this->parseBody;

Check warning on line 96 in src/CsrfMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/CsrfMiddleware.php#L96

Added line #L96 was not covered by tests
}

private function validateCsrfToken(ServerRequestInterface $request): bool
{
if (in_array($request->getMethod(), [Method::GET, Method::HEAD, Method::OPTIONS], true)) {
Expand All @@ -96,12 +109,12 @@ private function validateCsrfToken(ServerRequestInterface $request): bool

private function getTokenFromRequest(ServerRequestInterface $request): ?string
{
$parsedBody = $request->getParsedBody();
$headers = $request->getHeader($this->headerName);
$token = reset($headers);

$token = $parsedBody[$this->parameterName] ?? null;
if (empty($token)) {
$headers = $request->getHeader($this->headerName);
$token = reset($headers);
if (empty($token) && $this->parseBody) {
$parsedBody = $request->getParsedBody();
$token = $parsedBody[$this->parameterName] ?? null;
}

return is_string($token) ? $token : null;
Expand Down
21 changes: 21 additions & 0 deletions src/EmptyCsrfToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Csrf;

/**
* `EmptyCsrfToken` represents an implementation of `CsrfTokenInterface` with empty value.
*/
final class EmptyCsrfToken implements CsrfTokenInterface
{
public function getValue(): string

Check warning on line 12 in src/EmptyCsrfToken.php

View check run for this annotation

Codecov / codecov/patch

src/EmptyCsrfToken.php#L12

Added line #L12 was not covered by tests
{
return '';

Check warning on line 14 in src/EmptyCsrfToken.php

View check run for this annotation

Codecov / codecov/patch

src/EmptyCsrfToken.php#L14

Added line #L14 was not covered by tests
}

public function validate(string $token): bool

Check warning on line 17 in src/EmptyCsrfToken.php

View check run for this annotation

Codecov / codecov/patch

src/EmptyCsrfToken.php#L17

Added line #L17 was not covered by tests
{
return true;

Check warning on line 19 in src/EmptyCsrfToken.php

View check run for this annotation

Codecov / codecov/patch

src/EmptyCsrfToken.php#L19

Added line #L19 was not covered by tests
}
}

0 comments on commit b62935b

Please sign in to comment.