Skip to content

Commit ba58d55

Browse files
committed
fix(Cookie): refine prefix validation and update documentation
- Prohibit only PHP-rejected characters and NUL in cookie prefixes to preserve compatibility with separators like ':' and '/' - Update cookie prefix validation documentation in User Guide - Separate Cookie raw value fix and path/domain/prefix validation entries in changelog and remove RFC 6265 reference - Add test coverage for PHP-prohibited prefix characters and allowed separators
1 parent 6a650d2 commit ba58d55

4 files changed

Lines changed: 77 additions & 27 deletions

File tree

‎system/Cookie/Cookie.php‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,14 @@ class Cookie implements ArrayAccess, CloneableCookieInterface
127127
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes
128128
* @see https://tools.ietf.org/html/rfc2616#section-2.2
129129
*/
130-
private static string $reservedCharsList = "=,; \t\r\n\v\f()<>@:\\\"/[]?{}";
130+
private static string $reservedCharsList = "=,; \t\r\n\v\f\0()<>@:\\\"/[]?{}";
131+
132+
/**
133+
* Prohibited characters in cookie prefix and name per PHP setcookie() constraints.
134+
*
135+
* @see https://www.php.net/manual/en/function.setcookie.php
136+
*/
137+
private static string $reservedPrefixCharsList = "=,; \t\r\n\v\f\0";
131138

132139
/**
133140
* @see https://www.php.net/manual/en/function.setrawcookie.php
@@ -270,9 +277,6 @@ final public function __construct(string $name, string $value = '', array $optio
270277
$httponly = $options['httponly'];
271278

272279
$this->validateName($name, $raw);
273-
if ($prefix !== '') {
274-
$this->validateName($prefix, true);
275-
}
276280
$this->validateValue($value, $raw);
277281
$this->validatePath($path);
278282
$this->validateDomain($domain);
@@ -454,9 +458,6 @@ public function getOptions(): array
454458
public function withPrefix(string $prefix = '')
455459
{
456460
$this->validatePrefix($prefix, $this->secure, $this->path, $this->domain);
457-
if ($prefix !== '') {
458-
$this->validateName($prefix, true);
459-
}
460461

461462
$cookie = clone $this;
462463

@@ -596,9 +597,6 @@ public function withSameSite(string $samesite)
596597
public function withRaw(bool $raw = true)
597598
{
598599
$this->validateName($this->name, $raw);
599-
if ($this->prefix !== '') {
600-
$this->validateName($this->prefix, true);
601-
}
602600
$this->validateValue($this->value, $raw);
603601

604602
$cookie = clone $this;
@@ -804,7 +802,7 @@ protected function validateValue(string $value, bool $raw): void
804802
}
805803

806804
/**
807-
* Validates the cookie path per RFC 6265 and PHP setcookie() constraints.
805+
* Validates the cookie path per PHP setcookie() constraints.
808806
*
809807
* @throws CookieException
810808
*/
@@ -816,7 +814,7 @@ protected function validatePath(string $path): void
816814
}
817815

818816
/**
819-
* Validates the cookie domain per RFC 6265 and PHP setcookie() constraints.
817+
* Validates the cookie domain per PHP setcookie() constraints.
820818
*
821819
* @throws CookieException
822820
*/
@@ -828,12 +826,17 @@ protected function validateDomain(string $domain): void
828826
}
829827

830828
/**
831-
* Validates the special prefixes if some attribute requirements are met.
829+
* Validates the special prefixes if some attribute requirements are met,
830+
* and ensures the prefix contains no PHP-prohibited characters.
832831
*
833832
* @throws CookieException
834833
*/
835834
protected function validatePrefix(string $prefix, bool $secure, string $path, string $domain): void
836835
{
836+
if (strpbrk($prefix, self::$reservedPrefixCharsList) !== false) {
837+
throw CookieException::forInvalidCookieName($prefix);
838+
}
839+
837840
if (str_starts_with($prefix, '__Secure-') && ! $secure) {
838841
throw CookieException::forInvalidSecurePrefix();
839842
}

‎tests/system/Cookie/CookieTest.php‎

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -603,33 +603,79 @@ public function testValidCookiePathAndDomain(): void
603603
$this->assertSame('', $cookie3->getDomain());
604604
}
605605

606-
public function testValidationOfCookiePrefix(): void
606+
#[DataProvider('provideValidationOfCookiePrefix')]
607+
public function testValidationOfCookiePrefix(string $prefix): void
607608
{
608609
$this->expectException(CookieException::class);
609-
$this->expectExceptionMessage(lang('Cookie.invalidCookieName', ["bad\r\n"]));
610-
new Cookie('test', 'val', ['prefix' => "bad\r\n"]);
610+
$this->expectExceptionMessage(lang('Cookie.invalidCookieName', [$prefix]));
611+
new Cookie('test', 'val', ['prefix' => $prefix]);
611612
}
612613

613-
public function testValidationOfCookiePrefixInWithPrefix(): void
614+
#[DataProvider('provideValidationOfCookiePrefix')]
615+
public function testValidationOfCookiePrefixInWithPrefix(string $prefix): void
614616
{
615617
$this->expectException(CookieException::class);
616-
$this->expectExceptionMessage(lang('Cookie.invalidCookieName', ["bad\r\n"]));
618+
$this->expectExceptionMessage(lang('Cookie.invalidCookieName', [$prefix]));
617619
$cookie = new Cookie('test', 'val');
618-
$cookie->withPrefix("bad\r\n");
620+
$cookie->withPrefix($prefix);
619621
}
620622

621-
public function testValidationOfRawCookiePrefix(): void
623+
#[DataProvider('provideValidationOfCookiePrefix')]
624+
public function testValidationOfRawCookiePrefix(string $prefix): void
622625
{
623626
$this->expectException(CookieException::class);
624-
$this->expectExceptionMessage(lang('Cookie.invalidCookieName', ["bad\r\n"]));
625-
new Cookie('test', 'val', ['prefix' => "bad\r\n", 'raw' => true]);
627+
$this->expectExceptionMessage(lang('Cookie.invalidCookieName', [$prefix]));
628+
new Cookie('test', 'val', ['prefix' => $prefix, 'raw' => true]);
626629
}
627630

628-
public function testValidationOfRawCookiePrefixInWithPrefix(): void
631+
#[DataProvider('provideValidationOfCookiePrefix')]
632+
public function testValidationOfRawCookiePrefixInWithPrefix(string $prefix): void
629633
{
630634
$this->expectException(CookieException::class);
631-
$this->expectExceptionMessage(lang('Cookie.invalidCookieName', ["bad\r\n"]));
635+
$this->expectExceptionMessage(lang('Cookie.invalidCookieName', [$prefix]));
632636
$cookie = new Cookie('test', 'val', ['raw' => true]);
633-
$cookie->withPrefix("bad\r\n");
637+
$cookie->withPrefix($prefix);
638+
}
639+
640+
/**
641+
* @return iterable<string, array{string}>
642+
*/
643+
public static function provideValidationOfCookiePrefix(): iterable
644+
{
645+
yield 'equals' => ['prefix='];
646+
647+
yield 'comma' => ['prefix,'];
648+
649+
yield 'semicolon' => ['prefix;'];
650+
651+
yield 'space' => ['prefix '];
652+
653+
yield 'tab' => ["prefix\t"];
654+
655+
yield 'carriage return' => ["prefix\r"];
656+
657+
yield 'newline' => ["prefix\n"];
658+
659+
yield 'vertical tab' => ["prefix\v"];
660+
661+
yield 'form feed' => ["prefix\f"];
662+
663+
yield 'null byte' => ["prefix\0"];
664+
665+
yield 'CRLF' => ["prefix\r\n"];
666+
}
667+
668+
public function testValidCookiePrefixAllowedSeparators(): void
669+
{
670+
$cookie = new Cookie('test', 'val', ['prefix' => 'ci:session/']);
671+
$this->assertSame('ci:session/', $cookie->getPrefix());
672+
$this->assertSame('ci:session/test', $cookie->getPrefixedName());
673+
674+
$cookie2 = $cookie->withPrefix('my-app:v1/');
675+
$this->assertSame('my-app:v1/', $cookie2->getPrefix());
676+
$this->assertSame('my-app:v1/test', $cookie2->getPrefixedName());
677+
678+
$cookie3 = new Cookie('test', 'val', ['prefix' => 'ci:session/', 'raw' => false]);
679+
$this->assertSame('ci:session/test=val; Path=/; HttpOnly; SameSite=Lax', $cookie3->toHeaderString());
634680
}
635681
}

‎user_guide_src/source/changelogs/v4.7.5.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ Bugs Fixed
4343
- **CLIRequest:** Fixed a bug where ``parseCommand()`` could throw a TypeError when ``argv`` is missing.
4444
- **CodeIgniter:** Fixed a bug where ``gatherOutput()`` could be called twice when ``startController()`` returned a ``ResponseInterface`` (e.g., from filter attributes or closure routes).
4545
- **Content Security Policy:** Fixed a bug where empty ``Content-Security-Policy``, ``Content-Security-Policy-Report-Only``, and ``Reporting-Endpoints`` response headers were generated when no corresponding values existed.
46-
- **Cookie:** Fixed a bug where ``Cookie`` instances allowed invalid characters in cookie values (with ``raw: true``), path, domain, and prefix attributes rejected by ``setcookie()``, ``setrawcookie()``, and RFC 6265.
46+
- **Cookie:** Fixed a bug where ``Cookie`` instances created with ``raw: true`` allowed invalid characters in cookie values rejected by ``setrawcookie()``.
47+
- **Cookie:** Fixed a bug where ``Cookie`` instances allowed invalid characters in path, domain, and prefix attributes rejected by ``setcookie()`` and ``setrawcookie()``.
4748
- **Database:** Fixed a bug where rebuilding a SQLite3 table (e.g., ``Forge::dropColumn()``, ``Forge::modifyColumn()``, ``Forge::dropForeignKey()`` and ``Forge::dropPrimaryKey()``) corrupted the table names referenced by its foreign keys when ``DBPrefix`` was set.
4849
- **Files:** Fixed a bug where ``File::move()`` and ``UploadedFile::move()`` set executable and overly permissive file permissions (``0777 & ~umask()`` instead of ``0666 & ~umask()``), and ``UploadedFile::move()`` targeted the parent directory instead of the destination file for ``chmod()``.
4950
- **Helpers:** Fixed a bug where ``get_dir_file_info()`` returned incomplete entries for subdirectories and missing files instead of omitting them.

‎user_guide_src/source/libraries/cookies.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Validating the Prefix Attribute
122122
===============================
123123

124124
The cookie prefix must not contain control characters, spaces, tabs, or separator characters
125-
(``= , ; \t \r \n \v \f ( ) < > @ : \" / [ ] ? { }``) as `setcookie()` and
125+
(``= , ; \t \r \n \v \f \0``) as `setcookie() <https://www.php.net/manual/en/function.setcookie.php>`_ and
126126
`setrawcookie() <https://www.php.net/manual/en/function.setrawcookie.php>`_ will reject them.
127127

128128
When using the ``__Secure-`` prefix, cookies must be set with the ``$secure`` flag set to ``true``. If

0 commit comments

Comments
 (0)