From ce98226a024133471575ee76da2d8afe8ce66b59 Mon Sep 17 00:00:00 2001 From: Sergey Chernyshev Date: Thu, 25 Apr 2024 04:20:20 +0200 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 --- graal-nodejs/lib/internal/modules/esm/formats.js | 2 +- graal-nodejs/test/es-module/test-esm-invalid-data-urls.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/graal-nodejs/lib/internal/modules/esm/formats.js b/graal-nodejs/lib/internal/modules/esm/formats.js index b081cbe8dd5..f56da5de7cb 100644 --- a/graal-nodejs/lib/internal/modules/esm/formats.js +++ b/graal-nodejs/lib/internal/modules/esm/formats.js @@ -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'; } diff --git a/graal-nodejs/test/es-module/test-esm-invalid-data-urls.js b/graal-nodejs/test/es-module/test-esm-invalid-data-urls.js index e434c895a2e..6eafe75e7d7 100644 --- a/graal-nodejs/test/es-module/test-esm-invalid-data-urls.js +++ b/graal-nodejs/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());