Skip to content

Commit e0a91a0

Browse files
author
Pietro Albini
committed
Fix docstrings not escaped in the /help command if no syntax was used
Before this commit, even if you didn't use rich formatting in your docstrings you had to escape any >, < or & in them. This commit fixes that by automatically escaping the docstring if no syntax was used. You need to escape those chars manually if you use rich formatting though. Fixes: GH-67
1 parent 664034d commit e0a91a0

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

botogram/defaults.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
Released under the MIT license
77
"""
88

9+
import html
10+
11+
from . import syntaxes
912
from . import components
1013
from . import decorators
1114

@@ -76,7 +79,7 @@ def _help_generic_message(self, bot, commands):
7679
if len(commands) > 0:
7780
message.append(bot._("<b>This bot supports those commands:</b>"))
7881
for name in sorted(commands.keys()):
79-
summary = commands[name].summary
82+
summary = escape_html(commands[name].summary)
8083
if summary is None:
8184
summary = "<i>%s</i>" % bot._("No description available.")
8285
message.append("/%s <code>-</code> %s" % (name, summary))
@@ -104,7 +107,7 @@ def _help_command_message(self, bot, commands, command):
104107
"""Generate a command's help message"""
105108
message = []
106109

107-
docstring = commands[command].docstring
110+
docstring = escape_html(commands[command].docstring)
108111
if docstring is None:
109112
docstring = "<i>%s</i>" % bot._("No description available.")
110113
message.append("/%s <code>-</code> %s" % (command, docstring))
@@ -151,3 +154,12 @@ def no_commands_hook(self, bot, chat, message):
151154
bot._("Use /help to get a list of the commands."),
152155
]), syntax="html")
153156
return True
157+
158+
159+
def escape_html(text):
160+
"""Escape a docstring"""
161+
# The docstring is escaped only if it doesn't contain HTML markup
162+
if not syntaxes.is_html(text):
163+
return html.escape(text)
164+
165+
return text

docs/changelog.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ Performance improvements
7878
* Updates queueing performance improved
7979
* Backlog processing is now instantaneous
8080

81+
Bug fixes
82+
---------
83+
84+
* Fix docstrings not escaped in the ``/help`` command if no syntax was used
85+
(`issue 67`_)
86+
87+
* Now docstrings are escaped if you don't use any HTML syntax in them, but
88+
if you use HTML you need to manually escape that specific docstring.
89+
8190
Deprecated features
8291
-------------------
8392

@@ -87,6 +96,8 @@ Deprecated features will be removed in botogram 1.0!
8796
* ``Message.left_chat_participant`` is now deprecated
8897
* ``Bot.hide_commands`` is now deprecated
8998

99+
.. _issue 67: https://github.com/pietroalbini/botogram/issues/67
100+
90101
.. _changelog-0.2.1:
91102

92103
botogram 0.2.1

0 commit comments

Comments
 (0)