Skip to content

Commit a08658f

Browse files
Copilotmrjf
andauthored
fix: avoid regex redos in named group extraction
Agent-Logs-Url: https://github.com/githubnext/tsessebe/sessions/ef322c76-17f1-4be8-89de-3297824d5fdf Co-authored-by: mrjf <180956+mrjf@users.noreply.github.com>
1 parent ca53fae commit a08658f

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

src/stats/string_ops_extended.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,29 @@ export function strExtractGroups(
213213

214214
/** Parse named capture group names from a regex source string. */
215215
function extractGroupNames(re: RegExp): string[] {
216-
const namedGroupPattern = /\(\?<([^>]+)>/g;
217216
const names: string[] = [];
218-
for (;;) {
219-
const m = namedGroupPattern.exec(re.source);
220-
if (m === null) {
221-
break;
222-
}
223-
const name = m[1];
224-
if (name !== undefined) {
225-
names.push(name);
217+
const source = re.source;
218+
let i = 0;
219+
while (i < source.length - 2) {
220+
if (source[i] === "(" && source[i + 1] === "?" && source[i + 2] === "<") {
221+
const first = source[i + 3];
222+
if (first !== undefined && /[A-Za-z_]/.test(first)) {
223+
let j = i + 4;
224+
while (j < source.length) {
225+
const ch = source[j];
226+
if (ch === ">") {
227+
names.push(source.slice(i + 3, j));
228+
i = j;
229+
break;
230+
}
231+
if (ch !== undefined && !/[A-Za-z0-9_]/.test(ch)) {
232+
break;
233+
}
234+
j++;
235+
}
236+
}
226237
}
238+
i++;
227239
}
228240
return names;
229241
}

0 commit comments

Comments
 (0)