Skip to content

Commit 81eceff

Browse files
Comments: Escape the comment type label in comment_type().
The default branch of comment_type() echoed a registered type's singular_name label without escaping. Labels are developer-supplied (via register_comment_type() or the comment_type_labels_{$type} filter) and reach the public comment list unescaped, so wrap the output in esc_html() to match the post type and taxonomy label contract. Document that labels are stored unescaped and add a regression test asserting an HTML payload in the label is escaped.
1 parent 506e9c6 commit 81eceff

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎src/wp-includes/comment-template.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ function comment_type( $comment_text = false, $trackback_text = false, $pingback
12141214
$comment_type_object = $comment_text_overridden ? null : get_comment_type_object( $type );
12151215

12161216
if ( $comment_type_object && ! $comment_type_object->_builtin && isset( $comment_type_object->labels->singular_name ) ) {
1217-
echo $comment_type_object->labels->singular_name;
1217+
echo esc_html( $comment_type_object->labels->singular_name );
12181218
} else {
12191219
echo $comment_text;
12201220
}

‎src/wp-includes/comment.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,9 @@ function get_comment_type_labels( $comment_type_object ) {
556556
*
557557
* @see get_comment_type_labels() for the full list of comment type labels.
558558
*
559+
* Labels are stored unescaped, mirroring the post type and taxonomy label
560+
* contract; callers must escape them on output (for example with esc_html()).
561+
*
559562
* @param object $labels Object with labels for the comment type as member variables.
560563
*/
561564
$labels = apply_filters( "comment_type_labels_{$comment_type}", $labels );

‎tests/phpunit/tests/comment/commentType.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,25 @@ public function test_custom_text_override_wins_over_registered_label() {
114114

115115
$this->assertSame( 'Custom', $this->get_comment_type_output( 'foo', 'Custom' ) );
116116
}
117+
118+
/**
119+
* The registered label is escaped on output to guard against HTML/script injection.
120+
*
121+
* @ticket 35214
122+
*/
123+
public function test_registered_label_is_escaped_on_output() {
124+
register_comment_type(
125+
'foo',
126+
array(
127+
'labels' => array(
128+
'singular_name' => '<img src=x onerror=alert(1)>Foo',
129+
),
130+
)
131+
);
132+
133+
$this->assertSame(
134+
esc_html( '<img src=x onerror=alert(1)>Foo' ),
135+
$this->get_comment_type_output( 'foo' )
136+
);
137+
}
117138
}

0 commit comments

Comments
 (0)