Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
aa98465
KSES: Add wp_kses_sanitize_srcset()
adamsilverstein Oct 24, 2026
5bcfdcb
KSES: Sanitize srcset values by element and attribute
adamsilverstein Oct 24, 2026
6090c9f
KSES: Allow picture, source and responsive img attributes
adamsilverstein Oct 24, 2026
77ce137
Tests: Add native types to the wp_kses_sanitize_srcset() tests.
adamsilverstein Oct 25, 2026
144732e
KSES: Keep commas inside srcset descriptor parentheses.
adamsilverstein Oct 25, 2026
b04ba88
KSES: Always normalize the output of wp_kses_sanitize_srcset().
adamsilverstein Oct 28, 2026
9e9da82
Merge branch 'trunk' into ticket/29807
adamsilverstein Nov 8, 2026
6e53fc0
Merge branch 'trunk' into ticket/29807-kses-wiring
adamsilverstein Nov 8, 2026
961da7d
Merge branch 'trunk' into ticket/29807-kses-wiring
adamsilverstein Nov 21, 2026
02cbf44
Merge branch 'trunk' into ticket/29807
adamsilverstein Nov 21, 2026
1917c94
Merge branch 'trunk' into ticket/29807-srcset-parser
adamsilverstein Nov 21, 2026
e74032e
KSES: Clarify when wp_kses_sanitize_srcset() removes a candidate
adamsilverstein Nov 29, 2026
ffa3674
Merge branch 'ticket/29807-srcset-parser' into ticket/29807-kses-wiring
adamsilverstein Nov 29, 2026
080add4
Merge branch 'ticket/29807-kses-wiring' into ticket/29807
adamsilverstein Nov 29, 2026
0281cfe
Merge branch 'ticket/29807-kses-wiring' into ticket/29807
adamsilverstein Nov 29, 2026
e763468
Tests: Add native types to the srcset wiring tests
adamsilverstein Nov 29, 2026
0e624a7
Tests: Add native types to the responsive image markup tests
adamsilverstein Nov 29, 2026
afa51d9
Tests: Set the srcset test allowances without nested writes
adamsilverstein Nov 29, 2026
4096c9c
Merge branch 'ticket/29807-kses-wiring' into ticket/29807
adamsilverstein Nov 29, 2026
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
199 changes: 176 additions & 23 deletions src/wp-includes/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,21 @@
),
'i' => array(),
'img' => array(
'alt' => true,
'align' => true,
'border' => true,
'height' => true,
'hspace' => true,
'loading' => true,
'longdesc' => true,
'vspace' => true,
'src' => true,
'usemap' => true,
'width' => true,
'align' => true,
'alt' => true,
'border' => true,
'decoding' => true,
'fetchpriority' => true,
'height' => true,
'hspace' => true,
'loading' => true,
'longdesc' => true,
'sizes' => true,
'src' => true,
'srcset' => true,
'usemap' => true,
'vspace' => true,
'width' => true,
),
'ins' => array(
'datetime' => true,
Expand Down Expand Up @@ -287,6 +291,7 @@
'p' => array(
'align' => true,
),
'picture' => array(),
'pre' => array(
'width' => true,
),
Expand All @@ -312,6 +317,15 @@
'align' => true,
),
'small' => array(),
'source' => array(
'height' => true,
'media' => true,
'sizes' => true,
'src' => true,
'srcset' => true,
'type' => true,
'width' => true,
),
'strike' => array(),
'strong' => array(),
'sub' => array(),
Expand Down Expand Up @@ -1887,10 +1901,7 @@ public function sanitize() {
}
}

$is_url_ish = in_array( strtolower( $name ), $this->uri_attributes, true );
if ( $is_url_ish ) {
$value = wp_kses_bad_protocol( $value, $this->allowed_protocols );
}
$value = _wp_kses_sanitize_url_attribute( $tag_name, strtolower( $name ), $value, $this->allowed_protocols, $this->uri_attributes );

/*
* Process the remaining attributes according to their policies.
Expand Down Expand Up @@ -2106,9 +2117,14 @@ function wp_kses_one_attr( $attr, $element ) {
// Sanitize quotes, angle braces, and entities.
$value = esc_attr( $value );

// Sanitize URI values.
if ( in_array( strtolower( $name ), $uris, true ) ) {
$value = wp_kses_bad_protocol( $value, $allowed_protocols );
/*
* Sanitize URL values. The value is already escaped, so it is decoded
* for sanitization and re-encoded only when sanitization changed it.
*/
$decoded = WP_HTML_Decoder::decode_attribute( $value );
$sanitized = _wp_kses_sanitize_url_attribute( strtolower( $element ), strtolower( $name ), $decoded, $allowed_protocols, $uris );
if ( $sanitized !== $decoded ) {
$value = _wp_specialchars( $sanitized, ENT_QUOTES, false, true );
}

$attr = "$name=$quote$value$quote";
Expand Down Expand Up @@ -2428,6 +2444,50 @@ function wp_kses_uri_attributes() {
return $uri_attributes;
}

/**
* Sanitizes the value of an attribute that a browser reads as a URL or a list of URLs.
*
* Most URL attributes hold a single URL and are listed by {@see wp_kses_uri_attributes()}.
* A few hold a list whose syntax depends on the element they appear on: `srcset` on
* `img` and `source`, and `imagesrcset` on `link`, hold a comma-separated list of
* image candidate strings and are handled by {@see wp_kses_sanitize_srcset()}. On any
* other element a browser ignores those attributes, so they are left alone.
*
* Takes and returns decoded attribute text.
*
* @since 7.2.0
* @access private
*
* @param string $element Lowercase element name, or an empty string when unknown.
* @param string $attribute Lowercase attribute name.
* @param string $value Decoded attribute value.
* @param string[] $allowed_protocols Array of allowed URL protocols.
* @param string[] $uri_attributes Names of attributes holding a single URL,
* as returned by wp_kses_uri_attributes().
* @return string Sanitized value.
*/
function _wp_kses_sanitize_url_attribute( string $element, string $attribute, string $value, array $allowed_protocols, array $uri_attributes ): string {
switch ( $attribute ) {
case 'srcset':
if ( '' === $element || 'img' === $element || 'source' === $element ) {
return wp_kses_sanitize_srcset( $value, $allowed_protocols );
}
break;

case 'imagesrcset':
if ( '' === $element || 'link' === $element ) {
return wp_kses_sanitize_srcset( $value, $allowed_protocols );
}
break;
}

if ( in_array( $attribute, $uri_attributes, true ) ) {
return wp_kses_bad_protocol( $value, $allowed_protocols );
}

return $value;
}

/**
* Callback for `wp_kses_split()`.
*
Expand Down Expand Up @@ -2617,7 +2677,7 @@ function wp_kses_attr( $element, $attr, $allowed_html, $allowed_protocols ) {
}

// Split it.
$attrarr = wp_kses_hair( $attr, $allowed_protocols );
$attrarr = wp_kses_hair( $attr, $allowed_protocols, $element );

// Check if there are attributes that are required.
$required_attrs = array_filter(
Expand Down Expand Up @@ -2774,7 +2834,7 @@ function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowe
* This function performs a number of transformations while parsing attribute strings:
* - It normalizes attribute values and surrounds them with double quotes.
* - It normalizes HTML character references inside attribute values.
* - It removes “bad” URL protocols from attribute values.
* - It removes “bad” URL protocols from attribute values, including each URL in a `srcset`.
*
* Otherwise this reads the attributes as if they were part of an HTML tag. It performs
* these transformations to lower the risk of mis-parsing down the line and to perform
Expand All @@ -2795,14 +2855,18 @@ function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowe
*
* @since 1.0.0
* @since 7.0.0 Reliably parses HTML via the HTML API.
* @since 7.2.0 Added the optional `$element` parameter.
*
* @param string $attr Attribute list from HTML element to closing HTML element tag.
* @param string[] $allowed_protocols Array of allowed URL protocols.
* @param string $element Optional. Name of the element the attributes belong to, which
* decides how URL attributes such as `srcset` are read. Default ''.
* @return array<string, array{name: string, value: string, whole: string, vless: 'y'|'n'}> Array of attribute information after parsing.
*/
function wp_kses_hair( $attr, $allowed_protocols ) {
function wp_kses_hair( $attr, $allowed_protocols, $element = '' ) {
$attributes = array();
$uris = wp_kses_uri_attributes();
$element = strtolower( (string) $element );

$processor = new WP_HTML_Tag_Processor( "<wp {$attr}>" );
$processor->next_token();
Expand All @@ -2823,8 +2887,8 @@ function wp_kses_hair( $attr, $allowed_protocols ) {
foreach ( $attribute_names as $name ) {
$value = $processor->get_attribute( $name );
$is_bool = true === $value;
if ( is_string( $value ) && in_array( $name, $uris, true ) ) {
$value = wp_kses_bad_protocol( $value, $allowed_protocols );
if ( is_string( $value ) ) {
$value = _wp_kses_sanitize_url_attribute( $element, $name, $value, $allowed_protocols, $uris );
}

// Reconstruct and normalize the attribute value.
Expand Down Expand Up @@ -3228,6 +3292,95 @@ function wp_kses_bad_protocol_once2( $scheme, $allowed_protocols ) {
}
}

/**
* Removes image candidates with a disallowed URL protocol from a `srcset` value.
*
* A `srcset` attribute holds a comma-separated list of image candidate strings,
* each a URL optionally followed by whitespace and width or density descriptors.
* The value is split into candidates following the HTML specification's srcset
* parsing algorithm.
*
* Each URL is checked with {@see wp_kses_bad_protocol()}, and if that sanitization
* would produce a different URL, it is rejected and its entire image candidate
* string is removed from the `srcset`. As in {@see esc_url()}, a URL starting with
* `/` cannot carry a scheme and is kept as is. Descriptors are not validated.
*
* The remaining candidates are rejoined with `, `, so whitespace around them and
* empty candidates do not survive into the result.
*
* Example:
*
* // A candidate with a disallowed protocol is removed.
* 'a.jpg 1x, c.jpg 3x' === wp_kses_sanitize_srcset( 'a.jpg 1x, javascript:alert(1) 2x, c.jpg 3x', $protocols );
*
* // A comma inside a URL is part of the URL.
* 'https://cdn.example/w=400,q=80/a.jpg 400w' === wp_kses_sanitize_srcset( 'https://cdn.example/w=400,q=80/a.jpg 400w', $protocols );
*
* // A comma inside parentheses in the descriptors is part of the descriptors.
* 'a.jpg 1x(,), b.jpg 2x' === wp_kses_sanitize_srcset( 'a.jpg 1x(,),b.jpg 2x', $protocols );
*
* // Separators are normalized.
* 'a.jpg 1x, b.jpg 2x' === wp_kses_sanitize_srcset( " a.jpg 1x ,,\n b.jpg 2x ", $protocols );
*
* The value must be decoded attribute text, as returned by
* {@see WP_HTML_Tag_Processor::get_attribute()}, not raw HTML containing character
* references. The returned value is decoded text too; callers writing it back into
* HTML must escape it.
*
* @since 7.2.0
*
* @link https://html.spec.whatwg.org/multipage/images.html#parsing-a-srcset-attribute
*
* @param string $srcset Decoded `srcset` attribute value.
* @param string[] $allowed_protocols Array of allowed URL protocols.
* @return string The value without candidates whose URL uses a disallowed protocol.
*/
function wp_kses_sanitize_srcset( string $srcset, array $allowed_protocols ): string {
$whitespace = " \t\f\r\n";
$length = strlen( $srcset );
$at = 0;
$kept = array();

while ( $at < $length ) {
$at += strspn( $srcset, "{$whitespace},", $at );
if ( $at >= $length ) {
break;
}

// The URL is the run of non-whitespace characters, less any commas ending it.
$candidate_at = $at;
$url_length = strcspn( $srcset, $whitespace, $at );
$url = rtrim( substr( $srcset, $at, $url_length ), ',' );
$at += $url_length;

/*
* A URL ended by a comma has no descriptors. Otherwise they run to the next
* comma outside parentheses, or to the end when a parenthesis is left open.
*/
if ( strlen( $url ) === $url_length ) {
while ( $at < $length ) {
$at += strcspn( $srcset, ',(', $at );
if ( $at >= $length || ',' === $srcset[ $at ] ) {
break;
}

$closing_at = strpos( $srcset, ')', $at );
$at = false === $closing_at ? $length : $closing_at + 1;
}
$candidate = substr( $srcset, $candidate_at, $at - $candidate_at );
} else {
$candidate = $url;
}

// The comparison ignores case because wp_kses_bad_protocol() lowercases an allowed scheme.
if ( '/' === $url[0] || 0 === strcasecmp( wp_kses_bad_protocol( $url, $allowed_protocols ), $url ) ) {
$kept[] = rtrim( $candidate, $whitespace );
}
}

return implode( ', ', $kept );
}

/**
* Converts and fixes HTML entities.
*
Expand Down
Loading
Loading