[Security] Prevent HTTP Response Header Injection by sanitizing CR/LF characters#1167
[Security] Prevent HTTP Response Header Injection by sanitizing CR/LF characters#1167gittiver merged 1 commit intoCrowCpp:masterfrom
Conversation
…rld_inject example
Why Filtering on the Request Side Has Limited Impact1. The Nature of Request Headers 2. Usage Scope of Why It Is Still Worth Retaining1. Defense in Depth
2. Negligible Cost The Real Gap (and Why It's Not a Vulnerability)It is important to note that in However, this is not a security issue. The HTTP parser itself guarantees that each header field and value is already strictly delimited by lines. Since the protocol structure prevents newlines from existing within a parsed value, additional sanitization at the parser level is redundant for preventing header injection via network traffic. |
|
@gittiver could you please check this pr? |
…gfix release * master: (22 commits) fix#1165: sanitize header values to prevent injection and add helloworld_inject example (#1167) Handle headers for HTTPMethod::Options Fix build on OpenBSD (#1162) Enable compiler sanitizers for test builds (Fixes #1137) Enable compiler sanitizers for test builds (Fixes #1137) Enable compiler sanitizers for test builds (Fixes #1137) Feature#1129-CMake Error: ALIAS target "Boost::system" name collision when using vcpkg and FetchContent (#1133) Fix heap-buffer-overflow in query_string when parsing malformed '%'Fix heap-buffer-overflow in query_string for malformed '%' (#1134) Minor features to crow::json::wvalue (#1143) ... # Conflicts: # .github/workflows/build_and_test.yml
…gfix release * master: (22 commits) fix#1165: sanitize header values to prevent injection and add helloworld_inject example (#1167) Handle headers for HTTPMethod::Options Fix build on OpenBSD (#1162) Enable compiler sanitizers for test builds (Fixes #1137) Enable compiler sanitizers for test builds (Fixes #1137) Enable compiler sanitizers for test builds (Fixes #1137) Feature#1129-CMake Error: ALIAS target "Boost::system" name collision when using vcpkg and FetchContent (#1133) Fix heap-buffer-overflow in query_string when parsing malformed '%'Fix heap-buffer-overflow in query_string for malformed '%' (#1134) Minor features to crow::json::wvalue (#1143) ... # Conflicts: # .github/workflows/build_and_test.yml
for the bug issue:
#1165
Title
[Security] Prevent HTTP Response Header Injection by sanitizing CR/LF characters
🚨 Summary
This PR introduces a critical security fix to prevent HTTP Response Header Injection (also known as CRLF Injection). It ensures that any Carriage Return (
\r) or Line Feed (\n) characters are stripped from header names and values before they are stored or sent in an HTTP response.📖 Background: The Vulnerability
In the current implementation, the
crow::responseandcrow::requestobjects allow setting header keys and values directly from user input without validation. Since the HTTP protocol uses\r\nto delimit headers, an attacker who can control a header value can inject these characters to:Set-Cookieheader to hijack sessions.\r\n\r\n) and inject arbitrary content into the response body, leading to Cross-Site Scripting (XSS) attacks.Proof of Concept (PoC)
Before this fix, the following code would result in a malformed and dangerous HTTP response:
Resulting Raw HTTP Response (Vulnerable):
The browser interprets
Injected-Headeras a legitimate new header controlled by the attacker.🛠️ Solution Strategy
This fix implements a sanitization strategy at the entry point of all header modification methods. Instead of throwing an exception (which could break existing applications unexpectedly), we silently strip invalid characters. This approach:
std::remove_if).🔑 Key Changes
1. New Utility Function (
include/crow/http_request.h)Added an inline helper function
sanitize_header_valuethat removes\rand\nfrom a given string.2. Protection in Response Handlers (
include/crow/http_response.h)Applied sanitization to both
set_headerandadd_headermethods in theresponseclass.set_header: Cleans key and value before updating the map.add_header: Cleans key and value before insertion.3. Protection in Request Handlers (
include/crow/http_request.h)Applied sanitization to the
add_headermethod in therequestclass to prevent internal header pollution if requests are forwarded or processed internally.4. New Example/Test Case (
examples/helloworld_inject.cpp)Added a new example demonstrating the fix.
\r\n.examples/CMakeLists.txtto includehelloworld_inject.🧪 Testing & Verification
Manual Verification
curlin verbose mode:X-CustomandInjected.X-Customwith the valuesafeInjected: yes. The newline is gone, and no new header is created.Automated Testing Recommendation
It is recommended to add a unit test in
tests/unittest.cpp:crow::responseobject.set_headerwith a value containing\r\n.\ror\n.📉 Impact Analysis