Skip to content
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

get csrf once,avoid init csrfMiddleware from view injection #132

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/CsrfParametersInjectionInterface
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\View\Renderer;

/**
* CsrfParametersInjectionInterface is an interface that must be implemented by classes to inject Csrf Parameters.
*
* @psalm-type csrfParameters = array<array-key, \Yiisoft\Html\Tag\Meta|array<string,mixed>|string>
*/
interface CsrfParametersInjectionInterface
{
/**
* Returns array of csrf parameters,then merge to common parameters and metaTag parameters.
*
*
* For example:
*
* ```php
* [
* [ '_csrf' => $csrf ],
* [
* 'csrf' => [
* 'name' => 'csrf' ,
* 'content' => $tokenValue
* ],
* ],
* ...
* ]
* ```
*
* @return array
*
* @psalm-return csrfParameters
*/
public function getCsrfParameters(): array;
}
61 changes: 20 additions & 41 deletions src/CsrfViewInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,51 @@
namespace Yiisoft\Yii\View\Renderer;

use LogicException;
use Yiisoft\Csrf\CsrfMiddleware;
use Yiisoft\Csrf\CsrfTokenInterface;
use Yiisoft\Csrf\CsrfTrait;

/**
* `CsrfViewInjection` injects the necessary data into the view to protect against a CSRF attack.
*/
final class CsrfViewInjection implements CommonParametersInjectionInterface, MetaTagsInjectionInterface
final class CsrfViewInjection implements CsrfParametersInjectionInterface

Check failure on line 14 in src/CsrfViewInjection.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedClass

src/CsrfViewInjection.php:14:42: UndefinedClass: Class, interface or enum named Yiisoft\Yii\View\Renderer\CsrfParametersInjectionInterface does not exist (see https://psalm.dev/019)

Check failure on line 14 in src/CsrfViewInjection.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

UndefinedClass

src/CsrfViewInjection.php:14:42: UndefinedClass: Class, interface or enum named Yiisoft\Yii\View\Renderer\CsrfParametersInjectionInterface does not exist (see https://psalm.dev/019)
{
use CsrfTrait;

public const DEFAULT_META_ATTRIBUTE_NAME = 'csrf';
public const DEFAULT_PARAMETER_NAME = 'csrf';
public const META_TAG_KEY = 'csrf';

private string $metaAttributeName = self::DEFAULT_META_ATTRIBUTE_NAME;
private string $parameterName = self::DEFAULT_PARAMETER_NAME;

public function __construct(private CsrfTokenInterface $token, private CsrfMiddleware $middleware)
{
}

/**
* Returns a new instance with the specified parameter name.
*
* @param string $parameterName The parameter name.
*/
public function withParameterName(string $parameterName): self
public function __construct(private CsrfTokenInterface $token)
{
$new = clone $this;
$new->parameterName = $parameterName;
return $new;
}

/**
* Returns a new instance with the specified meta attribute name.
*
* @param string $metaAttributeName The meta attribute name.
*/
public function withMetaAttributeName(string $metaAttributeName): self
{
$new = clone $this;
$new->metaAttributeName = $metaAttributeName;
return $new;
}

/**
* @throws LogicException when CSRF token is not defined
*/
public function getCommonParameters(): array
public function getCsrfParameters(): array
{
$tokenValue = $this->token->getValue();
$csrf = new Csrf(
$this->token->getValue(),
$this->middleware->getParameterName(),
$this->middleware->getHeaderName(),
$tokenValue,
$this->getFormParameterName(),
$this->getHeaderName(),
);
return [$this->parameterName => $csrf];
}

/**
* @throws LogicException when CSRF token is not defined
*/
public function getMetaTags(): array
{
return [
self::META_TAG_KEY => [
'name' => $this->metaAttributeName,
'content' => $this->token->getValue(),
[
$this->parameterName => $csrf
],
[
self::META_TAG_KEY => [
'name' => $this->metaAttributeName,
'content' => $tokenValue
],
],
];
}


}
27 changes: 23 additions & 4 deletions src/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,16 @@
*/
public function render(string $view, array $parameters = []): DataResponse
{
$commonParameters = $this->getCommonParameters();
$csrfParameters = $this->getCsrfParameters();
$commonParameters = array_merge($this->getCommonParameters(), $csrfParameters[0]);

Check failure on line 115 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidArrayOffset

src/ViewRenderer.php:115:71: InvalidArrayOffset: Cannot access value on variable $csrfParameters using offset value of '0', expecting string (see https://psalm.dev/115)

Check failure on line 115 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgument

src/ViewRenderer.php:115:71: MixedArgument: Argument 2 of array_merge cannot be mixed, expecting array<array-key, mixed> (see https://psalm.dev/030)

Check failure on line 115 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

InvalidArrayOffset

src/ViewRenderer.php:115:71: InvalidArrayOffset: Cannot access value on variable $csrfParameters using offset value of '0', expecting string (see https://psalm.dev/115)

Check failure on line 115 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArgument

src/ViewRenderer.php:115:71: MixedArgument: Argument 2 of array_merge cannot be mixed, expecting array<array-key, mixed> (see https://psalm.dev/030)
$layoutParameters = $this->getLayoutParameters();
$metaTags = $this->getMetaTags();
$metaTags = array_merge($this->getMetaTags(), $csrfParameters[1]);

Check failure on line 117 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidArrayOffset

src/ViewRenderer.php:117:55: InvalidArrayOffset: Cannot access value on variable $csrfParameters using offset value of '1', expecting string (see https://psalm.dev/115)

Check failure on line 117 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgument

src/ViewRenderer.php:117:55: MixedArgument: Argument 2 of array_merge cannot be mixed, expecting array<array-key, mixed> (see https://psalm.dev/030)

Check failure on line 117 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

InvalidArrayOffset

src/ViewRenderer.php:117:55: InvalidArrayOffset: Cannot access value on variable $csrfParameters using offset value of '1', expecting string (see https://psalm.dev/115)

Check failure on line 117 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArgument

src/ViewRenderer.php:117:55: MixedArgument: Argument 2 of array_merge cannot be mixed, expecting array<array-key, mixed> (see https://psalm.dev/030)
$linkTags = $this->getLinkTags();

return $this->responseFactory->createResponse(fn (): string => $this->renderProxy(
return $this->responseFactory->createResponse(fn(): string => $this->renderProxy(
$view,
$parameters,
$commonParameters,

Check failure on line 123 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgumentTypeCoercion

src/ViewRenderer.php:123:13: MixedArgumentTypeCoercion: Argument 3 of Yiisoft\Yii\View\Renderer\ViewRenderer::renderProxy expects array<string, mixed>, but parent type array<array-key, mixed> provided (see https://psalm.dev/194)

Check failure on line 123 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArgumentTypeCoercion

src/ViewRenderer.php:123:13: MixedArgumentTypeCoercion: Argument 3 of Yiisoft\Yii\View\Renderer\ViewRenderer::renderProxy expects array<string, mixed>, but parent type array<array-key, mixed> provided (see https://psalm.dev/194)
$layoutParameters,
$metaTags,
$linkTags,
Expand Down Expand Up @@ -336,7 +337,7 @@
$layoutParameters = array_filter(
$injectLayoutParameters,
/** @psalm-suppress MissingClosureParamType */
static fn ($_value, string $key): bool => !$currentView->hasParameter($key),
static fn($_value, string $key): bool => !$currentView->hasParameter($key),
ARRAY_FILTER_USE_BOTH,
);

Expand All @@ -345,6 +346,24 @@
->render($layout, ['content' => $content]);
}


/**
* Gets csrf injection parameters merged with parameters specified during rendering.
*
*
* @return array The csrf injection parameters ot merged to common parameters and metaTag parameters.
*
* @psalm-return array<string, mixed>

Check failure on line 356 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedReturnTypeCoercion

src/ViewRenderer.php:356:22: MixedReturnTypeCoercion: The declared return type 'array<string, mixed>' for Yiisoft\Yii\View\Renderer\ViewRenderer::getCsrfParameters is more specific than the inferred return type 'array<array-key, mixed>' (see https://psalm.dev/197)

Check failure on line 356 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedReturnTypeCoercion

src/ViewRenderer.php:356:22: MixedReturnTypeCoercion: The declared return type 'array<string, mixed>' for Yiisoft\Yii\View\Renderer\ViewRenderer::getCsrfParameters is more specific than the inferred return type 'array<array-key, mixed>' (see https://psalm.dev/197)
*/
private function getCsrfParameters(): array
{
$parameters = [];
foreach ($this->getInjections($this->layout, CsrfParametersInjectionInterface::class) as $injection) {

Check failure on line 361 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedClass

src/ViewRenderer.php:361:54: UndefinedClass: Class, interface or enum named Yiisoft\Yii\View\Renderer\CsrfParametersInjectionInterface does not exist (see https://psalm.dev/019)

Check failure on line 361 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedArgument

src/ViewRenderer.php:361:54: MixedArgument: Argument 2 of Yiisoft\Yii\View\Renderer\ViewRenderer::getInjections cannot be mixed, expecting class-string (see https://psalm.dev/030)

Check failure on line 361 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

UndefinedClass

src/ViewRenderer.php:361:54: UndefinedClass: Class, interface or enum named Yiisoft\Yii\View\Renderer\CsrfParametersInjectionInterface does not exist (see https://psalm.dev/019)

Check failure on line 361 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedArgument

src/ViewRenderer.php:361:54: MixedArgument: Argument 2 of Yiisoft\Yii\View\Renderer\ViewRenderer::getInjections cannot be mixed, expecting class-string (see https://psalm.dev/030)
$parameters[] = $injection->getCsrfParameters();

Check failure on line 362 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

MixedMethodCall

src/ViewRenderer.php:362:41: MixedMethodCall: Cannot determine the type of $injection when calling method getCsrfParameters (see https://psalm.dev/015)

Check failure on line 362 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

MixedMethodCall

src/ViewRenderer.php:362:41: MixedMethodCall: Cannot determine the type of $injection when calling method getCsrfParameters (see https://psalm.dev/015)
}
return array_merge(...$parameters);
}

/**
* Gets injection common parameters merged with parameters specified during rendering.
*
Expand Down
Loading