From 00dbd9454ae87f8ebaff0a48a01c7d71a78c80ee Mon Sep 17 00:00:00 2001 From: Andre Alves Date: Fri, 11 Aug 2023 01:47: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". Fixes: https://github.com/nodejs/node/issues/48957 --- 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());