Avoid ReDoS by using better regexes

This commit is contained in:
Alexander Farber
2025-12-15 16:23:17 +01:00
parent 5d94763581
commit 1956507d90

View File

@@ -362,15 +362,19 @@ Co-authored-by: ${gitCoAuthorSettings.name} <${gitCoAuthorSettings.email}>`;
// Handle different git commit patterns // Handle different git commit patterns
// Match -m "message" or -m 'message', including combined flags like -am // Match -m "message" or -m 'message', including combined flags like -am
const messagePattern = /(-[a-zA-Z]*m\s+)(['"])((?:\\.|[^\\])*?)(\2)/; // Use separate patterns to avoid ReDoS (catastrophic backtracking)
const match = command.match(messagePattern); const doubleQuotePattern = /(-[a-zA-Z]*m\s+)"((?:[^"\\]|\\.)*)"/;
const singleQuotePattern = /(-[a-zA-Z]*m\s+)'((?:[^'\\]|\\.)*)'/;
const match =
command.match(doubleQuotePattern) || command.match(singleQuotePattern);
const quote = command.match(doubleQuotePattern) ? '"' : "'";
console.error('[gitCoAuthor] Message pattern match:', match ? 'YES' : 'NO'); console.error('[gitCoAuthor] Message pattern match:', match ? 'YES' : 'NO');
if (match) { if (match) {
const [fullMatch, prefix, quote, existingMessage, closingQuote] = match; const [fullMatch, prefix, existingMessage] = match;
const newMessage = existingMessage + coAuthor; const newMessage = existingMessage + coAuthor;
const replacement = prefix + quote + newMessage + closingQuote; const replacement = prefix + quote + newMessage + quote;
console.error('[gitCoAuthor] Adding co-author trailer'); console.error('[gitCoAuthor] Adding co-author trailer');
return command.replace(fullMatch, replacement); return command.replace(fullMatch, replacement);