Skip to content

Commit

Permalink
fix: Fix incorrect typing of 'same_site' cookie option (#81)
Browse files Browse the repository at this point in the history
* chore: Correct casing when dealing with cookie resolver options

* fix: Fix incorrect typing of 'same_site' cookie option

The 'same_site' value was always expected to be a bool, even though it would always be 'string', 'lax', 'none' or null
  • Loading branch information
ollieread authored Dec 23, 2024
1 parent bd2e402 commit 23f0dac
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/Http/Resolvers/CookieIdentityResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ private function getCookieDetails(array $details): array
$details['secure'] = $this->options['secure'];
}

if (isset($this->options['httpOnly'])) {
$details['httpOnly'] = $this->options['httpOnly'];
if (isset($this->options['http_only'])) {
$details['http_only'] = $this->options['http_only'];
}

if (isset($this->options['sameSite'])) {
$details['sameSite'] = $this->options['sameSite'];
if (isset($this->options['same_site'])) {
$details['same_site'] = $this->options['same_site'];
}

return $details;
Expand Down
2 changes: 1 addition & 1 deletion src/Overrides/CookieOverride.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function setup(Tenancy $tenancy, Tenant $tenant): void
$path = settings()->getUrlPath(config('session.path') ?? '/'); // @phpstan-ignore-line
$domain = settings()->getUrlDomain(config('session.domain')); // @phpstan-ignore-line
$secure = settings()->shouldCookieBeSecure(config('session.secure', false)); // @phpstan-ignore-line
$sameSite = settings()->shouldCookeBeSameSite(config('session.same_site')); // @phpstan-ignore-line
$sameSite = settings()->getCookieSameSite(config('session.same_site')); // @phpstan-ignore-line

/**
* This is here to make PHPStan quiet down
Expand Down
2 changes: 1 addition & 1 deletion src/Overrides/SessionOverride.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function setup(Tenancy $tenancy, Tenant $tenant): void
}

if ($settings->has(Settings::COOKIE_SAME_SITE)) {
$config->set('session.same_site', $settings->shouldCookeBeSameSite());
$config->set('session.same_site', $settings->getCookieSameSite());
}

$config->set('session.cookie', $this->getCookieName($tenancy, $tenant));
Expand Down
14 changes: 11 additions & 3 deletions src/Support/SettingsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,22 @@ public function shouldCookieBeSecure(?bool $default = null): bool
return $this->boolean(Settings::COOKIE_SECURE, $default);
}

public function setCookieSameSite(bool $sameSite): void
public function setCookieSameSite(?string $sameSite): void
{
$this->set(Settings::COOKIE_SAME_SITE, $sameSite);
}

public function shouldCookeBeSameSite(?bool $default = null): bool
public function getCookieSameSite(?string $default = null): ?string
{
return $this->boolean(Settings::COOKIE_SAME_SITE, $default);
/**
* This is only here because the config repository has terrible support
* for typing, as you'd expect.
*
* @var string|null $sameSite
*/
$sameSite = $this->get(Settings::COOKIE_SAME_SITE, $default);

return $sameSite;
}

public function doNotOverrideTheDatabase(): void
Expand Down

0 comments on commit 23f0dac

Please sign in to comment.