Skip to content

Commit 6a650d2

Browse files
committed
fix(Cookie): validate cookie prefix independently of raw mode
1 parent 1539867 commit 6a650d2

4 files changed

Lines changed: 25 additions & 11 deletions

File tree

‎system/Cookie/Cookie.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ final public function __construct(string $name, string $value = '', array $optio
271271

272272
$this->validateName($name, $raw);
273273
if ($prefix !== '') {
274-
$this->validateName($prefix, $raw);
274+
$this->validateName($prefix, true);
275275
}
276276
$this->validateValue($value, $raw);
277277
$this->validatePath($path);
@@ -455,7 +455,7 @@ public function withPrefix(string $prefix = '')
455455
{
456456
$this->validatePrefix($prefix, $this->secure, $this->path, $this->domain);
457457
if ($prefix !== '') {
458-
$this->validateName($prefix, $this->raw);
458+
$this->validateName($prefix, true);
459459
}
460460

461461
$cookie = clone $this;
@@ -597,7 +597,7 @@ public function withRaw(bool $raw = true)
597597
{
598598
$this->validateName($this->name, $raw);
599599
if ($this->prefix !== '') {
600-
$this->validateName($this->prefix, $raw);
600+
$this->validateName($this->prefix, true);
601601
}
602602
$this->validateValue($this->value, $raw);
603603

‎tests/system/Cookie/CookieTest.php‎

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

606-
public function testValidationOfRawCookiePrefix(): void
606+
public function testValidationOfCookiePrefix(): void
607607
{
608608
$this->expectException(CookieException::class);
609-
new Cookie('test', 'val', ['prefix' => "bad\r\n", 'raw' => true]);
609+
$this->expectExceptionMessage(lang('Cookie.invalidCookieName', ["bad\r\n"]));
610+
new Cookie('test', 'val', ['prefix' => "bad\r\n"]);
610611
}
611612

612-
public function testValidationOfRawCookiePrefixInWithPrefix(): void
613+
public function testValidationOfCookiePrefixInWithPrefix(): void
613614
{
614615
$this->expectException(CookieException::class);
615-
$cookie = new Cookie('test', 'val', ['raw' => true]);
616+
$this->expectExceptionMessage(lang('Cookie.invalidCookieName', ["bad\r\n"]));
617+
$cookie = new Cookie('test', 'val');
616618
$cookie->withPrefix("bad\r\n");
617619
}
618620

619-
public function testValidationOfRawCookiePrefixInWithRaw(): void
621+
public function testValidationOfRawCookiePrefix(): void
620622
{
621623
$this->expectException(CookieException::class);
622-
$cookie = new Cookie('test', 'val', ['prefix' => "bad\r\n", 'raw' => false]);
623-
$cookie->withRaw(true);
624+
$this->expectExceptionMessage(lang('Cookie.invalidCookieName', ["bad\r\n"]));
625+
new Cookie('test', 'val', ['prefix' => "bad\r\n", 'raw' => true]);
626+
}
627+
628+
public function testValidationOfRawCookiePrefixInWithPrefix(): void
629+
{
630+
$this->expectException(CookieException::class);
631+
$this->expectExceptionMessage(lang('Cookie.invalidCookieName', ["bad\r\n"]));
632+
$cookie = new Cookie('test', 'val', ['raw' => true]);
633+
$cookie->withPrefix("bad\r\n");
624634
}
625635
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ 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, and domain attributes rejected by ``setrawcookie()`` and RFC 6265.
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.
4747
- **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.
4848
- **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()``.
4949
- **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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ The cookie domain must not contain control characters, spaces, tabs, or separato
121121
Validating the Prefix Attribute
122122
===============================
123123

124+
The cookie prefix must not contain control characters, spaces, tabs, or separator characters
125+
(``= , ; \t \r \n \v \f ( ) < > @ : \" / [ ] ? { }``) as `setcookie()` and
126+
`setrawcookie() <https://www.php.net/manual/en/function.setrawcookie.php>`_ will reject them.
127+
124128
When using the ``__Secure-`` prefix, cookies must be set with the ``$secure`` flag set to ``true``. If
125129
using the ``__Host-`` prefix, cookies must exhibit the following:
126130

0 commit comments

Comments
 (0)