WordPress / WordPress/WordPress-Coding-Standards

Require "secure" cookies

Open
#1,704 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Component: Extra Focus: Security Type: Enhancement
Dominant language
PHP
Stars
2.8k
Forks
521
Avg merge
5d 20h
Merged PRs (30d)
1

Description

Is your feature request related to a problem?

setcookie should have the $secure parameter be set to true in all cases for improved security.
There is no downside to it.

Optionally a separate rule that requires httponly true (can be easily disabled for phpcs:ignore if one needs them in js), again for improved security.

Describe the solution you'd like

If a/any setcookie function does not have true as the last parameter (if checking for httponly too - the last 2 parameters), throw an error.

setcookie($cookie_name);
setcookie($cookie_name, $cookie_value );
setcookie($cookie_name, $cookie_value, time() + (86400 * 30) );
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/", '' );
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/", 'test.com' );
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/", '', true ); // this is correct
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/", 'test.com', true ); // this is correct
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/", '', false, true); // Attention here!
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/", '', true, true ); // this is correct

So it could work for:
setcookie
wc_setcookie
custom_setcookie

Additional context (optional)

This is relatively simple to check, EXCEPT that some plugins provide the flag to be filterable, which would be impossible to detect.
e.g.

wc_setcookie( $this->_cookie, $cookie_value, $this->_session_expiration, apply_filters( 'wc_session_use_secure_cookie', false ) );

However, there really is no reason in 2019, where you get SSL certs for free with letsencrypt, to not use secure cookies, thus I think we can ignore these filterable ones and flag them as wrong.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue provides no repository file or test path; begin by locating the existing PHP_CodeSniffer coverage for setcookie, wc_setcookie, and custom_setcookie. Use the listed call forms, including filterable flags, to define accepted argument positions and add coverage for the requested secure-cookie behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
security, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.