src: detect nul bytes in InternalModuleReadFile() · nodejs/node@b2112f8

2 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -571,6 +571,9 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {

571571

CHECK(args[0]->IsString());

572572

node::Utf8Value path(env->isolate(), args[0]);

573573
574+

if (strlen(*path) != path.length())

575+

return; // Contains a nul byte.

576+
574577

uv_fs_t open_req;

575578

const int fd = uv_fs_open(loop, &open_req, *path, O_RDONLY, 0, nullptr);

576579

uv_fs_req_cleanup(&open_req);

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'/);