Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/carson.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# https://github.com/NexusPHP/carson/blob/1.x/SUBSCRIBERS.md

version: 1
subscribers:
- pr-title-linter
settings:
pr-title-linter:
name: 'Carson / pr-title'
rules:
- pattern: '^(\[\d+\.\d+\]\s{1})?(feat|fix|chore|docs|perf|refactor|style|test|config|revert)(\([\-.@:`a-zA-Z0-9]+\))?!?:\s{1}\S.+\S|Prep for \d\.\d\.\d release|\d\.\d\.\d (Ready|Merge) code$'
description: 'PR title must include the type (feat, fix, chore, docs, perf, refactor, style, test, config, revert) of the commit per Conventional Commits specification. See https://www.conventionalcommits.org/en/v1.0.0/ for the discussion.'
level: error
8 changes: 0 additions & 8 deletions .github/prlint.json

This file was deleted.

20 changes: 20 additions & 0 deletions .github/workflows/run-carson.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Carson

on:
pull_request_target:
types:
- opened
- edited

permissions: {}

jobs:
carson:
runs-on: ubuntu-24.04
steps:
- name: Run Carson
uses: NexusPHP/carson@62602be15470a18edd31f217b785863e997572d5 # v1.4.0
with:
app_id: ${{ secrets.CARSON_APP_ID }}
private_key: ${{ secrets.CARSON_PRIVATE_KEY }}
log_level: info
56 changes: 48 additions & 8 deletions system/Cookie/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,14 @@ class Cookie implements ArrayAccess, CloneableCookieInterface
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes
* @see https://tools.ietf.org/html/rfc2616#section-2.2
*/
private static string $reservedCharsList = "=,; \t\r\n\v\f()<>@:\\\"/[]?{}";
private static string $reservedCharsList = "=,; \t\r\n\v\f\0()<>@:\\\"/[]?{}";

/**
* Prohibited characters in cookie prefix and name per PHP setcookie() constraints.
*
* @see https://www.php.net/manual/en/function.setcookie.php
*/
private static string $reservedPrefixCharsList = "=,; \t\r\n\v\f\0";

/**
* @see https://www.php.net/manual/en/function.setrawcookie.php
Expand Down Expand Up @@ -232,11 +239,11 @@ public static function fromHeaderString(string $cookie, bool $raw = false)
* @param string $name The cookie's name
* @param string $value The cookie's value
* @param array{
* prefix?: string,
* prefix?: string|null,
* max-age?: int|numeric-string,
* expires?: DateTimeInterface|int|string,
* path?: string,
* domain?: string,
* path?: string|null,
* domain?: string|null,
* secure?: bool,
* httponly?: bool,
* samesite?: string,
Expand All @@ -258,9 +265,9 @@ final public function __construct(string $name, string $value = '', array $optio
}

// to preserve backward compatibility with array-based cookies in previous CI versions
$prefix = ($options['prefix'] === '') ? self::$defaults['prefix'] : $options['prefix'];
$path = ($options['path'] === '') ? self::$defaults['path'] : $options['path'];
$domain = ($options['domain'] === '') ? self::$defaults['domain'] : $options['domain'];
$prefix = in_array($options['prefix'], [null, ''], true) ? self::$defaults['prefix'] : $options['prefix'];
$path = in_array($options['path'], [null, '', '0'], true) ? self::$defaults['path'] : $options['path'];
$domain = in_array($options['domain'], [null, ''], true) ? self::$defaults['domain'] : $options['domain'];

// empty string SameSite should use the default for browsers
$samesite = ($options['samesite'] === '') ? self::$defaults['samesite'] : $options['samesite'];
Expand All @@ -271,6 +278,8 @@ final public function __construct(string $name, string $value = '', array $optio

$this->validateName($name, $raw);
$this->validateValue($value, $raw);
$this->validatePath($path);
$this->validateDomain($domain);
$this->validatePrefix($prefix, $secure, $path, $domain);
$this->validateSameSite($samesite, $secure);

Expand Down Expand Up @@ -515,6 +524,7 @@ public function withExpired()
public function withPath(?string $path)
{
$path = in_array($path, [null, '', '0'], true) ? self::$defaults['path'] : $path;
$this->validatePath($path);
$this->validatePrefix($this->prefix, $this->secure, $path, $this->domain);

$cookie = clone $this;
Expand All @@ -530,6 +540,7 @@ public function withPath(?string $path)
public function withDomain(?string $domain)
{
$domain ??= self::$defaults['domain'];
$this->validateDomain($domain);
$this->validatePrefix($this->prefix, $this->secure, $this->path, $domain);

$cookie = clone $this;
Expand Down Expand Up @@ -791,12 +802,41 @@ protected function validateValue(string $value, bool $raw): void
}

/**
* Validates the special prefixes if some attribute requirements are met.
* Validates the cookie path per PHP setcookie() constraints.
*
* @throws CookieException
*/
protected function validatePath(string $path): void
{
if (strpbrk($path, self::$reservedValueCharsList) !== false) {
throw CookieException::forInvalidCookiePath();
}
}

/**
* Validates the cookie domain per PHP setcookie() constraints.
*
* @throws CookieException
*/
protected function validateDomain(string $domain): void
{
if ($domain !== '' && strpbrk($domain, self::$reservedValueCharsList) !== false) {
throw CookieException::forInvalidCookieDomain();
}
}

/**
* Validates the special prefixes if some attribute requirements are met,
* and ensures the prefix contains no PHP-prohibited characters.
*
* @throws CookieException
*/
protected function validatePrefix(string $prefix, bool $secure, string $path, string $domain): void
{
if (strpbrk($prefix, self::$reservedPrefixCharsList) !== false) {
throw CookieException::forInvalidCookieName($prefix);
}

if (str_starts_with($prefix, '__Secure-') && ! $secure) {
throw CookieException::forInvalidSecurePrefix();
}
Expand Down
20 changes: 20 additions & 0 deletions system/Cookie/Exceptions/CookieException.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ public static function forInvalidCookieValue()
return new static(lang('Cookie.invalidCookieValue'));
}

/**
* Thrown when the cookie path contains invalid characters.
*
* @return static
*/
public static function forInvalidCookiePath()
{
return new static(lang('Cookie.invalidCookiePath'));
}

/**
* Thrown when the cookie domain contains invalid characters.
*
* @return static
*/
public static function forInvalidCookieDomain()
{
return new static(lang('Cookie.invalidCookieDomain'));
}

/**
* Thrown when using the `__Secure-` prefix but the `Secure` attribute
* is not set to true.
Expand Down
2 changes: 2 additions & 0 deletions system/Language/en/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
'invalidExpiresValue' => 'The cookie expiration time is not valid.',
'invalidCookieName' => 'The cookie name "{0}" contains invalid characters.',
'invalidCookieValue' => 'The cookie value contains invalid characters.',
'invalidCookiePath' => 'The cookie path contains invalid characters.',
'invalidCookieDomain' => 'The cookie domain contains invalid characters.',
'emptyCookieName' => 'The cookie name cannot be empty.',
'invalidSecurePrefix' => 'Using the "__Secure-" prefix requires setting the "Secure" attribute.',
'invalidHostPrefix' => 'Using the "__Host-" prefix must be set with the "Secure" flag, must not have a "Domain" attribute, and the "Path" is set to "/".',
Expand Down
Loading
Loading