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.

PR-URL: #49104
Fixes: #48957
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
andremralves authored and targos committed Nov 26, 2023
1 parent d585d13 commit 981aa78
Show file tree
Hide file tree
Showing 2 changed files with 4 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 @@ -38,7 +38,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
3 changes: 3 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,7 @@ 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',
});
})().then(common.mustCall());

0 comments on commit 981aa78

Please sign in to comment.