Skip to content

Commit

Permalink
src: detect nul bytes in InternalModuleReadFile()
Browse files Browse the repository at this point in the history
Throw an exception when the path contains nul bytes, don't abort.

Fixes: #13787
PR-URL: #14854
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
bnoordhuis authored and addaleax committed Aug 21, 2017
1 parent 6a0e3b1 commit ffed7b6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,9 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsString());
node::Utf8Value path(env->isolate(), args[0]);

if (strlen(*path) != path.length())
return; // Contains a nul byte.

uv_fs_t open_req;
const int fd = uv_fs_open(loop, &open_req, *path, O_RDONLY, 0, nullptr);
uv_fs_req_cleanup(&open_req);
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-require-nul.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

require('../common');
const assert = require('assert');

// Nul bytes should throw, not abort.
assert.throws(() => require('\u0000ab'), /Cannot find module '\u0000ab'/);
assert.throws(() => require('a\u0000b'), /Cannot find module 'a\u0000b'/);
assert.throws(() => require('ab\u0000'), /Cannot find module 'ab\u0000'/);

0 comments on commit ffed7b6

Please sign in to comment.