GeezerVison is showing github.com — worker: restrict supported extensions · nodejs/node@c97fb91
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/errors.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -856,4 +856,7 @@ E('ERR_WORKER_NEED_ABSOLUTE_PATH', | 856 | 856 | TypeError); | 857 | 857 | E('ERR_WORKER_UNSERIALIZABLE_ERROR', | 858 | 858 | 'Serializing an uncaught exception failed', Error); | | 859 | +E('ERR_WORKER_UNSUPPORTED_EXTENSION', | | 860 | + 'The worker script extension must be ".js" or ".mjs". Received "%s"', | | 861 | + TypeError); | 859 | 862 | E('ERR_ZLIB_INITIALIZATION_FAILED', 'Initialization failed', Error); |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/worker.js +10-3Lines changed: 10 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,7 +8,8 @@ const util = require('util'); | 8 | 8 | const { | 9 | 9 | ERR_INVALID_ARG_TYPE, | 10 | 10 | ERR_WORKER_NEED_ABSOLUTE_PATH, | 11 | | - ERR_WORKER_UNSERIALIZABLE_ERROR | | 11 | + ERR_WORKER_UNSERIALIZABLE_ERROR, | | 12 | + ERR_WORKER_UNSUPPORTED_EXTENSION, | 12 | 13 | } = require('internal/errors').codes; | 13 | 14 |
| 14 | 15 | const { internalBinding } = require('internal/bootstrap/loaders'); | @@ -136,8 +137,14 @@ class Worker extends EventEmitter { | 136 | 137 | throw new ERR_INVALID_ARG_TYPE('filename', 'string', filename); | 137 | 138 | } | 138 | 139 |
| 139 | | - if (!options.eval && !path.isAbsolute(filename)) { | 140 | | - throw new ERR_WORKER_NEED_ABSOLUTE_PATH(filename); | | 140 | + if (!options.eval) { | | 141 | + if (!path.isAbsolute(filename)) { | | 142 | + throw new ERR_WORKER_NEED_ABSOLUTE_PATH(filename); | | 143 | + } | | 144 | + const ext = path.extname(filename); | | 145 | + if (ext !== '.js' && ext !== '.mjs') { | | 146 | + throw new ERR_WORKER_UNSUPPORTED_EXTENSION(ext); | | 147 | + } | 141 | 148 | } | 142 | 149 |
| 143 | 150 | // Set up the C++ handle for the worker, as well as some internal wiring. | |
Collapse file Copy file name to clipboard +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Flags: --experimental-worker | | 2 | +'use strict'; | | 3 | + | | 4 | +const common = require('../common'); | | 5 | +const assert = require('assert'); | | 6 | +const { Worker } = require('worker'); | | 7 | + | | 8 | +{ | | 9 | + const expectedErr = common.expectsError({ | | 10 | + code: 'ERR_WORKER_NEED_ABSOLUTE_PATH', | | 11 | + type: TypeError | | 12 | + }, 4); | | 13 | + assert.throws(() => { new Worker('a.js'); }, expectedErr); | | 14 | + assert.throws(() => { new Worker('b'); }, expectedErr); | | 15 | + assert.throws(() => { new Worker('c/d.js'); }, expectedErr); | | 16 | + assert.throws(() => { new Worker('a.mjs'); }, expectedErr); | | 17 | +} | | 18 | + | | 19 | +{ | | 20 | + const expectedErr = common.expectsError({ | | 21 | + code: 'ERR_WORKER_UNSUPPORTED_EXTENSION', | | 22 | + type: TypeError | | 23 | + }, 3); | | 24 | + assert.throws(() => { new Worker('/b'); }, expectedErr); | | 25 | + assert.throws(() => { new Worker('/c.wasm'); }, expectedErr); | | 26 | + assert.throws(() => { new Worker('/d.txt'); }, expectedErr); | | 27 | +} |
|