filters applied in wrong order #389
Replies: 4 comments 7 replies
-
cant reproduce, please provide a script example from mako.template import Template
t = Template("""
<%
def keep_spaces(p_s):
return p_s.replace(' ',' ')
%>
${x |h, keep_spaces}
""")
print(t.render(x=' ABC')) output:
|
Beta Was this translation helpful? Give feedback.
-
Likely security issues, CVEs and such. Someone contributed it long ago. Feel free to make your own "simple" HTML filter.
Mako is not really maintained very much, only recently have we gotten some PRs, and we definitely don't want to deal with HTML escaping that's fertile ground for CVE posters. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to see where I wrote that, I wrote, "Mako is not really maintained very much, only recently have we gotten some PRs, and we definitely don't want to deal with HTML escaping that's fertile ground for CVE posters.", so, colloquialism here, "no longer maintained" != "not really maintained very much", we of course do releases and bug fixes and all that. I just do not have a current initiative of the pattern "what am I going to add to Mako this month, test, document, release, support, fix for years, all by myself, to make it better?" I have too much else going on to devise, test, implement, and support new features alone. I dont know what the current Python templating ecosystem looks like and if it is in fact still just Jinja2 and Mako that would seem...pretty surprising.
But after all that, this is not a broken light bulb. The Markup object is the best choice here and we certainly can't just switch it out as that would break billions of existing templates that rely on its behavior. the fact that it continues to escape new strings that are added to it is certainly good for security; users who want to render raw HTML and open themselves up for scripting attacks if they aren't careful have to explicitly opt into that, and that insulates Mako from causing security problems even more. Mako using a widely used and trusted library for HTML escaping is still what I'd be doing even if Mako were my only project full time.
That's not true at all, take a look at stats: https://www.pepy.tech/projects/jinja2 - 27,452,524 downloads in 7 days Jinja2 is the template language I see in basically everything Python that I didnt write, Sphinx, Ansible, etc. Mako is my own little thing that I personally prefer but as it allows raw Python code, most people dont really want that.
it's certainly not going anywhere and releases will keep working, but we can't just change API behaviors that have been the same way for 15 years |
Beta Was this translation helpful? Give feedback.
-
Well, you already changed the API behavior in 2010 breaking existing templates. I don't see how continuing escaping added strings would add to security. That's a not directly documented behaviour, thus unexpected to the developers, and it's never a good thing when functions behave differently than expected. Secondly, if I wanted the added string to be escaped, I'd add it before applying the |h filter. That's a no-brainer. What makes this even worse is that there's no way to switch that escaping off, apart from converting the markup object back to a string. Converting forth and back is detrimental to performance. Of course, every developer can write their own HTML-escaping filter, but you can't recommend that when you talk about security concerns, as every code that is written from scratch can contain mistakes, and all the supposed magic that markupsafe.escape() does would have to be re-invented by each developer individually. I tried to overload the |h filter with a self-written function anyway (by redefining the h function), but for some reason Mako's h function was called instead of mine. I bet there is a proper way to do it and I'm just too unskilled as a python developer, but anyway it's all unnecessarily complicated. It would all be so easy if the h filter returned just a string, as (as far I see) all the other predefined filters do. |
Beta Was this translation helpful? Give feedback.
-
According to the documentation:
But when I do:
it transforms " ABC" into
instead of just
Beta Was this translation helpful? Give feedback.
All reactions