Code

Opened 5 years ago

Closed 2 years ago

#10912 closed Bug (fixed)

Autoescaping variable input in template tags

Reported by: andrewbadr Owned by:
Component: Template system Version: 1.0
Severity: Release blocker Keywords:
Cc: mtredinnick Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by mtredinnick)

(revised description)

When variables are used as input to template tags, we should handle autoescaping a bit better.

Original description was even shorter:

I'd expect firstof to escape variables.

Attachments (1)

firstof_autoescape_t10912_r10630.diff (1.9 KB) - added by andrewbadr 5 years ago.
Fix, with test

Download all attachments as: .zip

Change History (14)

Changed 5 years ago by andrewbadr

Fix, with test

comment:1 Changed 5 years ago by andrewbadr

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

My expected behavior for Django is: The results of all template tags should be escaped unless marked safe. For builtin templatetags that return safe strings, this should include escaping any variables involved in rendering that template tag.

The included patch solves locally what I see as part of a bigger problem.

comment:2 Changed 5 years ago by mtredinnick

  • Cc mtredinnick added
  • Description modified (diff)
  • Patch needs improvement set
  • Summary changed from firstof template tag should autoescape variables to Autoescaping variable input in template tags
  • Triage Stage changed from Unreviewed to Accepted

Hmm. This is a bit of a slight change in policy, but not necessarily a bad one (right now, intentionally, only filters autoescape). However,

  1. Import _render_change_in_context() smells and not of roses. Needs to be renamed or not imported or something. That leading underscore is a big clue.
  2. We need something similar to the autoescape parameter that is passed to filters, so that template tags do not autoescape results when they are being used inside a block that does not do auto-escaping.

I guess template tags know whether they're putting the results into the context -- which means the output shouldn't be autoescaped, since it will be autoescaped on output and you don't know at template tag usage time whether output will involve auto-escaping or not, so it must be delayed until rendering -- or directly displaying the results? I suspect so, but that's something to think about and confirm.

What are the backwards compatibility issues here? In particular, right now, a template tag doesn't have to care about calling mark_safe() on the result, since it just inserts it into the template. If you add the auto-escaping behaviour at the wrong place, you'll undo that and a lot of existing template tags will break. That's going to be a little fiddly to work around.

I'm unenthusiastic about fixing just this single tag, since it does change the currently consistent behaviour across all template tags. I'm more enthusiastic about addressing the general issue, but it's a lot more tricky. I'm really enthusiastic about you doing it, Andrew, since I know you guys work in the template space a lot, so the results will have some validity and it means I don't have to work on this (that latter point is important to me; maybe less so to you).

comment:3 Changed 5 years ago by krystal

Just to mention that I just discovered a flaw on my website due to this non-escaping-feature when using firstof tags ; It should really be at least explicitly documented so people get aware of this while reading the firstof (or cycle) doc.

A little paragraph with a one line example should be nice until a good way to do is found.

comment:4 Changed 5 years ago by krystal

  • milestone set to 1.1

I move this to milestone 1.1 so the big-boss-team give his opinion ; default django security is involved and the doc clearly say that "firstof" is equivalent to {%if bla %}{{varx}} for now, it's very missleading.

I can wrote a patch for the documentation if needed, just tell me if I have to open a new ticket.

comment:5 Changed 5 years ago by andrewbadr

  • milestone 1.1 deleted

I'm not big-boss-team, but this feature is definitely not making it into 1.1, so I'm removing the milestone. I think your concern about documentation is valid, but I would make a new ticket.

comment:6 Changed 4 years ago by andrewbadr

  • Owner changed from andrewbadr to nobody

comment:7 Changed 4 years ago by russellm

  • milestone set to 1.3

Promoting this to ensure it is looked at in 1.3

comment:8 Changed 3 years ago by SmileyChris

  • milestone 1.3 deleted
  • Severity set to Release blocker
  • Triage Stage changed from Accepted to Design decision needed
  • Type set to Bug

Guess it didn't get looked at in 1.3... I'm going to bump severity.

Since the accepted was 2 years ago, I'm also going to push back to a design decision.

comment:9 Changed 2 years ago by kenth

  • Easy pickings unset
  • Owner changed from nobody to kenth
  • Status changed from new to assigned
  • UI/UX unset

comment:10 Changed 2 years ago by kenth

  • Owner kenth deleted
  • Status changed from assigned to new

There are two template tags which generate output that is not autoescaped: firstof & cycle. This behavior might be unexpected, but it appears to be well documented even back in the version 1.0 documents. There is a policy statement in the documentation stating output is not escaped "... because template tags do not escape their content."

The code appears to work as documented & the documentation appears to be clear. As to if the behavior is as it should be, that's clearly a design decision...

comment:11 Changed 2 years ago by adrian

In [17176]:

Added a test that 'firstof' template filter doesn't auto-escape. cycle tag already has such a test (cycle20). Refs #10912

comment:12 Changed 2 years ago by adrian

In [17177]:

Tweaked templates/builtins.txt to make it clearer that cycle and firstof filters don't auto-escape. Refs #10912

comment:13 Changed 2 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

OK, I'm marking this as fixed because the documentation is clear about the fact that firstof and cycle don't auto-escape. I also added a test for firstof (we already had one for cycle).

Just for future reference, we added some original docs in [11163].

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.