From 9399bc2d6e48c6820a1d55624864d4e81bdb06cb Mon Sep 17 00:00:00 2001 From: Jash Ambaliya <87864439+AJ0070@users.noreply.github.com> Date: Fri, 27 Nov 2026 08:06:32 +0530 Subject: [PATCH] fix: keep header and proxy credentials out of the ffmpeg input options error refs #7801 (#7802) --- src/zm_ffmpeg_camera.cpp | 2 +- src/zm_utils.cpp | 24 ++++++++++++++++++++++++ src/zm_utils.h | 4 ++++ tests/zm_utils.cpp | 27 +++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index b9e079355d..252b24cc60 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -462,7 +462,7 @@ int FfmpegCamera::OpenFfmpeg() { ret = av_dict_parse_string(&opts, mOptions.c_str(), "=", kOptionSeparators, 0); if (ret < 0) { Error("Could not parse ffmpeg input options '%s'. Options must be key=value pairs separated by commas or " - "new lines, such as tls_verify=0, not ffmpeg command line flags like -tls_verify 0.", mOptions.c_str()); + "new lines, such as tls_verify=0, not ffmpeg command line flags like -tls_verify 0.", options_for_log(mOptions).c_str()); } // "loop=1" is handled by us (seek-to-start on EOF), not by ffmpeg, which diff --git a/src/zm_utils.cpp b/src/zm_utils.cpp index ef30d3e01a..58e3041afa 100644 --- a/src/zm_utils.cpp +++ b/src/zm_utils.cpp @@ -694,6 +694,30 @@ std::string request_for_log(const std::string &request) { return out; } +std::string options_for_log(const std::string &options) { + std::string out; + out.reserve(options.size()); + std::size_t start = 0; + while (start < options.size()) { + std::size_t sep = options.find_first_of(kOptionSeparators, start); + std::size_t end = (sep == std::string::npos) ? options.size() : sep; + std::string entry = options.substr(start, end - start); + std::size_t eq = entry.find('='); + std::string key = (eq == std::string::npos) ? "" : StringToLower(TrimSpaces(entry.substr(0, eq))); + if (key == "headers" or key == "cookies" or key == "http_proxy") { + out += entry.substr(0, eq + 1) + ""; + } else if (StringToLower(entry).find("authorization") != std::string::npos) { + out += ""; + } else { + out += entry; + } + if (sep == std::string::npos) break; + out += options[sep]; + start = sep + 1; + } + return out; +} + std::string remove_newlines( std::string str ) { while (!str.empty() && str.find("\n") != std::string::npos) str.erase(std::remove(str.begin(), str.end(), '\n'), str.cend()); diff --git a/src/zm_utils.h b/src/zm_utils.h index dd1dcabdd7..3c8e1471b9 100644 --- a/src/zm_utils.h +++ b/src/zm_utils.h @@ -197,6 +197,10 @@ std::string url_for_log(const std::string &url); // url_for_log() and the value of any Authorization or Proxy-Authorization // header (Basic carries user:password, only base64-encoded) replaced. std::string request_for_log(const std::string &request); +// A monitor's Options string as it may be logged: the values of headers, +// cookies and http_proxy (which carry credentials) and any entry mentioning +// authorization are replaced with "". +std::string options_for_log(const std::string &options); std::string UriEncode(const std::string &value); // url with user:pass@ inserted after its scheme://, as stored (not encoded: a diff --git a/tests/zm_utils.cpp b/tests/zm_utils.cpp index f96bcf6560..5d882a3411 100644 --- a/tests/zm_utils.cpp +++ b/tests/zm_utils.cpp @@ -556,6 +556,33 @@ TEST_CASE("request_for_log") { } } +TEST_CASE("options_for_log") { + SECTION("a headers value is removed") { + REQUIRE(options_for_log("headers=Authorization: Basic dXNlcjpwYXNz") == "headers="); + } + SECTION("the separators around a removed value are kept") { + REQUIRE(options_for_log("tls_verify=0,headers=Authorization: Bearer abc,timeout=5") + == "tls_verify=0,headers=,timeout=5"); + } + SECTION("a newline separated options string") { + REQUIRE(options_for_log("cookies=sid=abc\ntimeout=5000000") + == "cookies=\ntimeout=5000000"); + } + SECTION("a proxy url carrying credentials is removed") { + REQUIRE(options_for_log("http_proxy=http://u:p@host:3128") == "http_proxy="); + } + SECTION("a command-line style authorization value without '=' is removed") { + REQUIRE(options_for_log("-headers Authorization: Basic xyz") == ""); + } + SECTION("options without credentials are unchanged") { + std::string opts = "tls_verify=0,rw_timeout=5000000"; + REQUIRE(options_for_log(opts) == opts); + } + SECTION("the empty string") { + REQUIRE(options_for_log("") == ""); + } +} + TEST_CASE("url_for_log") { SECTION("plain url is unchanged") { REQUIRE(url_for_log("rtsp://127.0.0.1:8554/cam") == "rtsp://127.0.0.1:8554/cam");