Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion classes/Visualizer/Module/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note The title already comes from esc_html__(), and line 483 now wraps it in esc_attr() too. A translated title with an apostrophe or & could then show an entity such as &amp; as text in the tooltip. A gettext test showed title="Imprimer l&#039;&amp;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.

Copy link
Copy Markdown
Contributor Author

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 as l&#039;&amp; is the same in the markup before and after this change, as the gettext run shows. No change needed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note A visualizer_action_buttons callback can return a title that it has already entity-encoded, for example Save &amp; share. Line 483 now passes that title through esc_attr(). Check that the tooltip of such a filtered action still reads Save & share. Safe: one level of encoding in the markup. Broken: the tooltip shows &amp; as text.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reason as above: an already encoded Save &amp; share stays Save &amp; share in the attribute, so the tooltip reads Save & share. No change needed.


if ( 'copy' === $key ) {
$copy = $this->_getDataAs( $atts['id'], 'csv' );
Expand Down
69 changes: 69 additions & 0 deletions tests/test-frontend-actions.php
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&quot; onfocus=&quot;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&quot; data-x=&quot;1"', $markup );
$this->assertStringContainsString( 'title="Share&quot; onfocus=&quot;alert(1)"', $markup );
}
}
Loading