Skip to content

[5.x] Default cookies to HttpOnly, SameSite=Lax and Secure on HTTPS - #67

Open
Hackwar wants to merge 3 commits into
joomla-framework:5.x-devfrom
Hackwar:5.x-cookie-secure-defaults
Open

Hackwar wants to merge 3 commits into
joomla-framework:5.x-devfrom
Hackwar:5.x-cookie-secure-defaults

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary of Changes

Cookie::set() hands its options to setcookie() unchanged, so every cookie is written without HttpOnly, SameSite and Secure unless the caller remembers to pass them. Options that are not passed now default to httponly => true, samesite => 'Lax' and secure => true when the current request uses HTTPS ($_SERVER['HTTPS'] set and not off; proxy headers are not trusted). Options passed to set() still win.

The signature becomes set(string $name, mixed $value, array $options = []): bool: it returns the result of setcookie() and only stores the value in the object if the cookie was sent. ($value stays mixed because Input::set() accepts mixed, and a parameter cannot be narrowed in a subclass.)

This is a BC break for cookies that JavaScript reads or that must be sent on cross-site requests; the update guide says what to pass for those.

Depends on #66 (native types, which types the parameters of Input::set() so Cookie::set() can type its own) and #65 (update guide); this branch contains their commits until they are merged.

Testing Instructions

New tests in CookieTest: testSetSecureDefaults() (HTTP, HTTPS, HTTPS=off), testSetOverridesDefaults() and testSetFailure(). The first and the last fail before the change and pass after it.

Documentation Changes Required

docs/overview.md ("Cookies"): the defaults and the return value. docs/v4-to-v5-update.md: new section "Secure cookie defaults" with what to check.

Depends on #66

🤖 Generated with Claude Code

Hackwar and others added 3 commits November 13, 2026 06:41
Several 5.x changes add entries to docs/v4-to-v5-update.md; creating the
file once keeps them from conflicting over its creation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The package requires PHP 8.3 but still declared its API in docblocks,
and count() carried #[\ReturnTypeWillChange]. Parameters, return values
and properties are now typed natively. set() keeps an untyped return
value because Cookie::set() returns whether the cookie was sent, and the
$filter argument of get() stays untyped until getArray() no longer passes
input values as filter names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cookie::set() handed its options to setcookie() unchanged, so cookies were
written without HttpOnly, SameSite and Secure unless every caller passed
them. Missing options now default to httponly = true, samesite = Lax and
secure when the request uses HTTPS. set() returns whether the cookie was
sent and only stores the value if it was.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant