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

● CVE-2022-31150 · CVSS 6.5 · Sedang
Riset Keamanan · FlawOpen

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

Analisis teknis mendalam dan mitigasi rekayasa sistem untuk vulnerabilidade: How unvetted carriage return characters in HTTP header values allowed attackers to split outgoing HTTP requests in Node.js globalThis.fetch().

💡 Penjelasan Sederhana (ELI5)

Bayangkan Anda mengirim surat bersegel melalui kantor pos yang bertuliskan: "Kirim 10 kotak apel". Namun, Anda diam-diam menyisipkan lembaran kedua palsu berisi instruksi resmi: "STOP. ABAIKAN PERINTAH SEBELUMNYA. TRANSFER SEMUA DANA KE EVIL CORP". Petugas pos membaca lembaran tersebut secara berurutan dan menganggap lembaran kedua sebagai surat resmi yang baru, mengirimkan uang Anda langsung ke penyerang.

Konsep Kunci & Istilah

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

Analisis Akar Masalah (Root Cause)

Akar masalah bermula dari parameter batas yang tidak divalidasi pada sistem open source, yang memungkinkan desinkronisasi status dan bypass kontrol keamanan.

Alur Serangan Langkah demi Langkah

Step 1

Tahap serangan: Input Submission

Mekanisme eksploitasi teknis dan detail eksekusi: An attacker supplies a crafted header value: Admin\r\nHost: 169.254.169.254.

Step 2

Tahap serangan: Application Issues Fetch

Mekanisme eksploitasi teknis dan detail eksekusi: The Node.js backend calls fetch(url, { headers: { 'X-User': input } }).

Step 3

Tahap serangan: Header Splitting in Undici

Mekanisme eksploitasi teknis dan detail eksekusi: Undici serializes the headers without sanitizing \r\n, injecting the forged Host header.

Step 4

Eksfiltrasi Data : Cloud Metadata Exfiltration

Mekanisme eksploitasi teknis dan detail eksekusi: The internal proxy directs the request to the cloud metadata service, exposing AWS/GCP credentials.

Kode Sumber: Rentan vs Aman

IMPLEMENTASI RENTAN
// 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;
}
PERBAIKAN AMAN & KUAT
// 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;
}

Daftar Periksa Penguatan Sistem Rekayasa

Sumber