Skip to content

Commit c673e1f

Browse files
committed
apply HTML-autoescaping to all values
Providing a custom autoescape function is currently not possible, so only HTML can be autoescaped for now (see #13). The filter `escape_md`, is added to escape markdown, but it needs to be applied manually.
1 parent 3e7b63a commit c673e1f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ For more information on Jinja2 templates please refer to https://jinja.palletspr
144144
For more information on URL path templates in aiohttp, see https://docs.aiohttp.org/en/stable/web_quickstart.html#variable-resources.
145145

146146

147+
### Escaping
148+
HTML-escaping is performed on all values automatically.
149+
To prevent a value from being escaped, use the `safe` filter: `{{ foo | safe }}`
150+
Refer to the Jinja2 docs for more information on autoescaping: https://jinja.palletsprojects.com/en/stable/templates/#working-with-automatic-escaping
151+
152+
> [!WARNING]
153+
> While HTML is escaped automatically, markdown is not. It needs to be escaped manually via the `escape_md` filter: `{{ foo | escape_md }}`
154+
> This behavior will eventually change when Jinja2 allows custom autoescape functions (see [#13](https://github.com/jkhsjdhjs/maubot-webhook/issues/13) for more information).
155+
156+
147157

148158
## Building
149159
Use the `mbc` tool to build this plugin:
@@ -162,6 +172,7 @@ zip -9r webhook.mbp *
162172
```
163173

164174

175+
165176
## License
166177
<img align="right" src="https://www.gnu.org/graphics/agplv3-155x51.png"/>
167178

webhook.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
2727
import mautrix.types
2828
import jinja2
29+
import re
30+
31+
32+
def escape_md(value: str) -> str:
33+
# based on https://github.com/tulir/gomuks/blob/e0f107f0285936964afeeec8f4efbb312d9e3c22/web/src/util/markdown.ts
34+
# replacing < and > is not necessary as HTML autoescaping is performed anyway
35+
return re.sub(r'([\\`*_[\]])', r'\\\1', value)
2936

3037

3138
class Config(BaseProxyConfig):
@@ -120,7 +127,7 @@ def reload_template(self, key: str) -> None:
120127

121128
def load_template(self, key: str) -> None:
122129
try:
123-
self.templates[key] = jinja2.Template(self.config[key])
130+
self.templates[key] = self.jinja_env.from_string(self.config[key])
124131
except jinja2.TemplateSyntaxError as e:
125132
# avoid 'During handling of the above exception, another exception occurred'
126133
# to keep the error message in the log as short as possible.
@@ -146,6 +153,9 @@ def register_webhook(self) -> None:
146153

147154
async def start(self) -> None:
148155
self.templates: Dict[str, jinja2.Template] = {}
156+
self.jinja_env: jinja2.Environment = jinja2.Environment(autoescape=True)
157+
self.jinja_env.filters["escape_md"] = escape_md
158+
149159
self.config.load_and_update()
150160
self.load_template("room")
151161
self.load_template("message")

0 commit comments

Comments
 (0)