Skip to content

Commit

Permalink
lib: fix MIME overmatch in data URLs
Browse files Browse the repository at this point in the history
This commit adds the delimiters ^ and $ to the regex that matches the
MIME types for "data:URLs".
Fixes: #48957
  • Loading branch information
andremralves committed Aug 11, 2023
1 parent 7f2c810 commit fc3b4dc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if (experimentalWasmModules) {
function mimeToFormat(mime) {
if (
RegExpPrototypeExec(
/\s*(text|application)\/javascript\s*(;\s*charset=utf-?8\s*)?/i,
/^\s*(text|application)\/javascript\s*(;\s*charset=utf-?8\s*)?$/i,
mime,
) !== null
) return 'module';
Expand Down
5 changes: 5 additions & 0 deletions test/es-module/test-esm-invalid-data-urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ const assert = require('assert');
code: 'ERR_UNKNOWN_MODULE_FORMAT',
message: 'Unknown module format: text/css for URL data:text/css,.error { color: red; }',
});
await assert.rejects(import('data:WRONGtext/javascriptFORMAT,console.log("hello!");'), {
code: 'ERR_UNKNOWN_MODULE_FORMAT',
message: 'Unknown module format: WRONGtext/javascriptFORMAT for URL data:WRONGtext/javascriptFORMAT,\
console.log("hello!");',
});
})().then(common.mustCall());

0 comments on commit fc3b4dc

Please sign in to comment.