From 023e29983f8cfc1955b936eeebe9136465606e13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Alves?= <71379045+andremralves@users.noreply.github.com> Date: Sun, 13 Aug 2023 18:24:22 -0300 Subject: [PATCH] lib: fix MIME overmatch in data URLs This commit adds the delimiters ^ and $ to the regex that matches the MIME types for `data:` URLs. PR-URL: https://github.com/nodejs/node/pull/49104 Fixes: https://github.com/nodejs/node/issues/48957 Reviewed-By: Geoffrey Booth Reviewed-By: Antoine du Hamel --- lib/internal/modules/esm/formats.js | 2 +- test/es-module/test-esm-invalid-data-urls.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/formats.js b/lib/internal/modules/esm/formats.js index 63742914597c46..c4181b50f10451 100644 --- a/lib/internal/modules/esm/formats.js +++ b/lib/internal/modules/esm/formats.js @@ -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'; diff --git a/test/es-module/test-esm-invalid-data-urls.js b/test/es-module/test-esm-invalid-data-urls.js index e434c895a2e37d..6eafe75e7d7ed7 100644 --- a/test/es-module/test-esm-invalid-data-urls.js +++ b/test/es-module/test-esm-invalid-data-urls.js @@ -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());