-
-
Notifications
You must be signed in to change notification settings - Fork 16.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow using |tojson in double-quoted HTML attributes again #1002
Comments
Can you give an example? afaik nothing should have changed at all. |
Example What I was doing was rendering the following with something like <div class="my-container" data-ids="{{ ids|tojson }}">
...
</div> Result <div class="my-container" data-ids="["abc", "xyz"]">
...
</div> It's also documented to behave this way as of 0.10.
Note that prior to 0.10, autoescape took care of escaping both single and double quotes in attributes. Now that |
Doesn't this paragraph help you? |
It's more of a workaround. The underlying problem is a design issue. It's quirky and inconsistent that you can only use Even more so since you need to patch it up with |
Are you expecting Jinja to 1.) Find out if you're generating HTML Jinja doesn't make that assumption. The fact that it doesn't is not a "design issue". |
I'd rather it just behave the way it used to. Or have a designated filter for rendering JSON into JavaScript code (i.e. within Before that breaking change, you had to use |
It's safe, it just doesn't work. Before that change using |
It's now vulnerable to XSS without Try replacing the value of |
I'd like to work on this as part of the PyCon sprints. Do we want to allow double quotes or not for the tojson filter? |
I'm not really sure if we want to go back after two releases. It would be nice to figure out a way to make the XSS case @joeyespo posted safe again, but catering to all sorts of styleguides while still being mostly secure is an impossible balancing act. |
I'm fine with adding some documentation "don't put json in an html attribute, but if you do, use |
No, the requirement is to always use single quotes. On Mon, Jun 06, 2016 at 02:57:33PM -0700, David Lord wrote:
|
Sure, that works too. Still seems awkward to try and shove json in an html attribute in the first place. Are we agreeing that |
It's common when using data-* attributes to preload structured data onto the page without requiring additional AJAX calls. Particularly when mixing Flask with a front-end JS framework. The problem is more about breaking the principle of least astonishment. Right now If shoving json in HTML attributes seems wildly awkward to the Flask community, why not favor consistency and make |
That is not true. The filter outputs the same thing in either case. That is like saying
No, not really. |
True. It's different behavior though. One quotation style breaks your front-end, the other doesn't. It's a dangerous side effect from how this is currently implemented. |
cc @mitsuhiko since he implemented this behavior |
I should probably reply to this but the current filter works this way because it works in two situations: within |
I should clarify that i close this because changing this behavior would break lots of code in very subtle and potentially dangerous ways. |
I know it's closed, but I just want to add my +1 here in case there's a possibility of this getting fixed in a major update down the road. I'm not gonna comment on proper ways to fix the issue, but I will say this: The name and function of 'tojson' don't imply to me that a different escape mechanism will be used, so it's pretty astonishing to find that |
I'm ambivalent wrt this behavior but it takes much better arguments to change it now. Both usecases make sense. However, I don't understand how reverting to the old behavior would break code in dangerous ways as claimed by @mitsuhiko |
I'm way late for this but I must say it also doesn't make sense to me that |
@odraencoded It's escaped for use in inline script elements - i.e. JavaScript within HTML, which is markup. So there is some logic to it. |
I'm in a project where double-quoted HTML attributes is part of the style guide. We're also using data- attributes to pass data to code and consider it a bad practice to render anything directly within scripts.
Being able to remove
|safe
within<script>
seems convenient at first glance. But after discovering that it changes the behavior of|tojson
everywhere else when using double-quoted attributes, it doesn't seem as nice. It's just less intuitive overall. Forcing you to use a particular HTML style or requiring|forceescape
(as opposed to just|e
) isn't really a great tradeoff for implying|safe
within<script>
.So the underlying question. Can we allow
|tojson
to work within double-quotes attributes again? Or is there some technical reason why we can't. And if not, why not add another filter to use within<script>
blocks instead so the principal of least surprise isn't violated?The text was updated successfully, but these errors were encountered: