Skip to content

Commit 4a37dfe

Browse files
SONARJAVA-6245 Update rules metadata (#5559)
1 parent 293d562 commit 4a37dfe

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S2092.html

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
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 Servlet</h2>
13+
<p>Call <code>setSecure(true)</code> on the <code>Cookie</code> object to ensure it is only transmitted over HTTPS.</p>
14+
<h3>Code examples</h3>
15+
<h4>Noncompliant code example</h4>
1716
<p>If you create a security-sensitive cookie in your JAVA code:</p>
18-
<pre>
17+
<pre data-diff-id="1" data-diff-type="noncompliant">
1918
Cookie c = new Cookie(COOKIENAME, sensitivedata);
20-
c.setSecure(false); // Sensitive: a security-ensitive cookie is created with the secure flag set to false
19+
c.setSecure(false); // Noncompliant
2120
</pre>
2221
<p>By default the <a href="https://docs.oracle.com/javaee/6/api/javax/servlet/http/Cookie.html#setSecure(boolean)"><code>secure</code></a> flag is set
2322
to <em>false:</em></p>
24-
<pre>
25-
Cookie c = new Cookie(COOKIENAME, sensitivedata); // Sensitive: a security-sensitive cookie is created with the secure flag not defined (by default set to false)
23+
<pre data-diff-id="2" data-diff-type="noncompliant">
24+
Cookie c = new Cookie(COOKIENAME, sensitivedata); // Noncompliant: cookies are created by default without a secure flag
25+
</pre>
26+
<h4>Compliant solution</h4>
27+
<pre data-diff-id="1" data-diff-type="compliant">
28+
Cookie c = new Cookie(COOKIENAME, sensitivedata);
29+
c.setSecure(true);
2630
</pre>
27-
<h2>Compliant Solution</h2>
28-
<pre>
31+
<pre data-diff-id="2" data-diff-type="compliant">
2932
Cookie c = new Cookie(COOKIENAME, sensitivedata);
30-
c.setSecure(true); // Compliant: the sensitive cookie will not be send during an unencrypted HTTP request thanks to the secure flag set to true
33+
c.setSecure(true);
3134
</pre>
32-
<h2>See</h2>
35+
<h2>Resources</h2>
36+
<h3>Standards</h3>
3337
<ul>
3438
<li>OWASP - <a href="https://owasp.org/Top10/A04_2021-Insecure_Design/">Top 10 2021 Category A4 - Insecure Design</a></li>
3539
<li>OWASP - <a href="https://owasp.org/Top10/A05_2021-Security_Misconfiguration/">Top 10 2021 Category A5 - Security Misconfiguration</a></li>

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S2092.json

Lines changed: 4 additions & 4 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"
@@ -49,6 +50,5 @@
4950
"STIG ASD_V5R3": [
5051
"V-222576"
5152
]
52-
},
53-
"quickfix": "unknown"
53+
}
5454
}

sonarpedia.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"languages": [
44
"JAVA"
55
],
6-
"latest-update": "2026-03-12T13:07:16.598544876Z",
6+
"latest-update": "2026-04-09T13:46:03.313330Z",
77
"options": {
88
"no-language-in-filenames": true,
99
"preserve-filenames": false

0 commit comments

Comments
 (0)