|
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 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> |
17 | 16 | <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"> |
19 | 18 | 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 |
21 | 20 | </pre> |
22 | 21 | <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 |
23 | 22 | 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); |
26 | 30 | </pre> |
27 | | -<h2>Compliant Solution</h2> |
28 | | -<pre> |
| 31 | +<pre data-diff-id="2" data-diff-type="compliant"> |
29 | 32 | 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); |
31 | 34 | </pre> |
32 | | -<h2>See</h2> |
| 35 | +<h2>Resources</h2> |
| 36 | +<h3>Standards</h3> |
33 | 37 | <ul> |
34 | 38 | <li>OWASP - <a href="https://owasp.org/Top10/A04_2021-Insecure_Design/">Top 10 2021 Category A4 - Insecure Design</a></li> |
35 | 39 | <li>OWASP - <a href="https://owasp.org/Top10/A05_2021-Security_Misconfiguration/">Top 10 2021 Category A5 - Security Misconfiguration</a></li> |
|
0 commit comments