Skip to content

Commit 2efd41a

Browse files
committed
Fix std::terminate when uri_log receives null uri pointer
libmicrohttpd may invoke MHD_OPTION_URI_LOG_CALLBACK with a null uri pointer before the request line is parsed - for example on port scans, TLS clients hitting a plain HTTP port, or half-open connections. The previous code assigned the raw pointer directly into a std::string, which throws std::logic_error("basic_string::_M_construct null not valid"). Because the throw originates inside an MHD C callback with no enclosing handler, std::terminate() was called and the process aborted under load. Treat a null uri as an empty string so the assignment is well-defined. An empty URI fails to match any registered resource and surfaces as a 404, which is the correct graceful behaviour. Resolves #371.
1 parent 6c115f3 commit 2efd41a

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ Version 0.20.0
1717
Fixed auth skip path bypass via path traversal (e.g. /public/../protected).
1818
Fixed use of free() instead of MHD_free() for digest auth username.
1919
Fixed unchecked write error during file upload.
20+
Fixed std::terminate when MHD invokes the URI log callback with a
21+
null uri pointer (e.g. port scans, half-open connections, or
22+
non-HTTP traffic). Resolves issue #371.
2023

2124
Version 0.19.0 - 2023-06-15
2225

src/webserver.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,12 @@ void* uri_log(void* cls, const char* uri, struct MHD_Connection *con) {
626626
std::ignore = con;
627627

628628
auto mr = std::make_unique<details::modded_request>();
629-
mr->complete_uri = uri;
629+
// MHD may invoke this callback with a null uri before the request line
630+
// has been parsed (e.g. port scans, half-open connections, or non-HTTP
631+
// traffic on the listening port). Treat that as an empty URI so the
632+
// std::string assignment does not throw std::logic_error and abort the
633+
// process via std::terminate. See issue #371.
634+
mr->complete_uri = (uri != nullptr) ? uri : "";
630635
return reinterpret_cast<void*>(mr.release());
631636
}
632637

0 commit comments

Comments
 (0)