-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Extend allowed attributes for non-admin users #9954
Closed
Closed
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e9e1784
Extend allowed attributes for non-admin users
notnownikki 7ae5b80
Remove include
notnownikki 50fba05
Exclude cover image until it's fixed
notnownikki 9904297
Remove attempt to fix cover image
notnownikki 576a433
lint
notnownikki 10febd9
Lint
notnownikki 2d6a3d7
Ignore lint rule because we need to access tagName
notnownikki 6bbf37a
KSES: Safely assign tag attributes
aduth 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
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
/** | ||
* KSES related functions for the Gutenberg plugin. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Adds attributes to kses allowed tags that aren't in the default list | ||
* and that Gutenberg needs to save blocks such as the Gallery block. | ||
* | ||
* @param array $tags Allowed HTML. | ||
* @return array (Maybe) modified allowed HTML. | ||
*/ | ||
function gutenberg_kses_allowedtags( $tags ) { | ||
$tags['a']['download'] = true; | ||
$tags['img']['data-link'] = true; | ||
$tags['img']['data-id'] = true; | ||
$tags['div']['style'] = true; | ||
$tags['div']['aria-hidden'] = true; | ||
return $tags; | ||
} | ||
|
||
add_filter( 'wp_kses_allowed_html', 'gutenberg_kses_allowedtags', 10, 2 ); |
This file contains 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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
/** | ||
* Tests that serialized HTML is not altered by KSES. | ||
* | ||
* @package Gutenberg | ||
*/ | ||
|
||
require_once dirname( __FILE__ ) . '/../lib/kses.php'; | ||
|
||
class KSES_Test extends WP_UnitTestCase { | ||
protected static $fixtures_dir; | ||
|
||
/** | ||
* Generates a normalized array of tag names and attributes. | ||
* Takes into account how kses alters CSS. | ||
* | ||
* @param string $html HTML. | ||
* @return array Normalized list of tags and attributes. | ||
*/ | ||
function get_normalized_dom( $html ) { | ||
$normalized = array(); | ||
$document = new DOMDocument(); | ||
libxml_use_internal_errors( true ); // Avoid errors with HTML5 elements. | ||
$document->loadHTML( $html ); | ||
$node_list = $document->getElementsByTagName( '*' ); | ||
foreach ( $node_list as $node ) { | ||
$node_repr = array( | ||
'tag_name' => $node->tagName, // @codingStandardsIgnoreLine WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar | ||
'attributes' => array(), | ||
); | ||
foreach ( $node->attributes as $attr ) { | ||
$name = $attr->name; | ||
$value = $attr->value; | ||
|
||
// Normalise css. | ||
if ( 'style' === $name && ';' === substr( $value, -1 ) ) { | ||
$value = substr( $value, 0, -1 ); | ||
} | ||
|
||
$node_repr['attributes'][ $name ] = $value; | ||
} | ||
$normalized[] = $node_repr; | ||
} | ||
libxml_clear_errors(); | ||
return $normalized; | ||
} | ||
|
||
function assert_html_is_equivalent( $expected_html, $actual_html, $message ) { | ||
$expected = $this->get_normalized_dom( $expected_html ); | ||
$actual = $this->get_normalized_dom( $actual_html ); | ||
$this->assertEquals( $expected, $actual, $message ); | ||
} | ||
|
||
function kses_test_filenames() { | ||
self::$fixtures_dir = dirname( dirname( __FILE__ ) ) . '/test/integration/full-content/fixtures'; | ||
|
||
// Exclude fixtures that would require admin to save, | ||
// and so wouldn't go through kses. | ||
$fixture_files = array_filter( | ||
glob( self::$fixtures_dir . '/*.serialized.html' ), | ||
array( $this, 'is_saved_through_kses' ) | ||
); | ||
|
||
return array_map( | ||
array( $this, 'filename_to_args' ), | ||
$fixture_files | ||
); | ||
} | ||
|
||
function is_saved_through_kses( $filename ) { | ||
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. The function name is not very obvious to its purpose. Doesn't our PHPCS setup force function documentation? Might be good in this instance. |
||
$admin_only = array( | ||
// Freeform HTML. | ||
'core__html.serialized.html', | ||
// Currently broken for non-admin users, https://github.com/WordPress/gutenberg/issues/2539 . | ||
'core__cover-image.serialized.html', | ||
); | ||
|
||
foreach ( $admin_only as $excluded ) { | ||
if ( substr( $filename, -strlen( $excluded ) ) === $excluded ) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function filename_to_args( $filename ) { | ||
return array( $filename ); | ||
} | ||
|
||
function strip_r( $input ) { | ||
return str_replace( "\r", '', $input ); | ||
} | ||
|
||
/** | ||
* @dataProvider kses_test_filenames | ||
*/ | ||
function test_kses_does_not_alter_output( $html_path ) { | ||
$html = self::strip_r( file_get_contents( $html_path ) ); | ||
$kses_html = wp_unslash( wp_filter_post_kses( $html ) ); | ||
|
||
$this->assert_html_is_equivalent( | ||
$html, | ||
$kses_html, | ||
"File '$html_path' was altered by kses" | ||
); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something the previous implementation had accounted for which this does not is if a developer calls to
unset( $tags['a'] );
in their own filter handler before this one is called. Here, I think an error may occur.