From d3bcfeb6eef3e098819321c106fc63050d523b5c Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Wed, 18 Nov 2026 23:32:51 -0500 Subject: [PATCH 1/6] Apply suggested fix to src/video/onvif_detection.c from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --- src/video/onvif_detection.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/video/onvif_detection.c b/src/video/onvif_detection.c index 9bb6ea2f5..b4e0ae67c 100644 --- a/src/video/onvif_detection.c +++ b/src/video/onvif_detection.c @@ -1354,7 +1354,7 @@ static bool has_motion_event(const char *response, char *label, size_t label_sz) static void store_lpr_notifications(const char *response, const char *stream_name) { if (!response || !stream_name || !stream_name[0]) return; - onvif_lpr_event_t events[16] = {0}; + onvif_lpr_event_t events[100] = {0}; int count = onvif_parse_lpr_events(response, events, sizeof(events) / sizeof(events[0])); if (count <= 0) { From 8ec5d8a08cb1bba9229d76def1c68f20043ec117 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Wed, 18 Nov 2026 23:32:52 -0500 Subject: [PATCH 2/6] Apply suggested fix to src/video/onvif_detection.c from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --- src/video/onvif_detection.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/video/onvif_detection.c b/src/video/onvif_detection.c index b4e0ae67c..8ee097f81 100644 --- a/src/video/onvif_detection.c +++ b/src/video/onvif_detection.c @@ -1328,11 +1328,15 @@ static bool has_motion_event(const char *response, char *label, size_t label_sz) if (!copy) return false; ezxml_t xml = ezxml_parse_str(copy, strlen(copy)); - if (!xml) { + const char *parse_error = xml ? ezxml_error(xml) : NULL; + if (!xml || (parse_error && parse_error[0] != '\0')) { /* Malformed XML from buggy firmware: fall back to substring matching, * but at least require a truthy Value somewhere in the payload so a * plain "motion ended" message doesn't trigger. */ log_debug("ONVIF Detection: failed to parse PullMessages response, using fallback heuristic"); + if (xml) { + ezxml_free(xml); + } bool fallback = (strstr(response, "Value=\"true\"") || strstr(response, "Value='true'") || strstr(response, "Value=\"1\"") || strstr(response, "Value='1'")); From f761277f2f825e8a4a06d7726239df12464310ca Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Wed, 18 Nov 2026 23:32:53 -0500 Subject: [PATCH 3/6] Apply suggested fix to src/video/onvif_detection.c from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --- src/video/onvif_detection.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/video/onvif_detection.c b/src/video/onvif_detection.c index 8ee097f81..704c30331 100644 --- a/src/video/onvif_detection.c +++ b/src/video/onvif_detection.c @@ -1187,6 +1187,10 @@ static const char *normalize_object_class(const char *raw) { ci_contains(raw, "person") || ci_contains(raw, "pedestrian")) { return "person"; } + /* Handle motorized two-wheelers before broad bicycle/cycle matching. */ + if (ci_contains(raw, "motorcycle") || ci_contains(raw, "motorbike")) { + return "vehicle"; + } /* Check non-motorized before vehicle so "NonMotor" isn't caught by "motor". */ if (ci_contains(raw, "nonmotor") || ci_contains(raw, "bicycle") || ci_contains(raw, "bike") || ci_contains(raw, "cycle")) { From b95a2c015c3d225039e246c0d605a13bbcf49be4 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Wed, 18 Nov 2026 23:32:53 -0500 Subject: [PATCH 4/6] Apply suggested fix to src/video/onvif_detection.c from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --- src/video/onvif_detection.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/video/onvif_detection.c b/src/video/onvif_detection.c index 704c30331..0dbf02bbe 100644 --- a/src/video/onvif_detection.c +++ b/src/video/onvif_detection.c @@ -931,10 +931,19 @@ static onvif_subscription_t *get_subscription(const char *url, const char *usern if (subscriptions[i].active && now < subscriptions[i].expiration_time) { if (now >= subscriptions[i].renewal_time) { + onvif_subscription_t *sub = &subscriptions[i]; + bool renewed_ok = false; log_debug("Renewing ONVIF subscription for %s", url); - if (!renew_subscription_locked(&subscriptions[i])) { + + pthread_mutex_unlock(&subscription_mutex); + renewed_ok = renew_subscription_locked(sub); + if (!renewed_ok) { log_warn("ONVIF subscription renewal failed for %s; recreating it", url); - unsubscribe_subscription_locked(&subscriptions[i]); + unsubscribe_subscription_locked(sub); + } + pthread_mutex_lock(&subscription_mutex); + + if (!renewed_ok) { break; } } @@ -949,8 +958,11 @@ static onvif_subscription_t *get_subscription(const char *url, const char *usern /* We missed the renewal window. Best-effort Unsubscribe avoids * consuming another slot if the camera granted a longer lease * than the timestamps indicated. */ + onvif_subscription_t *sub = &subscriptions[i]; log_info("ONVIF subscription for %s expired, replacing it", url); - unsubscribe_subscription_locked(&subscriptions[i]); + pthread_mutex_unlock(&subscription_mutex); + unsubscribe_subscription_locked(sub); + pthread_mutex_lock(&subscription_mutex); break; } } From 0e23dae8c7db39784339874aa4c5f336032ce3b9 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Wed, 18 Nov 2026 23:32:54 -0500 Subject: [PATCH 5/6] Apply suggested fix to src/video/onvif_detection.c from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --- src/video/onvif_detection.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/video/onvif_detection.c b/src/video/onvif_detection.c index 0dbf02bbe..70276f763 100644 --- a/src/video/onvif_detection.c +++ b/src/video/onvif_detection.c @@ -762,22 +762,26 @@ static char *send_subscription_request(onvif_subscription_t *subscription, return response; } - // Only genuine relative paths may use the legacy camera-local fallback. + // Resolve relative subscription references against the camera endpoint. // An unsupported scheme or network-path URL is not a relative service. if (strchr(subscription->subscription_address, ':') || strncmp(subscription->subscription_address, "//", 2) == 0) { pthread_mutex_unlock(lock); return NULL; } - const char *last_slash = strrchr(subscription->subscription_address, '/'); - if (!last_slash || last_slash[1] == '\0') { + + char *resolved_url = resolve_reference_url(subscription->camera_url, + subscription->subscription_address); + if (!resolved_url) { pthread_mutex_unlock(lock); return NULL; } - char *response = send_onvif_request(subscription->camera_url, - subscription->username, - subscription->password, - request_body, last_slash + 1, operation); + + char *response = send_onvif_request_absolute(resolved_url, + subscription->username, + subscription->password, + request_body, operation); + free(resolved_url); pthread_mutex_unlock(lock); return response; } From c8dd697d698aeda2891105c8813b4115aaa6504c Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Wed, 18 Nov 2026 23:37:44 -0500 Subject: [PATCH 6/6] Keep safe ONVIF event fixes and cover malformed motion --- src/video/onvif_detection.c | 60 ++++++++++++++----------------- tests/unit/test_onvif_transport.c | 15 ++++++++ 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/src/video/onvif_detection.c b/src/video/onvif_detection.c index 70276f763..be69b223b 100644 --- a/src/video/onvif_detection.c +++ b/src/video/onvif_detection.c @@ -762,26 +762,22 @@ static char *send_subscription_request(onvif_subscription_t *subscription, return response; } - // Resolve relative subscription references against the camera endpoint. + // Only genuine relative paths may use the legacy camera-local fallback. // An unsupported scheme or network-path URL is not a relative service. if (strchr(subscription->subscription_address, ':') || strncmp(subscription->subscription_address, "//", 2) == 0) { pthread_mutex_unlock(lock); return NULL; } - - char *resolved_url = resolve_reference_url(subscription->camera_url, - subscription->subscription_address); - if (!resolved_url) { + const char *last_slash = strrchr(subscription->subscription_address, '/'); + if (!last_slash || last_slash[1] == '\0') { pthread_mutex_unlock(lock); return NULL; } - - char *response = send_onvif_request_absolute(resolved_url, - subscription->username, - subscription->password, - request_body, operation); - free(resolved_url); + char *response = send_onvif_request(subscription->camera_url, + subscription->username, + subscription->password, + request_body, last_slash + 1, operation); pthread_mutex_unlock(lock); return response; } @@ -935,19 +931,10 @@ static onvif_subscription_t *get_subscription(const char *url, const char *usern if (subscriptions[i].active && now < subscriptions[i].expiration_time) { if (now >= subscriptions[i].renewal_time) { - onvif_subscription_t *sub = &subscriptions[i]; - bool renewed_ok = false; log_debug("Renewing ONVIF subscription for %s", url); - - pthread_mutex_unlock(&subscription_mutex); - renewed_ok = renew_subscription_locked(sub); - if (!renewed_ok) { + if (!renew_subscription_locked(&subscriptions[i])) { log_warn("ONVIF subscription renewal failed for %s; recreating it", url); - unsubscribe_subscription_locked(sub); - } - pthread_mutex_lock(&subscription_mutex); - - if (!renewed_ok) { + unsubscribe_subscription_locked(&subscriptions[i]); break; } } @@ -962,11 +949,8 @@ static onvif_subscription_t *get_subscription(const char *url, const char *usern /* We missed the renewal window. Best-effort Unsubscribe avoids * consuming another slot if the camera granted a longer lease * than the timestamps indicated. */ - onvif_subscription_t *sub = &subscriptions[i]; log_info("ONVIF subscription for %s expired, replacing it", url); - pthread_mutex_unlock(&subscription_mutex); - unsubscribe_subscription_locked(sub); - pthread_mutex_lock(&subscription_mutex); + unsubscribe_subscription_locked(&subscriptions[i]); break; } } @@ -1377,12 +1361,17 @@ static bool has_motion_event(const char *response, char *label, size_t label_sz) * or placed on the general detection/event path. */ static void store_lpr_notifications(const char *response, const char *stream_name) { if (!response || !stream_name || !stream_name[0]) return; - - onvif_lpr_event_t events[100] = {0}; - int count = onvif_parse_lpr_events(response, events, - sizeof(events) / sizeof(events[0])); + if (!ci_contains(response, "LicensePlate") && !ci_contains(response, "ANPR") && + !ci_contains(response, "ALPR") && !ci_contains(response, "LPR")) return; + + enum { MAX_LPR_EVENTS_PER_PULL = 100 }; + const size_t events_size = MAX_LPR_EVENTS_PER_PULL * sizeof(onvif_lpr_event_t); + onvif_lpr_event_t *events = calloc(MAX_LPR_EVENTS_PER_PULL, sizeof(*events)); + if (!events) return; + int count = onvif_parse_lpr_events(response, events, MAX_LPR_EVENTS_PER_PULL); if (count <= 0) { - secure_clear(events, sizeof(events)); + secure_clear(events, events_size); + free(events); return; } @@ -1392,7 +1381,8 @@ static void store_lpr_notifications(const char *response, const char *stream_nam log_warn("LPR reads are available but protected storage is disabled: " "LIGHTNVR_LPR_MASTER_KEY_HEX is not configured"); } - secure_clear(events, sizeof(events)); + secure_clear(events, events_size); + free(events); return; } @@ -1400,7 +1390,8 @@ static void store_lpr_notifications(const char *response, const char *stream_nam if (get_stream_config_by_name(stream_name, &stream) != 0 || !stream.camera_uuid[0]) { log_warn("Cannot associate ONVIF LPR notifications with stream identity"); - secure_clear(events, sizeof(events)); + secure_clear(events, events_size); + free(events); return; } @@ -1432,7 +1423,8 @@ static void store_lpr_notifications(const char *response, const char *stream_nam secure_clear(&input, sizeof(input)); secure_clear(read_uuid, sizeof(read_uuid)); } - secure_clear(events, sizeof(events)); + secure_clear(events, events_size); + free(events); } /** diff --git a/tests/unit/test_onvif_transport.c b/tests/unit/test_onvif_transport.c index 39cb65c8d..b6cc48f16 100644 --- a/tests/unit/test_onvif_transport.c +++ b/tests/unit/test_onvif_transport.c @@ -455,6 +455,19 @@ static void test_fixture_send_stops_on_zero_progress_or_closed_peer(void) { TEST_ASSERT_EQUAL_UINT(2, send_calls); } +static void test_motorcycle_class_precedes_bicycle_substrings(void) { + TEST_ASSERT_EQUAL_STRING("vehicle", normalize_object_class("Motorcycle")); + TEST_ASSERT_EQUAL_STRING("vehicle", normalize_object_class("motorbike")); + TEST_ASSERT_EQUAL_STRING("bicycle", normalize_object_class("bicycle")); +} + +static void test_malformed_motion_message_uses_truthy_fallback(void) { + char label[32]; + TEST_ASSERT_TRUE(has_motion_event("