Skip to content

Commit 95d6df4

Browse files
authored
Merge pull request #367 from etr/feature/bauth-conditional-compile
Add HAVE_BAUTH conditional compilation for basic auth
2 parents 1a26dfc + ae59cc7 commit 95d6df4

19 files changed

+93
-8
lines changed

ChangeLog

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
Version 0.19.0 - 2023-06-15
1+
Version 0.20.0
22

3+
Added conditional compilation for basic auth (HAVE_BAUTH), mirroring
4+
existing HAVE_DAUTH pattern for digest auth. Basic auth support
5+
is auto-detected via AC_CHECK_LIB and can be disabled at build time.
36
Fixed path traversal vulnerability in file uploads when
47
generate_random_filename_on_upload is disabled.
58
Fixed TOCTOU race in file_response by replacing stat-then-open with
@@ -12,6 +15,9 @@ Version 0.19.0 - 2023-06-15
1215
Fixed auth skip path bypass via path traversal (e.g. /public/../protected).
1316
Fixed use of free() instead of MHD_free() for digest auth username.
1417
Fixed unchecked write error during file upload.
18+
19+
Version 0.19.0 - 2023-06-15
20+
1521
Considering family_url as part of the priority when selecting a URL to match.
1622
More explicit selection of C++ version.
1723
Ability to handle multiple parameters with the same name on the URL.

configure.ac

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
AC_PREREQ(2.57)
2323
m4_define([libhttpserver_MAJOR_VERSION],[0])dnl
24-
m4_define([libhttpserver_MINOR_VERSION],[19])dnl
24+
m4_define([libhttpserver_MINOR_VERSION],[20])dnl
2525
m4_define([libhttpserver_REVISION],[0])dnl
2626
m4_define([libhttpserver_PKG_VERSION],[libhttpserver_MAJOR_VERSION.libhttpserver_MINOR_VERSION.libhttpserver_REVISION])dnl
2727
m4_define([libhttpserver_LDF_VERSION],[libhttpserver_MAJOR_VERSION:libhttpserver_MINOR_VERSION:libhttpserver_REVISION])dnl
@@ -149,6 +149,11 @@ fi
149149
AM_CONDITIONAL([COND_CROSS_COMPILE],[test x"$cond_cross_compile" = x"yes"])
150150
AC_SUBST(COND_CROSS_COMPILE)
151151

152+
# Check for basic auth support in libmicrohttpd
153+
AC_CHECK_LIB([microhttpd], [MHD_queue_basic_auth_fail_response],
154+
[have_bauth="yes"],
155+
[have_bauth="no"; AC_MSG_WARN("libmicrohttpd basic auth support not found. Basic auth will be disabled")])
156+
152157
# Check for digest auth support in libmicrohttpd
153158
AC_CHECK_LIB([microhttpd], [MHD_queue_auth_fail_response],
154159
[have_dauth="yes"],
@@ -264,6 +269,13 @@ fi
264269

265270
AM_CONDITIONAL([HAVE_GNUTLS],[test x"$have_gnutls" = x"yes"])
266271

272+
if test x"$have_bauth" = x"yes"; then
273+
AM_CXXFLAGS="$AM_CXXFLAGS -DHAVE_BAUTH"
274+
AM_CFLAGS="$AM_CXXFLAGS -DHAVE_BAUTH"
275+
fi
276+
277+
AM_CONDITIONAL([HAVE_BAUTH],[test x"$have_bauth" = x"yes"])
278+
267279
if test x"$have_dauth" = x"yes"; then
268280
AM_CXXFLAGS="$AM_CXXFLAGS -DHAVE_DAUTH"
269281
AM_CFLAGS="$AM_CXXFLAGS -DHAVE_DAUTH"
@@ -327,6 +339,7 @@ AC_MSG_NOTICE([Configuration Summary:
327339
License : LGPL only
328340
Debug : ${debugit}
329341
TLS Enabled : ${have_gnutls}
342+
Basic Auth : ${have_bauth}
330343
Digest Auth : ${have_dauth}
331344
TCP_FASTOPEN : ${is_fastopen_supported}
332345
Static : ${static}

examples/Makefile.am

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
LDADD = $(top_builddir)/src/libhttpserver.la
2020
AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/src/httpserver/
2121
METASOURCES = AUTO
22-
noinst_PROGRAMS = hello_world service minimal_hello_world custom_error allowing_disallowing_methods handlers hello_with_get_arg args_processing setting_headers custom_access_log basic_authentication minimal_https minimal_file_response minimal_deferred url_registration minimal_ip_ban benchmark_select benchmark_threads benchmark_nodelay deferred_with_accumulator file_upload file_upload_with_callback
22+
noinst_PROGRAMS = hello_world service minimal_hello_world custom_error allowing_disallowing_methods handlers hello_with_get_arg args_processing setting_headers custom_access_log minimal_https minimal_file_response minimal_deferred url_registration minimal_ip_ban benchmark_select benchmark_threads benchmark_nodelay deferred_with_accumulator file_upload file_upload_with_callback
2323

2424
hello_world_SOURCES = hello_world.cpp
2525
service_SOURCES = service.cpp
@@ -31,7 +31,6 @@ hello_with_get_arg_SOURCES = hello_with_get_arg.cpp
3131
args_processing_SOURCES = args_processing.cpp
3232
setting_headers_SOURCES = setting_headers.cpp
3333
custom_access_log_SOURCES = custom_access_log.cpp
34-
basic_authentication_SOURCES = basic_authentication.cpp
3534
minimal_https_SOURCES = minimal_https.cpp
3635
minimal_file_response_SOURCES = minimal_file_response.cpp
3736
minimal_deferred_SOURCES = minimal_deferred.cpp
@@ -44,6 +43,12 @@ benchmark_nodelay_SOURCES = benchmark_nodelay.cpp
4443
file_upload_SOURCES = file_upload.cpp
4544
file_upload_with_callback_SOURCES = file_upload_with_callback.cpp
4645

46+
if HAVE_BAUTH
47+
noinst_PROGRAMS += basic_authentication centralized_authentication
48+
basic_authentication_SOURCES = basic_authentication.cpp
49+
centralized_authentication_SOURCES = centralized_authentication.cpp
50+
endif
51+
4752
if HAVE_GNUTLS
4853
LDADD += -lgnutls
4954
noinst_PROGRAMS += minimal_https_psk

src/Makefile.am

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@
1919
AM_CPPFLAGS = -I../ -I$(srcdir)/httpserver/
2020
METASOURCES = AUTO
2121
lib_LTLIBRARIES = libhttpserver.la
22-
libhttpserver_la_SOURCES = string_utilities.cpp webserver.cpp http_utils.cpp file_info.cpp http_request.cpp http_response.cpp string_response.cpp basic_auth_fail_response.cpp digest_auth_fail_response.cpp deferred_response.cpp file_response.cpp http_resource.cpp create_webserver.cpp details/http_endpoint.cpp
22+
libhttpserver_la_SOURCES = string_utilities.cpp webserver.cpp http_utils.cpp file_info.cpp http_request.cpp http_response.cpp string_response.cpp digest_auth_fail_response.cpp deferred_response.cpp file_response.cpp http_resource.cpp create_webserver.cpp details/http_endpoint.cpp
2323
noinst_HEADERS = httpserver/string_utilities.hpp httpserver/details/modded_request.hpp gettext.h
24-
nobase_include_HEADERS = httpserver.hpp httpserver/create_webserver.hpp httpserver/webserver.hpp httpserver/http_utils.hpp httpserver/file_info.hpp httpserver/details/http_endpoint.hpp httpserver/http_request.hpp httpserver/http_response.hpp httpserver/http_resource.hpp httpserver/string_response.hpp httpserver/basic_auth_fail_response.hpp httpserver/digest_auth_fail_response.hpp httpserver/deferred_response.hpp httpserver/file_response.hpp httpserver/http_arg_value.hpp
24+
nobase_include_HEADERS = httpserver.hpp httpserver/create_webserver.hpp httpserver/webserver.hpp httpserver/http_utils.hpp httpserver/file_info.hpp httpserver/details/http_endpoint.hpp httpserver/http_request.hpp httpserver/http_response.hpp httpserver/http_resource.hpp httpserver/string_response.hpp httpserver/digest_auth_fail_response.hpp httpserver/deferred_response.hpp httpserver/file_response.hpp httpserver/http_arg_value.hpp
25+
26+
if HAVE_BAUTH
27+
libhttpserver_la_SOURCES += basic_auth_fail_response.cpp
28+
nobase_include_HEADERS += httpserver/basic_auth_fail_response.hpp
29+
endif
2530

2631
AM_CXXFLAGS += -fPIC -Wall
2732

src/basic_auth_fail_response.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
USA
1919
*/
2020

21+
#ifdef HAVE_BAUTH
22+
2123
#include "httpserver/basic_auth_fail_response.hpp"
2224
#include <microhttpd.h>
2325
#include <iosfwd>
@@ -32,3 +34,5 @@ int basic_auth_fail_response::enqueue_response(MHD_Connection* connection, MHD_R
3234
}
3335

3436
} // namespace httpserver
37+
38+
#endif // HAVE_BAUTH

src/create_test_request.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ http_request create_test_request::build() {
4949
req.cache->querystring = std::move(_querystring);
5050
}
5151

52+
#ifdef HAVE_BAUTH
5253
req.cache->username = std::move(_user);
5354
req.cache->password = std::move(_pass);
55+
#endif // HAVE_BAUTH
5456

5557
#ifdef HAVE_DAUTH
5658
req.cache->digested_user = std::move(_digested_user);

src/http_request.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ MHD_Result http_request::build_request_querystring(void *cls, enum MHD_ValueKind
310310
return MHD_YES;
311311
}
312312

313+
#ifdef HAVE_BAUTH
313314
void http_request::fetch_user_pass() const {
314315
char* password = nullptr;
315316
auto* username = MHD_basic_auth_get_username_password(underlying_connection, &password);
@@ -339,6 +340,7 @@ std::string_view http_request::get_pass() const {
339340
fetch_user_pass();
340341
return cache->password;
341342
}
343+
#endif // HAVE_BAUTH
342344

343345
#ifdef HAVE_DAUTH
344346
std::string_view http_request::get_digested_user() const {
@@ -557,8 +559,11 @@ uint16_t http_request::get_requestor_port() const {
557559
}
558560

559561
std::ostream &operator<< (std::ostream &os, const http_request &r) {
560-
os << r.get_method() << " Request [user:\"" << r.get_user() << "\" pass:\"" << r.get_pass() << "\"] path:\""
561-
<< r.get_path() << "\"" << std::endl;
562+
os << r.get_method() << " Request [";
563+
#ifdef HAVE_BAUTH
564+
os << "user:\"" << r.get_user() << "\" pass:\"" << r.get_pass() << "\"";
565+
#endif // HAVE_BAUTH
566+
os << "] path:\"" << r.get_path() << "\"" << std::endl;
562567

563568
http::dump_header_map(os, "Headers", r.get_headers());
564569
http::dump_header_map(os, "Footers", r.get_footers());

src/httpserver.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727

2828
#define _HTTPSERVER_HPP_INSIDE_
2929

30+
#ifdef HAVE_BAUTH
3031
#include "httpserver/basic_auth_fail_response.hpp"
32+
#endif // HAVE_BAUTH
3133
#include "httpserver/deferred_response.hpp"
3234
#ifdef HAVE_DAUTH
3335
#include "httpserver/digest_auth_fail_response.hpp"

src/httpserver/basic_auth_fail_response.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#ifndef SRC_HTTPSERVER_BASIC_AUTH_FAIL_RESPONSE_HPP_
2626
#define SRC_HTTPSERVER_BASIC_AUTH_FAIL_RESPONSE_HPP_
2727

28+
#ifdef HAVE_BAUTH
29+
2830
#include <string>
2931
#include "httpserver/http_utils.hpp"
3032
#include "httpserver/string_response.hpp"
@@ -60,4 +62,7 @@ class basic_auth_fail_response : public string_response {
6062
};
6163

6264
} // namespace httpserver
65+
66+
#endif // HAVE_BAUTH
67+
6368
#endif // SRC_HTTPSERVER_BASIC_AUTH_FAIL_RESPONSE_HPP_

src/httpserver/create_test_request.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class create_test_request {
8383
return *this;
8484
}
8585

86+
#ifdef HAVE_BAUTH
8687
create_test_request& user(const std::string& user) {
8788
_user = user;
8889
return *this;
@@ -92,6 +93,7 @@ class create_test_request {
9293
_pass = pass;
9394
return *this;
9495
}
96+
#endif // HAVE_BAUTH
9597

9698
#ifdef HAVE_DAUTH
9799
create_test_request& digested_user(const std::string& digested_user) {
@@ -129,8 +131,10 @@ class create_test_request {
129131
http::header_map _cookies;
130132
std::map<std::string, std::vector<std::string>, http::arg_comparator> _args;
131133
std::string _querystring;
134+
#ifdef HAVE_BAUTH
132135
std::string _user;
133136
std::string _pass;
137+
#endif // HAVE_BAUTH
134138
#ifdef HAVE_DAUTH
135139
std::string _digested_user;
136140
#endif // HAVE_DAUTH

0 commit comments

Comments
 (0)