From 28d5125d5e7bce8d38e9381586dac84418c26ea7 Mon Sep 17 00:00:00 2001 From: zaneni6 Date: Wed, 25 Nov 2026 10:22:53 -0700 Subject: [PATCH] fix(devkit bench): the PR comment renders the summary's tables The bench comment wrapped summary.md in a ```text fence, so its Markdown tables showed as raw pipes. The fence was the safety measure: a pull request's code writes that artifact, and the comment is posted with a write token. The comment now renders it as Markdown, inside
, after sanitize() takes out everything that acts: - backslashes are escaped first, then [ ], so no link or image survives (a backslash in the input cannot cancel an added one); - < > & are escaped: no HTML, so the text cannot close the
, hide or forge the marker, or add an element; - every //, www. and github.com, @ mentions, and #12, GH-12 and .../issues|pull|discussions/12 references are broken with a zero-width space: no URL of any scheme, no mention, no cross-reference; - control and bidi characters are dropped; the text is cut at a line past 60000 characters. sanitize() stays inline (the workflow checks nothing out). test_devkit_bench.py BenchComment extracts and tests it. An adversarial review found the backslash and path-reference bypasses, which are fixed and tested. Rendered through GitHub's Markdown API, #1131's summary gives 4 tables and only the run link. Its bypass inputs give no link, image, mention, issue link or
. Co-Authored-By: Claude --- .github/script/ci/test_devkit_bench.py | 61 ++++++++++++++++++++++ .github/workflows/devkit-bench-comment.yml | 47 ++++++++++++++--- 2 files changed, 100 insertions(+), 8 deletions(-) diff --git a/.github/script/ci/test_devkit_bench.py b/.github/script/ci/test_devkit_bench.py index a537545e7..a82eb73fe 100644 --- a/.github/script/ci/test_devkit_bench.py +++ b/.github/script/ci/test_devkit_bench.py @@ -481,6 +481,67 @@ def test_real_maps_load(self): self.assertLess(max(footprints), 2.0, res["models"]) +def bench_comment_sanitize(): + """sanitize() from devkit-bench-comment.yml's inline script (the + workflow checks nothing out, so the code lives there): the part above + its "--- main" line.""" + text = (REPO / ".github/workflows/devkit-bench-comment.yml").read_text(encoding="utf-8") + block = text.split("python3 - <<'EOF'\n", 1)[1].split("\n EOF\n", 1)[0] + lines = [l[10:] if l.startswith(" " * 10) else l.strip() for l in block.splitlines()] + code = "\n".join(lines).split("# --- main", 1)[0] + namespace: dict = {} + exec(compile(code, "devkit-bench-comment.yml", "exec"), namespace) # noqa: S102 - our own workflow's code + return namespace["sanitize"] + + +class BenchComment(unittest.TestCase): + """The PR comment renders the summary as Markdown (its tables), with + everything that acts taken out: the summary is an artifact a pull + request's code writes.""" + + sanitize = staticmethod(bench_comment_sanitize()) + + def test_tables_headings_and_code_render_as_they_are(self): + summary = ("## Dev-kit bench (pr) on `1e02187951`\n\nāœ… No hard-gate failure.\n\n### Linux XRT\n\n" + "| Model | Machine | Context | Decode base → dev-kit | Ī” |\n|---|---|---|---|---|\n" + "| qwen3:0.6b | ryzenai-npu4 | 1k | 20.0 → 18.0 | -10.0% |\n\n- Linux XRT qwen3:0.6b: decode -10.0%\n") + self.assertEqual(self.sanitize(summary), summary) + + def test_nothing_acts(self): + zw = "\u200b" + cases = { + "
": "</details><!-- devkit-bench-comment -->", + '': '<img src="https:/' + zw + '/x/t.png">', + "[click](https://evil.example)": "\\[click\\](https:/" + zw + "/evil.example)", + # An input backslash must not cancel the added one. + "\\[x\\](//evil.example)": "\\\\\\[x\\\\\\](/" + zw + "/evil.example)", + "github.com/o/r/pull/1 and /issues/4": "github" + zw + ".com/o/r/pull/" + zw + "1 and /issues/" + zw + "4", + "![pixel](http://t/x.gif)": "!\\[pixel\\](http:/" + zw + "/t/x.gif)", + "[ref]: https://evil.example": "\\[ref\\]: https:/" + zw + "/evil.example", + "see www.evil.example": "see www" + zw + ".evil.example", + "cc @someone @org/team": "cc @" + zw + "someone @" + zw + "org/team", + "fixes #12 and GH-34, gh-5": "fixes #" + zw + "12 and GH-" + zw + "34, gh-" + zw + "5", + "a & b": "a & b", + "safe\u202eevil\x07": "safeevil", + } + for raw, want in cases.items(): + with self.subTest(raw=raw): + self.assertEqual(self.sanitize(raw), want) + # A heading's # stays a heading. + self.assertEqual(self.sanitize("### Linux XRT"), "### Linux XRT") + + def test_long_summary_is_cut_at_a_line(self): + out = self.sanitize("| a | b |\n" * 10000) + self.assertLessEqual(len(out), 60000 + 100) + self.assertTrue(out.endswith("(cut: see the run's bench-summary artifact)")) + self.assertIn("| a | b |\n\n(cut", out) + + def test_workflow_renders_markdown_not_a_code_block(self): + text = (REPO / ".github/workflows/devkit-bench-comment.yml").read_text(encoding="utf-8") + self.assertIn("text = sanitize(raw.decode(", text) + self.assertNotIn("{fence}text", text) + + def run(**kw): base = {"id": 5, "run_number": 3, "run_attempt": 1, "path": resolve.BUILD_WORKFLOW, "repository": {"full_name": "ROCm/FastFlowLM"}, "head_repository": {"full_name": "ROCm/FastFlowLM"}, diff --git a/.github/workflows/devkit-bench-comment.yml b/.github/workflows/devkit-bench-comment.yml index 5558edd9c..1ae85de26 100644 --- a/.github/workflows/devkit-bench-comment.yml +++ b/.github/workflows/devkit-bench-comment.yml @@ -24,9 +24,13 @@ name: Dev-kit Bench comment # commit and head repository; never from the artifact # - the artifact: only bench-summary, at most 64 KB as listed, a zip of # exactly summary.md (no links or other entries), at most 60000 bytes -# read; @ is defused so nothing mentions anyone, and the text goes in a -# code fence longer than any backtick run in it, inside
, so it -# cannot close the fence, forge the header or hide a marker +# read; it is rendered as Markdown (its tables), inside
, after +# sanitize() takes out everything that acts: < > & are escaped (no HTML, +# so it cannot close the
, forge the header or hide a marker), +# backslashes and [ ] are escaped (no link or image), every // and +# www. and github.com, @ mentions and #123 / GH-123 / .../issues/123 +# references are broken with a zero-width space, control and bidi +# characters are dropped (test_devkit_bench.py BenchComment) # - the comment to update is the newest by github-actions[bot] starting # with the marker; otherwise a new one # - every event field reaches the scripts through env: @@ -118,19 +122,46 @@ jobs: set -euo pipefail python3 - <<'EOF' import json, os, re + + ZWSP = "\u200b" + # Control and bidi characters (no hidden or reordered text). + INVISIBLE = re.compile("[\x00-\x08\x0b-\x1f\x7f\u200e\u200f\u202a-\u202e\u2066-\u2069]") + LIMIT = 60000 + + def sanitize(text): + """The bench summary as Markdown that renders its headings, + tables, lists and inline code, and nothing that acts: no HTML + (so no element, comment or closing tag of this comment's own), + no link, image or bare URL, no mention or issue reference.""" + text = INVISIBLE.sub("", text.replace("\r", "")) + # Backslashes first: an input \ must not pair with one added + # below and leave a [ live. + text = text.replace("\\", "\\\\") + text = text.replace("&", "&").replace("<", "<").replace(">", ">") + text = text.replace("[", "\\[").replace("]", "\\]") + # Every //, not only ://: no URL of any scheme, nor //host. + text = text.replace("//", "/" + ZWSP + "/").replace("www.", "www" + ZWSP + ".") + text = re.sub(r"(?i)github\.com", "github" + ZWSP + ".com", text) + text = text.replace("@", "@" + ZWSP) + # Issue and pull request references: #12, GH-12, .../issues/12. + text = re.sub(r"#(?=\d)", "#" + ZWSP, text) + text = re.sub(r"(?i)\b(GH)-(?=\d)", r"\1-" + ZWSP, text) + text = re.sub(r"(?i)/(issues|pulls?|discussions)/(?=\d)", r"/\1/" + ZWSP, text) + if len(text) > LIMIT: + text = text[:LIMIT].rsplit("\n", 1)[0] + "\n\n(cut: see the run's bench-summary artifact)" + return text + + # --- main (test_devkit_bench.py runs only what is above) --- e = os.environ raw = open(os.path.join(e["RUNNER_TEMP"], "summary", "summary.md"), "rb").read() - text = raw.decode("utf-8", errors="replace").replace("\r", "") - # No mentions or team pings: a zero-width space after every @. - text = text.replace("@", "@\u200b") - fence = "`" * max(3, 1 + max((len(m) for m in re.findall(r"`+", text)), default=0)) + text = sanitize(raw.decode("utf-8", errors="replace")) run_id = re.sub(r"\D", "", e["RUN_ID"]) sha = re.sub(r"[^0-9a-f]", "", e["HEAD_SHA"])[:10] url = f"https://github.com/{e['GITHUB_REPOSITORY']}/actions/runs/{run_id}" body = (f"{e['MARKER']}\n### Dev-kit bench\n\n" f"Results of the dev-kit benches in [Build and Test run {run_id}]({url}) for `{sha}`, as that run reported " f"them (its artifacts hold the per-model reports).\n\n" - f"
Summary\n\n{fence}text\n{text}\n{fence}\n\n
\n") + f"
Summary\n\n{text}\n\n
\n") with open(os.path.join(e["RUNNER_TEMP"], "body.json"), "w", encoding="utf-8") as f: json.dump({"body": body}, f) EOF