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/25 07:20:13 UTC

(magpie) branch main updated: fix(cve-json): keep JSON escapes intact when replacing an embedded attachment (#1377)

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 d93fcb17 fix(cve-json): keep JSON escapes intact when replacing an embedded attachment (#1377)
d93fcb17 is described below

commit d93fcb1764538bb24fb20948ce259d9dc26a8947
Author: Jarek Potiuk <ja...@potiuk.com>
AuthorDate: Fri Sep 25 09:20:07 2026 +0200

    fix(cve-json): keep JSON escapes intact when replacing an embedded attachment (#1377)
    
    _splice_attachment_into_body replaced an existing CVE-JSON block with
    re.sub and a string replacement. re.sub processes backslash escapes in
    a string replacement, so every JSON "\n" escape inside a string value
    (for example a multi-paragraph summary) became a real newline and the
    embedded JSON no longer parsed. Only the replace path was affected;
    the first append does not go through re.sub.
    
    Pass the replacement as a callable so it is inserted verbatim, and add
    a regression test covering \n, \\ and \1 in the attachment JSON.
    
    Generated-by: Claude Opus 5
---
 .../src/generate_cve_json/cve_json.py                  |  5 ++++-
 .../generate-cve-json/tests/test_cli.py                | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/tools/cve-tool-vulnogram/generate-cve-json/src/generate_cve_json/cve_json.py b/tools/cve-tool-vulnogram/generate-cve-json/src/generate_cve_json/cve_json.py
index 75b510d7..efb01f73 100644
--- a/tools/cve-tool-vulnogram/generate-cve-json/src/generate_cve_json/cve_json.py
+++ b/tools/cve-tool-vulnogram/generate-cve-json/src/generate_cve_json/cve_json.py
@@ -2020,7 +2020,10 @@ def _splice_attachment_into_body(issue_body: str, attachment: str, cve_id: str)
     end_escaped = re.escape(end_marker)
     pattern = rf"{begin_escaped}.*?{end_escaped}\n?"
     if re.search(pattern, issue_body, re.DOTALL):
-        new_body = re.sub(pattern, attachment.rstrip("\n") + "\n", issue_body, count=1, flags=re.DOTALL)
+        # A callable replacement is inserted verbatim; a string would have its
+        # backslash escapes processed, turning the JSON's ``\n`` into newlines.
+        replacement = attachment.rstrip("\n") + "\n"
+        new_body = re.sub(pattern, lambda _: replacement, issue_body, count=1, flags=re.DOTALL)
         return new_body.rstrip() + "\n"
 
     # Also tolerate a legacy single-marker attachment that was embedded
diff --git a/tools/cve-tool-vulnogram/generate-cve-json/tests/test_cli.py b/tools/cve-tool-vulnogram/generate-cve-json/tests/test_cli.py
index 26227546..0b683fd5 100644
--- a/tools/cve-tool-vulnogram/generate-cve-json/tests/test_cli.py
+++ b/tools/cve-tool-vulnogram/generate-cve-json/tests/test_cli.py
@@ -176,6 +176,24 @@ class TestSpliceAttachment:
         assert begin in result
         assert end in result
 
+    def test_replace_keeps_json_backslash_escapes_verbatim(self):
+        # Replacing an existing block must not process backslash escapes in
+        # the new attachment: a JSON ``\n`` inside a string value has to stay
+        # a two-character escape, or the embedded JSON stops parsing.
+        begin, end = self._markers()
+        payload = {
+            "summary": "First paragraph.\n\nSecond paragraph.",
+            "path": "C:\\temp\\x",
+            "group": "keep \\1 literal",
+        }
+        json_text = json.dumps(payload, indent=2)
+        attachment = f"{begin}\n## CVE JSON\n```json\n{json_text}\n```\n{end}\n"
+        body = f"### Body before\nstuff\n\n{begin}\nOLD CONTENT\n{end}\ntrailing line\n"
+        result = cve_json._splice_attachment_into_body(body, attachment, self.cve_id)
+        embedded = result.split("```json\n", 1)[1].split("\n```", 1)[0]
+        assert embedded == json_text
+        assert json.loads(embedded) == payload
+
     def test_legacy_single_marker_path(self):
         begin, _ = self._markers()
         body = f"### Body\n\n{begin}\nleftover legacy content with no end marker\n"