[php-src] PR #13720: Make session cookies httponly by default
| From: | DaWe35 | Date: | Fri, 15 Mar 2024 08:40:44 +0000 |
| Subject: | [php-src] PR #13720: Make session cookies httponly by default | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-25431@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/13720
Author: DaWe35
### Story
My website I wrote 9 years ago was hacked recently. It was quite tricky to exploit (I mean it took 9
years for someone to discover the bug), but I made a mistake and my website was vulnerable to XSS.
I'm a self-taught programmer and PHP was the first language I learnt. I think 9 years ago I did
a pretty good job eliminating XSS and other injections, but I never expected anyone to go this deep
in a hobby website and actually compose a working exploit.
### Importance of default values
With the injected code, the attacker was able to steal session cookies. I'm trying hard to make
session cookies httponly on all my websites, but somehow this one was not set (probably because of
the age of the site). I believe this attack wouldn't have any consequences if httponly were 1
by default. I hope my input helps to make the web better, and I'm sorry if my knowledge lacks.
I really believe defaults should be safe (I tried to read all the related discussions eg. #7913)
### httponly
I believe it is uncommon that a website needs javascript access for a session cookie*, therefore
making httponly=1 by default would not break many websites. I also believe it would make a lot of
website much safer, since there are a tons of websites with the insecure default. Changing this
default value is a net positive in my opinion.
> * it is not really useful to read a session id by JS, because it's just a random hash
> without the session data. Websites usually pass actual session values (and not the hash itself) to
> JS for example by echoing them. When sending an Ajax request to the same domain, the cookie is
> already part of the request header, further reducing the need of the token access by JS
### use_strict_mode
According to
[php.net](https://www.php.net/manual/en/session.configuration.php#ini.session.use-strict-mode):
> Enabling session.use_strict_mode is mandatory for general session security. All sites are
> advised to enable this. See
> [session_create_id()](https://www.php.net/manual/en/function.session-create-id.php) example code for
> more details.
My only question is that why it's not default? I can hardly imagine changing this variable
resulting in any damage or broken code. If it is advised to be 1, I think it should be 1 by default.
### ~secure~ ??
**In this PR, I did not include any changes related to secure, but I'd be happy to make more
defaults secure.**
I think changing secure to 1 would be amazing, but I also understand that this can break things (for
example development mostly happens on localhost without SSL, and some websites still run in flexible
SSL mode on Cloudflare). I'm not sure what is the best approach to this, I think we should
still try to move forward with secure defaults, but displaying a helpful error message is essential
so at least dev envs can be fixed in no time, when this config variable is missing in the local dev
env.
### Only sessions?
**In this PR, I only changed defaults related to session cookies**
I'm not sure if it's a good idea to set all cookies to httponly=1, but as I said, the
safer is better, so I can support that. It will cause inconvenience in some situations, therefore
I'm not changing any setcookie() or setrawcookie() defaults now.