Skip to content

Commit a5b1fdd

Browse files
Update rule metadata (#1673)
Co-authored-by: nils-werner-sonarsource <nils-werner-sonarsource@users.noreply.github.com>
1 parent b153002 commit a5b1fdd

3 files changed

Lines changed: 34 additions & 41 deletions

File tree

php-checks/src/main/resources/org/sonar/l10n/php/rules/php/S2092.html

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,45 @@
11
<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
22
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>
1717
<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
2020
</pre>
2121
<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_
2524
</pre>
2625
<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">
3527
$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_
3829
</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;
4233
</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);
4536
</pre>
46-
<pre>
37+
<pre data-diff-id="3" data-diff-type="compliant">
4738
$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);
5040
</pre>
51-
<h2>See</h2>
41+
<h2>Resources</h2>
42+
<h3>Standards</h3>
5243
<ul>
5344
<li>OWASP - <a href="https://owasp.org/Top10/A04_2021-Insecure_Design/">Top 10 2021 Category A4 - Insecure Design</a></li>
5445
<li>OWASP - <a href="https://owasp.org/Top10/A05_2021-Security_Misconfiguration/">Top 10 2021 Category A5 - Security Misconfiguration</a></li>

php-checks/src/main/resources/org/sonar/l10n/php/rules/php/S2092.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
2-
"title": "Creating cookies without the \"secure\" flag is security-sensitive",
3-
"type": "SECURITY_HOTSPOT",
2+
"title": "Cookies should have the \"secure\" flag",
3+
"type": "VULNERABILITY",
4+
"quickfix": "unknown",
45
"code": {
56
"impacts": {
67
"SECURITY": "LOW"
@@ -14,6 +15,7 @@
1415
},
1516
"tags": [
1617
"cwe",
18+
"former-hotspot",
1719
"privacy"
1820
],
1921
"defaultSeverity": "Minor",

sonarpedia.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"languages": [
44
"PHP"
55
],
6-
"latest-update": "2026-03-19T05:34:08.225567209Z",
6+
"latest-update": "2026-04-09T05:41:07.653307556Z",
77
"options": {
88
"no-language-in-filenames": true,
99
"preserve-filenames": true

0 commit comments

Comments
 (0)