Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/video/onvif_detection.c
Original file line number Diff line number Diff line change
Expand Up @@ -957,11 +957,15 @@ static onvif_subscription_t *get_subscription(const char *url, const char *usern
char attempted_event_url[512] = {0};

if (discovered_url) {
log_info("ONVIF: Sending CreatePullPointSubscription to discovered endpoint");
safe_strcpy(attempted_event_url, discovered_url,
sizeof(attempted_event_url), 0);
response = send_onvif_request_to_url(discovered_url, username, password, request_body,
ONVIF_OPERATION_CREATE_PULLPOINT);
if (subscription_address_matches_camera_host(url, discovered_url)) {
log_info("ONVIF: Sending CreatePullPointSubscription to discovered endpoint");
safe_strcpy(attempted_event_url, discovered_url,
sizeof(attempted_event_url), 0);
response = send_onvif_request_to_url(discovered_url, username, password, request_body,
ONVIF_OPERATION_CREATE_PULLPOINT);
} else {
log_warn("ONVIF: Ignoring discovered event endpoint outside the camera host");
}
free(discovered_url);
}

Expand Down Expand Up @@ -1021,7 +1025,17 @@ static onvif_subscription_t *get_subscription(const char *url, const char *usern
if (slot >= 0) {
/* An inactive slot can contain failure state and an old subscription
* address. Start the replacement with a completely clean record. */
release_subscription_curl(&subscriptions[slot]);
pthread_mutex_t *slot_lock = subscription_lock(&subscriptions[slot]);
if (!slot_lock) {
free(subscription_address);
pthread_mutex_unlock(&subscription_mutex);
return NULL;
}
pthread_mutex_lock(slot_lock);
if (subscriptions[slot].curl) {
curl_easy_cleanup(subscriptions[slot].curl);
subscriptions[slot].curl = NULL;
}
memset(&subscriptions[slot], 0, sizeof(subscriptions[slot]));

// Store camera URL, username, and password
Expand All @@ -1038,6 +1052,7 @@ static onvif_subscription_t *get_subscription(const char *url, const char *usern
// Schedule renewal from the lease the camera actually granted.
apply_subscription_lease(&subscriptions[slot], lease_seconds);
subscriptions[slot].active = true;
pthread_mutex_unlock(slot_lock);

log_info("Successfully created ONVIF subscription (lease: %d seconds)", lease_seconds);
free(subscription_address);
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_onvif_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,31 @@ static void test_duplicate_fallback_logs_only_fixed_service_name(void) {
TEST_ASSERT_NULL(strstr(log_capture, "fixture-password"));
}

static void test_discovered_endpoint_on_other_host_is_not_contacted(void) {
unsigned short port;
origin = listen_local(&port);
destination = listen_local(&destination_port);
TEST_ASSERT_GREATER_OR_EQUAL_INT(0, origin);
TEST_ASSERT_GREATER_OR_EQUAL_INT(0, destination);
char base[128];
snprintf(base, sizeof(base), "http://127.0.0.1:%u", port);
snprintf(advertised_endpoint, sizeof(advertised_endpoint),
"http://localhost:%u/steal", destination_port);
fallback_requests = 0;
curl_handle = create_curl_handle();
TEST_ASSERT_NOT_NULL(curl_handle);
curl_easy_setopt(curl_handle, CURLOPT_PROXY, "");
pthread_t worker;
TEST_ASSERT_EQUAL_INT(0, pthread_create(&worker, NULL, fallback_server, NULL));
onvif_subscription_t *subscription = get_subscription(base, "fixture-user", "fixture-password");
TEST_ASSERT_EQUAL_INT(0, pthread_join(worker, NULL));
TEST_ASSERT_NULL(subscription);
TEST_ASSERT_EQUAL_INT(0, server_failed);
TEST_ASSERT_EQUAL_INT(4, fallback_requests);
struct pollfd pending = {.fd = destination, .events = POLLIN};
TEST_ASSERT_EQUAL_INT(0, poll(&pending, 1, 0));
}

/* Independent wire expectations: changing a SOAP identifier to HTTPS breaks
* the protocol even when the transport endpoint itself uses HTTPS. */
static const char *const expected_actions[] = {
Expand Down Expand Up @@ -436,6 +461,7 @@ int main(void) {
RUN_TEST(test_invalid_endpoint_is_rejected_before_transfer);
RUN_TEST(test_trace_never_logs_headers_or_curl_notes);
RUN_TEST(test_duplicate_fallback_logs_only_fixed_service_name);
RUN_TEST(test_discovered_endpoint_on_other_host_is_not_contacted);
RUN_TEST(test_subscription_origin_allows_camera_local_port);
RUN_TEST(test_rejected_subscription_never_contacts_target_or_legacy_fallback);
RUN_TEST(test_same_origin_subscription_keeps_full_request_target);
Expand Down
Loading