Skip to content

Commit 3ab9619

Browse files
aduh95MylesBorins
authored andcommittedApr 5, 2021
module: improve error message for invalid data URL
Fixes: #37647 PR-URL: #37701 Reviewed-By: Derek Lewis <DerekNonGeneric@inf.is> Reviewed-By: Guy Bedford <guybedford@gmail.com>
1 parent 0243376 commit 3ab9619

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed
 

‎lib/internal/modules/esm/loader.js

+11
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ require('internal/modules/cjs/loader');
66
const {
77
FunctionPrototypeBind,
88
ObjectSetPrototypeOf,
9+
RegExpPrototypeExec,
910
SafeWeakMap,
1011
StringPrototypeStartsWith,
1112
} = primordials;
1213

1314
const {
1415
ERR_INVALID_ARG_VALUE,
16+
ERR_INVALID_MODULE_SPECIFIER,
1517
ERR_INVALID_RETURN_PROPERTY,
1618
ERR_INVALID_RETURN_PROPERTY_VALUE,
1719
ERR_INVALID_RETURN_VALUE,
@@ -107,6 +109,15 @@ class Loader {
107109
}
108110

109111
const { format } = getFormatResponse;
112+
if (format === null) {
113+
const dataUrl = RegExpPrototypeExec(
114+
/^data:([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/,
115+
url,
116+
);
117+
throw new ERR_INVALID_MODULE_SPECIFIER(
118+
url,
119+
dataUrl ? `has an unsupported MIME type "${dataUrl[1]}"` : '');
120+
}
110121
if (typeof format !== 'string') {
111122
throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
112123
'string', 'loader getFormat', 'format', format);

‎test/es-module/test-esm-data-urls.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function createBase64URL(mime, body) {
9999
await import(plainESMURL);
100100
common.mustNotCall()();
101101
} catch (e) {
102-
assert.strictEqual(e.code, 'ERR_INVALID_RETURN_PROPERTY_VALUE');
102+
assert.strictEqual(e.code, 'ERR_INVALID_MODULE_SPECIFIER');
103103
}
104104
}
105105
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
(async () => {
6+
await assert.rejects(import('data:text/plain,export default0'), {
7+
code: 'ERR_INVALID_MODULE_SPECIFIER',
8+
message:
9+
'Invalid module "data:text/plain,export default0" has an unsupported ' +
10+
'MIME type "text/plain"',
11+
});
12+
await assert.rejects(import('data:text/plain;base64,'), {
13+
code: 'ERR_INVALID_MODULE_SPECIFIER',
14+
message:
15+
'Invalid module "data:text/plain;base64," has an unsupported ' +
16+
'MIME type "text/plain"',
17+
});
18+
await assert.rejects(import('data:application/json,[]'), {
19+
code: 'ERR_INVALID_MODULE_SPECIFIER',
20+
message:
21+
'Invalid module "data:application/json,[]" has an unsupported ' +
22+
'MIME type "application/json"',
23+
});
24+
})().then(common.mustCall());

0 commit comments

Comments
 (0)
Please sign in to comment.