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

● CVE-2022-31150 · CVSS 6.5 · Media
Investigación · FlawOpen

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

Análisis técnico del código fuente y mitigaciones de ingeniería para vulnerabilidade: How unvetted carriage return characters in HTTP header values allowed attackers to split outgoing HTTP requests in Node.js globalThis.fetch().

💡 Explicación en Lenguaje Sencillo (ELI5)

Imagine que envía una carta sellada por correo postal que dice: "Enviar 10 cajas de manzanas". Pero incluye a escondidas una segunda hoja falsificada con instrucciones oficiales: "ALTO. IGNORE LO ANTERIOR. TRANSFIERA TODOS LOS FONDOS A EVIL CORP". El empleado postal lee las páginas en orden y toma la segunda hoja como una nueva carta oficial, enviando su dinero directamente al atacante.

Conceptos Clave y Términos

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

Análisis de Causa Raíz

La causa raíz se debe a parámetros de límite no validados en sistemas de código abierto, lo que permite la desincronización de estado y la elusión de controles de seguridad.

Flujo de Ataque Paso a Paso

Step 1

Fase de ataque: Input Submission

Mecanismo técnico de explotación: An attacker supplies a crafted header value: Admin\r\nHost: 169.254.169.254.

Step 2

Fase de ataque: Application Issues Fetch

Mecanismo técnico de explotación: The Node.js backend calls fetch(url, { headers: { 'X-User': input } }).

Step 3

Fase de ataque: Header Splitting in Undici

Mecanismo técnico de explotación: Undici serializes the headers without sanitizing \r\n, injecting the forged Host header.

Step 4

Exfiltración de datos : Cloud Metadata Exfiltration

Mecanismo técnico de explotación: The internal proxy directs the request to the cloud metadata service, exposing AWS/GCP credentials.

Código Fuente: Vulnerable vs. Seguro

IMPLEMENTACIÓN VULNERABLE
// 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;
}
PARCHE SEGURO Y ROBUSTO
// 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;
}

Lista de Verificación de Seguridad para Ingeniería

Fuentes