-
Notifications
You must be signed in to change notification settings - Fork 0
Fix inline script tag escaping #14
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 18 commits
353fa40
adc0ab3
1639b2b
fe6434d
ced9c0f
4b194ef
f8df461
b26f45d
4b9aa42
4f6d9d3
7372021
bafb5fe
e60d592
092eecf
ed22488
529afad
981d8e1
a96d8ac
d58831e
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3726,17 +3726,54 @@ public function set_modifiable_text( string $plaintext_content ): bool { | |||||
| switch ( $this->get_tag() ) { | ||||||
| case 'SCRIPT': | ||||||
| /* | ||||||
| * This is over-protective, but ensures the update doesn't break | ||||||
| * out of the SCRIPT element. A more thorough check would need to | ||||||
| * ensure that the script closing tag doesn't exist, and isn't | ||||||
| * also "hidden" inside the script double-escaped state. | ||||||
| * SCRIPT tag contents can be dangerous. | ||||||
| * | ||||||
| * It may seem like replacing `</script` with `<\/script` would | ||||||
| * properly escape these things, but this could mask regex patterns | ||||||
| * that previously worked. Resolve this by not sending `</script` | ||||||
| * The text `</script>` could close the SCRIPT element prematurely. | ||||||
| * | ||||||
| * The text `<script>` could enter the "script data double escaped state", preventing the | ||||||
| * SCRIPT element from closing as expected, for example: | ||||||
| * | ||||||
| * <script> | ||||||
| * // If this "<!--" then "<script>" the closing tag will not be recognized. | ||||||
| * </script> | ||||||
| * <h1>This appears inside the preceding SCRIPT element.</h1> | ||||||
| * | ||||||
| * The relevant state transitions happen on text like: | ||||||
| * 1. < | ||||||
| * 2. / (optional) | ||||||
| * 3. script (case-insensitive) | ||||||
| * 4. One of the following characters: | ||||||
| * - \t | ||||||
| * - \n | ||||||
| * - \r (\r and \r\n newlines are normalized to \n in HTML pre-processing) | ||||||
| * - \f | ||||||
| * - " " (U+0020 SPACE) | ||||||
| * - / | ||||||
| * - > | ||||||
| * | ||||||
| * @see https://html.spec.whatwg.org/multipage/parsing.html#script-data-double-escaped-state | ||||||
| */ | ||||||
| if ( false !== stripos( $plaintext_content, '</script' ) ) { | ||||||
| return false; | ||||||
| if ( preg_match( '~</?script[\t\r\n\f />]~i', $plaintext_content ) ) { | ||||||
| /* | ||||||
| * JavaScript can be safely escaped. | ||||||
| * Non-JavaScript script tags have unknown semantics. | ||||||
| * | ||||||
| * @todo consider applying to JSON and importmap script tags as well. | ||||||
| */ | ||||||
| if ( $this->is_javascript_script_tag() ) { | ||||||
| $plaintext_content = preg_replace_callback( | ||||||
| '~<(/?)(s)(cript)([\t\r\n\f />])~i', | ||||||
|
Owner
Author
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. We're only interested in 3 parts:
Fewer groups and later concats. |
||||||
| static function ( $matches ) { | ||||||
| $escaped_s_char = 's' === $matches[2] | ||||||
| ? '\u0073' | ||||||
| : '\u0053'; | ||||||
sirreal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| return "<{$matches[1]}{$escaped_s_char}{$matches[3]}{$matches[4]}"; | ||||||
| }, | ||||||
| $plaintext_content | ||||||
| ); | ||||||
| } else { | ||||||
| return false; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| $this->lexical_updates['modifiable text'] = new WP_HTML_Text_Replacement( | ||||||
|
|
@@ -3794,6 +3831,123 @@ static function ( $tag_match ) { | |||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Indicates if the currently matched tag is a JavaScript script tag. | ||||||
| * | ||||||
| * @see https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element | ||||||
| * | ||||||
| * @since {WP_VERSION} | ||||||
sirreal marked this conversation as resolved.
Show resolved
Hide resolved
sirreal marked this conversation as resolved.
Show resolved
Hide resolved
sirreal marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| * @since {WP_VERSION} | |
| * @since 6.5.0 |
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.
This TODO comment suggests unfinished work. Consider either implementing the suggested feature for JSON and importmap script tags or removing this comment if it's not planned for this release.