http: stricter Transfer-Encoding and header separator parsing

Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
PR-URL: https://github.com/nodejs-private/node-private/pull/315
CVE-ID: CVE-2022-32215,CVE-2022-32214,CVE-2022-32212
This commit is contained in:
Paolo Insogna authored and RafaelGSS committed 2022-07-07 13:20:40 -03:00
1 parent 91fe38e6ab
commit d9b71f4c24
11 files changed
+1137 -560

No files matched your search

+89 -27
View File
@@ -1,31 +1,101 @@
cmake_minimum_required(VERSION 3.5.1)
cmake_policy(SET CMP0069 NEW)
project(llhttp C)
project(llhttp VERSION 6.0.5)
include(GNUInstallDirs)
set(CMAKE_C_STANDARD 99)
# By default build in relwithdebinfo type, supports both lowercase and uppercase
if(NOT CMAKE_CONFIGURATION_TYPES)
set(allowableBuileTypes DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${allowableBuileTypes}")
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RELWITHDEBINFO CACHE STRING "" FORCE)
else()
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
if(NOT CMAKE_BUILD_TYPE IN_LIST allowableBuileTypes)
message(FATEL_ERROR "Invalid build type: ${CMAKE_BUILD_TYPE}")
endif()
endif()
endif()
#
# Options
#
# Generic option
option(BUILD_SHARED_LIBS "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)" OFF)
option(BUILD_SHARED_LIBS "Build shared libraries (.dll/.so)" ON)
option(BUILD_STATIC_LIBS "Build static libraries (.lib/.a)" OFF)
# Source code
set(LLHTTP_SOURCES
src/llhttp.c
src/http.c
src/api.c
${CMAKE_CURRENT_SOURCE_DIR}/src/llhttp.c
${CMAKE_CURRENT_SOURCE_DIR}/src/http.c
${CMAKE_CURRENT_SOURCE_DIR}/src/api.c
)
set(LLHTTP_HEADERS
include/llhttp.h
${CMAKE_CURRENT_SOURCE_DIR}/include/llhttp.h
)
add_library(llhttp)
add_library(llhttp::llhttp ALIAS llhttp)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/libllhttp.pc.in
${CMAKE_CURRENT_SOURCE_DIR}/libllhttp.pc
@ONLY
)
target_sources(llhttp PRIVATE ${LLHTTP_SOURCES} ${LLHTTP_HEADERS})
function(config_library target)
target_sources(${target} PRIVATE ${LLHTTP_SOURCES} ${LLHTTP_HEADERS})
target_include_directories(${target} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
)
set_target_properties(${target} PROPERTIES
OUTPUT_NAME llhttp
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
PUBLIC_HEADER ${LLHTTP_HEADERS}
)
install(TARGETS ${target}
EXPORT llhttp
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/libllhttp.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
# This is required to work with FetchContent
install(EXPORT llhttp
FILE llhttp-config.cmake
NAMESPACE llhttp::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/llhttp)
endfunction(config_library target)
if(BUILD_SHARED_LIBS)
add_library(llhttp_shared SHARED
${llhttp_src}
)
add_library(llhttp::llhttp ALIAS llhttp_shared)
config_library(llhttp_shared)
endif()
if(BUILD_STATIC_LIBS)
add_library(llhttp_static STATIC
${llhttp_src}
)
if(BUILD_SHARED_LIBS)
add_library(llhttp::llhttp ALIAS llhttp_shared)
else()
add_library(llhttp::llhttp ALIAS llhttp_static)
endif()
config_library(llhttp_static)
endif()
# On windows with Visual Studio, add a debug postfix so that release
# and debug libraries can coexist.
@@ -33,21 +103,13 @@ if(MSVC)
set(CMAKE_DEBUG_POSTFIX "d")
endif()
target_include_directories(llhttp PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
set_target_properties(llhttp PROPERTIES PUBLIC_HEADER ${LLHTTP_HEADERS})
install(TARGETS llhttp
EXPORT llhttp
ARCHIVE DESTINATION lib
PUBLIC_HEADER DESTINATION include/
)
# This is required to work with FetchContent
install(EXPORT llhttp
FILE llhttp-config.cmake
NAMESPACE llhttp::
DESTINATION lib/cmake/llhttp)
# Print project configure summary
message(STATUS "")
message(STATUS "")
message(STATUS "Project configure summary:")
message(STATUS "")
message(STATUS " CMake build type .................: ${CMAKE_BUILD_TYPE}")
message(STATUS " Install prefix ...................: ${CMAKE_INSTALL_PREFIX}")
message(STATUS " Build shared library .............: ${BUILD_SHARED_LIBS}")
message(STATUS " Build static library .............: ${BUILD_STATIC_LIBS}")
message(STATUS "")
+33 -3
View File
@@ -90,7 +90,16 @@ if (err == HPE_OK) {
parser.reason);
}
```
For more information on API usage, please refer to [src/native/api.h](https://github.com/nodejs/llhttp/blob/master/src/native/api.h).
For more information on API usage, please refer to [src/native/api.h](https://github.com/nodejs/llhttp/blob/main/src/native/api.h).
## Build Instructions
Make sure you have [Node.js](https://nodejs.org/), npm and npx installed. Then under project directory run:
```sh
npm install
make
```
---
@@ -99,20 +108,41 @@ For more information on API usage, please refer to [src/native/api.h](https://gi
* Python: [pallas/pyllhttp][8]
* Ruby: [metabahn/llhttp][9]
### Using with CMake
If you want to use this library in a CMake project you can use the snippet below.
```
FetchContent_Declare(llhttp
URL "https://github.com/nodejs/llhttp/releases/download/v6.0.4/llhttp-release-v6.0.4.tar.gz") # Using version 6.0.4
URL "https://github.com/nodejs/llhttp/releases/download/v6.0.5/llhttp-release-v6.0.5.tar.gz") # Using version 6.0.5
FetchContent_MakeAvailable(llhttp)
target_link_libraries(${EXAMPLE_PROJECT_NAME} ${PROJECT_LIBRARIES} llhttp ${PROJECT_NAME})
```
## Building on Windows
### Installation
* `choco install git`
* `choco install node`
* `choco install llvm` (or install the `C++ Clang tools for Windows` optional package from the Visual Studio 2019 installer)
* `choco install make` (or if you have MinGW, it comes bundled)
1. Ensure that `Clang` and `make` are in your system path.
2. Using Git Bash, clone the repo to your preferred location.
3. Cd into the cloned directory and run `npm install`
5. Run `make`
6. Your `repo/build` directory should now have `libllhttp.a` and `libllhttp.so` static and dynamic libraries.
7. When building your executable, you can link to these libraries. Make sure to set the build folder as an include path when building so you can reference the declarations in `repo/build/llhttp.h`.
### A simple example on linking with the library:
Assuming you have an executable `main.cpp` in your current working directory, you would run: `clang++ -Os -g3 -Wall -Wextra -Wno-unused-parameter -I/path/to/llhttp/build main.cpp /path/to/llhttp/build/libllhttp.a -o main.exe`.
If you are getting `unresolved external symbol` linker errors you are likely attempting to build `llhttp.c` without linking it with object files from `api.c` and `http.c`.
#### LICENSE
This software is licensed under the MIT License.
+18 -6
View File
@@ -3,7 +3,7 @@
#define LLHTTP_VERSION_MAJOR 6
#define LLHTTP_VERSION_MINOR 0
#define LLHTTP_VERSION_PATCH 6
#define LLHTTP_VERSION_PATCH 7
#ifndef LLHTTP_STRICT_MODE
# define LLHTTP_STRICT_MODE 0
@@ -59,6 +59,7 @@ enum llhttp_errno {
HPE_OK = 0,
HPE_INTERNAL = 1,
HPE_STRICT = 2,
HPE_CR_EXPECTED = 25,
HPE_LF_EXPECTED = 3,
HPE_UNEXPECTED_CONTENT_LENGTH = 4,
HPE_CLOSED_CONNECTION = 5,
@@ -100,7 +101,8 @@ typedef enum llhttp_flags llhttp_flags_t;
enum llhttp_lenient_flags {
LENIENT_HEADERS = 0x1,
LENIENT_CHUNKED_LENGTH = 0x2,
LENIENT_KEEP_ALIVE = 0x4
LENIENT_KEEP_ALIVE = 0x4,
LENIENT_TRANSFER_ENCODING = 0x8
};
typedef enum llhttp_lenient_flags llhttp_lenient_flags_t;
@@ -172,6 +174,7 @@ typedef enum llhttp_method llhttp_method_t;
XX(0, OK, OK) \
XX(1, INTERNAL, INTERNAL) \
XX(2, STRICT, STRICT) \
XX(25, CR_EXPECTED, CR_EXPECTED) \
XX(3, LF_EXPECTED, LF_EXPECTED) \
XX(4, UNEXPECTED_CONTENT_LENGTH, UNEXPECTED_CONTENT_LENGTH) \
XX(5, CLOSED_CONNECTION, CLOSED_CONNECTION) \
@@ -374,8 +377,6 @@ LLHTTP_EXPORT
void llhttp_init(llhttp_t* parser, llhttp_type_t type,
const llhttp_settings_t* settings);
#if defined(__wasm__)
LLHTTP_EXPORT
llhttp_t* llhttp_alloc(llhttp_type_t type);
@@ -400,8 +401,6 @@ int llhttp_get_status_code(llhttp_t* parser);
LLHTTP_EXPORT
uint8_t llhttp_get_upgrade(llhttp_t* parser);
#endif // defined(__wasm__)
/* Reset an already initialized parser back to the start state, preserving the
* existing parser type, callback settings, user data, and lenient flags.
*/
@@ -556,6 +555,19 @@ void llhttp_set_lenient_chunked_length(llhttp_t* parser, int enabled);
*/
void llhttp_set_lenient_keep_alive(llhttp_t* parser, int enabled);
/* Enables/disables lenient handling of `Transfer-Encoding` header.
*
* Normally `llhttp` would error when a `Transfer-Encoding` has `chunked` value
* and another value after it (either in a single header or in multiple
* headers whose value are internally joined using `, `).
* This is mandated by the spec to reliably determine request body size and thus
* avoid request smuggling.
* With this flag the extra value will be parsed normally.
*
* **(USE AT YOUR OWN RISK)**
*/
void llhttp_set_lenient_transfer_encoding(llhttp_t* parser, int enabled);
#ifdef __cplusplus
} /* extern "C" */
#endif
+10
View File
@@ -0,0 +1,10 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=@CMAKE_INSTALL_PREFIX@
libdir=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@
includedir=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@
Name: libllhttp
Description: Node.js llhttp Library
Version: @PROJECT_VERSION@
Libs: -L${libdir} -lllhttp
Cflags: -I${includedir}
+8
View File
@@ -253,6 +253,14 @@ void llhttp_set_lenient_keep_alive(llhttp_t* parser, int enabled) {
}
}
void llhttp_set_lenient_transfer_encoding(llhttp_t* parser, int enabled) {
if (enabled) {
parser->lenient_flags |= LENIENT_TRANSFER_ENCODING;
} else {
parser->lenient_flags &= ~LENIENT_TRANSFER_ENCODING;
}
}
/* Callbacks */
+2 -1
View File
@@ -52,7 +52,8 @@ int llhttp__after_headers_complete(llhttp_t* parser, const char* p,
return 2;
} else if (parser->flags & F_TRANSFER_ENCODING) {
if (parser->type == HTTP_REQUEST &&
(parser->lenient_flags & LENIENT_CHUNKED_LENGTH) == 0) {
(parser->lenient_flags & LENIENT_CHUNKED_LENGTH) == 0 &&
(parser->lenient_flags & LENIENT_TRANSFER_ENCODING) == 0) {
/* RFC 7230 3.3.3 */
/* If a Transfer-Encoding header field
+863 -511
View File
File diff suppressed because it is too large. Load diff
+1 -1
View File
@@ -13,7 +13,7 @@ Content-Type: text/plain; charset=utf-8
Host: hacker.exploit.com
Connection: keep-alive
Content-Length: 10
Transfer-Encoding: chunked, eee
Transfer-Encoding: eee, chunked
HELLOWORLDPOST / HTTP/1.1
Content-Type: text/plain; charset=utf-8
@@ -0,0 +1,43 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');
const msg = [
'GET / HTTP/1.1',
'Host: localhost',
'Dummy: x\nContent-Length: 23',
'',
'GET / HTTP/1.1',
'Dummy: GET /admin HTTP/1.1',
'Host: localhost',
'',
'',
].join('\r\n');
const server = http.createServer(common.mustNotCall());
server.listen(0, common.mustSucceed(() => {
const client = net.connect(server.address().port, 'localhost');
let response = '';
client.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
}));
client.setEncoding('utf8');
client.on('error', common.mustNotCall());
client.on('end', common.mustCall(() => {
assert.strictEqual(
response,
'HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n'
);
server.close();
}));
client.write(msg);
client.resume();
}));
@@ -0,0 +1,51 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');
const msg = [
'POST / HTTP/1.1',
'Host: 127.0.0.1',
'Transfer-Encoding: chunkedchunked',
'',
'1',
'A',
'0',
'',
].join('\r\n');
const server = http.createServer(common.mustCall((req, res) => {
// Verify that no data is received
req.on('data', common.mustNotCall());
req.on('end', common.mustNotCall(() => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end();
}));
}, 1));
server.listen(0, common.mustSucceed(() => {
const client = net.connect(server.address().port, 'localhost');
let response = '';
client.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
}));
client.setEncoding('utf8');
client.on('error', common.mustNotCall());
client.on('end', common.mustCall(() => {
assert.strictEqual(
response,
'HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n'
);
server.close();
}));
client.write(msg);
client.resume();
}));
@@ -1,6 +1,7 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');
@@ -22,23 +23,30 @@ const msg = [
'',
].join('\r\n');
// Verify that the server is called only once even with a smuggled request.
const server = http.createServer(common.mustCall((req, res) => {
const server = http.createServer(common.mustNotCall((req, res) => {
res.end();
}, 1));
function send(next) {
server.listen(0, common.mustSucceed(() => {
const client = net.connect(server.address().port, 'localhost');
let response = '';
// Verify that the server listener is never called
client.on('data', common.mustCall((chunk) => {
response += chunk.toString('utf-8');
}));
client.setEncoding('utf8');
client.on('error', common.mustNotCall());
client.on('end', next);
client.write(msg);
client.resume();
}
server.listen(0, common.mustSucceed(() => {
send(common.mustCall(() => {
client.on('end', common.mustCall(() => {
assert.strictEqual(
response,
'HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n'
);
server.close();
}));
client.write(msg);
client.resume();
}));