Skip to content

Commit

Permalink
Merge pull request #19352 from asselitx/zap-zip-rce-hpcc-32999
Browse files Browse the repository at this point in the history
HPCC-32999 Prevent command injection with zip password when requesting ZAP file

Reviewed-By: Dan S. Camper <[email protected]>
Reviewed-by: Gavin Halliday <[email protected]>
Merged-by: Gavin Halliday <[email protected]>
  • Loading branch information
ghalliday authored Dec 16, 2024
2 parents d7baff7 + 1c60d2e commit 4ec268d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion esp/services/ws_workunits/ws_workunitsHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4386,7 +4386,11 @@ void CWsWuFileHelper::zipZAPFiles(const char* parentFolder, const char* zapFiles
else
zipCommand.setf("cd %s\nzip -r", parentFolder);
if (!isEmptyString(passwordReq))
zipCommand.append(" --password ").append(passwordReq);
{
StringBuffer sanitizedPassword;
sanitizeCommandArg(passwordReq, sanitizedPassword);
zipCommand.append(" --password ").append(sanitizedPassword);
}
zipCommand.append(" ").append(zipFileNameWithFullPath).append(" ").append(zapFiles);
int zipRet = system(zipCommand);
if (zipRet != 0)
Expand Down
29 changes: 29 additions & 0 deletions system/jlib/jstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2902,3 +2902,32 @@ const char * stristr (const char *haystack, const char *needle)

return nullptr;
}

/**
* For preventing command injection, sanitize the argument to be passed to the system command.
* - Quote the entire argument with single quotes to prevent interpretation of shell metacharacters.
* - Since a single-quoted string can't contain single quotes, even escaped, replace each single
* quote in the argument with the sequence '"'"' . That closes the single quoted string, appends
* a literal single quote, and reopens the single quoted string
*/
StringBuffer& sanitizeCommandArg(const char* arg, StringBuffer& sanitized)
{
#if defined(__linux__) || defined(__APPLE__)
if (!isEmptyString(arg))
{
size_t len = strlen(arg);
sanitized.append('\'');
for (size_t i = 0; i < len; i++)
{
if (arg[i] == '\'')
sanitized.append(R"('"'"')");
else
sanitized.append(arg[i]);
}
sanitized.append('\'');
}
#else
sanitized.append(arg);
#endif
return sanitized;
}
3 changes: 3 additions & 0 deletions system/jlib/jstring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,4 +641,7 @@ extern jlib_decl void processOptionString(const char * options, optionCallback c

extern jlib_decl const char * stristr(const char *haystack, const char *needle);

// For preventing command injection, sanitize the argument to be passed to the system command
extern jlib_decl StringBuffer& sanitizeCommandArg(const char* arg, StringBuffer& sanitized);

#endif

0 comments on commit 4ec268d

Please sign in to comment.