-
Notifications
You must be signed in to change notification settings - Fork 11
CSRF protection #389
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
Merged
Merged
CSRF protection #389
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
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,67 @@ | ||
| <?php | ||
| namespace UnityWebPortal\lib; | ||
|
|
||
| class CSRFToken | ||
| { | ||
| private static function ensureSessionCSRFTokensSanity(): void | ||
| { | ||
| if (!isset($_SESSION)) { | ||
| throw new \RuntimeException("Session is not started. Call session_start() first."); | ||
| } | ||
| if (!array_key_exists("csrf_tokens", $_SESSION)) { | ||
| UnityHTTPD::errorLog( | ||
| "invalid session", | ||
| '$_SESSION has no array key "csrf_tokens"', | ||
| data: ['$_SESSION' => $_SESSION], | ||
| ); | ||
| $_SESSION["csrf_tokens"] = []; | ||
| } | ||
| if (!is_array($_SESSION["csrf_tokens"])) { | ||
| UnityHTTPD::errorLog( | ||
| "invalid session", | ||
| '$_SESSION["csrf_tokens"] is not an array', | ||
| data: ['$_SESSION' => $_SESSION], | ||
| ); | ||
| $_SESSION["csrf_tokens"] = []; | ||
| } | ||
| } | ||
|
|
||
| public static function generate(): string | ||
| { | ||
| self::ensureSessionCSRFTokensSanity(); | ||
| $token = bin2hex(random_bytes(32)); | ||
| $_SESSION["csrf_tokens"][$token] = false; | ||
| return $token; | ||
| } | ||
|
|
||
| public static function validate(string $token): bool | ||
| { | ||
| self::ensureSessionCSRFTokensSanity(); | ||
| if ($token === "") { | ||
| UnityHTTPD::errorLog("empty CSRF token", ""); | ||
| return false; | ||
| } | ||
| if (!array_key_exists($token, $_SESSION["csrf_tokens"])) { | ||
simonLeary42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| UnityHTTPD::errorLog("unknown CSRF token", $token); | ||
| return false; | ||
| } | ||
| $entry = $_SESSION["csrf_tokens"][$token]; | ||
| if ($entry === true) { | ||
| UnityHTTPD::errorLog("reused CSRF token", $token); | ||
| return false; | ||
| } | ||
| $_SESSION["csrf_tokens"][$token] = true; | ||
| return true; | ||
| } | ||
|
|
||
| public static function clear(): void | ||
| { | ||
| if (!isset($_SESSION)) { | ||
| return; | ||
| } | ||
| if (array_key_exists("csrf_tokens", $_SESSION)) { | ||
| unset($_SESSION["csrf_tokens"]); | ||
| } | ||
| $_SESSION["csrf_tokens"] = []; | ||
| } | ||
| } | ||
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
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,83 @@ | ||
| <?php | ||
| use PHPUnit\Framework\TestCase; | ||
| use UnityWebPortal\lib\CSRFToken; | ||
|
|
||
| class CSRFTokenTest extends TestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| session_id(uniqid()); | ||
| session_start(); | ||
| $_SESSION["csrf_tokens"] = []; | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| CSRFToken::clear(); | ||
| session_write_close(); | ||
| session_id(uniqid()); | ||
| } | ||
|
|
||
| public function testGenerateCreatesToken(): void | ||
| { | ||
| $token = CSRFToken::generate(); | ||
| $this->assertIsString($token); | ||
| $this->assertEquals(64, strlen($token)); | ||
| $this->assertMatchesRegularExpression('/^[0-9a-f]{64}$/', $token); | ||
| } | ||
|
|
||
| public function testGenerateStoresTokenInSession(): void | ||
| { | ||
| $token = CSRFToken::generate(); | ||
| $this->assertArrayHasKey("csrf_tokens", $_SESSION); | ||
| $this->assertArrayHasKey($token, $_SESSION["csrf_tokens"]); | ||
| $this->assertFalse($_SESSION["csrf_tokens"][$token]); | ||
| } | ||
|
|
||
| public function testValidateWithValidToken(): void | ||
| { | ||
| $token = CSRFToken::generate(); | ||
| $this->assertTrue(CSRFToken::validate($token)); | ||
| $this->assertTrue($_SESSION["csrf_tokens"][$token]); | ||
| } | ||
|
|
||
| public function testValidateWithInvalidToken(): void | ||
| { | ||
| CSRFToken::generate(); | ||
| $this->assertFalse(CSRFToken::validate("invalid_token")); | ||
| } | ||
|
|
||
| public function testValidateWithEmptyToken(): void | ||
| { | ||
| CSRFToken::generate(); | ||
| $this->assertFalse(CSRFToken::validate("")); | ||
| } | ||
|
|
||
| public function testValidateWithoutSessionToken(): void | ||
| { | ||
| $this->assertFalse(CSRFToken::validate("any_token")); | ||
| } | ||
|
|
||
| public function testClearRemovesToken(): void | ||
| { | ||
| CSRFToken::generate(); | ||
| $this->assertNotEmpty($_SESSION["csrf_tokens"]); | ||
| CSRFToken::clear(); | ||
| $this->assertEmpty($_SESSION["csrf_tokens"]); | ||
| } | ||
|
|
||
| public function testMultipleTokenGenerations(): void | ||
| { | ||
| $token1 = CSRFToken::generate(); | ||
| $token2 = CSRFToken::generate(); | ||
| $this->assertNotEquals($token1, $token2); | ||
| } | ||
|
|
||
| public function testTokenIsSingleUse(): void | ||
| { | ||
| $token = CSRFToken::generate(); | ||
| $this->assertTrue(CSRFToken::validate($token)); | ||
| $this->assertFalse(CSRFToken::validate($token)); | ||
| $this->assertTrue($_SESSION["csrf_tokens"][$token]); | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.