-
Notifications
You must be signed in to change notification settings - Fork 3.4k
wp_kses: add support for picture element and srcset attribute on img tags #6184
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
Open
adamsilverstein
wants to merge
30
commits into
WordPress:trunk
Choose a base branch
from
adamsilverstein:ticket/29807
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 17 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
2bf9709
attempt to bring 29807.8.diff up to date with trunk
adamsilverstein 11336a3
Merge remote-tracking branch 'upstream/trunk' into pr/6184
azaozz 05dcd69
Fix coding standards spaces and methods visibility
azaozz 499dc82
More CS empty space fixes.
azaozz 7a75eba
Merge branch 'trunk' into ticket/29807
adamsilverstein 69312e7
Apply suggestion from @azaozz
adamsilverstein 3c6defd
string in_array test
adamsilverstein c88673d
expand test slightly
adamsilverstein 022def5
Cleanup; move multi_uri to parameter
adamsilverstein 321e441
phpcbf
adamsilverstein c7c63dd
add sizes attribute to test
adamsilverstein e3c1684
enable sizes
adamsilverstein 3862627
Improve doc block
adamsilverstein 4cb049a
test clean up
adamsilverstein ca94908
Update tests/phpunit/tests/kses.php
adamsilverstein 49f1ba6
Update src/wp-includes/kses.php
adamsilverstein 2b5706c
remove unused $uris
adamsilverstein d116c8e
Add additional test cases.
adamsilverstein fbeed82
Merge branch 'trunk' into ticket/29807
adamsilverstein f9e002c
phpcbf
adamsilverstein 80521e4
Merge remote-tracking branch 'origin/trunk' into ticket/29807
adamsilverstein 0068aac
Add tests exposing srcset and img attribute bugs
adamsilverstein f11aeb2
Add decoding and fetchpriority to img allowed attrs
adamsilverstein cdb84fc
Fix srcset URI sanitization for URLs with commas
adamsilverstein 399aa30
Fix array double arrow alignment in kses tests
adamsilverstein e5488a6
Merge branch 'trunk' into ticket/29807
adamsilverstein 1f02276
Add kses srcset/picture tests, update @since tag
adamsilverstein 9bb1ebf
Fix array double arrow alignment in new tests
adamsilverstein 2bdc1f9
Merge branch 'trunk' into ticket/29807
adamsilverstein 61881bf
Merge branch 'trunk' into ticket/29807
adamsilverstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -206,8 +206,10 @@ | |
| 'longdesc' => true, | ||
| 'vspace' => true, | ||
| 'src' => true, | ||
| 'srcset' => true, | ||
| 'usemap' => true, | ||
| 'width' => true, | ||
| 'sizes' => true, | ||
| ), | ||
| 'ins' => array( | ||
| 'datetime' => true, | ||
|
|
@@ -250,6 +252,7 @@ | |
| 'p' => array( | ||
| 'align' => true, | ||
| ), | ||
| 'picture' => array(), | ||
| 'pre' => array( | ||
| 'width' => true, | ||
| ), | ||
|
|
@@ -270,6 +273,12 @@ | |
| 'align' => true, | ||
| ), | ||
| 'small' => array(), | ||
| 'source' => array( | ||
| 'srcset' => true, | ||
| 'type' => true, | ||
| 'media' => true, | ||
| 'sizes' => true, | ||
| ), | ||
| 'strike' => array(), | ||
| 'strong' => array(), | ||
| 'sub' => array(), | ||
|
|
@@ -768,7 +777,6 @@ function wp_kses( $content, $allowed_html, $allowed_protocols = array() ) { | |
| * @return string Filtered attribute. | ||
| */ | ||
| function wp_kses_one_attr( $attr, $element ) { | ||
| $uris = wp_kses_uri_attributes(); | ||
| $allowed_html = wp_kses_allowed_html( 'post' ); | ||
| $allowed_protocols = wp_allowed_protocols(); | ||
| $attr = wp_kses_no_null( $attr, array( 'slash_zero' => 'keep' ) ); | ||
|
|
@@ -812,10 +820,7 @@ 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 ); | ||
| } | ||
| $value = wp_kses_sanitize_uris( $name, $value, $allowed_protocols ); | ||
|
|
||
| $attr = "$name=$quote$value$quote"; | ||
| $vless = 'n'; | ||
|
|
@@ -1034,6 +1039,7 @@ function wp_kses_uri_attributes() { | |
| 'src', | ||
| 'usemap', | ||
| 'xmlns', | ||
| 'srcset', | ||
| ); | ||
|
|
||
| /** | ||
|
|
@@ -1394,7 +1400,6 @@ function wp_kses_hair( $attr, $allowed_protocols ) { | |
| $attrarr = array(); | ||
| $mode = 0; | ||
| $attrname = ''; | ||
| $uris = wp_kses_uri_attributes(); | ||
|
|
||
| // Loop through the whole attribute list. | ||
|
|
||
|
|
@@ -1442,9 +1447,9 @@ function wp_kses_hair( $attr, $allowed_protocols ) { | |
| if ( preg_match( '%^"([^"]*)"(\s+|/?$)%', $attr, $match ) ) { | ||
| // "value" | ||
| $thisval = $match[1]; | ||
| if ( in_array( strtolower( $attrname ), $uris, true ) ) { | ||
| $thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols ); | ||
| } | ||
|
|
||
| // Sanitize URI values. | ||
| $thisval = wp_kses_sanitize_uris( $attrname, $thisval, $allowed_protocols ); | ||
|
|
||
| if ( false === array_key_exists( $attrname, $attrarr ) ) { | ||
| $attrarr[ $attrname ] = array( | ||
|
|
@@ -1464,9 +1469,8 @@ function wp_kses_hair( $attr, $allowed_protocols ) { | |
| if ( preg_match( "%^'([^']*)'(\s+|/?$)%", $attr, $match ) ) { | ||
| // 'value' | ||
| $thisval = $match[1]; | ||
| if ( in_array( strtolower( $attrname ), $uris, true ) ) { | ||
| $thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols ); | ||
| } | ||
| // Sanitize URI values. | ||
| $thisval = wp_kses_sanitize_uris( $attrname, $thisval, $allowed_protocols ); | ||
|
|
||
| if ( false === array_key_exists( $attrname, $attrarr ) ) { | ||
| $attrarr[ $attrname ] = array( | ||
|
|
@@ -1486,9 +1490,8 @@ function wp_kses_hair( $attr, $allowed_protocols ) { | |
| if ( preg_match( "%^([^\s\"']+)(\s+|/?$)%", $attr, $match ) ) { | ||
| // value | ||
| $thisval = $match[1]; | ||
| if ( in_array( strtolower( $attrname ), $uris, true ) ) { | ||
| $thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols ); | ||
| } | ||
| // Sanitize URI values. | ||
| $thisval = wp_kses_sanitize_uris( $attrname, $thisval, $allowed_protocols ); | ||
|
|
||
| if ( false === array_key_exists( $attrname, $attrarr ) ) { | ||
| $attrarr[ $attrname ] = array( | ||
|
|
@@ -1530,6 +1533,42 @@ function wp_kses_hair( $attr, $allowed_protocols ) { | |
| return $attrarr; | ||
| } | ||
|
|
||
| /** | ||
| * Sanitizes URI values in HTML attributes. | ||
| * | ||
| * This function centralizes logic for cleaning attribute values that are expected to contain URLs. | ||
| * It checks if the attribute name is one that should contain a URI (e.g., 'href', 'src', 'srcset'). | ||
| * For attributes that can contain multiple URIs (such as 'srcset'), it splits the value and sanitizes each URI individually. | ||
| * All URI values are passed through {@see wp_kses_bad_protocol()} to remove disallowed protocols (e.g., 'javascript:'). | ||
| * | ||
| * @since 6.9.0 | ||
| * | ||
| * @param string $attrname The attribute name to test. | ||
| * @param string $attrvalue The attribute value to sanitize. | ||
| * @param string[] $allowed_protocols Array of allowed URL protocols. | ||
| * @param string[] $multi_uri Optional. Attributes that can contain multiple URIs. Default is array( 'srcset' ). | ||
| * @return string Sanitized attribute value. | ||
| */ | ||
| function wp_kses_sanitize_uris( $attrname, $attrvalue, $allowed_protocols, $multi_uri = array( 'srcset' ) ) { | ||
|
Member
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. I think these can add PHP type hints as well for the params and the return value. |
||
| $uris = wp_kses_uri_attributes(); | ||
|
|
||
| if ( ! in_array( strtolower( $attrname ), $uris, true ) ) { | ||
| return $attrvalue; | ||
| } else { | ||
| if ( in_array( strtolower( $attrname ), $multi_uri, true ) ) { | ||
| $thesevals = preg_split( '/\s*,\s*/', $attrvalue ); | ||
| } else { | ||
| $thesevals = array( $attrvalue ); | ||
| } | ||
|
adamsilverstein marked this conversation as resolved.
Outdated
adamsilverstein marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| foreach ( (array) $thesevals as $key => $val ) { | ||
| $thesevals[ $key ] = wp_kses_bad_protocol( $val, $allowed_protocols ); | ||
| } | ||
|
|
||
| return implode( ', ', $thesevals ); | ||
| } | ||
|
|
||
| /** | ||
| * Finds all attributes of an HTML element. | ||
| * | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.