Skip to content

Commit 4a04e7c

Browse files
[client-python] fix <code> tags conversion to backticks (#15374) (#15393)
1 parent 514f7d2 commit 4a04e7c

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

client-python/pycti/utils/opencti_stix2.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import os
55
import random
6+
import re
67
import time
78
import traceback
89
import uuid
@@ -158,7 +159,10 @@ def convert_markdown(self, text: str) -> str:
158159
:rtype: str
159160
"""
160161
if text is not None:
161-
return text.replace("<code>", "`").replace("</code>", "`")
162+
# (.*?) → lazy match captures the content between the tags (shortest possible, so nested/adjacent pairs don't bleed into each other)
163+
# re.DOTALL → allows . to match newlines, so multi-line code content inside the tags still works
164+
# \1 in the replacement → puts the captured content back between the backticks
165+
return re.sub(r"<code>(.*?)</code>", r"`\1`", text, flags=re.DOTALL)
162166
else:
163167
return None
164168

client-python/tests/01-unit/utils/test_opencti_stix2.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,37 @@ def test_unknown_type(opencti_stix2: OpenCTIStix2, caplog):
1818

1919

2020
def test_convert_markdown(opencti_stix2: OpenCTIStix2):
21+
# Matched pair is converted to backticks
2122
result = opencti_stix2.convert_markdown(
2223
" my <code> is very </special> </code> to me"
2324
)
2425
assert " my ` is very </special> ` to me" == result
2526

2627

28+
def test_convert_markdown_multiple_pairs(opencti_stix2: OpenCTIStix2):
29+
# Multiple matched pairs are all converted
30+
result = opencti_stix2.convert_markdown("<code>foo</code> and <code>bar</code>")
31+
assert "`foo` and `bar`" == result
32+
33+
2734
def test_convert_markdown_typo(opencti_stix2: OpenCTIStix2):
28-
result = opencti_stix2.convert_markdown(
29-
" my <code is very </special> </code> to me"
30-
)
31-
assert " my <code is very </special> ` to me" == result
35+
# Malformed opening tag (<code missing closing >) means no valid pair exists; nothing should be replaced
36+
text = " my <code is very </special> </code> to me"
37+
result = opencti_stix2.convert_markdown(text)
38+
assert text == result
39+
40+
41+
def test_convert_markdown_literal_code_tag(opencti_stix2: OpenCTIStix2):
42+
# A lone <code> without a matching </code> is literal content and must not be altered
43+
text = 'Run python3 -c "<code>" and pass it to subprocess.run(..., shell=True)'
44+
result = opencti_stix2.convert_markdown(text)
45+
assert text == result
46+
47+
48+
def test_convert_markdown_mixed_matched_and_lone(opencti_stix2: OpenCTIStix2):
49+
# A matched pair is converted, but a trailing lone <code> is left untouched
50+
result = opencti_stix2.convert_markdown("<code>foo</code> and <code>")
51+
assert "`foo` and <code>" == result
3252

3353

3454
def test_format_date_with_tz(opencti_stix2: OpenCTIStix2):

0 commit comments

Comments
 (0)