Opened 6 years ago

Closed 5 years ago

Last modified 3 years ago

#29791 closed Bug (fixed)

Engine.render_to_string() should honor the autoescape attribute

Reported by: Claude Paroz Owned by: Nathan Gaberel
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Claude Paroz Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In Engine.render_to_string, a Context is created without specifying the engine autoescape attribute. So if you create en engine with autoescape=False and then call its render_to_string() method, the result will always be autoescaped. It was probably overlooked in [19a5f6da329d58653bcda85].

Attachments (3)

autoescape and safe ignored 2021-08-31 16-15-39.png (191.3 KB ) - added by Malik A. Rumi 3 years ago.
autoescape off and safe filter both ignored, html entities applied
still escaped 2021-08-31 20-11-56.png (50.9 KB ) - added by Malik A. Rumi 3 years ago.
get_absolute_url shown as removed in chrome dev tools. full name with domain does show.
library.html still escaped after restart and turning off in PyCharm actions 2021-08-31 20-06-12.png (245.6 KB ) - added by Malik A. Rumi 3 years ago.
link destination in bottom left of screen shows html entities escaping my code

Download all attachments as: .zip

Change History (10)

comment:1 by Tim Graham, 6 years ago

Summary: Engine.render_to_string should honor the autoescape attributeEngine.render_to_string() should honor the autoescape attribute
Triage Stage: UnreviewedAccepted

comment:2 by Claude Paroz, 6 years ago

Owner: changed from nobody to Claude Paroz
Status: newassigned

I'd like to reserve this for an event I'll attend on Oct 4th.

comment:3 by Nathan Gaberel, 5 years ago

I'd like to give this a try. Is it OK to reassign to myself since there's been no update in 6 months?

comment:4 by Simon Charette, 5 years ago

Cc: Claude Paroz added

I think it's safe to re-assign yourself the ticket to you at this point. Are you still planing on working on this in the near future Claude?

comment:5 by Nathan Gaberel, 5 years ago

Has patch: set
Owner: changed from Claude Paroz to Nathan Gaberel

I went ahead and wrote a patch for it, hopefully Claude won't mind. :)
PR

comment:6 by Tim Graham <timograham@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 62457372:

Fixed #29791 -- Made Engine.render_to_string() honor autoescape.

by Malik A. Rumi, 3 years ago

autoescape off and safe filter both ignored, html entities applied

by Malik A. Rumi, 3 years ago

get_absolute_url shown as removed in chrome dev tools. full name with domain does show.

by Malik A. Rumi, 3 years ago

link destination in bottom left of screen shows html entities escaping my code

comment:7 by Malik A. Rumi, 3 years ago

I came here because I cannot get autoescape to turn off. First I put it in my template, surrounding the links I wanted to work, but it did not. I looked around and saw that I could control it the same way template inheritance works, so I went to my base html template and put the autoescape tags there. It still had no effect. Then I rebooted, hoping that would make a difference. It did not.

I do not understand how this patch works. Given that at line 20 of engine.py, the init has

autoescape = True,

what are

self.autoescape=autoescape [line 44]

and

autoescape=self.autoescape [line 163]

actually doing? It just looks like a circular assignment to me.

Further, I don’t see how this is connected to template tags. I see no reference to tags in any of this code. Perhaps it is somewhere else, and happens magically, but regardless, I don’t see it.

Finally, I do not understand the test you ran.

self.assertEqual(
            engine.render_to_string('test_context.html', {'obj': '<script>'}),
            'obj:<script>\n',
        )

It looks to me like all you are doing here is removing the curly braces from a dict, and adding a newline. What has that got to do with autoescaping html? The angle brackets in your test object are still there.

In my templates, all that html is removed in favor of html entities - as you can see from the screen shots I attached.. That means the links don't work, don’t display, and give me repeated NoReverseMatch errors even when the code Django tried was correct.

I would ask that you re-open this ticket and make the autoescape tag work as advertised.

Note: See TracTickets for help on using tickets.
Back to Top