Repository navigation
Fix: Allow text fragments in kses for Trac ticket #60347 #7890
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
080885e
33105b2
f9ddc00
57777e1
8032d86
067c31d
17a0b10
9a01f29
3b3ad66
374f911
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2910,4 +2910,122 @@ public function data_allowed_attributes_in_descriptions() { | |
| ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests sanitization of text fragment URLs and ordinary anchors. | ||
| * | ||
| * @ticket 60347 | ||
| * @dataProvider data_wp_kses_text_fragments | ||
| * | ||
| * @param string $url URL to sanitize. | ||
| * @param string $expected Expected sanitized URL. | ||
| * @param string[] $allowed_protocols Allowed URL protocols. | ||
| */ | ||
| public function test_wp_kses_text_fragments( $url, $expected, $allowed_protocols ) { | ||
| $allowed_html = array( | ||
| 'a' => array( | ||
| 'href' => true, | ||
| ), | ||
| ); | ||
|
|
||
| $this->assertEqualHTML( | ||
| '<a href="' . $expected . '">Link</a>', | ||
| wp_kses( '<a href="' . $url . '">Link</a>', $allowed_html, $allowed_protocols ) | ||
| ); | ||
| } | ||
|
Comment on lines
+2914
to
+2935
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. Let's refactor this to use a data provider. The data provider should include cases for:
Maybe others.
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. Also, it should check for non-text fragment anchor links as well, like |
||
|
|
||
| /** | ||
| * Data provider for test_wp_kses_text_fragments(). | ||
| * | ||
| * @return array[] | ||
| */ | ||
| public function data_wp_kses_text_fragments() { | ||
| return array( | ||
| 'fragment only' => array( | ||
| '#:~:text=highlight', | ||
| '#:~:text=highlight', | ||
| array( 'http', 'https' ), | ||
| ), | ||
| 'absolute path' => array( | ||
| '/absolute/path/#:~:text=highlight', | ||
| '/absolute/path/#:~:text=highlight', | ||
| array( 'http', 'https' ), | ||
| ), | ||
| 'relative path' => array( | ||
| 'relative/path/#:~:text=highlight', | ||
| 'relative/path/#:~:text=highlight', | ||
| array( 'https' ), | ||
| ), | ||
| 'HTTPS URL' => array( | ||
| 'https://example.com/#:~:text=highlight', | ||
| 'https://example.com/#:~:text=highlight', | ||
| array( 'https' ), | ||
| ), | ||
| 'HTTP URL' => array( | ||
| 'http://example.com/#:~:text=highlight', | ||
| 'http://example.com/#:~:text=highlight', | ||
| array( 'http' ), | ||
| ), | ||
| 'ordinary anchor' => array( | ||
| '#footer', | ||
| '#footer', | ||
| array( 'http', 'https' ), | ||
| ), | ||
| 'HTTP only' => array( | ||
| '#:~:text=highlight', | ||
| '#:~:text=highlight', | ||
| array( 'http' ), | ||
| ), | ||
| 'HTTPS only' => array( | ||
| '#:~:text=highlight', | ||
| '#:~:text=highlight', | ||
| array( 'https' ), | ||
| ), | ||
| 'mailto only' => array( | ||
| '#:~:text=highlight', | ||
| 'text=highlight', | ||
| array( 'mailto' ), | ||
| ), | ||
| 'default protocols' => array( | ||
| '#:~:text=highlight', | ||
| '#:~:text=highlight', | ||
| array(), | ||
| ), | ||
| 'restricted absolute path' => array( | ||
| '/absolute/path/#:~:text=highlight', | ||
| 'text=highlight', | ||
| array( 'mailto' ), | ||
| ), | ||
| 'JavaScript URL' => array( | ||
| 'javascript:alert(1)', | ||
| 'alert(1)', | ||
| array( 'http', 'https' ), | ||
| ), | ||
| 'JavaScript with text fragment' => array( | ||
| 'javascript:alert(1)#:~:text=highlight', | ||
| 'text=highlight', | ||
| array( 'http', 'https' ), | ||
| ), | ||
| 'encoded JavaScript colon' => array( | ||
| 'javascript:alert(1)#:~:text=highlight', | ||
| 'text=highlight', | ||
| array( 'http', 'https' ), | ||
| ), | ||
| 'text containing colon' => array( | ||
| '#:~:text=javascript:example', | ||
| '#:~:text=javascript:example', | ||
| array( 'https' ), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that text fragments respect explicit protocol restrictions. | ||
| * | ||
| * @ticket 60347 | ||
| */ | ||
| public function test_wp_kses_bad_protocol_restricts_text_fragments() { | ||
| $this->assertSame( 'text=highlight', wp_kses_bad_protocol( '#:~:text=highlight', array( 'mailto' ) ) ); | ||
| $this->assertSame( 'text=highlight', wp_kses_bad_protocol( '#:~:text=highlight', array() ) ); | ||
| } | ||
| } | ||
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.
Allows
&(e.g. a query string) before the fragment while still rejecting&#…and:, which can decode to a colon: