esm: data URLs should ignore unknown parameters

PR-URL: https://github.com/nodejs/node/pull/30593
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
This commit is contained in:
Bradley Farias 2019-11-22 10:34:31 -06:00
parent 8c41b19619
commit 568968e39b
3 changed files with 43 additions and 2 deletions

View File

@ -55,7 +55,7 @@ function resolve(specifier, parentURL) {
try {
const parsed = new URL(specifier);
if (parsed.protocol === 'data:') {
const [ , mime ] = /^([^/]+\/[^;,]+)(;base64)?,/.exec(parsed.pathname) || [ null, null, null ];
const [ , mime ] = /^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/.exec(parsed.pathname) || [ null, null, null ];
const format = ({
'__proto__': null,
'text/javascript': 'module',

View File

@ -38,7 +38,7 @@ const debug = debuglog('esm');
const translators = new SafeMap();
exports.translators = translators;
const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(;base64)?,([\s\S]*)$/;
const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/;
function getSource(url) {
const parsed = new URL(url);
if (parsed.protocol === 'file:') {

View File

@ -35,6 +35,47 @@ function createBase64URL(mime, body) {
assert.deepStrictEqual(Object.keys(ns), ['default']);
assert.deepStrictEqual(ns.default, plainESMURL);
}
{
const body = 'export default import.meta.url;';
const plainESMURL = createURL('text/javascript;charset=UTF-8', body);
const ns = await import(plainESMURL);
assert.deepStrictEqual(Object.keys(ns), ['default']);
assert.deepStrictEqual(ns.default, plainESMURL);
}
{
const body = 'export default import.meta.url;';
const plainESMURL = createURL('text/javascript;charset="UTF-8"', body);
const ns = await import(plainESMURL);
assert.deepStrictEqual(Object.keys(ns), ['default']);
assert.deepStrictEqual(ns.default, plainESMURL);
}
{
const body = 'export default import.meta.url;';
const plainESMURL = createURL('text/javascript;;a=a;b=b;;', body);
const ns = await import(plainESMURL);
assert.deepStrictEqual(Object.keys(ns), ['default']);
assert.deepStrictEqual(ns.default, plainESMURL);
}
{
const ns = await import('data:application/json;foo="test,"this"');
assert.deepStrictEqual(Object.keys(ns), ['default']);
assert.deepStrictEqual(ns.default, 'this');
}
{
const ns = await import(`data:application/json;foo=${
encodeURIComponent('test,')
},0`);
assert.deepStrictEqual(Object.keys(ns), ['default']);
assert.deepStrictEqual(ns.default, 0);
}
{
await assert.rejects(async () => {
return import('data:application/json;foo="test,",0');
}, {
name: 'SyntaxError',
message: /Unexpected end of JSON input/
});
}
{
const body = '{"x": 1}';
const plainESMURL = createURL('application/json', body);