-
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
Open
simonLeary42
wants to merge
28
commits into
main
Choose a base branch
from
csrf
base: main
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
CSRF protection #389
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
7ce98ca
copilot 1st draft
simonLeary42 db933b0
remove form helpers
simonLeary42 f6ec3c3
remove constants
simonLeary42 40bb069
remove magic
simonLeary42 93b44f6
array_key_exists
simonLeary42 0ce0313
WIP single use tokens
simonLeary42 983eba4
remove normal token in favor of single use tokens
simonLeary42 3717068
rename function
simonLeary42 7b1a6db
better logging
simonLeary42 0b43ca5
fixup
simonLeary42 242a934
require
simonLeary42 436a564
refactor
simonLeary42 13e402b
add inputs to all forms
simonLeary42 05a1d51
validate in all pages
simonLeary42 06db60b
remove file
simonLeary42 5c37312
remove from init
simonLeary42 e2a8049
remove from ajax
simonLeary42 68c3baa
fixup tests
simonLeary42 7049556
init csrf_tokens array
simonLeary42 eb5409d
unique sessions for test cases
simonLeary42 16e819e
automatically generate csrf tokens for http_post in testing
simonLeary42 990ef60
add missing imports
simonLeary42 5514b56
don't overwrite tokens
simonLeary42 50b8568
validate in POST in header.php
simonLeary42 9e9954c
remove bad test
simonLeary42 7800a74
set csrf_tokens array even if unauthenticated
simonLeary42 02d652a
add session cleanup
simonLeary42 ab8aed0
write session data to filesystem immediately on cleanup
simonLeary42 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
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,66 @@ | ||
| <?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"]); | ||
| } | ||
| } | ||
| } | ||
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->assertArrayHasKey("csrf_tokens", $_SESSION); | ||
| CSRFToken::clear(); | ||
| $this->assertArrayNotHasKey("csrf_tokens", $_SESSION); | ||
| } | ||
|
|
||
simonLeary42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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
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.