diff options
| author | Leitwolf | 2026-11-06 09:34:16 +0300 |
|---|---|---|
| committer | GitHub | 2026-11-06 09:34:16 +0300 |
| commit | f7b935972ae17dfab13195a51f3d1f3b89d77c48 (patch) | |
| tree | 99c4d53afb06eb1f2479392bdf56d9bc95621166 /src/Progmasoft/Runner/Process.cpp | |
| parent | 268698be92962247a21a86dda4de2d4267adcf8f (diff) | |
| parent | ad3633ea59ec491c9694a10307beb7d7189db744 (diff) | |
| download | catch3-f7b935972ae17dfab13195a51f3d1f3b89d77c48.tar.gz | |
Merge pull request #37 from Progmasoft/fix/child-environment
Withhold secret-looking variables from the processes the runner starts
Diffstat (limited to 'src/Progmasoft/Runner/Process.cpp')
| -rw-r--r-- | src/Progmasoft/Runner/Process.cpp | 84 |
1 files changed, 82 insertions, 2 deletions
diff --git a/src/Progmasoft/Runner/Process.cpp b/src/Progmasoft/Runner/Process.cpp index c1d01302..85987210 100644 --- a/src/Progmasoft/Runner/Process.cpp +++ b/src/Progmasoft/Runner/Process.cpp @@ -1,9 +1,12 @@ // Copyright (c) 2026 Progmasoft. // SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 #include <algorithm> +#include <cctype> #include <cstdio> #include <cstdlib> #include <cstring> +#include <iterator> +#include <string_view> #include <thread> #include "Process.hpp" @@ -39,8 +42,64 @@ extern char **environ; namespace Progmasoft::Catch3::Runner { + bool + IsSecretVariable(std::string_view name) + { + std::string upper; + upper.reserve(name.size()); + for (const char character : name) + { + upper.push_back(character >= 'a' && character <= 'z' + ? static_cast<char>(character - 'a' + 'A') + : character); + } + constexpr std::string_view kSecretWords[] = { + "TOKEN", "SECRET", "PASSWORD", "PASSWD", "CREDENTIAL", + "API_KEY", "APIKEY", "PRIVATE_KEY", "ACCESS_KEY", + }; + return std::any_of(std::begin(kSecretWords), + std::end(kSecretWords), + [&](std::string_view word) { + return upper.find(word) != std::string::npos; + }); + } + namespace { + /// Whether a variable of this process stays out of a child: a secret + /// that the options do not keep. Windows spells names in any case. + bool + Withheld(std::string_view name, const std::vector<std::string> &kept) + { + if (!IsSecretVariable(name)) + { + return false; + } + return std::none_of( + kept.begin(), + kept.end(), + [&](const std::string &keptName) { + if (keptName.size() != name.size()) + { + return false; + } +#if defined(_WIN32) + return std::equal( + keptName.begin(), + keptName.end(), + name.begin(), + [](char left, char right) { + return std::toupper( + static_cast<unsigned char>(left)) + == std::toupper( + static_cast<unsigned char>(right)); + }); +#else + return keptName == name; +#endif + }); + } + #if defined(_WIN32) HANDLE AsHandle(std::intptr_t value) noexcept @@ -175,7 +234,8 @@ namespace Progmasoft::Catch3::Runner std::wstring EnvironmentBlock( - const std::vector<std::pair<std::string, std::string>> &additions) + const std::vector<std::pair<std::string, std::string>> &additions, + const std::vector<std::string> &kept) { std::vector<std::wstring> entries; wchar_t *current = GetEnvironmentStringsW(); @@ -184,6 +244,13 @@ namespace Progmasoft::Catch3::Runner for (const wchar_t *entry = current; *entry != L'\0'; entry += wcslen(entry) + 1) { + // A leading '=' marks the hidden per-drive variables. + const std::wstring_view text{ entry }; + const auto equals = text.find(L'=', 1); + if (Withheld(Narrow(text.substr(0, equals)), kept)) + { + continue; + } entries.emplace_back(entry); } FreeEnvironmentStringsW(current); @@ -215,6 +282,12 @@ namespace Progmasoft::Catch3::Runner block += entry; block.push_back(L'\0'); } + // A block ends in an empty entry; one without variables is two + // terminators. + if (entries.empty()) + { + block.push_back(L'\0'); + } block.push_back(L'\0'); return block; } @@ -634,7 +707,8 @@ namespace Progmasoft::Catch3::Runner } AppendQuotedArgument(commandLine, Widen(options.arguments[index])); } - std::wstring environment = EnvironmentBlock(options.environment); + std::wstring environment + = EnvironmentBlock(options.environment, options.keptEnvironment); const std::wstring directory = options.workingDirectory.wstring(); PROCESS_INFORMATION information{}; @@ -957,6 +1031,12 @@ namespace Progmasoft::Catch3::Runner entry != nullptr && *entry != nullptr; ++entry) { + const std::string_view text{ *entry }; + if (Withheld(text.substr(0, text.find('=')), + options.keptEnvironment)) + { + continue; + } environmentStorage.emplace_back(*entry); } for (const auto &[name, value] : options.environment) |