Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

worker: restrict supported extensions #117

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ E('ERR_WORKER_NEED_ABSOLUTE_PATH',
E('ERR_WORKER_OUT_OF_MEMORY', 'The worker script ran out of memory');
E('ERR_WORKER_UNSERIALIZABLE_ERROR',
'Serializing an uncaught exception failed');
E('ERR_WORKER_UNSUPPORTED_EXTENSION',
'The worker script extension must be ".js" or ".mjs". Received "%s"');
E('ERR_ZLIB_BINDING_CLOSED', 'zlib binding closed');

function invalidArgType(name, expected, actual) {
Expand Down
10 changes: 8 additions & 2 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,14 @@ class Worker extends EventEmitter {
'string', filename);
}

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

const resourceLimits = {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/worker-script.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import worker from 'worker';

worker.postMessage('Hello, world!');
11 changes: 11 additions & 0 deletions test/parallel/test-worker-esmodule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
// Flags: --experimental-modules
const common = require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');
const { Worker } = require('worker');

const w = new Worker(fixtures.path('worker-script.mjs'));
w.on('message', common.mustCall((message) => {
assert.strictEqual(message, 'Hello, world!');
}));
26 changes: 26 additions & 0 deletions test/parallel/test-worker-unsupported-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'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);
}