From 8290c98d35465a38090cdcc1b17be69967092a15 Mon Sep 17 00:00:00 2001
From: Byron <sebastian.thiel@icloud.com>
Date: Sun, 27 Sep 2026 19:31:47 +0200
Subject: [PATCH] fix: bound actor and date parsing

<!-- agent -->
Address `GHSA-m64x-33q8-m5h7` in the shared `parse_actor_and_date()`
helper used for commit authors, committers, and annotated taggers.
Malformed metadata could make its regular expressions retry overlapping
field boundaries and consume excessive CPU time before returning.

Bound the leading field name at its first separator and check the line
ending once, before extracting the actor and date. The date expression
then needs no trailing wildcard or end assertion. Apply the same field
boundary to the actor-only fallback. This prevents repeated scans while
preserving accepted suffixes, Unicode digits, final newlines, and the
existing zero-date fallback for malformed input.

Add compatibility cases and CPU-time regressions for long malformed
metadata and multiline input, covering all three field names and long
valid names. Both timing regressions failed before the fix and pass
afterward. A separate comparison preserved capture groups in 3,240 cases.

Git reference: `git/git@d38352cd43ab9745686d697872408bc3249a153f`,
`ident.c:split_ident_line()` and `t/t4212-log-corrupt.sh`, which scan
identity delimiters directly and cover tolerant handling of invalid
dates. Retain GitPython's existing return values for malformed input.

Assisted-by: GPT 6.0
Co-authored-by: GPT 6.0 <codex@openai.com>
---
 doc/source/changes.rst |  1 +
 git/objects/util.py    |  7 +++---
 test/test_util.py      | 49 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 3 deletions(-)

Index: GitPython-3.1.59/git/objects/util.py
===================================================================
--- GitPython-3.1.59.orig/git/objects/util.py
+++ GitPython-3.1.59/git/objects/util.py
@@ -318,9 +318,10 @@ def parse_date(string_date: Union[str, d
     # END handle exceptions
 
 
-# Precompiled regexes
-_re_actor_epoch = re.compile(r"^.+? (.*) (\d+) ([+-]\d+).*$")
-_re_only_actor = re.compile(r"^.+? (.*)$")
+# Check the line ending once, before parsing fields, to avoid repeated backtracking.
+# Keep the field name from consuming spaces belonging to the actor.
+_re_actor_epoch = re.compile(r"^(?=[^\n]*$).[^ \n]* (.*) (\d+) ([+-]\d+)")
+_re_only_actor = re.compile(r"^.[^ \n]* (.*)$")
 
 
 def parse_actor_and_date(line: str) -> Tuple[Actor, int, int]:
Index: GitPython-3.1.59/test/test_util.py
===================================================================
--- GitPython-3.1.59.orig/test/test_util.py
+++ GitPython-3.1.59/test/test_util.py
@@ -22,6 +22,7 @@ from git.cmd import dashify
 from git.objects.util import (
     altz_to_utctz_str,
     from_timestamp,
+    parse_actor_and_date,
     parse_date,
     tzoffset,
     utctz_to_altz,
@@ -572,6 +573,54 @@ class TestUtils(TestBase):
         )
 
     @ddt.data(
+        ("", Actor("", None), 0, 0),
+        ("author", Actor("author", None), 0, 0),
+        ("author Name <email> 42 -0700", Actor("Name", "email"), 42, 25200),
+        ("committer Name <email> 42 +0530\n", Actor("Name", "email"), 42, -19800),
+        ("tagger Name <email> 42 +0000\r\n", Actor("Name", "email"), 42, 0),
+        ("author Name <email> 42 -0700 trailing", Actor("Name", "email"), 42, 25200),
+        ("author Name <email> 1 +0 42 -0700", Actor("Name", "email"), 42, 25200),
+        ("author Name <email> invalid -0700", Actor("Name", "email"), 0, 0),
+        ("author Name <email> 42 invalid", Actor("Name", "email"), 0, 0),
+        ("author 42 -0700", Actor("42 -0700", None), 0, 0),
+        ("author  42 -0700", Actor("", None), 42, 25200),
+        (" author Name <email> 42 -0700", Actor("Name", "email"), 42, 25200),
+        ("author Näme <email> ١ +٠١٣٠", Actor("Näme", "email"), 1, -5400),
+        ("author\nName <email> 42 -0700", Actor("author\nName <email> 42 -0700", None), 0, 0),
+        ("author Name <email> 42 -0700\nextra", Actor("author Name", "email"), 0, 0),
+    )
+    @ddt.unpack
+    def test_parse_actor_and_date(self, line, actor, epoch, offset):
+        self.assertEqual(parse_actor_and_date(line), (actor, epoch, offset))
+
+    def test_parse_actor_and_date_long_malformed_lines(self):
+        padding = " " * 64_000
+        for field in ("author", "committer", "tagger"):
+            for tail in ("", "<unterminated", "invalid -0700", "42", "-0700", "42 invalid", "42 +"):
+                actor_text = padding + tail
+                start = time.process_time()
+                result = parse_actor_and_date(f"{field} {actor_text}")
+                elapsed = time.process_time() - start
+                # Leave ample CPU time for slow runners, but catch excessive backtracking.
+                self.assertLess(elapsed, 1.0, (field, tail))
+                self.assertEqual(result, (Actor(actor_text, None), 0, 0))
+
+            name = "Long name " * 6_400
+            self.assertEqual(
+                parse_actor_and_date(f"{field} {name}<email> 42 -0700"),
+                (Actor(name.rstrip(), "email"), 42, 25200),
+            )
+
+    def test_parse_actor_and_date_long_multiline_input(self):
+        for field in ("author", "committer", "tagger"):
+            line = f"{field} Name <email> 42 +" + "0" * 64_000 + "\nextra"
+            start = time.process_time()
+            result = parse_actor_and_date(line)
+            elapsed = time.process_time() - start
+            self.assertLess(elapsed, 1.0, field)
+            self.assertEqual(result, (Actor(f"{field} Name", "email"), 0, 0))
+
+    @ddt.data(
         ("name", ""),
         ("name", "prefix_"),
     )
