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

Commit d934a8d

Browse files
TimothyGuaddaleax
authored andcommitted
worker: restrict supported extensions
Only allow `.js` and `.mjs` extensions to provide future-proofing for file type detection. PR-URL: #117 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Olivia Hugger <olivia@fastmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent f27b650 commit d934a8d

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

lib/internal/errors.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ E('ERR_WORKER_NEED_ABSOLUTE_PATH',
357357
E('ERR_WORKER_OUT_OF_MEMORY', 'The worker script ran out of memory');
358358
E('ERR_WORKER_UNSERIALIZABLE_ERROR',
359359
'Serializing an uncaught exception failed');
360+
E('ERR_WORKER_UNSUPPORTED_EXTENSION',
361+
'The worker script extension must be ".js" or ".mjs". Received "%s"');
360362
E('ERR_ZLIB_BINDING_CLOSED', 'zlib binding closed');
361363

362364
function invalidArgType(name, expected, actual) {

lib/internal/worker.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,14 @@ class Worker extends EventEmitter {
129129
'string', filename);
130130
}
131131

132-
if (!options.eval && !path.isAbsolute(filename)) {
133-
throw new errors.TypeError('ERR_WORKER_NEED_ABSOLUTE_PATH', filename);
132+
if (!options.eval) {
133+
if (!path.isAbsolute(filename)) {
134+
throw new errors.TypeError('ERR_WORKER_NEED_ABSOLUTE_PATH', filename);
135+
}
136+
const ext = path.extname(filename);
137+
if (ext !== '.js' && ext !== '.mjs') {
138+
throw new errors.TypeError('ERR_WORKER_UNSUPPORTED_EXTENSION', ext);
139+
}
134140
}
135141

136142
const resourceLimits = {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { Worker } = require('worker');
6+
7+
{
8+
const expectedErr = common.expectsError({
9+
code: 'ERR_WORKER_NEED_ABSOLUTE_PATH',
10+
type: TypeError
11+
}, 4);
12+
assert.throws(() => { new Worker('a.js'); }, expectedErr);
13+
assert.throws(() => { new Worker('b'); }, expectedErr);
14+
assert.throws(() => { new Worker('c/d.js'); }, expectedErr);
15+
assert.throws(() => { new Worker('a.mjs'); }, expectedErr);
16+
}
17+
18+
{
19+
const expectedErr = common.expectsError({
20+
code: 'ERR_WORKER_UNSUPPORTED_EXTENSION',
21+
type: TypeError
22+
}, 3);
23+
assert.throws(() => { new Worker('/b'); }, expectedErr);
24+
assert.throws(() => { new Worker('/c.wasm'); }, expectedErr);
25+
assert.throws(() => { new Worker('/d.txt'); }, expectedErr);
26+
}

0 commit comments

Comments
 (0)