Skip to content

Commit 09899e6

Browse files
committed
src: handle permissive extension on cmd check
PR-URL: nodejs-private/node-private#596 Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> CVE-ID: CVE-2024-36138
1 parent 9357433 commit 09899e6

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/util-inl.h

Lines changed: 15 additions & 3 deletions
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

@@ -543,9 +544,20 @@ bool IsWindowsBatchFile(const char* filename) {
543544
#else
544545
static constexpr bool kIsWindows = false;
545546
#endif // _WIN32
546-
if (kIsWindows)
547-
if (const char* p = strrchr(filename, '.'))
548-
return StringEqualNoCase(p, ".bat") || StringEqualNoCase(p, ".cmd");
547+
if (kIsWindows) {
548+
std::string file_with_extension = filename;
549+
// Regex to match the last extension part after the last dot, ignoring
550+
// trailing spaces and dots
551+
std::regex extension_regex(R"(\.([a-zA-Z0-9]+)\s*[\.\s]*$)");
552+
std::smatch match;
553+
std::string extension;
554+
555+
if (std::regex_search(file_with_extension, match, extension_regex)) {
556+
extension = ToLower(match[1].str());
557+
}
558+
559+
return !extension.empty() && (extension == "cmd" || extension == "bat");
560+
}
549561
return false;
550562
}
551563

test/parallel/test-child-process-spawn-windows-batch-file.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const expectedCode = isWindows ? 'EINVAL' : 'ENOENT';
2323
const expectedStatus = isWindows ? 1 : 127;
2424

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

2929
function testExec(filename) {
3030
return new Promise((resolve) => {

0 commit comments

Comments
 (0)