Files
node/benchmark/esm/get-data-protocol-format.js
T
Sumit Kumar Das 3ff8c2dbde 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 <skdas5405@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/61951
Reviewed-By: Aviv Keller <me@aviv.sh>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2026-08-22 20:37:46 +00:00

31 lines
873 B
JavaScript

// 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);
}