diff --git a/src/util-inl.h b/src/util-inl.h index 78eef6de08f6d9..1e761d0aa54c09 100644 --- a/src/util-inl.h +++ b/src/util-inl.h @@ -27,6 +27,7 @@ #include #include #include +#include // NOLINT(build/c++11) #include "node_revert.h" #include "util.h" @@ -624,10 +625,20 @@ bool IsWindowsBatchFile(const char* filename) { #else static constexpr bool kIsWindows = false; #endif // _WIN32 - if (kIsWindows) - if (!IsReverted(SECURITY_REVERT_CVE_2024_27980)) - if (const char* p = strrchr(filename, '.')) - return StringEqualNoCase(p, ".bat") || StringEqualNoCase(p, ".cmd"); + if (kIsWindows && !IsReverted(SECURITY_REVERT_CVE_2024_27980)) { + std::string file_with_extension = filename; + // Regex to match the last extension part after the last dot, ignoring + // trailing spaces and dots + std::regex extension_regex(R"(\.([a-zA-Z0-9]+)\s*[\.\s]*$)"); + std::smatch match; + std::string extension; + + if (std::regex_search(file_with_extension, match, extension_regex)) { + extension = ToLower(match[1].str()); + } + + return !extension.empty() && (extension == "cmd" || extension == "bat"); + } return false; } diff --git a/test/parallel/test-child-process-spawn-windows-batch-file.js b/test/parallel/test-child-process-spawn-windows-batch-file.js index 81d0c64500d7ed..7f917022f90e73 100644 --- a/test/parallel/test-child-process-spawn-windows-batch-file.js +++ b/test/parallel/test-child-process-spawn-windows-batch-file.js @@ -26,8 +26,8 @@ const expectedCode = isWindows && !isRevert ? 'EINVAL' : 'ENOENT'; const expectedStatus = isWindows ? 1 : 127; const suffixes = - 'BAT bAT BaT baT BAt bAt Bat bat CMD cMD CmD cmD CMd cMd Cmd cmd' - .split(' '); + 'BAT|bAT|BaT|baT|BAt|bAt|Bat|bat|CMD|cMD|CmD|cmD|CMd|cMd|Cmd|cmd|cmd |cmd .|cmd ....' + .split('|'); if (process.argv[2] === undefined) { const a = cp.spawnSync(process.execPath, [__filename, 'child']);