-
-
Notifications
You must be signed in to change notification settings - Fork 8
Add CsrfTokenCookieMiddleware
#100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vjik
wants to merge
13
commits into
master
Choose a base branch
from
token-middleware
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5c4505b
Add CsrfTokenCookieMiddleware
vjik 3035fed
Apply PHP CS Fixer and Rector changes (CI)
github-actions[bot] dee9227
fix
vjik ee9a0c8
fix
vjik 45c0d2d
Merge remote-tracking branch 'origin/token-middleware' into token-mid…
vjik 0f55242
fix
vjik 92f9c97
fix
vjik 369e1ac
fix
vjik 2549d62
Resolve CsrfTokenCookieMiddleware "secure" flag from request scheme w…
vjik 27681e6
Add $cacheControl option to CsrfTokenCookieMiddleware
vjik 331c90f
Always send Secure for SameSite=None CSRF cookie
vjik a3a528d
fix
vjik 907ed93
Let $cacheControl override an existing Cache-Control header
vjik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Csrf; | ||
|
|
||
| use InvalidArgumentException; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Http\Message\ServerRequestInterface; | ||
| use Psr\Http\Server\MiddlewareInterface; | ||
| use Psr\Http\Server\RequestHandlerInterface; | ||
| use Yiisoft\Http\Header; | ||
|
|
||
| use function gettype; | ||
| use function implode; | ||
| use function in_array; | ||
| use function is_bool; | ||
| use function is_string; | ||
| use function preg_match; | ||
| use function rawurlencode; | ||
| use function sprintf; | ||
|
|
||
| /** | ||
| * PSR-15 middleware that publishes the current CSRF token in a JavaScript-readable response cookie. | ||
| * | ||
| * It is intended for AJAX/SPA clients that read the token from a cookie and send it back explicitly in a request | ||
| * header (the "cookie-to-header" pattern), for example Inertia and Axios which use the `XSRF-TOKEN` cookie and the | ||
| * `X-XSRF-TOKEN` header. | ||
| * | ||
| * The middleware only writes the cookie. Token validation stays the responsibility of {@see CsrfTokenMiddleware}, | ||
| * which reads the submitted token from a header or a body parameter. Place this middleware before | ||
| * {@see CsrfTokenMiddleware} in the stack, so the cookie is published even on a rejected request. | ||
| * | ||
| * A `Set-Cookie` header does not by itself prevent a response from being cached (RFC 9111 section 7.3). A cached | ||
| * response may replay a stale token, or reach a shared (proxy or CDN) cache and hand one user's token to another. By | ||
| * default the middleware guards against this by setting `Cache-Control: no-store` on every response it touches; see | ||
| * the `$cacheControl` constructor parameter to change or disable this. | ||
| * | ||
| * @link https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#alternative-using-a-double-submit-cookie-pattern | ||
| * @link https://www.rfc-editor.org/rfc/rfc9111#section-7.3 | ||
| */ | ||
| final class CsrfTokenCookieMiddleware implements MiddlewareInterface | ||
| { | ||
| public const COOKIE_NAME = 'XSRF-TOKEN'; | ||
|
|
||
| public const SAME_SITE_LAX = 'Lax'; | ||
| public const SAME_SITE_STRICT = 'Strict'; | ||
| public const SAME_SITE_NONE = 'None'; | ||
|
|
||
| /** | ||
| * Control characters (including CR and LF) and the `;` attribute separator. These would let a configured | ||
| * cookie name, path or domain inject extra attributes or split the response header. Whether the values are | ||
| * otherwise well-formed is left to the caller. | ||
| */ | ||
| private const PATTERN_HEADER_INJECTION = '/[\x00-\x1F\x7F\x3B]/'; | ||
|
|
||
| private CsrfTokenInterface $token; | ||
| private string $cookieName; | ||
| private string $path; | ||
| private ?string $domain; | ||
| private ?bool $secure; | ||
| private ?string $sameSite; | ||
|
|
||
| /** | ||
| * @var bool|string | ||
| */ | ||
| private $cacheControl; | ||
|
|
||
| /** | ||
| * The cookie name, path and domain are checked only for control characters and `;` to prevent response header | ||
| * injection; making sure they are otherwise valid is up to the caller. | ||
| * | ||
| * @param CsrfTokenInterface $token The CSRF token to publish. | ||
| * @param string $cookieName The name of the cookie holding the token. | ||
| * @param string $path The `Path` attribute of the cookie. | ||
| * @param string|null $domain The `Domain` attribute of the cookie, or `null` to omit it. | ||
| * @param bool|null $secure Whether the cookie should only be sent over HTTPS. A non-null value is used as given. | ||
| * When `null`, it is resolved automatically: `true` for `self::SAME_SITE_NONE` (browsers require `Secure` for such | ||
| * cookies), otherwise from the request URI scheme (`true` when the scheme is `https`). | ||
| * @param string|null $sameSite The `SameSite` attribute of the cookie: one of `self::SAME_SITE_LAX`, | ||
| * `self::SAME_SITE_STRICT`, `self::SAME_SITE_NONE`, or `null` to omit it. When `self::SAME_SITE_NONE` is used, | ||
| * `$secure` must not be `false`. | ||
| * @param bool|string $cacheControl How to handle the `Cache-Control` response header: `true` to set it to | ||
| * `no-store` (keeping the published token out of any cache), `false` to leave the header untouched, or a string | ||
| * to set it to that exact value. `true` and a string value replace an existing `Cache-Control`. | ||
| * | ||
| * @throws InvalidArgumentException When a cookie attribute or the `$cacheControl` argument is not valid. | ||
| */ | ||
| public function __construct( | ||
| CsrfTokenInterface $token, | ||
| string $cookieName = self::COOKIE_NAME, | ||
| string $path = '/', | ||
| ?string $domain = null, | ||
| ?bool $secure = null, | ||
| ?string $sameSite = self::SAME_SITE_LAX, | ||
| $cacheControl = true | ||
| ) { | ||
| if (preg_match(self::PATTERN_HEADER_INJECTION, $cookieName)) { | ||
| throw new InvalidArgumentException( | ||
| sprintf('The cookie name "%s" contains invalid characters.', $cookieName), | ||
| ); | ||
| } | ||
|
|
||
| if (preg_match(self::PATTERN_HEADER_INJECTION, $path)) { | ||
| throw new InvalidArgumentException( | ||
| sprintf('The cookie path "%s" contains invalid characters.', $path), | ||
| ); | ||
| } | ||
|
|
||
| if ($domain !== null && preg_match(self::PATTERN_HEADER_INJECTION, $domain)) { | ||
| throw new InvalidArgumentException( | ||
| sprintf('The cookie domain "%s" contains invalid characters.', $domain), | ||
| ); | ||
| } | ||
|
|
||
| $allowedSameSite = [self::SAME_SITE_LAX, self::SAME_SITE_STRICT, self::SAME_SITE_NONE]; | ||
| if ($sameSite !== null && !in_array($sameSite, $allowedSameSite, true)) { | ||
| throw new InvalidArgumentException( | ||
| sprintf('The "SameSite" attribute value "%s" is not valid.', $sameSite), | ||
| ); | ||
| } | ||
|
|
||
| if ($sameSite === self::SAME_SITE_NONE && $secure === false) { | ||
| throw new InvalidArgumentException( | ||
| 'The "secure" flag is required for cookies with "SameSite" attribute set to "None".', | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * The parameter has no native type because a `bool|string` union is not available on PHP 7.4, so a value of | ||
| * any other type can still be passed at runtime. Once the minimum PHP version is 8.0+, type the parameter as | ||
| * `bool|string` and remove this check together with the suppression below. | ||
| * | ||
| * @psalm-suppress DocblockTypeContradiction | ||
| */ | ||
| if (!is_bool($cacheControl) && !is_string($cacheControl)) { | ||
| throw new InvalidArgumentException( | ||
| sprintf('The "cacheControl" argument must be a bool or a string, "%s" given.', gettype($cacheControl)), | ||
| ); | ||
| } | ||
|
|
||
| $this->token = $token; | ||
| $this->cookieName = $cookieName; | ||
| $this->path = $path; | ||
| $this->domain = $domain; | ||
|
vjik marked this conversation as resolved.
|
||
| $this->secure = $secure; | ||
| $this->sameSite = $sameSite; | ||
| $this->cacheControl = $cacheControl; | ||
| } | ||
|
|
||
| public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
| { | ||
| $response = $handler->handle($request); | ||
|
|
||
| $response = $response->withAddedHeader(Header::SET_COOKIE, $this->buildCookieHeaderValue($request)); | ||
|
|
||
| return $this->applyCacheControl($response); | ||
| } | ||
|
|
||
| private function buildCookieHeaderValue(ServerRequestInterface $request): string | ||
| { | ||
| $secure = $this->secure | ||
| ?? ($this->sameSite === self::SAME_SITE_NONE || $request->getUri()->getScheme() === 'https'); | ||
|
|
||
| $parts = [$this->cookieName . '=' . rawurlencode($this->token->getValue())]; | ||
|
|
||
| if ($this->domain !== null) { | ||
| $parts[] = 'Domain=' . $this->domain; | ||
| } | ||
|
|
||
| $parts[] = 'Path=' . $this->path; | ||
|
|
||
| if ($secure) { | ||
| $parts[] = 'Secure'; | ||
| } | ||
|
|
||
| if ($this->sameSite !== null) { | ||
| $parts[] = 'SameSite=' . $this->sameSite; | ||
| } | ||
|
|
||
| return implode('; ', $parts); | ||
| } | ||
|
|
||
| private function applyCacheControl(ResponseInterface $response): ResponseInterface | ||
| { | ||
| if ($this->cacheControl === false) { | ||
| return $response; | ||
| } | ||
|
|
||
| return $response->withHeader( | ||
| Header::CACHE_CONTROL, | ||
| $this->cacheControl === true ? 'no-store' : $this->cacheControl, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it include
=as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
=only matters forcookieName, but this pattern also validates path and domain, where=is. This class doesn't aim to fully validate cookie attributes against the RFC — only to keep the response header safe frominjection. So
=doesn't belong here.