flawopen.com/Teardowns/cve-2022-31150-undici-crlf-injection-ssrf

● CVE-2022-31150 · CVSS 6.5 · Moyenne
Recherche · FlawOpen

CVE-2022-31150: Node.js Undici CRLF Injection & SSRF Teardown

Analyse technique détaillée du code source et mesures de durcissement pour vulnerabilidade : How unvetted carriage return characters in HTTP header values allowed attackers to split outgoing HTTP requests in Node.js globalThis.fetch().

💡 Explication en Langage Simple (ELI5)

Imaginez que vous envoyiez une lettre scellée par la poste disant : "Expédiez 10 caisses de pommes". Mais vous glissez discrètement une seconde feuille officielle falsifiée disant : "STOP. IGNOREZ CE QUI PRÉCÈDE. TRANSFÉREZ TOUS LES FONDS À EVIL CORP". L'agent postal lit les pages dans l'ordre et traite la seconde feuille comme une toute nouvelle lettre officielle, envoyant vos fonds directement à l'attaquant.

Concepts Clés et Termes

Undici
The official next-generation HTTP/1.1 client for Node.js, powering the global fetch() implementation.
CRLF Injection
Injecting Carriage Return (\r) and Line Feed (\n) characters into HTTP headers to create a new header or a whole new request.
HTTP Request Splitting
A vulnerability where an attacker splits a single outgoing HTTP connection into two separate requests.
SSRF (Server-Side Request Forgery)
Coercing a backend server to issue unauthorized requests to internal cloud metadata APIs (169.254.169.254).

Analyse de Cause Racine

La cause fondamentale provient de paramètres de limites non validés dans les systèmes open source, permettant une désynchronisation d'état et le contournement des contrôles de sécurité.

Déroulement de l'Attaque Étape par Étape

Step 1

Étape d'attaque : Input Submission

Mécanisme technique d'exploitation : An attacker supplies a crafted header value: Admin\r\nHost: 169.254.169.254.

Step 2

Étape d'attaque : Application Issues Fetch

Mécanisme technique d'exploitation : The Node.js backend calls fetch(url, { headers: { 'X-User': input } }).

Step 3

Étape d'attaque : Header Splitting in Undici

Mécanisme technique d'exploitation : Undici serializes the headers without sanitizing \r\n, injecting the forged Host header.

Step 4

Exfiltration de données : Cloud Metadata Exfiltration

Mécanisme technique d'exploitation : The internal proxy directs the request to the cloud metadata service, exposing AWS/GCP credentials.

Code Source : Vulnérable vs Sécurisé

IMPLÉMENTATION VULNÉRABLE
// VULNERABLE: lib/core/request.js before patch
function addHeader(headers, key, value) {
    // ROOT CAUSE:
    // Does not sanitize or reject carriage return (\r) and line feed (\n) in values!
    // Allows attackers to split headers and inject arbitrary HTTP directives!
    headers[key] = value;
}
PATCH SÉCURISÉ ET ROBUSTE
// SECURE: lib/core/request.js patch
function addHeader(headers, key, value) {
    // 1. Strict regex checking for dangerous control characters
    const INVALID_HEADER_CHAR_REGEX = /[\r\n]/;
    
    if (INVALID_HEADER_CHAR_REGEX.test(key) || INVALID_HEADER_CHAR_REGEX.test(value)) {
        throw new TypeError(`Invalid character in header content: ["${key}": "${value}"]`);
    }
    
    headers[key] = value;
}

Liste de Contrôle de Sécurité pour l'Ingénierie

Sources