Repository navigation
Escape the attributes of chart action buttons #1426
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
base: development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -475,9 +475,12 @@ public function renderChart( $atts ) { | |
| $key = $array[0]; | ||
| $mime = end( $array ); | ||
| } | ||
| if ( ! isset( $actions[ $key ] ) ) { | ||
| continue; | ||
| } | ||
| $label = $actions[ $key ]['label']; | ||
| $title = $actions[ $key ]['title']; | ||
| $actions_div .= sprintf( '<a href="#" class="visualizer-action visualizer-action-%s" data-visualizer-type="%s" data-visualizer-chart-id="%s" data-visualizer-container-id="%s" data-visualizer-mime="%s" title="%s"', $key, $key, $atts['id'], $id, $mime, $title ); | ||
| $actions_div .= sprintf( '<a href="#" class="visualizer-action visualizer-action-%s" data-visualizer-type="%s" data-visualizer-chart-id="%s" data-visualizer-container-id="%s" data-visualizer-mime="%s" title="%s"', esc_attr( $key ), esc_attr( $key ), esc_attr( $atts['id'] ), esc_attr( $id ), esc_attr( $mime ), esc_attr( $title ) ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same reason as above: an already encoded |
||
|
|
||
| if ( 'copy' === $key ) { | ||
| $copy = $this->_getDataAs( $atts['id'], 'csv' ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
| /** | ||
| * Chart action buttons tests. | ||
| * | ||
| * @package Visualizer | ||
| */ | ||
| class Test_Visualizer_Frontend_Actions extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Render the shortcode of a chart that has the given action buttons setting. | ||
| * | ||
| * @param string[] $actions Saved action buttons. | ||
| * | ||
| * @return string | ||
| */ | ||
| private function render_chart_with_actions( $actions ) { | ||
| $id = self::factory()->post->create( | ||
| array( | ||
| 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, | ||
| 'post_content' => serialize( array( array( 'a', 1 ) ) ), | ||
| ) | ||
| ); | ||
| update_post_meta( $id, Visualizer_Plugin::CF_CHART_TYPE, 'pie' ); | ||
| update_post_meta( $id, Visualizer_Plugin::CF_SERIES, array( array( 'label' => 'x', 'type' => 'string' ), array( 'label' => 'y', 'type' => 'number' ) ) ); | ||
| update_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, array( 'actions' => $actions ) ); | ||
|
|
||
| return do_shortcode( '[visualizer id="' . $id . '"]' ); | ||
| } | ||
|
|
||
| /** | ||
| * The part after the action name stays inside its attribute. | ||
| */ | ||
| public function test_action_mime_cannot_add_an_attribute() { | ||
| $markup = $this->render_chart_with_actions( array( 'print;x" onfocus="alert(1)' ) ); | ||
|
|
||
| $this->assertStringContainsString( 'data-visualizer-mime="x" onfocus="alert(1)"', $markup ); | ||
| } | ||
|
|
||
| /** | ||
| * An action that the plugin does not define gets no button. | ||
| */ | ||
| public function test_unknown_action_gets_no_button() { | ||
| $markup = $this->render_chart_with_actions( array( 'x" onfocus="alert(1)', 'csv;application/csv' ) ); | ||
|
|
||
| $this->assertStringNotContainsString( 'onfocus', $markup ); | ||
| $this->assertStringContainsString( 'data-visualizer-type="csv"', $markup ); | ||
| } | ||
|
|
||
| /** | ||
| * The key and the title of an action that a filter adds stay inside their attributes. | ||
| */ | ||
| public function test_filtered_action_key_and_title_cannot_add_an_attribute() { | ||
| add_filter( | ||
| 'visualizer_action_buttons', | ||
| function ( $actions ) { | ||
| $actions['share" data-x="1'] = array( | ||
| 'label' => 'Share', | ||
| 'title' => 'Share" onfocus="alert(1)', | ||
| ); | ||
| return $actions; | ||
| } | ||
| ); | ||
|
|
||
| $markup = $this->render_chart_with_actions( array( 'share" data-x="1' ) ); | ||
|
|
||
| $this->assertStringContainsString( 'data-visualizer-type="share" data-x="1"', $markup ); | ||
| $this->assertStringContainsString( 'title="Share" onfocus="alert(1)"', $markup ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
noteThe title already comes fromesc_html__(), and line 483 now wraps it inesc_attr()too. A translated title with an apostrophe or&could then show an entity such as&as text in the tooltip. A gettext test showedtitle="Imprimer l'&graphique"at both the base and this commit. Check one real translation with such a character. Safe: the tooltip shows the plain character. Broken: it shows the entity.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked:
esc_attr()does not encode an existing entity a second time (_wp_specialchars()runs with$double_encode = false), so a translated title such asl'&is the same in the markup before and after this change, as the gettext run shows. No change needed.