Skip to content

Commit b1fdb51

Browse files
committed
Fix ReDoS for data URLs
1 parent b98fe7e commit b1fdb51

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const testParameter = (name, filters) => {
99
};
1010

1111
const normalizeDataURL = (urlString, {stripHash}) => {
12-
const match = /^data:(?<type>.*?),(?<data>.*?)(?:#(?<hash>.*))?$/.exec(urlString);
12+
const match = /^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(urlString);
1313

1414
if (!match) {
1515
throw new Error(`Invalid URL: ${urlString}`);

test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,17 @@ test('view-source URL', t => {
344344
normalizeUrl('view-source:https://www.sindresorhus.com');
345345
}, '`view-source:` is not supported as it is a non-standard protocol');
346346
});
347+
348+
test('does not have exponential performance for data URLs', t => {
349+
for (let index = 0; index < 1000; index += 50) {
350+
const url = 'data:' + Array.from({length: index}).fill(',#').join('') + '\ra';
351+
const start = Date.now();
352+
353+
try {
354+
normalizeUrl(url);
355+
} catch {}
356+
357+
const difference = Date.now() - start;
358+
t.true(difference < 100, `Execution time: ${difference}`);
359+
}
360+
});

0 commit comments

Comments
 (0)