diff --git a/src/httpd_libevhttp.c b/src/httpd_libevhttp.c index b5ac8d5b..242f0636 100644 --- a/src/httpd_libevhttp.c +++ b/src/httpd_libevhttp.c @@ -24,6 +24,7 @@ #include #include #include // listen() +#include // inet_pton() #include #include // flags in struct evhttp @@ -543,6 +544,29 @@ httpd_backend_peer_get(const char **addr, uint16_t *port, httpd_backend *backend return 0; } +// When removing this workaround then also remove the include of arpa/inet.h +static bool +address_is_trusted_workaround(httpd_backend *backend) +{ + union net_sockaddr naddr = { 0 }; + const char *saddr; + uint16_t port; + + DPRINTF(E_DBG, L_HTTPD, "Detected libevent version with buggy evhttp_connection_get_addr()\n"); + + if (httpd_backend_peer_get(&saddr, &port, backend, NULL) < 0) + return false; + + if (inet_pton(AF_INET, saddr, &naddr.sin.sin_addr) == 1) + naddr.sa.sa_family = AF_INET; + else if (inet_pton(AF_INET6, saddr, &naddr.sin6.sin6_addr) == 1) + naddr.sa.sa_family = AF_INET6; + else + return false; + + return net_peer_address_is_trusted(&naddr); +} + bool httpd_backend_peer_is_trusted(httpd_backend *backend) { @@ -556,6 +580,10 @@ httpd_backend_peer_is_trusted(httpd_backend *backend) if (!addr) return false; + // Workaround for bug in libevent 2.1.6 and .8, see #1775 + if (addr->sa_family == AF_UNSPEC) + return address_is_trusted_workaround(backend); + return net_peer_address_is_trusted((union net_sockaddr *)addr); }