- Timestamp:
- 01/13/2026 01:11:23 PM (10 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/html-api/class-wp-html-tag-processor.php
r61346 r61477 3722 3722 * Not all modifiable text may be set by this method, and not all content 3723 3723 * may be set as modifiable text. In the case that this fails it will return 3724 * `false` indicating as much. For instance, it will not allow inserting the 3725 * string `</script` into a SCRIPT element, because the rules for escaping 3726 * that safely are complicated. Similarly, it will not allow setting content 3727 * into a comment which would prematurely terminate the comment. 3724 * `false` indicating as much. For instance, if the contents of a SCRIPT 3725 * element are neither JavaScript nor JSON, it’s not possible to guarantee 3726 * that escaping strings like `</script>` won’t break the script; in these 3727 * cases, updates will be rejected and it’s up to calling code to perform 3728 * language-specific escaping or workarounds. Similarly, it will not allow 3729 * setting content into a comment which would prematurely terminate the comment. 3728 3730 * 3729 3731 * Example: … … 3812 3814 switch ( $this->get_tag() ) { 3813 3815 case 'SCRIPT': 3814 /** 3815 * This is over-protective, but ensures the update doesn't break 3816 * the HTML structure of the SCRIPT element. 3816 $script_content_type = $this->get_script_content_type(); 3817 3818 switch ( $script_content_type ) { 3819 case 'javascript': 3820 case 'json': 3821 $this->lexical_updates['modifiable text'] = new WP_HTML_Text_Replacement( 3822 $this->text_starts_at, 3823 $this->text_length, 3824 self::escape_javascript_script_contents( $plaintext_content ) 3825 ); 3826 return true; 3827 } 3828 3829 /* 3830 * If the script’s content type isn’t recognized and understandable then it’s 3831 * impossible to guarantee that escaping the content won’t cause runtime breakage. 3832 * For instance, if the script content type were PHP code then escaping with 3833 * `\u0073` would not be met by unescaping; rather, it could result in corrupted 3834 * data or even syntax errors. 3817 3835 * 3818 * More thorough analysis could track the HTML tokenizer states 3819 * and to ensure that the SCRIPT element closes at the expected 3820 * SCRIPT close tag as is done in {@see ::skip_script_data()}. 3821 * 3822 * A SCRIPT element could be closed prematurely by contents 3823 * like `</script>`. A SCRIPT element could be prevented from 3824 * closing by contents like `<!--<script>`. 3825 * 3826 * The following strings are essential for dangerous content, 3827 * although they are insufficient on their own. This trade-off 3828 * prevents dangerous scripts from being sent to the browser. 3829 * It is also unlikely to produce HTML that may confuse more 3830 * basic HTML tooling. 3836 * Because of this, content which could potentially modify the SCRIPT tag’s 3837 * HTML structure is rejected here. It’s the responsibility of calling code to 3838 * perform whatever semantic escaping is necessary to avoid problematic strings. 3831 3839 */ 3832 3840 if ( 3833 false !== stripos( $plaintext_content, '< /script' ) ||3834 false !== stripos( $plaintext_content, '< script' )3841 false !== stripos( $plaintext_content, '<script' ) || 3842 false !== stripos( $plaintext_content, '</script' ) 3835 3843 ) { 3836 3844 return false; 3837 3845 } 3838 3839 3846 $this->lexical_updates['modifiable text'] = new WP_HTML_Text_Replacement( 3840 3847 $this->text_starts_at, … … 3842 3849 $plaintext_content 3843 3850 ); 3844 3845 3851 return true; 3846 3852 … … 3890 3896 3891 3897 return false; 3898 } 3899 3900 /** 3901 * Returns the content type of the currently-matched HTML SCRIPT tag, if matched and 3902 * recognized, otherwise returns `null` to indicate an unrecognized content type. 3903 * 3904 * An HTML SCRIPT tag is a normal SCRIPT tag, but there can be SCRIPT elements inside 3905 * SVG and MathML elements as well, and these have different parsing rules than those 3906 * in general HTML. For this reason, no content-type inference is performed on those. 3907 * 3908 * Note! This concept is related but distinct from the MIME type of the script. 3909 * Parsing MUST match the specific algorithm in the HTML specification, which 3910 * relies on exact string comparison in some cases. MIME type decoding may be 3911 * performed on SVG or MathML SCRIPT tags. 3912 * 3913 * Only 'javascript' and 'json' content types are currently recognized. 3914 * 3915 * @see https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element 3916 * 3917 * @since 7.0.0 3918 * 3919 * @return 'javascript'|'json'|null Type of script element content if matched and recognized. 3920 */ 3921 private function get_script_content_type(): ?string { 3922 // SVG and MathML SCRIPT elements are not recognized. 3923 if ( 'SCRIPT' !== $this->get_tag() || $this->get_namespace() !== 'html' ) { 3924 return null; 3925 } 3926 3927 /* 3928 * > If any of the following are true: 3929 * > - el has a type attribute whose value is the empty string; 3930 * > - el has no type attribute but it has a language attribute and that attribute's 3931 * > value is the empty string; or 3932 * > - el has neither a type attribute nor a language attribute, 3933 * > then let the script block's type string for this script element be "text/javascript". 3934 */ 3935 $type = $this->get_attribute( 'type' ); 3936 $lang = $this->get_attribute( 'language' ); 3937 3938 if ( true === $type || '' === $type ) { 3939 return 'javascript'; 3940 } 3941 3942 if ( null === $type && ( null === $lang || true === $lang || '' === $lang ) ) { 3943 return 'javascript'; 3944 } 3945 3946 /* 3947 * > Otherwise, if el has a type attribute, then let the script block's type string be 3948 * > the value of that attribute with leading and trailing ASCII whitespace stripped. 3949 * > Otherwise, el has a non-empty language attribute; let the script block's type string 3950 * > be the concatenation of "text/" and the value of el's language attribute. 3951 */ 3952 $type_string = is_string( $type ) ? trim( $type, " \t\f\r\n" ) : "text/{$lang}"; 3953 3954 // All matches are ASCII case-insensitive; eagerly lower-case for comparison. 3955 $type_string = strtolower( $type_string ); 3956 3957 /* 3958 * > If the script block's type string is a JavaScript MIME type essence match, then 3959 * > set el's type to "classic". 3960 * 3961 * > A string is a JavaScript MIME type essence match if it is an ASCII case-insensitive 3962 * > match for one of the JavaScript MIME type essence strings. 3963 * 3964 * > A JavaScript MIME type is any MIME type whose essence is one of the following: 3965 * > 3966 * > - application/ecmascript 3967 * > - application/javascript 3968 * > - application/x-ecmascript 3969 * > - application/x-javascript 3970 * > - text/ecmascript 3971 * > - text/javascript 3972 * > - text/javascript1.0 3973 * > - text/javascript1.1 3974 * > - text/javascript1.2 3975 * > - text/javascript1.3 3976 * > - text/javascript1.4 3977 * > - text/javascript1.5 3978 * > - text/jscript 3979 * > - text/livescript 3980 * > - text/x-ecmascript 3981 * > - text/x-javascript 3982 * 3983 * @see https://mimesniff.spec.whatwg.org/#javascript-mime-type-essence-match 3984 * @see https://mimesniff.spec.whatwg.org/#javascript-mime-type 3985 */ 3986 switch ( $type_string ) { 3987 case 'application/ecmascript': 3988 case 'application/javascript': 3989 case 'application/x-ecmascript': 3990 case 'application/x-javascript': 3991 case 'text/ecmascript': 3992 case 'text/javascript': 3993 case 'text/javascript1.0': 3994 case 'text/javascript1.1': 3995 case 'text/javascript1.2': 3996 case 'text/javascript1.3': 3997 case 'text/javascript1.4': 3998 case 'text/javascript1.5': 3999 case 'text/jscript': 4000 case 'text/livescript': 4001 case 'text/x-ecmascript': 4002 case 'text/x-javascript': 4003 return 'javascript'; 4004 4005 /* 4006 * > Otherwise, if the script block's type string is an ASCII case-insensitive match for 4007 * > the string "module", then set el's type to "module". 4008 * 4009 * A module is evaluated as JavaScript. 4010 */ 4011 case 'module': 4012 return 'javascript'; 4013 4014 /* 4015 * > Otherwise, if the script block's type string is an ASCII case-insensitive match for the string "importmap", then set el's type to "importmap". 4016 * > Otherwise, if the script block's type string is an ASCII case-insensitive match for the string "speculationrules", then set el's type to "speculationrules". 4017 * 4018 * These conditions indicate JSON content. 4019 */ 4020 case 'importmap': 4021 case 'speculationrules': 4022 return 'json'; 4023 4024 /** @todo Rely on a full MIME parser for determining JSON content. */ 4025 case 'application/json': 4026 case 'text/json': 4027 return 'json'; 4028 } 4029 4030 /* 4031 * > Otherwise, return. (No script is executed, and el's type is left as null.) 4032 */ 4033 return null; 4034 } 4035 4036 /** 4037 * Escape JavaScript and JSON script tag contents. 4038 * 4039 * Ensure that the script contents cannot modify the HTML structure or break out 4040 * of its containing SCRIPT element. JavaScript and JSON may both be escaped with 4041 * the same rules, even though there are additional escaping measures available 4042 * to JavaScript source code which aren’t applicable to serialized JSON data. 4043 * 4044 * A simple method safely escapes all content except for a few extremely rare and 4045 * unlikely exceptions: prevent the appearance of `<script` and `</script` within 4046 * the contents by replacing the first letter of the tag name with a Unicode escape. 4047 * 4048 * Example: 4049 * 4050 * $plaintext = '<script>document.write( "A </script> closes a script." );</script>'; 4051 * $escaped = '<script>document.write( "A </\u0073cript> closes a script." );</script>'; 4052 * 4053 * This works because of how parsing changes after encountering an opening SCRIPT 4054 * tag. The actual parsing comprises a complicated state machine, the result of 4055 * legacy behaviors and diverse browser support. However, without these two strings 4056 * in the script contents, two key things are ensured: `</script>` cannot appear to 4057 * prematurely close the tag, and the problematic double-escaped state becomes 4058 * unreachable. A JavaScript engine or JSON decoder will then decode the Unicode 4059 * escape (`\u0073`) back into its original plaintext value, but only after having 4060 * been safely extracted from the HTML. 4061 * 4062 * While it may seem tempting to replace the `<` character instead, doing so would 4063 * break JavaScript syntax. The `<` character is used in comparison operators and 4064 * other JavaScript syntax; replacing it would break valid JavaScript. Replacing 4065 * only the `s` in `<script` and `</script` avoids modifying JavaScript syntax. 4066 * 4067 * ### Exceptions 4068 * 4069 * This _should_ work everywhere, but there are some extreme exceptions. 4070 * 4071 * - Comments. 4072 * - Tagged templates, such as `String.raw()`, which provide access to “raw” strings. 4073 * - The `source` property of a RegExp object. 4074 * 4075 * Each of these exceptions appear at the source code level, not at the semantic or 4076 * evaluation level. Normal JavaScript will remain semantically equivalent after escaping, 4077 * but any JavaScript which analyzes the raw source code will see potentially-different 4078 * values. 4079 * 4080 * #### Comments 4081 * 4082 * Comments are never unescaped because they aren’t parsed by the JavaScript engine. 4083 * When viewing the source in a browser’s developer tools, the comments will retain 4084 * their escaped text. 4085 * 4086 * Example: 4087 * 4088 * // A comment: "</script>" 4089 * …becomes… 4090 * // A comment: "</\u0073cript>" 4091 * 4092 * #### Tagged templates. 4093 * 4094 * Tagged templates “enable the embedding of arbitrary string content, where escape 4095 * sequences may follow a different syntax.” For example, they can aid representing 4096 * a RegExp pattern or LaTex snippet within a JavaScript string, where the string 4097 * escape characters might get noisy and distracting. 4098 * 4099 * Example: 4100 * 4101 * console.log( 'A \notin B' ); // Prints a newline because of the "\n". 4102 * console.log( 'A \\notin B' ); // Prints "A \notin B". 4103 * console.log( String.raw`A \notin B` ); // Prints "A \notin B". 4104 * 4105 * This means that if `<script` transforms into `<\u0073cript` _inside_ a raw string 4106 * or tagged template literal which relies on its `.raw` property, the output of the 4107 * code will be different after escaping. 4108 * 4109 * Example: 4110 * 4111 * console.log( String.raw`</script>` ); // Prematurely closes the SCRIPT element. 4112 * console.log( String.raw`</\u0073cript>` ); // Prints "</\u0073cript". 4113 * 4114 * #### RegExp sources. 4115 * 4116 * The RegExp object exposes its raw source in a similar way to how tagged templates and raw 4117 * strings do. Thankfully, because escape sequences are decoded when compiling the pattern, 4118 * escaped RegExp patterns will match the same way as the plaintext sequences would. 4119 * 4120 * Example: 4121 * 4122 * true === /<script>/.test( '<script>' ); 4123 * true === /<\u0073cript>/.test( '<script>' ); 4124 * 4125 * However, as with raw strings, any code which reads the source will see the escaped value 4126 * instead of the decoded one. 4127 * 4128 * Example: 4129 * 4130 * console.log( /<script>/.source ); // Prints "<script>". 4131 * console.log( /<\u0073cript>/.source ); // Prints "<\u0073cript>". 4132 * 4133 * #### Unsupported escaping. 4134 * 4135 * It is not possible to properly represent every possible JavaScript source file 4136 * inside a SCRIPT element. As with CSS stylesheets, SVG images, and MathML, the 4137 * only 100% reliable way to represent all possible inputs is to link to external 4138 * files of the given content-type. 4139 * 4140 * In some cases it’s possible to manually prevent escaping issues. These are not 4141 * automatically handled by this function because doing so would require a full 4142 * JavaScript tokenizer. Consider the following example listing various ways to 4143 * manually escape a closing script tag. 4144 * 4145 * Example: 4146 * 4147 * console.log( String.raw`</script>` ); // !!UNSAFE!! Will be escaped. 4148 * console.log( String.raw`</\u0073cript>` ); // "</\u0073cript>" 4149 * console.log( String.raw`</scr` + String.raw`ipt>` ); // "</script>" 4150 * console.log( String.raw`</${"script"}>` ); // "</script>" 4151 * console.log( '</scr' + 'ipt>' ); // "</script>" 4152 * console.log( "\x3C/script>" ); // "</script>" 4153 * console.log( "<\/script>" ); // "</script>" 4154 * 4155 * The following graph is a simplified interpretation of how HTML interprets the contents 4156 * of a SCRIPT tag and identifies the closing tag. It is useful to understand what text 4157 * is dangerous inside of a SCRIPT tag and why different approaches to escaping work. 4158 * 4159 * Open script 4160 * │ 4161 * ▼ 4162 * ╔═════════════════════════════════════════╗ <!--(…)> 4163 * ║ ║ (all dashes) 4164 * ║ script ╟────────────────╮ 4165 * ║ data ║ │ 4166 * ╭───────────╢ ║ ◀──────────────╯ 4167 * │ ╚═╤═══════════════════════════════════════╝ 4168 * │ │ ▲ ▲ 4169 * │ │ <!-- │ --> ╰─────╮ 4170 * │ ▼ │ │ 4171 * │ ┌─────────────────┴───────────────────────┐ │ 4172 * │ </script¹ │ escaped │ │ 4173 * │ └─┬─────────────────────────────┬─────────┘ │ 4174 * │ │ ▲ │ │ --> 4175 * │ │ </script¹ │ </script¹ │ <script¹ │ 4176 * │ ▼ │ ▼ │ 4177 * │ ╔══════════════╗ │ ┌───────────┐ │ 4178 * │ ║ Close script ║ │ │ double │ │ 4179 * ╰──────────▶║ ║ ╰───────────┤ escaped ├──╯ 4180 * ╚══════════════╝ └───────────┘ 4181 * 4182 * ¹ = Case insensitive 'script' followed by one of ' \t\f\r\n/>', known 4183 * as “tag-name-terminating characters.” This sequence forms the start 4184 * of what could be a SCRIPT opening or closing tag. 4185 * 4186 * @see https://html.spec.whatwg.org/#restrictions-for-contents-of-script-elements 4187 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#specifications 4188 * @see wp_html_api_script_element_escaping_diagram_source() 4189 * 4190 * @since 7.0.0 4191 * 4192 * @param string $sourcecode Raw contents intended to be serialized into an HTML SCRIPT element. 4193 * @return string Escaped form of input contents which will not lead to premature closing of the containing SCRIPT element. 4194 */ 4195 private static function escape_javascript_script_contents( string $sourcecode ): string { 4196 $at = 0; 4197 $was_at = 0; 4198 $end = strlen( $sourcecode ); 4199 $escaped = ''; 4200 4201 /* 4202 * Replace all instances of the ASCII case-insensitive match of "<script" 4203 * and "</script", when followed by whitespace or "/" or ">", by using a 4204 * character replacement for the "s" (or the "S"). 4205 */ 4206 while ( $at < $end ) { 4207 $tag_at = strpos( $sourcecode, '<', $at ); 4208 if ( false === $tag_at ) { 4209 break; 4210 } 4211 4212 $tag_name_at = $tag_at + 1; 4213 $has_closing_slash = $tag_name_at < $end && '/' === $sourcecode[ $tag_name_at ]; 4214 $tag_name_at += $has_closing_slash ? 1 : 0; 4215 4216 if ( 0 !== substr_compare( $sourcecode, 'script', $tag_name_at, 6, true ) ) { 4217 $at = $tag_at + 1; 4218 continue; 4219 } 4220 4221 if ( 1 !== strspn( $sourcecode, " \t\f\r\n/>", $tag_name_at + 6, 1 ) ) { 4222 $at = $tag_name_at + 6; 4223 continue; 4224 } 4225 4226 $escaped .= substr( $sourcecode, $was_at, $tag_name_at - $was_at ); 4227 $escaped .= 's' === $sourcecode[ $tag_name_at ] ? '\u0073' : '\u0053'; 4228 $was_at = $tag_name_at + 1; 4229 $at = $tag_name_at + 7; 4230 } 4231 4232 if ( '' === $escaped ) { 4233 return $sourcecode; 4234 } 4235 4236 if ( $was_at < $end ) { 4237 $escaped .= substr( $sourcecode, $was_at ); 4238 } 4239 4240 return $escaped; 3892 4241 } 3893 4242
Note:
See TracChangeset
for help on using the changeset viewer.