This site is the archived OWASP Foundation Wiki and is no longer accepting Account Requests.
To view the new OWASP Foundation website, please visit https://owasp.org

Difference between revisions of "SecureFlag"

From OWASP
Jump to: navigation, search
m (Made ASP.NET as sub section of Setting the Secure Flag)
(Added section for Java)
Line 9: Line 9:
 
By setting the secure flag, the browser will prevent the transmission of a cookie over an unencrypted channel.<br>
 
By setting the secure flag, the browser will prevent the transmission of a cookie over an unencrypted channel.<br>
  
= Setting the Secure Flag<br> =
+
= Setting the Secure Flag =
  
==ASP.NET==
+
Following sections describes setting the Secure Flag in respective technologies.
 +
 
 +
 
 +
== Java ==
 +
 
 +
=== Servlet 3.0 (Java EE 6) ===
 +
Sun Java EE supports secure flag in Cookie interface since version 6 (Servlet class version 3)[http://java.sun.com/javaee/6/docs/api/javax/servlet/http/Cookie.html#setSecure%28boolean%29], also for session cookies (JSESSIONID)[http://java.sun.com/javaee/6/docs/api/javax/servlet/SessionCookieConfig.html#setSecure%28boolean%29]. Methods ''setSecure'' and ''isSecure'' can be used to set and check for secure value in cookies.
 +
 
 +
==== web.xml ====
 +
Servlet 3.0 (Java EE 6) introduced a standard way to configure secure attribute for the session cookie, this can be done by applying the following configuration in web.xml
 +
&lt;session-config&gt;
 +
  &lt;cookie-config&gt;
 +
  &lt;secure&gt;true&lt;/secure&gt;
 +
  &lt;/cookie-config>
 +
&lt;session-config&gt;
 +
 
 +
=== Tomcat ===
 +
In '''Tomcat 6''' if the first request for session is using ''https'' then it automatically sets secure attribute on session cookie.
 +
 
 +
=== Setting it as a custom header ===
 +
For '''older versions''' the workaround is to rewrite JSESSIONID value using and setting it as a custom header.
 +
 
 +
Advantage of using custom rewrite trick is that we can configure it differently for each environment driven by application  configuration as most local development or test environments may use ''http''.
 +
 
 +
The drawback is that servers can be configured to use a <b>different session identifier than JSESSIONID</b>. It will be best if configuring this attribute could be a feature supported by Servlet containers.
 +
 
 +
String sessionid = request.getSession().getId();
 +
response.setHeader("SET-COOKIE", "JSESSIONID=" + sessionid + "; secure");
 +
 
 +
 
 +
 
 +
== ASP.NET ==
  
 
<httpCookies requireSSL="true" />
 
<httpCookies requireSSL="true" />
 +
 +
  
 
= Testing for the Secure Flag<br> =
 
= Testing for the Secure Flag<br> =

Revision as of 19:54, 9 January 2013

This article is a stub. You can help OWASP by expanding it or discussing it on its Talk page.


Overview

The secure flag is an option that can be set by the application server when sending a new cookie to the user within an HTTP Response. The purpose of the secure flag is to prevent cookies from being observed by unauthorized parties due to the transmission of a the cookie in clear text.

To accomplish this goal, browsers which support the secure flag will only send cookies with the secure flag when the request is going to a HTTPS page. Said in another way, the browser will not send a cookie with the secure flag set over an unencrypted HTTP request.

By setting the secure flag, the browser will prevent the transmission of a cookie over an unencrypted channel.

Setting the Secure Flag

Following sections describes setting the Secure Flag in respective technologies.


Java

Servlet 3.0 (Java EE 6)

Sun Java EE supports secure flag in Cookie interface since version 6 (Servlet class version 3)[1], also for session cookies (JSESSIONID)[2]. Methods setSecure and isSecure can be used to set and check for secure value in cookies.

web.xml

Servlet 3.0 (Java EE 6) introduced a standard way to configure secure attribute for the session cookie, this can be done by applying the following configuration in web.xml

<session-config>
 <cookie-config>
  <secure>true</secure>
 </cookie-config>
<session-config>

Tomcat

In Tomcat 6 if the first request for session is using https then it automatically sets secure attribute on session cookie.

Setting it as a custom header

For older versions the workaround is to rewrite JSESSIONID value using and setting it as a custom header.

Advantage of using custom rewrite trick is that we can configure it differently for each environment driven by application configuration as most local development or test environments may use http.

The drawback is that servers can be configured to use a different session identifier than JSESSIONID. It will be best if configuring this attribute could be a feature supported by Servlet containers.

String sessionid = request.getSession().getId();
response.setHeader("SET-COOKIE", "JSESSIONID=" + sessionid + "; secure");


ASP.NET

<httpCookies requireSSL="true" />


Testing for the Secure Flag

Related Articles

Testing for Cookie Attributes
http://www.troyhunt.com/2011/11/owasp-top-10-for-net-developers-part-9.html