Skip to content

Commit dec9035

Browse files
author
Mariusz Pasinski
committed
feat: ensure that paths use safe ASCII alphanumericals
1 parent 439e7a6 commit dec9035

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

packages/host/cpp/CxxNodeApiHostModule.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22
#include <vector> // std::vector
33
#include <string> // std::string
44
#include <string_view> // std::string_view
5+
#include <algorithm> // std::all_of
6+
#include <cctype> // std::isalnum
57
#include "CxxNodeApiHostModule.hpp"
68
#include "Logger.hpp"
79

810
using namespace facebook;
911

1012
namespace {
1113

14+
bool isModulePathLike(const std::string_view &path) {
15+
return std::all_of(path.begin(), path.end(), [](unsigned char c) {
16+
return std::isalnum(c) || '_' == c || '-' == c
17+
|| '.' == c || '/' == c || ':' == c;
18+
});
19+
}
20+
1221
// NOTE: behaves like `explode()` in PHP
1322
std::vector<std::string_view> explodePath(const std::string_view &path) {
1423
std::vector<std::string_view> parts;
@@ -138,6 +147,13 @@ CxxNodeApiHostModule::requireNodeAddon(jsi::Runtime &rt,
138147
const std::string &requiredPath,
139148
const std::string &requiredPackageName,
140149
const std::string &requiredFrom) {
150+
// Ensure that user-supplied inputs contain only allowed characters
151+
if (!isModulePathLike(requiredPath)) {
152+
throw jsi::JSError(rt, "Invalid characters in `requiredPath`. Only ASCII alphanumerics are allowed.");
153+
}
154+
if (!isModulePathLike(requiredFrom)) {
155+
throw jsi::JSError(rt, "Invalid characters in `requiredFrom`. Only ASCII alphanumerics are allowed.");
156+
}
141157

142158
const std::string &libraryNameStr = requiredPath;
143159
auto [it, inserted] = nodeAddons_.emplace(libraryNameStr, NodeAddon());

0 commit comments

Comments
 (0)