X Tutup
Skip to content

Commit 9aa5522

Browse files
authored
sanitize html and markdown in Message.text_html and text_markdown (python-telegram-bot#621)
* sanitize html and markdown in Message.text_html and text_markdown * add import for escape_html
1 parent 9720f59 commit 9aa5522

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

telegram/message.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from telegram import (Audio, Contact, Document, Chat, Location, PhotoSize, Sticker, TelegramObject,
2626
User, Video, Voice, Venue, MessageEntity, Game)
27+
from telegram.utils.helpers import escape_html, escape_markdown
2728

2829

2930
class Message(TelegramObject):
@@ -635,6 +636,7 @@ def text_html(self):
635636
last_offset = 0
636637

637638
for entity, text in sorted(entities.items(), key=(lambda item: item[0].offset)):
639+
text = escape_html(text)
638640

639641
if entity.type == MessageEntity.TEXT_LINK:
640642
insert = '<a href="{}">{}</a>'.format(entity.url, text)
@@ -649,7 +651,7 @@ def text_html(self):
649651
else:
650652
insert = text
651653

652-
markdown_text += message_text[last_offset:entity.offset] + insert
654+
markdown_text += escape_html(message_text[last_offset:entity.offset]) + insert
653655
last_offset = entity.offset + entity.length
654656

655657
markdown_text += message_text[last_offset:]
@@ -673,6 +675,7 @@ def text_markdown(self):
673675
last_offset = 0
674676

675677
for entity, text in sorted(entities.items(), key=(lambda item: item[0].offset)):
678+
text = escape_markdown(text)
676679

677680
if entity.type == MessageEntity.TEXT_LINK:
678681
insert = '[{}]({})'.format(text, entity.url)
@@ -687,7 +690,7 @@ def text_markdown(self):
687690
else:
688691
insert = text
689692

690-
markdown_text += message_text[last_offset:entity.offset] + insert
693+
markdown_text += escape_markdown(message_text[last_offset:entity.offset]) + insert
691694
last_offset = entity.offset + entity.length
692695

693696
markdown_text += message_text[last_offset:]

telegram/utils/helpers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020

2121
import re
2222

23+
try:
24+
from html import escape as escape_html # noqa: F401
25+
except ImportError:
26+
from cgi import escape as escape_html # noqa: F401
27+
2328

2429
def escape_markdown(text):
2530
"""Helper function to escape telegram markup symbols"""

tests/test_message.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,33 @@ def setUp(self):
3737
self.test_entities = [
3838
{
3939
'length': 4,
40-
'offset': 9,
40+
'offset': 10,
4141
'type': 'bold'
4242
},
4343
{
44-
'length': 6,
45-
'offset': 15,
44+
'length': 7,
45+
'offset': 16,
4646
'type': 'italic'
4747
},
4848
{
4949
'length': 4,
50-
'offset': 23,
50+
'offset': 25,
5151
'type': 'code'
5252
},
5353
{
5454
'length': 5,
55-
'offset': 29,
55+
'offset': 31,
5656
'type': 'text_link',
5757
'url': 'http://github.com/'
5858
},
5959
{
6060
'length': 3,
61-
'offset': 39,
61+
'offset': 41,
6262
'type': 'pre'
6363
},
6464
]
6565

66-
self.test_text = 'Test for bold, italic, code, links and pre.'
66+
self.test_text = 'Test for <bold, ita_lic, code, links and pre.'
6767
self.test_message = telegram.Message(
6868
message_id=1,
6969
from_user=None,
@@ -99,12 +99,12 @@ def test_parse_entities(self):
9999
entity_2: 'h'})
100100

101101
def test_text_html(self):
102-
test_html_string = 'Test for <b>bold</b>, <i>italic</i>, <code>code</code>, ' '<a href="http://github.com/">links</a> and <pre>pre</pre>.'
102+
test_html_string = 'Test for &lt;<b>bold</b>, <i>ita_lic</i>, <code>code</code>, <a href="http://github.com/">links</a> and <pre>pre</pre>.'
103103
text_html = self.test_message.text_html
104104
self.assertEquals(test_html_string, text_html)
105105

106106
def test_text_markdown(self):
107-
test_md_string = 'Test for *bold*, _italic_, `code`, [links](http://github.com/) and ```pre```.'
107+
test_md_string = 'Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/) and ```pre```.'
108108
text_markdown = self.test_message.text_markdown
109109
self.assertEquals(test_md_string, text_markdown)
110110

0 commit comments

Comments
 (0)
X Tutup