[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.
This commit is contained in:
Dmitry Verenitsin committed 2026-11-18 22:36:36 +05:00
1 parent bffffa40a1
commit 68b340f5f5
2 files changed
+36 -3

No files matched your search

+14 -3
View File
@@ -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))) {
+22
View File
@@ -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()