|
1 | 1 | <p>When a cookie is protected with the <code>secure</code> attribute set to <em>true</em> it will not be send by the browser over an unencrypted HTTP |
2 | 2 | request and thus cannot be observed by an unauthorized person during a man-in-the-middle attack.</p> |
3 | | -<h2>Ask Yourself Whether</h2> |
4 | | -<ul> |
5 | | - <li>the cookie is for instance a <em>session-cookie</em> not designed to be sent over non-HTTPS communication.</li> |
6 | | - <li>it’s not sure that the website contains <a href="https://developer.mozilla.org/en-US/docs/Web/Security/Mixed_content">mixed content</a> or not |
7 | | - (ie HTTPS everywhere or not)</li> |
8 | | -</ul> |
9 | | -<p>There is a risk if you answered yes to any of those questions.</p> |
10 | | -<h2>Recommended Secure Coding Practices</h2> |
11 | | -<ul> |
12 | | - <li>It is recommended to use <code>HTTPs</code> everywhere so setting the <code>secure</code> flag to <em>true</em> should be the default behaviour |
13 | | - when creating cookies.</li> |
14 | | - <li>Set the <code>secure</code> flag to <em>true</em> for session-cookies.</li> |
15 | | -</ul> |
16 | | -<h2>Sensitive Code Example</h2> |
| 3 | +<h2>Why is this an issue?</h2> |
| 4 | +<p>When a cookie is created without the <code>secure</code> attribute set to <code>true</code>, browsers will transmit it over unencrypted HTTP |
| 5 | +connections as well as HTTPS. An attacker who can observe or intercept network traffic—for example on a public Wi-Fi network—can read the cookie value |
| 6 | +in cleartext.</p> |
| 7 | +<h3>What is the potential impact?</h3> |
| 8 | +<h4>Session hijacking</h4> |
| 9 | +<p>If a session cookie is transmitted over an unencrypted HTTP connection, an attacker who can intercept the traffic can steal it. With a valid |
| 10 | +session cookie, the attacker can impersonate the victim and gain full access to their account without knowing their password. Even on sites that |
| 11 | +primarily use HTTPS, a single HTTP request containing the session cookie is enough to expose it.</p> |
| 12 | +<h2>How to fix it in Core PHP</h2> |
| 13 | +<p>Set the <code>secure</code> parameter to <code>true</code> when creating cookies to prevent them from being transmitted over unencrypted HTTP |
| 14 | +connections.</p> |
| 15 | +<h3>Code examples</h3> |
| 16 | +<h4>Noncompliant code example</h4> |
17 | 17 | <p>In <em>php.ini</em> you can specify the flags for the session cookie which is security-sensitive:</p> |
18 | | -<pre> |
19 | | -session.cookie_secure = 0; // Sensitive: this security-sensitive session cookie is created with the secure flag set to false (cookie_secure = 0) |
| 18 | +<pre data-diff-id="1" data-diff-type="noncompliant"> |
| 19 | +session.cookie_secure = 0; // Noncompliant |
20 | 20 | </pre> |
21 | 21 | <p>Same thing in PHP code:</p> |
22 | | -<pre> |
23 | | -session_set_cookie_params($lifetime, $path, $domain, false); |
24 | | -// Sensitive: this security-sensitive session cookie is created with the secure flag (the fourth argument) set to _false_ |
| 22 | +<pre data-diff-id="2" data-diff-type="noncompliant"> |
| 23 | +session_set_cookie_params($lifetime, $path, $domain, false); // Noncompliant: this security-sensitive session cookie is created with the secure flag (the fourth argument) set to _false_ |
25 | 24 | </pre> |
26 | 25 | <p>If you create a custom security-sensitive cookie in your PHP code:</p> |
27 | | -<pre> |
28 | | -$value = "sensitive data"; |
29 | | -setcookie($name, $value, $expire, $path, $domain, false); // Sensitive: a security-sensitive cookie is created with the secure flag (the sixth argument) set to _false_ |
30 | | -</pre> |
31 | | -<p>By default <a href="https://www.php.net/manual/en/function.setcookie.php"><code>setcookie</code></a> and <a |
32 | | -href="https://www.php.net/manual/en/function.setrawcookie.php"><code>setrawcookie</code></a> functions set the sixth argument / <code>secure</code> |
33 | | -flag to <em>false:</em></p> |
34 | | -<pre> |
| 26 | +<pre data-diff-id="3" data-diff-type="noncompliant"> |
35 | 27 | $value = "sensitive data"; |
36 | | -setcookie($name, $value, $expire, $path, $domain); // Sensitive: a security-sensitive cookie is created with the secure flag (the sixth argument) not defined (by default to false) |
37 | | -setrawcookie($name, $value, $expire, $path, $domain); // Sensitive: a security-sensitive cookie is created with the secure flag (the sixth argument) not defined (by default to false) |
| 28 | +setcookie($name, $value, $expire, $path, $domain, false); // Noncompliant: a security-sensitive cookie is created with the secure flag (the sixth argument) set to _false_ |
38 | 29 | </pre> |
39 | | -<h2>Compliant Solution</h2> |
40 | | -<pre> |
41 | | -session.cookie_secure = 1; // Compliant: the sensitive cookie will not be send during an unencrypted HTTP request thanks to cookie_secure property set to 1 |
| 30 | +<h4>Compliant solution</h4> |
| 31 | +<pre data-diff-id="1" data-diff-type="compliant"> |
| 32 | +session.cookie_secure = 1; |
42 | 33 | </pre> |
43 | | -<pre> |
44 | | -session_set_cookie_params($lifetime, $path, $domain, true); // Compliant: the sensitive cookie will not be send during an unencrypted HTTP request thanks to the secure flag (the fouth argument) set to true |
| 34 | +<pre data-diff-id="2" data-diff-type="compliant"> |
| 35 | +session_set_cookie_params($lifetime, $path, $domain, true); |
45 | 36 | </pre> |
46 | | -<pre> |
| 37 | +<pre data-diff-id="3" data-diff-type="compliant"> |
47 | 38 | $value = "sensitive data"; |
48 | | -setcookie($name, $value, $expire, $path, $domain, true); // Compliant: the sensitive cookie will not be send during an unencrypted HTTP request thanks to the secure flag (the sixth argument) set to true |
49 | | -setrawcookie($name, $value, $expire, $path, $domain, true);// Compliant: the sensitive cookie will not be send during an unencrypted HTTP request thanks to the secure flag (the sixth argument) set to true |
| 39 | +setcookie($name, $value, $expire, $path, $domain, true); |
50 | 40 | </pre> |
51 | | -<h2>See</h2> |
| 41 | +<h2>Resources</h2> |
| 42 | +<h3>Standards</h3> |
52 | 43 | <ul> |
53 | 44 | <li>OWASP - <a href="https://owasp.org/Top10/A04_2021-Insecure_Design/">Top 10 2021 Category A4 - Insecure Design</a></li> |
54 | 45 | <li>OWASP - <a href="https://owasp.org/Top10/A05_2021-Security_Misconfiguration/">Top 10 2021 Category A5 - Security Misconfiguration</a></li> |
|
0 commit comments