Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 6 months ago

#30920 closed enhancement (invalid)

Add support for JavaScript templates (Underscore) to wp_kses()

Reported by: stevegrunwell's profile stevegrunwell Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.2
Component: Formatting Keywords: kses close
Focuses: template Cc:

Description

When working with Backbone/Underscore templates, wp_kses() will mangle placeholders like <%, <%=, and <%-.

Example:

$string = '<div id="post-<%- ID %>"><%= title %></div>';
$allowed_tags = array(
  'div' => array(
    'id' => true
  )
);
$result = wp_kses( $string, $allowed_tags );

My expected result would be the same as $string:

<div id="post-<%- ID %>"><%= title %></div>

However, the actual result is mangled:

&lt;div id=&quot;post-"&gt;</div>

Use-case for this enhancement:

Ajax-powered widgets and blocks (including those used within WordPress core) that want to offer filters for developers to use while safely escaping the potentially-filtered HTML of the templates before printing them to the screen.

Attachments (1)

30920.patch (2.4 KB) - added by stevegrunwell 10 years ago.
Adds new wp_kses_template() function (separate function since this is only needed in certain circumstances) that will automatically replace Underscore template tags with placeholders before running wp_kses(), then restores them afterwards.

Download all attachments as: .zip

Change History (10)

@stevegrunwell
10 years ago

Adds new wp_kses_template() function (separate function since this is only needed in certain circumstances) that will automatically replace Underscore template tags with placeholders before running wp_kses(), then restores them afterwards.

#1 @SergeyBiryukov
10 years ago

  • Component changed from Themes to Formatting

#2 @dd32
10 years ago

I'm not sure I see the need for kses to handle such data myself.

Kses is designed to be used to strip invalid/unsafe HTML code from a user-supplied string, and does a pretty good job at that.

Any templates should be hard-coded by a developer, either within their plugin, or allowing another plugin to filter the HTML, there's no real need to kses the HTML in those cases as it's not coming from an untrusted user.

Kses is an expensive function call, it should be called as rarely as possible, for WordPress posts that means it's called on save, and shouldn't be called on display.

#3 @stevegrunwell
10 years ago

  • Keywords has-patch added

@dd32, that's a very good point - the instances where JavaScript templates should be user provided (without at least requiring some extra work on the part of the developer) is slim to nil (hence why I wrote this as a separate function rather than build it into the main wp_kses()). Even if third-party plugins are able to filter templates and thus potentially make them evil before they're displayed, that would be the result of a third-party plugin that the user has installed on his/her WordPress site.

The behavior of wp_kses() still seems broken when it's handed an Underscore template, however. Do you think a simple str_replace() for Underscore template tags (<%, <%=, <%-, and %>, probably including trailing/leading spaces on opening/closing tags, respectively, but I would need to confirm with the Underscore parser rules on spacing) would suffice? Using the example in the original ticket, the expected output would then be a harmless <div id="post-ID">title</div>, which should prevent any parsers from breaking.

#4 @miqrogroove
10 years ago

I agree with dd32. wp_kses is working as intended in this situation.

#5 @dd32
10 years ago

The behavior of wp_kses() still seems broken when it's handed an Underscore template, however.

In my opinion it's working as intended, as Underscore template tags are not HTML, it's strippping/sanitizing unexpected/malfomed HTML from the string.

#6 @stevegrunwell
10 years ago

With the impending release of the JSON REST API, there will likely need to be more attention paid to providing a proper template sanitization method for JavaScript templates. If KSES isn't the route, that's fine - I have faith in the core team's direction :).

#7 @miqrogroove
10 years ago

  • Keywords kses close added; has-patch removed

#8 @nacin
10 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Aside from a separation of concerns issue that's been hashed out here already, the other issue is that Underscore templates are, by their very nature, JavaScript. They're insecure. Allowing them to be included would allow JavaScript to be executed in potentially unintended ways.

#9 @swissspidy
6 months ago

#62025 was marked as a duplicate.

Note: See TracTickets for help on using tickets.