Skip to content

Commit 1e77905

Browse files
authored
Merge pull request #981 from osm/add-exec-validation
Prevent downloadable files from being executed
2 parents 5dda74f + 57416b0 commit 1e77905

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/cl_cmd.c

+2-19
Original file line numberDiff line numberDiff line change
@@ -626,30 +626,13 @@ void CL_Rcon_f (void) {
626626

627627
qbool CL_Download_Accept(const char *filename)
628628
{
629-
char *str, *tmp, *ext;
630-
qbool is_valid = false;
631-
extern cvar_t cl_allow_downloads;
632-
633629
if (strstr(filename, "..") || !strcmp(filename, "") || filename[0] == '/' || strchr(filename, '\\') || strchr(filename, ':') || strstr(filename, "//")) {
634630
Com_Printf("Warning: Invalid characters in filename \"%s\"\n", filename);
635631
return false;
636632
}
637633

638-
ext = COM_FileExtension(filename);
639-
str = Q_strdup(cl_allow_downloads.string);
640-
tmp = strtok(str, ",");
641-
while (tmp != NULL) {
642-
if (strcmp(ext, tmp) == 0) {
643-
is_valid = true;
644-
break;
645-
}
646-
647-
tmp = strtok(NULL, ",");
648-
}
649-
Q_free(str);
650-
651-
if (!is_valid) {
652-
Com_Printf("Warning: Non-allowed file \"%s\" skipped. Add \"%s\" to cl_allow_download_file_extensions to allow the file to be downloaded\n", filename, ext);
634+
if (!CL_IsDownloadableFileExtension(filename)) {
635+
Com_Printf("Warning: Non-allowed file \"%s\" skipped. Add \"%s\" to cl_allow_downloads to allow the file to be downloaded\n", filename, COM_FileExtension(filename));
653636
return false;
654637
}
655638

src/cmd.c

+26
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ static void OnChange_remote_capabilities(cvar_t *var, char *string, qbool *cance
137137
Q_free(tmp);
138138
}
139139

140+
qbool CL_IsDownloadableFileExtension(const char *filename)
141+
{
142+
qbool is_allowed = false;
143+
char *ext, *str, *tmp;
144+
145+
ext = COM_FileExtension(filename);
146+
str = Q_strdup(cl_allow_downloads.string);
147+
tmp = strtok(str, ",");
148+
while (tmp != NULL) {
149+
if (strcmp(ext, tmp) == 0) {
150+
is_allowed = true;
151+
break;
152+
}
153+
154+
tmp = strtok(NULL, ",");
155+
}
156+
Q_free(str);
157+
158+
return is_allowed;
159+
}
160+
140161
//=============================================================================
141162

142163
//Causes execution of the remainder of the command buffer to be delayed until next frame.
@@ -540,6 +561,11 @@ void Cmd_Exec_f (void)
540561
server_command = cbuf_current == &cbuf_server || !strcmp(Cmd_Argv(0), "serverexec");
541562
#endif
542563

564+
if (CL_IsDownloadableFileExtension(Cmd_Argv(1))) {
565+
Com_Printf("Warning: \"%s\" is not allowed to be executed. Remove \"%s\" from cl_allow_downloads to allow execution\n", Cmd_Argv(1), COM_FileExtension(Cmd_Argv(1)));
566+
return;
567+
}
568+
543569
strlcpy (name, Cmd_Argv(1), sizeof(name) - 4);
544570
if (!(f = (char *) FS_LoadHeapFile(name, NULL))) {
545571
const char *p;

src/cmd.h

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ void Cbuf_AddEarlyCommands (void);
182182
void Cmd_StuffCmds_f (void);
183183
qbool Cmd_IsLegacyCommand (char *oldname);
184184
void Cmd_AddLegacyCommand (char *oldname, char *newname);
185+
qbool CL_IsDownloadableFileExtension(const char *filename);
185186

186187
//===========================================================================
187188

0 commit comments

Comments
 (0)