Commit 3e306b1
committed
fix: prevent httplib header processing crashes on Android
This fixes a crash in httplib::Server::process_request during header
map operations that was causing SIGSEGV on Android devices. The crash
occurred in std::map operations with case-insensitive header comparison
when processing malformed or excessive HTTP headers.
Root causes:
1. Unbounded header sizes: Headers could grow arbitrarily large,
causing memory allocation failures in map node construction
2. No payload limits: Requests without size limits could exhaust
memory during processing
3. No timeouts: Slow or malicious clients could tie up server threads
Fixes:
1. Runtime limits via httplib API:
- set_payload_max_length(100MB): Prevents unbounded memory usage
- set_read_timeout(30s): Prevents slow requests from blocking threads
- set_write_timeout(60s): Allows time for large document transfers
2. Compile-time limits via preprocessor definitions:
- CPPHTTPLIB_HEADER_MAX_LENGTH=4KB: Reduced from default 8KB to
limit header map memory consumption
- CPPHTTPLIB_PAYLOAD_MAX_LENGTH=100MB: Compile-time enforcement
as additional safety layer
These limits are appropriate for document serving use case and prevent
the crashes seen in Google Play crash reports affecting Android users.
Backtrace showed crash at:
httplib::Server::process_request
-> std::__tree::__assign_multi (header map operations)
-> std::__tree::__construct_node (memory allocation)1 parent 280ec20 commit 3e306b1
2 files changed
Lines changed: 18 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
198 | 198 | | |
199 | 199 | | |
200 | 200 | | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
201 | 210 | | |
202 | 211 | | |
203 | 212 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
22 | 31 | | |
23 | 32 | | |
24 | 33 | | |
| |||
0 commit comments