Skip to content

Commit

Permalink
worker: restrict supported extensions
Browse files Browse the repository at this point in the history
Only allow `.js` and `.mjs` extensions to provide future-proofing
for file type detection.

Refs: ayojs/ayo#117
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Olivia Hugger <olivia@fastmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>

PR-URL: #20876
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
  • Loading branch information
TimothyGu authored and targos committed Jun 13, 2018
1 parent 93ce63c commit c97fb91
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,4 +856,7 @@ E('ERR_WORKER_NEED_ABSOLUTE_PATH',
TypeError);
E('ERR_WORKER_UNSERIALIZABLE_ERROR',
'Serializing an uncaught exception failed', Error);
E('ERR_WORKER_UNSUPPORTED_EXTENSION',
'The worker script extension must be ".js" or ".mjs". Received "%s"',
TypeError);
E('ERR_ZLIB_INITIALIZATION_FAILED', 'Initialization failed', Error);
13 changes: 10 additions & 3 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const util = require('util');
const {
ERR_INVALID_ARG_TYPE,
ERR_WORKER_NEED_ABSOLUTE_PATH,
ERR_WORKER_UNSERIALIZABLE_ERROR
ERR_WORKER_UNSERIALIZABLE_ERROR,
ERR_WORKER_UNSUPPORTED_EXTENSION,
} = require('internal/errors').codes;

const { internalBinding } = require('internal/bootstrap/loaders');
Expand Down Expand Up @@ -136,8 +137,14 @@ class Worker extends EventEmitter {
throw new ERR_INVALID_ARG_TYPE('filename', 'string', filename);
}

if (!options.eval && !path.isAbsolute(filename)) {
throw new ERR_WORKER_NEED_ABSOLUTE_PATH(filename);
if (!options.eval) {
if (!path.isAbsolute(filename)) {
throw new ERR_WORKER_NEED_ABSOLUTE_PATH(filename);
}
const ext = path.extname(filename);
if (ext !== '.js' && ext !== '.mjs') {
throw new ERR_WORKER_UNSUPPORTED_EXTENSION(ext);
}
}

// Set up the C++ handle for the worker, as well as some internal wiring.
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-worker-unsupported-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Flags: --experimental-worker
'use strict';

const common = require('../common');
const assert = require('assert');
const { Worker } = require('worker');

{
const expectedErr = common.expectsError({
code: 'ERR_WORKER_NEED_ABSOLUTE_PATH',
type: TypeError
}, 4);
assert.throws(() => { new Worker('a.js'); }, expectedErr);
assert.throws(() => { new Worker('b'); }, expectedErr);
assert.throws(() => { new Worker('c/d.js'); }, expectedErr);
assert.throws(() => { new Worker('a.mjs'); }, expectedErr);
}

{
const expectedErr = common.expectsError({
code: 'ERR_WORKER_UNSUPPORTED_EXTENSION',
type: TypeError
}, 3);
assert.throws(() => { new Worker('/b'); }, expectedErr);
assert.throws(() => { new Worker('/c.wasm'); }, expectedErr);
assert.throws(() => { new Worker('/d.txt'); }, expectedErr);
}

0 comments on commit c97fb91

Please sign in to comment.