-
-
Notifications
You must be signed in to change notification settings - Fork 166
feat(http): add cookie config options to control unencrypted cookie discarding #2132
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
rexpl
wants to merge
7
commits into
tempestphp:3.x
Choose a base branch
from
rexpl:fix/discard-unencrypted-cookies
base: 3.x
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.
+256
−7
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1f83448
feat(http): add CookieConfig to control plaintext cookies
rexpl 2a5b768
testing details
rexpl 40ecf51
config property docblocks
rexpl f3ec431
docs
rexpl 86c0259
tests
rexpl 10c3f77
Merge branch '3.x' into fix/discard-unencrypted-cookies
brendt bd6089f
chore: minor readability changes
innocenzi 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\Http\Cookie; | ||
|
|
||
| final class CookieConfig | ||
| { | ||
| /** | ||
| * | ||
| * @param bool $discardUnencryptedCookies Whether to discard cookies that cannot be decrypted. | ||
| * @param string[] $plaintextCookies List of cookies keys that will not be decrypted by Tempest. Outgoing whitelisted cookies will be sent to the browser in plaintext. | ||
| */ | ||
| public function __construct( | ||
| public bool $discardUnencryptedCookies = true, | ||
| public array $plaintextCookies = [], | ||
| ) {} | ||
| } | ||
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,198 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Integration\Http; | ||
|
|
||
| use PHPUnit\Framework\Attributes\Test; | ||
| use ReflectionClass; | ||
| use Tempest\Cryptography\Encryption\Encrypter; | ||
| use Tempest\Http\Cookie\Cookie; | ||
| use Tempest\Http\Cookie\CookieConfig; | ||
| use Tempest\Http\Request; | ||
| use Tempest\Http\Responses\Ok; | ||
| use Tempest\Reflection\MethodReflector; | ||
| use Tempest\Router\Get; | ||
| use Tests\Tempest\Integration\FrameworkIntegrationTestCase; | ||
|
|
||
| final class CookieHandlingTest extends FrameworkIntegrationTestCase | ||
| { | ||
| #[Test] | ||
| public function encrypted_cookies_are_kept_when_default(): void | ||
| { | ||
| try { | ||
| $encrypter = $this->container->get(Encrypter::class); | ||
| $_COOKIE['Cookie_name'] = $encrypter->encrypt('myCookieValue')->serialize(); | ||
|
|
||
| $responseHelper = $this->http | ||
| ->registerRoute($this->returnCookieValueController()) | ||
| ->get('/get_cookie_value') | ||
| ->assertOk() | ||
| ->assertSee('myCookieValue'); | ||
|
|
||
| foreach ($responseHelper->headers as $header) { | ||
| if ($header->name !== 'set-cookie') { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($header->values as $value) { | ||
| $this->assertNotEquals( | ||
| $value, | ||
| 'Cookie_name=; Expires=Wed, 31-Dec-1969 23:59:59 GMT; Max-Age=0; Path=/; Secure; SameSite=Lax', | ||
| ); | ||
| } | ||
| } | ||
| } finally { | ||
| unset($_COOKIE['Cookie_name']); | ||
| } | ||
| } | ||
|
|
||
| #[Test] | ||
| public function unencrypted_cookies_are_discarded_when_default(): void | ||
| { | ||
| try { | ||
| $_COOKIE['Cookie_name'] = 'myCookieValue'; | ||
|
|
||
| $this->http | ||
| ->registerRoute($this->returnCookieValueController()) | ||
| ->get('/get_cookie_value') | ||
| ->assertOk() | ||
| ->assertHeaderMatches('set-cookie', 'Cookie_name=; Expires=Wed, 31-Dec-1969 23:59:59 GMT; Max-Age=0; Path=/; Secure; SameSite=Lax') | ||
| ->assertNotSee('myCookieValue'); | ||
| } finally { | ||
| unset($_COOKIE['Cookie_name']); | ||
| } | ||
| } | ||
|
|
||
| #[Test] | ||
| public function unencrypted_cookies_are_kept_when_discard_false(): void | ||
| { | ||
| $this->container->config(new CookieConfig(discardUnencryptedCookies: false)); | ||
|
|
||
| try { | ||
| $_COOKIE['Cookie_name'] = 'myCookieValue'; | ||
|
|
||
| $responseHelper = $this->http | ||
| ->registerRoute($this->returnCookieValueController()) | ||
| ->get('/get_cookie_value') | ||
| ->assertOk() | ||
| ->assertNotSee('myCookieValue'); // cookies are not discarded but not whitelisted so not available | ||
|
|
||
| foreach ($responseHelper->headers as $header) { | ||
| if ($header->name !== 'set-cookie') { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($header->values as $value) { | ||
| $this->assertNotEquals( | ||
| $value, | ||
| 'Cookie_name=; Expires=Wed, 31-Dec-1969 23:59:59 GMT; Max-Age=0; Path=/; Secure; SameSite=Lax', | ||
| ); | ||
| } | ||
| } | ||
| } finally { | ||
| unset($_COOKIE['Cookie_name']); | ||
| } | ||
| } | ||
|
|
||
| #[Test] | ||
| public function unencrypted_cookies_are_discarded_when_discard_true(): void | ||
| { | ||
| $this->container->config(new CookieConfig(discardUnencryptedCookies: true)); | ||
|
|
||
| try { | ||
| $_COOKIE['Cookie_name'] = 'myCookieValue'; | ||
|
|
||
| $this->http | ||
| ->registerRoute($this->returnCookieValueController()) | ||
| ->get('/get_cookie_value') | ||
| ->assertOk() | ||
| ->assertHeaderMatches('set-cookie', 'Cookie_name=; Expires=Wed, 31-Dec-1969 23:59:59 GMT; Max-Age=0; Path=/; Secure; SameSite=Lax') | ||
| ->assertNotSee('myCookieValue'); | ||
| } finally { | ||
| unset($_COOKIE['Cookie_name']); | ||
| } | ||
| } | ||
|
|
||
| #[Test] | ||
| public function whitelisted_plaintext_cookies_are_kept(): void | ||
| { | ||
| $this->container->config(new CookieConfig( | ||
| discardUnencryptedCookies: true, | ||
| plaintextCookies: ['Cookie_name'], | ||
| )); | ||
|
|
||
| try { | ||
| $_COOKIE['Cookie_name'] = 'myCookieValue'; | ||
|
|
||
| $responseHelper = $this->http | ||
| ->registerRoute($this->returnCookieValueController()) | ||
| ->get('/get_cookie_value') | ||
| ->assertOk() | ||
| ->assertSee('myCookieValue'); | ||
|
|
||
| foreach ($responseHelper->headers as $header) { | ||
| if ($header->name !== 'set-cookie') { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($header->values as $value) { | ||
| $this->assertNotEquals( | ||
| $value, | ||
| 'Cookie_name=; Expires=Wed, 31-Dec-1969 23:59:59 GMT; Max-Age=0; Path=/; Secure; SameSite=Lax', | ||
| ); | ||
| } | ||
| } | ||
| } finally { | ||
| unset($_COOKIE['Cookie_name']); | ||
| } | ||
| } | ||
|
|
||
| #[Test] | ||
| public function whitelisted_plaintext_cookies_are_send_in_plain(): void | ||
| { | ||
| $this->container->config(new CookieConfig( | ||
| plaintextCookies: ['Cookie_name'], | ||
| )); | ||
|
|
||
| $controller = new class { | ||
| #[Get('/test_whitelisted_unencrypted_cookies_are_send_in_plain')] | ||
| public function __invoke(): Ok | ||
| { | ||
| return new Ok()->addCookie( | ||
| new Cookie( | ||
| key: 'Cookie_name', | ||
| value: 'value', | ||
| ), | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| $reflection = new ReflectionClass($controller); | ||
| $method = $reflection->getMethod('__invoke'); | ||
|
|
||
| $this->http | ||
| ->registerRoute(new MethodReflector($method)) | ||
| ->get('/test_whitelisted_unencrypted_cookies_are_send_in_plain') | ||
| ->assertOk() | ||
| ->assertHeaderMatches('set-cookie', 'Cookie_name=value; Path=/; Secure; SameSite=Lax'); | ||
| } | ||
|
|
||
| private function returnCookieValueController(): MethodReflector | ||
| { | ||
| $controller = new class() { | ||
| #[Get('/get_cookie_value')] | ||
| public function __invoke(Request $request): Ok | ||
| { | ||
| return new Ok( | ||
| $request->getCookie('Cookie_name')->value ?? '', | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| $reflection = new ReflectionClass($controller); | ||
| $method = $reflection->getMethod('__invoke'); | ||
|
|
||
| return new MethodReflector($method); | ||
| } | ||
| } |
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.
Can you add docblocks to describe the functionality of each option?