From 68b340f5f5d4f086a5ba25276312d13f2fb357b5 Mon Sep 17 00:00:00 2001 From: Dmitry Verenitsin Date: Wed, 4 Nov 2026 19:21:51 +0500 Subject: [PATCH] [core] Confine MSRP remote file-selector name to the receive directory Without an explicit path, `msrp_recv_file_function()` joined the `sip_msrp_file_name` channel variable, taken from the remote SDP `a=file-selector` name, onto the base directory unchanged, so a name with directory components opened a file outside it. Reduce the name to its last path component with `switch_cut_path()` and reject an empty name, `.` and `..`. `test_switch_cut_path_strips_directory_components` covers the path reduction the receive path relies on. --- src/switch_msrp.c | 17 ++++++++++++++--- tests/unit/switch_utils.c | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/switch_msrp.c b/src/switch_msrp.c index f1a2d75cf2..8ea5ef80a8 100644 --- a/src/switch_msrp.c +++ b/src/switch_msrp.c @@ -1701,14 +1701,25 @@ SWITCH_STANDARD_APP(msrp_recv_file_function) switch_channel_answer(channel); if (zstr(data)) { - filename = switch_channel_get_variable(channel, "sip_msrp_file_name"); + const char *sip_file_name = switch_channel_get_variable(channel, "sip_msrp_file_name"); + const char *safe_name; - if (zstr(filename)) { + if (zstr(sip_file_name)) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "No file specified.\n"); + return; } - filename = switch_core_session_sprintf(session, "%s%s%s", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR, filename); + /* The name is taken from the remote SDP file-selector. Reduce it to its final + path component so the opened file always stays inside the receive directory. */ + safe_name = switch_cut_path(sip_file_name); + if (zstr(safe_name) || !strcmp(safe_name, ".") || !strcmp(safe_name, "..")) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Rejecting remote file name '%s'\n", sip_file_name); + + return; + } + + filename = switch_core_session_sprintf(session, "%s%s%s", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR, safe_name); } if (!(msrp_session = switch_core_media_get_msrp_session(session))) { diff --git a/tests/unit/switch_utils.c b/tests/unit/switch_utils.c index 07657141d5..c4ff2e3818 100644 --- a/tests/unit/switch_utils.c +++ b/tests/unit/switch_utils.c @@ -488,6 +488,28 @@ FST_TEST_BEGIN(test_switch_http_parse_header) } FST_TEST_END() +FST_TEST_BEGIN(test_switch_cut_path_strips_directory_components) +{ + size_t i; + struct { + const char *in; + const char *want; + } cases[] = { + { "plain.txt", "plain.txt" }, /* no separator: returned unchanged */ + { "../escape/x.txt", "x.txt" }, /* relative components dropped */ + { "/etc/passwd", "passwd" }, /* leading absolute path dropped */ + { "a/../../x", "x" }, /* embedded .. dropped with the rest of the path */ + { "dir\\sub\\f.txt", "f.txt" }, /* backslash is also a separator */ + { "trailing/", "" }, /* nothing follows the final separator */ + { "..", ".." }, /* no separator: caller must reject this residue */ + }; + + for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + fst_check_string_equals(switch_cut_path(cases[i].in), cases[i].want); + } +} +FST_TEST_END() + FST_SUITE_END() FST_MINCORE_END()