Skip to content

Commit

Permalink
Work around the missing DateTimeInterface|string types on setCookie()…
Browse files Browse the repository at this point in the history
…'s $expire param

Could be reverted if this PR is merged and released nette/http#237
  • Loading branch information
spaze committed Mar 30, 2024
1 parent 2725bcf commit 0a8a273
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
5 changes: 2 additions & 3 deletions site/app/EasterEgg/CrLfUrlInjections.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace MichalSpacekCz\EasterEgg;

use DateTimeImmutable;
use Nette\Http\IRequest;
use Nette\Http\IResponse;
use Nette\Utils\Strings;
Expand Down Expand Up @@ -33,8 +32,8 @@ public function detectAttempt(): bool
$this->httpResponse->setCookie(
self::COOKIE_NAME,
$match[1],
new DateTimeImmutable('-3 years 1 month 3 days 3 hours 7 minutes'),
'/expired=3years/1month/3days/3hours/7minutes/ago',
time() - 31337 * 1337,
'/expired=31337*1337seconds/(1.3years)/ago',
);
}
$this->httpResponse->setCode(IResponse::S204_NoContent, 'U WOT M8');
Expand Down
3 changes: 2 additions & 1 deletion site/app/Http/Cookies/Cookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nette\Http\IRequest;
use Nette\Http\IResponse;
use Nette\Http\Response;
use Nette\Utils\DateTime;

readonly class Cookies
{
Expand Down Expand Up @@ -40,7 +41,7 @@ public function set(
): void {
/** @var Response $response Not IResponse because https://github.com/nette/http/issues/200, can't use instanceof check because it's a different Response in tests */
$response = $this->response;
$response->setCookie($name->value, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite);
$response->setCookie($name->value, $value, (int)DateTime::from($expire)->format('U'), $path, $domain, $secure, $httpOnly, $sameSite);
}


Expand Down
13 changes: 11 additions & 2 deletions site/app/Test/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace MichalSpacekCz\Test\Http;

use DateTimeInterface;
use Nette\Http\IResponse;
use Override;

Expand Down Expand Up @@ -142,8 +143,16 @@ public function deleteHeader(string $name): self


#[Override]
public function setCookie(string $name, string $value, $expire, string $path = null, string $domain = null, bool $secure = null, bool $httpOnly = null, string $sameSite = null): self
{
public function setCookie(
string $name,
string $value,
string|int|DateTimeInterface|null $expire,
string $path = null,
string $domain = null,
bool $secure = null,
bool $httpOnly = null,
string $sameSite = null,
): self {
$this->cookies[$name][] = new Cookie(
$name,
$value,
Expand Down

0 comments on commit 0a8a273

Please sign in to comment.