Skip to content

Commit

Permalink
module: improve error message for invalid data URL
Browse files Browse the repository at this point in the history
Fixes: #37647
  • Loading branch information
aduh95 committed Mar 10, 2021
1 parent 117e293 commit 68fcf83
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/internal/modules/esm/loader.js
Expand Up @@ -6,6 +6,7 @@ require('internal/modules/cjs/loader');
const {
FunctionPrototypeBind,
ObjectSetPrototypeOf,
RegExpPrototypeExec,
SafeWeakMap,
StringPrototypeStartsWith,
} = primordials;
Expand Down Expand Up @@ -107,6 +108,13 @@ class Loader {
}

const { format } = getFormatResponse;
if (format === null) {
const dataUrl = RegExpPrototypeExec(
/^data:([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/,
url,
);
throw new ERR_UNKNOWN_MODULE_FORMAT(dataUrl ? dataUrl[1] : 'null');
}
if (typeof format !== 'string') {
throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
'string', 'loader getFormat', 'format', format);
Expand Down
18 changes: 18 additions & 0 deletions test/es-module/test-esm-invalid-data-urls.js
@@ -0,0 +1,18 @@
'use strict';
const common = require('../common');
const assert = require('assert');

(async () => {
await assert.rejects(import('data:text/plain,export default0'), {
code: 'ERR_UNKNOWN_MODULE_FORMAT',
message: 'Unknown module format: text/plain',
});
await assert.rejects(import('data:text/plain;base64,'), {
code: 'ERR_UNKNOWN_MODULE_FORMAT',
message: 'Unknown module format: text/plain',
});
await assert.rejects(import('data:application/json,[]'), {
code: 'ERR_UNKNOWN_MODULE_FORMAT',
message: 'Unknown module format: application/json',
});
})().then(common.mustCall());

0 comments on commit 68fcf83

Please sign in to comment.