Skip to content

Commit 992d1dd

Browse files
bnoordhuisMylesBorins
authored andcommitted
src: detect nul bytes in InternalModuleReadFile()
Throw an exception when the path contains nul bytes, don't abort. Fixes: #13787 PR-URL: #14854 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 4570fa1 commit 992d1dd

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/node_file.cc

+3
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
503503
CHECK(args[0]->IsString());
504504
node::Utf8Value path(env->isolate(), args[0]);
505505

506+
if (strlen(*path) != path.length())
507+
return; // Contains a nul byte.
508+
506509
uv_fs_t open_req;
507510
const int fd = uv_fs_open(loop, &open_req, *path, O_RDONLY, 0, nullptr);
508511
uv_fs_req_cleanup(&open_req);

test/parallel/test-require-nul.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
6+
// Nul bytes should throw, not abort.
7+
assert.throws(() => require('\u0000ab'), /Cannot find module '\u0000ab'/);
8+
assert.throws(() => require('a\u0000b'), /Cannot find module 'a\u0000b'/);
9+
assert.throws(() => require('ab\u0000'), /Cannot find module 'ab\u0000'/);

0 commit comments

Comments
 (0)