Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(regex): limit regex manager iterations to 10k to avoid OoM (#22084)
  • Loading branch information
rarkins committed May 11, 2023
1 parent 08233ff commit b5d87c6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/modules/manager/regex/utils.spec.ts
@@ -0,0 +1,14 @@
import { regEx } from '../../../util/regex';
import * as utils from './utils';

describe('modules/manager/regex/utils', () => {
it('does not crash for lazy regex', () => {
const lazyMatch = regEx('(?<currentDigest>.*?)', 'g');
expect(
utils.regexMatchAll(
lazyMatch,
'1f699d2bfc99bbbe4c1ed5bb8fc21e6911d69c6e\n'
)
).toBeArray();
});
});
8 changes: 7 additions & 1 deletion lib/modules/manager/regex/utils.ts
Expand Up @@ -85,12 +85,18 @@ export function regexMatchAll(
): RegExpMatchArray[] {
const matches: RegExpMatchArray[] = [];
let matchResult: RegExpMatchArray | null;
let iterations = 0;
const maxIterations = 10000;
do {
matchResult = regex.exec(content);
if (matchResult) {
matches.push(matchResult);
}
} while (matchResult);
iterations += 1;
} while (matchResult && iterations < maxIterations);
if (iterations === maxIterations) {
logger.warn('Max iterations reached for matchStrings');
}
return matches;
}

Expand Down

0 comments on commit b5d87c6

Please sign in to comment.