Commit fd911220be for freeswitch.com

commit fd911220bedb73d5351d60c1af7fc75d2f85a165
Author: Dmitry Verenitsin <morbit85@gmail.com>
Date:   Wed Nov 18 22:18:00 2026 +0500

    Merge commit from fork

    On a profile with `extended-info-parsing` set, an inbound SIP INFO whose
    content type is `freeswitch/api-request` puts an FSAPI command in the
    request body and gets its output back in the 200 OK. The branch needs no
    dialog, and no `mod_sofia` authentication or ACL setting covers an
    out-of-dialog INFO. That same param also enables the in-dialog
    `session-event` half, so there is no way to keep one and not the other.

    The new per-profile `enable-info-api-request` param gates the api-request
    branch alone and is off unless set, leaving `session-event` reachable
    under `extended-info-parsing` as before. An INFO selecting the branch on a
    profile without the new param is answered 403 and logged with the source
    and the requested command; a permitted one is logged too, since the source
    address appears in no event the API path fires. Both lines print the
    body's first line, capped, rather than the whole body.

diff --git a/conf/testing/sip_profiles/internal.xml b/conf/testing/sip_profiles/internal.xml
index 62f52af63f..c4efa30876 100644
--- a/conf/testing/sip_profiles/internal.xml
+++ b/conf/testing/sip_profiles/internal.xml
@@ -107,6 +107,10 @@
     <!-- extended info parsing -->
     <!-- <param name="extended-info-parsing" value="true"/> -->

+    <!-- with extended-info-parsing on, lets an INFO body of content type freeswitch/api-request
+         run FreeSWITCH API commands, so enable only for trusted peers -->
+    <!-- <param name="enable-info-api-request" value="true"/> -->
+
     <!--<param name="aggressive-nat-detection" value="true"/>-->
     <!--
         There are known issues (asserts and segfaults) when 100rel is enabled.
diff --git a/conf/vanilla/sip_profiles/internal.xml b/conf/vanilla/sip_profiles/internal.xml
index 79fcad741e..2e59767056 100644
--- a/conf/vanilla/sip_profiles/internal.xml
+++ b/conf/vanilla/sip_profiles/internal.xml
@@ -107,6 +107,10 @@
     <!-- extended info parsing -->
     <!-- <param name="extended-info-parsing" value="true"/> -->

+    <!-- with extended-info-parsing on, lets an INFO body of content type freeswitch/api-request
+         run FreeSWITCH API commands, so enable only for trusted peers -->
+    <!-- <param name="enable-info-api-request" value="true"/> -->
+
     <!-- lets an inbound MESSAGE to api+<command> run FreeSWITCH API commands, so enable only for trusted peers -->
     <!-- <param name="enable-chat-api-proto" value="true"/> -->

diff --git a/src/mod/endpoints/mod_sofia/conf/sofia.conf.xml b/src/mod/endpoints/mod_sofia/conf/sofia.conf.xml
index 4732ae6fbf..c3253ed0b8 100644
--- a/src/mod/endpoints/mod_sofia/conf/sofia.conf.xml
+++ b/src/mod/endpoints/mod_sofia/conf/sofia.conf.xml
@@ -148,6 +148,11 @@
         <!-- extended info parsing -->
         <!-- <param name="extended-info-parsing" value="true"/> -->

+        <!-- with extended-info-parsing on, lets an INFO body of content type
+             freeswitch/api-request run FreeSWITCH API commands, so enable only
+             for trusted peers -->
+        <!-- <param name="enable-info-api-request" value="true"/> -->
+
         <!-- lets an inbound MESSAGE to api+<command> run FreeSWITCH API commands,
              so enable only for trusted peers -->
         <!-- <param name="enable-chat-api-proto" value="true"/> -->
diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.h b/src/mod/endpoints/mod_sofia/mod_sofia.h
index 81d0517d78..433fff5c9b 100644
--- a/src/mod/endpoints/mod_sofia/mod_sofia.h
+++ b/src/mod/endpoints/mod_sofia/mod_sofia.h
@@ -312,6 +312,7 @@ typedef enum {
 	PFLAG_AUTH_CALLS_ACL_ONLY,
 	PFLAG_USE_PORT_FOR_ACL_CHECK,
 	PFLAG_ENABLE_CHAT_API_PROTO,
+	PFLAG_ENABLE_INFO_API_REQUEST,

 	/* No new flags below this line */
 	PFLAG_MAX
diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c
index 93583895c7..1a142761d9 100644
--- a/src/mod/endpoints/mod_sofia/sofia.c
+++ b/src/mod/endpoints/mod_sofia/sofia.c
@@ -5612,6 +5612,12 @@ switch_status_t config_sofia(sofia_config_t reload, char *profile_name)
 						} else {
 							sofia_clear_pflag(profile, PFLAG_EXTENDED_INFO_PARSING);
 						}
+					} else if (!strcasecmp(var, "enable-info-api-request")) {
+						if (switch_true(val)) {
+							sofia_set_pflag(profile, PFLAG_ENABLE_INFO_API_REQUEST);
+						} else {
+							sofia_clear_pflag(profile, PFLAG_ENABLE_INFO_API_REQUEST);
+						}
 					} else if (!strcasecmp(var, "nonce-ttl") && !zstr(val)) {
 						profile->nonce_ttl = atoi(val);
 					} else if (!strcasecmp(var, "max-auth-validity") && !zstr(val)) {
@@ -9952,10 +9958,42 @@ void sofia_handle_sip_i_info(nua_t *nua, sofia_profile_t *profile, nua_handle_t
 					return;

 				} else if (!strcasecmp(sip->sip_content_type->c_subtype, "api-request")) {
-					char *cmd = strdup(sip->sip_payload->pl_data);
+					char *cmd;
 					char *arg;
+					char network_ip[80] = "";
+					int network_port = 0;
+					size_t log_len;
 					switch_stream_handle_t stream = { 0 };

+					/* leaves the buffer empty when the message carries no address info */
+					sofia_glue_get_addr(de->data->e_msg, network_ip, sizeof(network_ip), &network_port);
+
+					/* Both log lines below print the body's first line only, capped: the body comes
+					   from the request, has no length bound, and may hold line breaks. */
+					log_len = strcspn(sip->sip_payload->pl_data, "\r\n");
+					if (log_len > 256) {
+						log_len = 256;
+					}
+
+					if (!sofia_test_pflag(profile, PFLAG_ENABLE_INFO_API_REQUEST)) {
+						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
+										  "Profile [%s] rejected an INFO from %s:%d requesting a FreeSWITCH API command [%.*s]. "
+										  "Set enable-info-api-request=true on the profile to permit it.\n",
+										  profile->name, network_ip, network_port, (int) log_len, sip->sip_payload->pl_data);
+
+						nua_respond(nh, SIP_403_FORBIDDEN, NUTAG_WITH_THIS_MSG(de->data->e_msg),
+									TAG_IF(!zstr(session_id_header), SIPTAG_HEADER_STR(session_id_header)),
+									TAG_END());
+
+						return;
+					}
+
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
+									  "Profile [%s] running a FreeSWITCH API command requested by an INFO from %s:%d [%.*s]\n",
+									  profile->name, network_ip, network_port, (int) log_len, sip->sip_payload->pl_data);
+
+					cmd = strdup(sip->sip_payload->pl_data);
+
 					switch_assert(cmd);
 					SWITCH_STANDARD_STREAM(stream);
 					switch_assert(stream.data);