You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@magpie.apache.org by po...@apache.org on 2026/09/27 15:16:37 UTC

(magpie) branch main updated: fix(pr-management-stats): treat the pr-triage-fold timestamp as untrusted input (#1419)

This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/magpie.git


The following commit(s) were added to refs/heads/main by this push:
     new b9b7f0bd fix(pr-management-stats): treat the pr-triage-fold timestamp as untrusted input (#1419)
b9b7f0bd is described below

commit b9b7f0bdc6fb36de224f413c1d4efcdd5743ad46
Author: kuse <31...@qq.com>
AuthorDate: Sun Sep 27 23:16:32 2026 +0800

    fix(pr-management-stats): treat the pr-triage-fold timestamp as untrusted input (#1419)
    
    * fix(pr-management-stats): treat the pr-triage-fold timestamp as untrusted input
    
    The fold block lives in the PR body, which the PR author controls. An
    unparseable triaged= value raised ValueError out of fold_triaged_at, and a
    timezone-naive one produced a naive datetime that crashed the velocity math
    with TypeError when subtracted from the aware now -- either way one
    hand-written marker in any PR description killed the whole dashboard run.
    
    Degrade both to no fold event (the framework always writes ISO-8601 UTC)
    and pin the behavior with regression tests.
    
    Generated-by: ZCode (GLM-5.3-Flash)
    
    * style: unparsable per the typos dictionary
    
    Generated-by: ZCode (GLM-5.3-Flash)
    
    * fix(pr-management-stats): try every pr-triage-fold marker, not only the first
    
    A malformed marker placed above the framework-written fold block no
    longer hides the real triage timestamp from the stats.
    
    Generated-by: Claude Code (Opus 5.5)
    
    ---------
    
    Co-authored-by: Jarek Potiuk <po...@apache.org>
---
 tools/pr-management-stats/reference.py             | 19 +++++-
 .../pr-management-stats/tests/test_fold_parsing.py | 73 ++++++++++++++++++++++
 2 files changed, 89 insertions(+), 3 deletions(-)

diff --git a/tools/pr-management-stats/reference.py b/tools/pr-management-stats/reference.py
index d56082ae..ce0e825c 100644
--- a/tools/pr-management-stats/reference.py
+++ b/tools/pr-management-stats/reference.py
@@ -328,9 +328,22 @@ def fold_triaged_at(pr):
     PR *description* as a ``<!-- pr-triage-fold: triaged=<ISO> ... -->`` block
     rather than posting a comment. A comment-only marker scan is blind to it,
     so every folded triage note goes uncounted -- the bug this fixes. A fold is
-    always AI-drafted (it is written by the automated triage tool)."""
-    m = _FOLD_TRIAGED_RE.search(pr.get("body") or "")
-    return parse_iso(m.group(1)) if m else None
+    always AI-drafted (it is written by the automated triage tool).
+
+    The block lives in the PR body, which the PR author controls, so the
+    marker text is untrusted input: an unparsable or timezone-naive
+    ``triaged=`` value means "no fold event", never a crash -- a
+    framework-written fold always carries an ISO-8601 UTC timestamp. Every
+    marker is tried, so a malformed one placed above the real block cannot
+    hide it."""
+    for m in _FOLD_TRIAGED_RE.finditer(pr.get("body") or ""):
+        try:
+            at = parse_iso(m.group(1))
+        except ValueError:
+            continue
+        if at.tzinfo is not None:
+            return at
+    return None
 
 
 def triage_marker_events(pr, ctx):
diff --git a/tools/pr-management-stats/tests/test_fold_parsing.py b/tools/pr-management-stats/tests/test_fold_parsing.py
new file mode 100644
index 00000000..4f8b29bd
--- /dev/null
+++ b/tools/pr-management-stats/tests/test_fold_parsing.py
@@ -0,0 +1,73 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""The fold block lives in the PR body, which the PR author controls.
+
+A hand-written or malformed ``pr-triage-fold`` marker must degrade to "no
+fold event" -- never crash the dashboard run (ValueError on an unparsable
+``triaged=`` value, TypeError downstream on a timezone-naive one).
+"""
+from __future__ import annotations
+
+import reference
+from helpers import make_ctx
+
+
+def _body_with(triaged):
+    return {
+        "body": "<!-- pr-triage-fold: triaged=%s head=abc1234 action=draft -->"
+        % triaged
+    }
+
+
+def test_garbage_triaged_value_returns_none():
+    assert reference.fold_triaged_at(_body_with("soon")) is None
+
+
+def test_impossible_date_returns_none():
+    assert reference.fold_triaged_at(_body_with("2026-13-99T99:99:99Z")) is None
+
+
+def test_timezone_naive_triaged_value_returns_none():
+    assert reference.fold_triaged_at(_body_with("2026-06-11T14:22:00")) is None
+
+
+def test_missing_body_returns_none():
+    assert reference.fold_triaged_at({"body": ""}) is None
+    assert reference.fold_triaged_at({}) is None
+
+
+def test_valid_fold_still_parses():
+    at = reference.fold_triaged_at(_body_with("2026-06-11T14:22:00Z"))
+    assert at is not None
+    assert at.tzinfo is not None
+
+
+def test_malformed_marker_does_not_hide_a_later_valid_fold():
+    pr = {
+        "body": "<!-- pr-triage-fold: triaged=oops -->\n"
+        + _body_with("2026-06-11T14:22:00Z")["body"]
+    }
+    at = reference.fold_triaged_at(pr)
+    assert at is not None
+    assert at.isoformat() == "2026-06-11T14:22:00+00:00"
+
+
+def test_marker_events_survive_malformed_fold():
+    """The dashboard entry point must not raise on author-controlled bodies."""
+    events = reference.triage_marker_events(_body_with("oops"), make_ctx())
+    assert events == []