-
Notifications
You must be signed in to change notification settings - Fork 9
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
How to handle unescaped output in widgets? #40
Comments
That's a really good question 🤔
Or maybe it'd be fine, as long as it covers 90% of the use cases, and the other 10% can use |
I was thinking about this the other day.. What if there was a null-escaping core function like this: function unescaped_html( $html ) {
return $html;
} Then you could use it as syntactic sugar to indicate that you're intentionally outputting raw HTML: public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
echo unescaped_html( $before_widget );
if ( ! empty( $title ) ) {
echo unescaped_html( $before_title . $title . $after_title );
}
echo esc_html__( 'Hello, World!', 'text_domain' );
echo unescaped_html( $after_widget );
} The intention would be very clear to a human code reviewer, and it's super easy to allow for in phpcs rules. |
🤔 Yeah, I like that better than What do you think about naming it something like |
Yeah hard to say on the naming. It's not always inherently dangerous as you say. If I'm echoing the result of a core function that is designed to produce markup (like A related thing that's become clear in investigating output escaping is that the docs rarely say what kind of escaping is needed for core functions. It's not documented in code and it's not in devhub. Should I escape the output of |
Forgot to add: the other difficulty besides naming is back compat. If it's a new API function then we'd probably need to backport it a few releases in order to get plugin devs to adopt it. |
Agreed. One helpful thing is that WPCS has done a lot of work to identify functions that don't need to be escaped, and then just assumes that everything else does:
That could help, but Is it possible to do that accurately? Couldn't e.g., Related #49 |
There is some "standard code" in (old-style) WordPress widgets (see the guide on .org):
Of course there are some lines with unescaped output, what to be done about those? It can really be anything, it's the theme that sets the
before*
andafter*
. The$title
c/should beesc_html
ed but the rest?The .org docs need to be updated as well / or more concrete advice given. One way would be to do a
wp_kses( $before_widget, 'post' )
but is it enough? Should there be awp_kses( $before_widget, 'widgetarea' )
?The text was updated successfully, but these errors were encountered: