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

Add StubCsrfToken #53

Merged
merged 7 commits into from
Feb 8, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 2.0.1 under development

- New #53: Add `StubCsrfToken` (@hacan359)
- Bug #36: Explicitly add transitive dependencies `yiisoft/strings`, `psr/http-server-handler`
and `ext-hash` (@vjik, @xepozz)

Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
The package provides [PSR-15](https://www.php-fig.org/psr/psr-15/) middleware for CSRF protection:

- It supports two algorithms out of the box:
- Synchronizer CSRF token with customizable token generation and storage. By default, it uses random data and session.
- Synchronizer CSRF token with customizable token generation and storage. By default, it uses random data and
session.
- HMAC based token with customizable identity generation. Uses session by default.
- It has ability to apply masking to CSRF token string to make [BREACH attack](https://breachattack.com/) impossible.

Expand All @@ -31,7 +32,7 @@ The package provides [PSR-15](https://www.php-fig.org/psr/psr-15/) middleware fo
The package could be installed with composer:

```shell
composer require yiisoft/csrf --prefer-dist
composer require yiisoft/csrf
```

## General usage
Expand Down Expand Up @@ -122,7 +123,7 @@ token that came from the form is compared against the token stored.
Package provides `RandomCsrfTokenGenerator` that generates a random token and
`SessionCsrfTokenStorage` that persists a token between requests in a user session.

To learn more about the synchronizer token pattern,
To learn more about the synchronizer token pattern,
[check OWASP CSRF cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#synchronizer-token-pattern).

### HMAC based token
Expand All @@ -144,6 +145,12 @@ Parameters set via the `HmacCsrfToken` constructor are:
To learn more about HMAC based token pattern
[check OWASP CSRF cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#hmac-based-token-pattern).

### Stub CSRF token

The `StubCsrfToken` simply stores and returns a token string. It does not perform any additional validation.
This implementation can be useful when mocking CSRF token behavior during unit testing or when providing
placeholder functionality in temporary solutions.

### Masked CSRF token

`MaskedCsrfToken` is a decorator for `CsrfTokenInterface` that applies masking to a token string.
Expand Down Expand Up @@ -181,7 +188,8 @@ The code is statically analyzed with [Psalm](https://psalm.dev/). To run static

## License

The Yii CSRF Protection Library is free software. It is released under the terms of the BSD License. Please see [`LICENSE`](./LICENSE.md) for more information.
The Yii CSRF Protection Library is free software. It is released under the terms of the BSD License. Please
see [`LICENSE`](./LICENSE.md) for more information.

Maintained by [Yii Software](https://www.yiiframework.com/).

Expand Down
36 changes: 36 additions & 0 deletions src/StubCsrfToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Csrf;

use Yiisoft\Security\Random;

/**
* `StubCsrfToken` represents a simple implementation of `CsrfTokenInterface`.
*
* This implementation simply stores and returns a token string. It does not perform any additional validation.
* It is primarily used for testing or as a placeholder implementation.
*/
final class StubCsrfToken implements CsrfTokenInterface
{
private string $token;

public function __construct(?string $token = null)
{
if (null === $token) {
$token = Random::string();
}
$this->token = $token;
}

public function getValue(): string
{
return $this->token;
}

public function validate(string $token): bool
{
return $this->token === $token;
}
}
32 changes: 32 additions & 0 deletions tests/StubCsrfTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Csrf\Tests;

use PHPUnit\Framework\TestCase;
use Yiisoft\Csrf\StubCsrfToken;

final class StubCsrfTokenTest extends TestCase
{
public function testValue(): void
{
$stubToken = new StubCsrfToken('test');
$this->assertSame('test', $stubToken->getValue());
}

public function testValidate(): void
{
$stubToken = new StubCsrfToken('test');
$this->assertTrue($stubToken->validate('test'));
$this->assertFalse($stubToken->validate('other'));
}

public function testEmptyToken(): void
{
$stubToken = new StubCsrfToken();
$token = $stubToken->getValue();
$this->assertNotEmpty($token);
$this->assertTrue($stubToken->validate($token));
}
}
Loading