Skip to content

Commit 554cfc0

Browse files
committed
Improve script tag test and sanitization
1 parent 3b21a97 commit 554cfc0

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/wp-includes/functions.wp-scripts.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ function wp_add_inline_script( $handle, $data, $position = 'after' ) {
139139
(
140140
"\t" === $data[7] ||
141141
"\n" === $data[7] ||
142+
/*
143+
* \r\n and \r are normalized to \n in HTML newline normalization.
144+
* Therefore, \r always behaves like \n and terminates a tag name.
145+
*/
146+
"\r" === $data[7] ||
142147
"\f" === $data[7] ||
143148
' ' === $data[7] ||
144149
'/' === $data[7] ||

src/wp-includes/html-api/class-wp-html-tag-processor.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3831,17 +3831,15 @@ public function set_modifiable_text( string $plaintext_content ): bool {
38313831
* 4. One of the following characters:
38323832
* - \t
38333833
* - \n
3834+
* - \r (\r and \r\n newlines are normalized to \n in HTML pre-processing)
38343835
* - \f
38353836
* - " " (U+0020 SPACE)
38363837
* - /
38373838
* - >
38383839
*
38393840
* @see https://html.spec.whatwg.org/multipage/parsing.html#script-data-double-escaped-state
38403841
*/
3841-
if (
3842-
false !== stripos( $plaintext_content, '</script' ) ||
3843-
false !== stripos( $plaintext_content, '<script' )
3844-
) {
3842+
if ( preg_match( '~</?script[\t\r\n\f />]~i', $plaintext_content ) ) {
38453843
/*
38463844
* JavaScript can be safely escaped.
38473845
* Non-JavaScript script tags have unknown semantics.
@@ -3850,12 +3848,12 @@ public function set_modifiable_text( string $plaintext_content ): bool {
38503848
*/
38513849
if ( $this->is_javascript_script_tag() ) {
38523850
$plaintext_content = preg_replace_callback(
3853-
'~<(/?)(s)(cript)~i',
3851+
'~<(/?)(s)(cript)([\t\r\n\f />])~i',
38543852
static function ( $matches ) {
38553853
$escaped_s_char = 's' === $matches[2]
38563854
? '\\u0073'
38573855
: '\\u0053';
3858-
return "<{$matches[1]}{$escaped_s_char}{$matches[3]}";
3856+
return "<{$matches[1]}{$escaped_s_char}{$matches[3]}{$matches[4]}";
38593857
},
38603858
$plaintext_content
38613859
);

0 commit comments

Comments
 (0)