esm: avoid super-linear data URL MIME regex · nodejs/node@4a1c9a7 · GitHub
Skip to content

Commit 4a1c9a7

Browse files
skdas20aduh95
authored andcommitted
esm: avoid super-linear data URL MIME regex
The MIME regex used for data: URLs could backtrack super-linearly on a malformed URL lacking a ',' separator. Make the optional parameter group anchored on ';' so it cannot overlap with the media-type group. Adds a benchmark (esm/get-data-protocol-format) parameterized over path length, so a backtracking regression shows up as an ops/sec cliff instead of a wall-clock assertion in a test, per review. Fixes: #61904 Signed-off-by: skdas20 <skdas5405@gmail.com> PR-URL: #61951 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 79c39c2 commit 4a1c9a7

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

Lines changed: 30 additions & 0 deletions

lib/internal/modules/esm/get_format.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function detectModuleFormat(source, url) {
104104
*/
105105
function getDataProtocolModuleFormat(parsed) {
106106
const { 1: mime } = RegExpPrototypeExec(
107-
/^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/,
107+
/^([^/]+\/[^;,]+)(?:;[^,]*)?,/,
108108
parsed.pathname,
109109
) || [ null, null, null ];
110110

lib/internal/modules/esm/load.js

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)