Introduce System::cmd_execute_clean() for sanitized executions
MODIFY
include/pch.h
include/pch.h
#include <shellapi.h>
#include <Shlobj.h>
#include <winhttp.h>
+#include <process.h>
#include <cassert>
#include <stdexcept>

MODIFY
include/vcpkg_System.h
include/vcpkg_System.h
@@ -17,6 +17,13 @@ namespace vcpkg::System
std::string output;
};
+ int cmd_execute_clean(const wchar_t* cmd_line);
+
+ inline int cmd_execute_clean(const std::wstring& cmd_line)
+ {
+ return cmd_execute_clean(cmd_line.c_str());
+ }
+
int cmd_execute(const wchar_t* cmd_line);
inline int cmd_execute(const std::wstring& cmd_line)

MODIFY
src/vcpkg_System.cpp
src/vcpkg_System.cpp
@@ -13,10 +13,84 @@ namespace vcpkg::System
return fs::path(buf, buf + bytes);
}
+ int cmd_execute_clean(const wchar_t* cmd_line)
+ {
+ static const std::wstring system_root = *get_environmental_variable(L"SystemRoot");
+ static const std::wstring system_32 = system_root + LR"(\system32)";
+ static const std::wstring new_PATH = Strings::wformat(LR"(Path=%s;%s;%s\WindowsPowerShell\v1.0\)", system_32, system_root, system_32);
+
+ std::vector<std::wstring> env_wstrings =
+ {
+ L"ALLUSERSPROFILE",
+ L"APPDATA",
+ L"CommonProgramFiles",
+ L"CommonProgramFiles(x86)",
+ L"CommonProgramW6432",
+ L"COMPUTERNAME",
+ L"ComSpec",
+ L"HOMEDRIVE",
+ L"HOMEPATH",
+ L"LOCALAPPDATA",
+ L"LOGONSERVER",
+ L"NUMBER_OF_PROCESSORS",
+ L"OS",
+ L"PATHEXT",
+ L"PROCESSOR_ARCHITECTURE",
+ L"PROCESSOR_IDENTIFIER",
+ L"PROCESSOR_LEVEL",
+ L"PROCESSOR_REVISION",
+ L"ProgramData",
+ L"ProgramFiles",
+ L"ProgramFiles(x86)",
+ L"ProgramW6432",
+ L"PROMPT",
+ L"PSModulePath",
+ L"PUBLIC",
+ L"SystemDrive",
+ L"SystemRoot",
+ L"TEMP",
+ L"TMP",
+ L"USERDNSDOMAIN",
+ L"USERDOMAIN",
+ L"USERDOMAIN_ROAMINGPROFILE",
+ L"USERNAME",
+ L"USERPROFILE",
+ L"windir",
+ // Enables proxy information to be passed to Curl, the underlying download library in cmake.exe
+ L"HTTP_PROXY",
+ L"HTTPS_PROXY",
+ };
+
+ // Flush stdout before launching external process
+ _flushall();
+
+ std::vector<const wchar_t*> env_cstr;
+ env_cstr.reserve(env_wstrings.size() + 2);
+
+ for (auto&& env_wstring : env_wstrings)
+ {
+ auto v = System::get_environmental_variable(env_wstring.c_str());
+ if (v == nullptr || v->empty())
+ continue;
+
+ env_wstring.push_back(L'=');
+ env_wstring.append(*v);
+ env_cstr.push_back(env_wstring.c_str());
+ }
+
+ env_cstr.push_back(new_PATH.c_str());
+ env_cstr.push_back(nullptr);
+
+ // Basically we are wrapping it in quotes
+ const std::wstring& actual_cmd_line = Strings::wformat(LR"###("%s")###", cmd_line);
+ auto exit_code = _wspawnlpe(_P_WAIT, L"cmd.exe", L"cmd.exe", L"/c", actual_cmd_line.c_str(), nullptr, env_cstr.data());
+ return static_cast<int>(exit_code);
+ }
+
int cmd_execute(const wchar_t* cmd_line)
{
// Flush stdout before launching external process
- fflush(stdout);
+ _flushall();
// Basically we are wrapping it in quotes
const std::wstring& actual_cmd_line = Strings::wformat(LR"###("%s")###", cmd_line);

Introduce System::cmd_execute_clean() for sanitized executions