Merge pull request #7306 from AJ0070/fix-rtsp-camera-session

fix: HTTP camera auth uses the Digest challenge when a camera offers Digest and Basic on separate lines
This commit is contained in:
Isaac Connor authored and GitHub committed 2026-11-22 09:15:01 -05:00
commit afdbf070af
2 files changed
+71 -1

No files matched your search

+26 -1
View File
@@ -21,6 +21,7 @@
#include "zm_crypt.h"
#include "zm_logger.h"
#include "zm_utils.h"
#include <algorithm>
#include <cstring>
#include <utility>
@@ -190,6 +191,11 @@ void Authenticator::checkAuthResponse(const std::string &response) {
const char* authenticate_match = "WWW-Authenticate:";
size_t authenticate_match_len = strlen(authenticate_match);
// A camera may offer Basic and Digest on separate lines in either order,
// and several Digest lines for different algorithms. Prefer Digest over
// Basic, and among Digest challenges the MD5 one (or one with no algorithm),
// since MD5 is the only digest computed here. Otherwise keep the last line.
std::string firstDigest;
for ( size_t i = 0; i < lines.size(); i++ ) {
// stop at end of headers
if ( lines[i].length() == 0 )
@@ -198,9 +204,28 @@ void Authenticator::checkAuthResponse(const std::string &response) {
if ( strncasecmp(lines[i].c_str(), authenticate_match, authenticate_match_len) == 0 ) {
authLine = lines[i];
Debug(2, "Found auth line at %zu:", i);
//break;
size_t value_start = lines[i].find_first_not_of(" \t", authenticate_match_len);
if ( value_start == std::string::npos ||
strncasecmp(lines[i].c_str() + value_start, "Digest ", 7) != 0 )
continue;
std::string lower = lines[i];
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
size_t alg = lower.find("algorithm=");
if ( alg == std::string::npos ) {
firstDigest = lines[i];
break;
}
size_t alg_value = alg + strlen("algorithm=");
if ( alg_value < lower.size() && lower[alg_value] == '"' ) alg_value++;
if ( lower.compare(alg_value, 3, "md5") == 0 &&
(alg_value + 3 >= lower.size() || lower[alg_value + 3] != '-') ) {
firstDigest = lines[i];
break;
}
if ( firstDigest.empty() ) firstDigest = lines[i];
}
}
if ( !firstDigest.empty() ) authLine = firstDigest;
if ( !authLine.empty() ) {
Debug(2, "Analyze auth line %s", authLine.c_str());
authHandleHeader(TrimSpaces(authLine.substr(authenticate_match_len, authLine.length() - authenticate_match_len)));
+45
View File
@@ -111,6 +111,51 @@ TEST_CASE("Authenticator Digest tolerates spaces around '='", "[rtsp_auth]") {
CHECK(auth.nonce() == "n1");
}
TEST_CASE("Authenticator prefers Digest over a later Basic challenge", "[rtsp_auth]") {
zm::Authenticator auth("user1", "pass1");
auth.checkAuthResponse(
"HTTP/1.1 401 Unauthorized\r\n"
"WWW-Authenticate: Digest realm=\"Cam\", nonce=\"n1\", qop=\"auth\"\r\n"
"WWW-Authenticate: Basic realm=\"Cam\"\r\n"
"\r\n");
CHECK(auth.auth_method() == zm::AUTH_DIGEST);
CHECK(auth.nonce() == "n1");
}
TEST_CASE("Authenticator uses Basic when only Basic is offered", "[rtsp_auth]") {
zm::Authenticator auth("user1", "pass1");
auth.checkAuthResponse(
"HTTP/1.1 401 Unauthorized\r\n"
"WWW-Authenticate: Basic realm=\"Cam\"\r\n"
"\r\n");
CHECK(auth.auth_method() == zm::AUTH_BASIC);
}
TEST_CASE("Authenticator prefers the MD5 Digest challenge when several are offered", "[rtsp_auth]") {
zm::Authenticator auth("user1", "pass1");
auth.checkAuthResponse(
"HTTP/1.1 401 Unauthorized\r\n"
"WWW-Authenticate: Digest realm=\"Cam\", nonce=\"sha\", algorithm=SHA-256\r\n"
"WWW-Authenticate: Digest realm=\"Cam\", nonce=\"md5\", algorithm=MD5\r\n"
"WWW-Authenticate: Basic realm=\"Cam\"\r\n"
"\r\n");
CHECK(auth.auth_method() == zm::AUTH_DIGEST);
CHECK(auth.nonce() == "md5");
}
TEST_CASE("Authenticator falls back to the first Digest challenge without MD5", "[rtsp_auth]") {
zm::Authenticator auth("user1", "pass1");
auth.checkAuthResponse(
"HTTP/1.1 401 Unauthorized\r\n"
"WWW-Authenticate: Digest realm=\"Cam\", nonce=\"sha\", algorithm=SHA-256\r\n"
"WWW-Authenticate: Basic realm=\"Cam\"\r\n"
"\r\n");
CHECK(auth.auth_method() == zm::AUTH_DIGEST);
CHECK(auth.nonce() == "sha");
TEST_CASE("Authenticator Digest drops qop when a later challenge has none", "[rtsp_auth]") {
zm::Authenticator auth("user1", "pass1");
auth.authHandleHeader("Digest realm=\"Cam\", nonce=\"n1\", qop=\"auth\"");