From 0312ee133c839c1f8adbab3f7e522352d39cb334 Mon Sep 17 00:00:00 2001 From: Sumit Kumar Das <151006536+skdas20@users.noreply.github.com> Date: Sun, 23 Aug 2026 02:07:46 +0530 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/61904 Signed-off-by: skdas20 PR-URL: https://github.com/nodejs/node/pull/61951 Reviewed-By: Aviv Keller Reviewed-By: Trivikram Kamat --- benchmark/esm/get-data-protocol-format.js | 30 +++++++++++++++++++++++ lib/internal/modules/esm/get_format.js | 2 +- lib/internal/modules/esm/load.js | 2 +- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 benchmark/esm/get-data-protocol-format.js diff --git a/benchmark/esm/get-data-protocol-format.js b/benchmark/esm/get-data-protocol-format.js new file mode 100644 index 00000000000..35e54a77003 --- /dev/null +++ b/benchmark/esm/get-data-protocol-format.js @@ -0,0 +1,30 @@ +// Benchmarks defaultGetFormat() on `data:` URLs. The MIME-matching regex used +// to be susceptible to catastrophic backtracking on malformed input lacking a +// `,` separator (https://github.com/nodejs/node/issues/61904); `pathLength` +// scales the malformed path so a regression shows up as a sharp drop in ops/sec +// rather than a hang. +'use strict'; + +const common = require('../common.js'); + +const configs = { + n: [1e4], + pathLength: [1e2, 1e3, 1e4], +}; + +const options = { + flags: ['--expose-internals'], +}; + +const bench = common.createBenchmark(main, configs, options); + +function main({ n, pathLength }) { + const { defaultGetFormat } = require('internal/modules/esm/get_format'); + const url = new URL(`data:a/${'a'.repeat(pathLength)}B`); + + bench.start(); + for (let i = 0; i < n; i++) { + defaultGetFormat(url, { parentURL: undefined }); + } + bench.end(n); +} diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 7bc69b3eb5c..b486337fac8 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -104,7 +104,7 @@ function detectModuleFormat(source, url) { */ function getDataProtocolModuleFormat(parsed) { const { 1: mime } = RegExpPrototypeExec( - /^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/, + /^([^/]+\/[^;,]+)(?:;[^,]*)?,/, parsed.pathname, ) || [ null, null, null ]; diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js index 94879761553..ed271a40e5c 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js @@ -203,7 +203,7 @@ function throwIfUnsupportedURLScheme(parsed) { */ function throwUnknownModuleFormat(url, format) { const dataUrl = RegExpPrototypeExec( - /^data:([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/, + /^data:([^/]+\/[^;,]+)(?:;[^,]*)?,/, url, );