[common] Prevent command execution in ExpandPathVariables (#87)

Command execution is not something users would expect. Even though
there is no security issue (right now), it's probably better to turn
it off.
This commit is contained in:
Lutz Justen
2023-03-06 15:25:49 +01:00
committed by GitHub
parent a8059e8572
commit c481b6a27f
2 changed files with 6 additions and 3 deletions

View File

@@ -219,9 +219,12 @@ absl::Status ExpandPathVariables(std::string* path) {
*path = Util::WideToUtf8Str(wchar_expanded); *path = Util::WideToUtf8Str(wchar_expanded);
return absl::OkStatus(); return absl::OkStatus();
#else #else
// Exclude command substitution. It.s not what users of this method would
// expect and could lead to security issues.
wordexp_t res; wordexp_t res;
wordexp(path->c_str(), &res, 0); wordexp(path->c_str(), &res, WRDE_NOCMD);
if (res.we_wordc > 1) { if (res.we_wordc > 1) {
wordfree(&res);
return absl::InvalidArgumentError( return absl::InvalidArgumentError(
"Path expands to multiple results (did you use * etc. ?"); "Path expands to multiple results (did you use * etc. ?");
} }

View File

@@ -104,8 +104,8 @@ absl::Status GetKnownFolderPath(FolderId folder_id, std::string* path);
// Expands environment path variables like %APPDATA% on Windows or ~ on Linux. // Expands environment path variables like %APPDATA% on Windows or ~ on Linux.
// On Windows, variables are matched case invariantly. Unknown environment // On Windows, variables are matched case invariantly. Unknown environment
// variables are not changed. // variables are not changed.
// On Linux, performs a shell-like expansion. Returns an error if multiple // On Linux, performs a shell-like expansion, but without command substitution.
// results would be returned, e.g. from *.txt. // Returns an error if multiple results would be returned, e.g. from *.txt.
absl::Status ExpandPathVariables(std::string* path); absl::Status ExpandPathVariables(std::string* path);
// Returns the environment variable with given |name| in |value|. // Returns the environment variable with given |name| in |value|.