Bug Tracker

Modify

Ticket #7199 (closed bug: duplicate)

Opened 3 years ago

Last modified 15 months ago

jQuery templating plug-in incorrect HTML encoding

Reported by: anonymous Owned by: BorisMoore
Priority: low Milestone:
Component: templates Version: 1.4.3
Keywords: ampersand templating Cc:
Blocking: Blocked by:

Description

templating plugin doesn't encode '&' character. When I have following text in data object: ' ', templating plug-in renders whitespace instead of this string.

jquery.tmpl.js contains following comment in 'encode' function: Do HTML encoding replacing < > & and ' and " by corresponding entities. But no manipulation with '&' character exists: return ("" + text).split("<").join("&lt;").split(">").join("&gt;").split('"').join("&#34;").split("'").join("&#39;");

Change History

comment:1 Changed 3 years ago by snover

  • Priority changed from undecided to blocker
  • Status changed from new to open
  • Component changed from unfiled to templates

comment:2 follow-up: ↓ 3 Changed 3 years ago by addyosmani

  • Keywords ampersand templating added
  • Priority changed from blocker to low
  • Status changed from open to closed
  • Resolution set to worksforme

&nbsp; is a non-breaking space (html entity). If you're trying to get the ampersand character to work, rather than including it in a reference to &nbsp;, simply try using &amp; instead and it should render fine.

If otherwise you are actually trying to render the complete string &nbsp; to be interpreted literally, you could probably do something like &amp;nbsp; instead.

comment:3 in reply to: ↑ 2 Changed 3 years ago by anonymous

Replying to addyosmani:

&nbsp; is a non-breaking space (html entity). If you're trying to get the ampersand character to work, rather than including it in a reference to &nbsp;, simply try using &amp; instead and it should render fine.

If otherwise you are actually trying to render the complete string &nbsp; to be interpreted literally, you could probably do something like &amp;nbsp; instead.

As I understand only {{html}} template tag should render unencoded HTML text. ${} tag should escape values. As you described, &nbsp; is HTML entity and it should be encoded into plain text. All '&' characters should be encoded in this plug-in, not on server side. Otherwise ${} will render "half-encoded" text (without tags but with HTML entities). To prevent XSS attacks all of these characters should be escaped: ", ', <, >, &.

comment:4 Changed 3 years ago by BorisMoore

  • Status changed from closed to reopened
  • Resolution worksforme deleted
  • Milestone 1.4.4 deleted

comment:5 Changed 3 years ago by BorisMoore

  • Owner set to BorisMoore
  • Status changed from reopened to assigned

I'll look at this.

comment:6 Changed 2 years ago by jonathan.protzenko@…

I've uploaded a testcase (reduced) at  http://jonathan.protzenko.free.fr/jquery-tmpl/t.xhtml .

The thing is, simply using ${t} within the template when t = "&" makes the ultimate .innerHTML call fail, because ampersands simply are not encoded properly. When the document happens to be XML, this is fatal. This is especially painful for XML+XHTML documents, because that makes the whole thing a syntax error, and the templating fails.

As a side note, I have a very good reason for using strict XML, so switching to a non-XML document is not a solution.

comment:7 Changed 2 years ago by BorisMoore

  • Status changed from assigned to closed
  • Resolution set to duplicate

Resolving as dup of the new issue I created for this here:  https://github.com/jquery/jquery-tmpl/issues/79 We will be using the Issue tracker for jQuery Template bugs...

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


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

 
Note: See TracTickets for help on using tickets.