Skip to content

Commit af4cdcd

Browse files
authored
src: do not pass user input to format string
PR-URL: #48973 Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent ce958d2 commit af4cdcd

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/node_file.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,10 +3042,11 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
30423042
return;
30433043
}
30443044

3045-
std::string err_module_message =
3046-
"Cannot find package '" + module_path + "' imported from " + module_base;
30473045
env->isolate()->ThrowException(
3048-
ERR_MODULE_NOT_FOUND(env->isolate(), err_module_message.c_str()));
3046+
ERR_MODULE_NOT_FOUND(env->isolate(),
3047+
"Cannot find package '%s' imported from %s",
3048+
module_path,
3049+
module_base));
30493050
}
30503051

30513052
void BindingData::MemoryInfo(MemoryTracker* tracker) const {

test/es-module/test-cjs-legacyMainResolve.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ describe('legacyMainResolve', () => {
133133
);
134134
});
135135

136+
it('should not crash when cannot resolve to a file that contains special chars', () => {
137+
const packageJsonUrl = pathToFileURL('/c/file%20with%20percents/package.json');
138+
assert.throws(
139+
() => legacyMainResolve(packageJsonUrl, { main: null }, packageJsonUrl),
140+
{ code: 'ERR_MODULE_NOT_FOUND' },
141+
);
142+
});
143+
136144
it('should throw when cannot resolve to a file (base not defined)', () => {
137145
const packageJsonUrl = pathToFileURL(
138146
path.resolve(

0 commit comments

Comments
 (0)