Skip to content

Commit 8a463d8

Browse files
committed
Prevent wp_targeted_link_rel() from corrupting JSON in amp_validation_error term_description
1 parent 956a391 commit 8a463d8

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

includes/validation/class-amp-validated-url-post-type.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -653,9 +653,14 @@ public static function store_validation_errors( $validation_errors, $url, $args
653653
$stored_validation_errors = array();
654654

655655
// Prevent Kses from corrupting JSON in description.
656-
$has_pre_term_description_filter = has_filter( 'pre_term_description', 'wp_filter_kses' );
657-
if ( false !== $has_pre_term_description_filter ) {
658-
remove_filter( 'pre_term_description', 'wp_filter_kses', $has_pre_term_description_filter );
656+
$pre_term_description_filters = array(
657+
'wp_filter_kses' => has_filter( 'pre_term_description', 'wp_filter_kses' ),
658+
'wp_targeted_link_rel' => has_filter( 'pre_term_description', 'wp_targeted_link_rel' ),
659+
);
660+
foreach ( $pre_term_description_filters as $callback => $priority ) {
661+
if ( false !== $priority ) {
662+
remove_filter( 'pre_term_description', $callback, $priority );
663+
}
659664
}
660665

661666
$terms = array();
@@ -713,8 +718,10 @@ public static function store_validation_errors( $validation_errors, $url, $args
713718
}
714719

715720
// Finish preventing Kses from corrupting JSON in description.
716-
if ( false !== $has_pre_term_description_filter ) {
717-
add_filter( 'pre_term_description', 'wp_filter_kses', $has_pre_term_description_filter );
721+
foreach ( $pre_term_description_filters as $callback => $priority ) {
722+
if ( false !== $priority ) {
723+
add_filter( 'pre_term_description', $callback, $priority );
724+
}
718725
}
719726

720727
$post_content = wp_json_encode( $stored_validation_errors );

tests/validation/test-class-amp-validated-url-post-type.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,16 @@ function( $sanitized, $error ) {
375375
),
376376
),
377377
),
378+
array(
379+
'code' => 'rejected',
380+
'evil' => '<script>document.write( \'<a href="#" target="_blank" rel="noopener noreferrer">test</a>\' );</script>', // Test protection against wp_targeted_link_rel JSON corruption.
381+
'sources' => array(
382+
array(
383+
'type' => 'theme',
384+
'name' => 'twentyseventeen',
385+
),
386+
),
387+
),
378388
array(
379389
'code' => 'new',
380390
'sources' => array(
@@ -451,6 +461,7 @@ function( $stored_error ) {
451461
$error_groups = array(
452462
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_ACCEPTED_STATUS,
453463
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_REJECTED_STATUS,
464+
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_REJECTED_STATUS,
454465
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_ACCEPTED_STATUS,
455466
);
456467

0 commit comments

Comments
 (0)