Skip to content

Commit eccd63b

Browse files
committedJul 2, 2024
src: handle permissive extension on cmd check
PR-URL: nodejs-private/node-private#596 Backport-PR-URL: nodejs-private/node-private#603 CVE-ID: CVE-2024-36138
1 parent dc9eed9 commit eccd63b

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed
 

Diff for: ‎src/util-inl.h

+15-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <cmath>
2828
#include <cstring>
2929
#include <locale>
30+
#include <regex> // NOLINT(build/c++11)
3031
#include "node_revert.h"
3132
#include "util.h"
3233

@@ -624,10 +625,20 @@ bool IsWindowsBatchFile(const char* filename) {
624625
#else
625626
static constexpr bool kIsWindows = false;
626627
#endif // _WIN32
627-
if (kIsWindows)
628-
if (!IsReverted(SECURITY_REVERT_CVE_2024_27980))
629-
if (const char* p = strrchr(filename, '.'))
630-
return StringEqualNoCase(p, ".bat") || StringEqualNoCase(p, ".cmd");
628+
if (kIsWindows && !IsReverted(SECURITY_REVERT_CVE_2024_27980)) {
629+
std::string file_with_extension = filename;
630+
// Regex to match the last extension part after the last dot, ignoring
631+
// trailing spaces and dots
632+
std::regex extension_regex(R"(\.([a-zA-Z0-9]+)\s*[\.\s]*$)");
633+
std::smatch match;
634+
std::string extension;
635+
636+
if (std::regex_search(file_with_extension, match, extension_regex)) {
637+
extension = ToLower(match[1].str());
638+
}
639+
640+
return !extension.empty() && (extension == "cmd" || extension == "bat");
641+
}
631642
return false;
632643
}
633644

Diff for: ‎test/parallel/test-child-process-spawn-windows-batch-file.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const expectedCode = isWindows && !isRevert ? 'EINVAL' : 'ENOENT';
2626
const expectedStatus = isWindows ? 1 : 127;
2727

2828
const suffixes =
29-
'BAT bAT BaT baT BAt bAt Bat bat CMD cMD CmD cmD CMd cMd Cmd cmd'
30-
.split(' ');
29+
'BAT|bAT|BaT|baT|BAt|bAt|Bat|bat|CMD|cMD|CmD|cmD|CMd|cMd|Cmd|cmd|cmd |cmd .|cmd ....'
30+
.split('|');
3131

3232
if (process.argv[2] === undefined) {
3333
const a = cp.spawnSync(process.execPath, [__filename, 'child']);

0 commit comments

Comments
 (0)
Please sign in to comment.