Skip to content

Commit abeebbc

Browse files
committed
security: fix test exec without filter issue
1 parent 75c41f5 commit abeebbc

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

test/test.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,31 @@ const lest::test specification[] =
11441144
log_test_message("no-flake: running 1000 iterations (this will take a moment)...");
11451145

11461146
std::string exe(g_argv0 ? g_argv0 : "./lsm_test");
1147+
1148+
// Validate exe path: only allow characters that are safe in a shell
1149+
// double-quoted string to prevent command injection via argv[0].
1150+
auto isPathSafe = [](char c) {
1151+
return std::isalnum(static_cast<unsigned char>(c)) ||
1152+
c == '/' || c == '.' || c == '-' || c == '_';
1153+
};
1154+
if (!std::all_of(exe.begin(), exe.end(), isPathSafe)) {
1155+
log_test_message("no-flake: SKIPPED (executable path contains unsafe characters)");
1156+
return;
1157+
}
1158+
1159+
// Verify the basename is exactly the expected test binary so an
1160+
// attacker cannot substitute a different executable via argv[0].
1161+
const std::string stem = exe.substr(exe.find_last_of("/\\") + 1);
1162+
#ifdef _WIN32
1163+
const bool knownBinary = (stem == "lsm_test.exe" || stem == "lsm_test");
1164+
#else
1165+
const bool knownBinary = (stem == "lsm_test");
1166+
#endif
1167+
if (!knownBinary) {
1168+
log_test_message("no-flake: SKIPPED (unexpected executable name: " + stem + ")");
1169+
return;
1170+
}
1171+
11471172
#ifdef _WIN32
11481173
std::string cmd = "set LSM_NOFLAKE=1 && \"" + exe + "\" > NUL 2>&1";
11491174
#else

0 commit comments

Comments
 (0)