Skip to content

Commit aeb18c6

Browse files
committed
Improve ValidatedSanitizedInput documentation based on PR review
- Add introductory standard block explaining what the sniff checks, using language that avoids the overloaded term "validated". - Rewrite standard descriptions to explain why each rule exists. - Add <em> tags to highlight key parts in code examples. - Remove specific mention of XSS as the attack vector. - Focus each section on its specific error without mixing concerns. - List which superglobals require wp_unslash() in MissingUnslash section.
1 parent d3b51f5 commit aeb18c6

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

WordPress/Docs/Security/ValidatedSanitizedInputStandard.xml

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,90 +5,97 @@
55
>
66
<standard>
77
<![CDATA[
8-
All user input data ($_POST, $_GET, $_REQUEST, $_SERVER, $_COOKIE, $_FILES, $_SESSION, $_ENV) must be validated, unslashed, and sanitized before use to prevent security vulnerabilities like XSS, SQL injection, and code injection attacks.
9-
10-
Validation ensures the input key exists (using isset(), empty(), array_key_exists(), or null coalescing operators). Unslashing removes WordPress's automatic backslashes using wp_unslash() or similar functions. Sanitization cleans the data using appropriate functions like sanitize_text_field(), absint(), etc.
8+
Superglobals ($_COOKIE, $_ENV, $_FILES, $_GET, $_POST, $_REQUEST, $_SERVER, $_SESSION) must be properly handled before use: array keys must be checked for existence and values must be sanitized.
119
]]>
1210
</standard>
1311
<standard>
1412
<![CDATA[
15-
String interpolation with superglobals requires validation and sanitization. Using $_POST, $_GET, etc. directly in strings can lead to XSS attacks if the input contains malicious code.
13+
Superglobals must not be used directly in string interpolation or heredocs. The array key might not exist, or its value could contain malicious content that gets included in the string without sanitization.
1614
]]>
1715
</standard>
1816
<code_comparison>
19-
<code title="Valid: String interpolation with proper validation and sanitization.">
17+
<code title="Valid: No superglobal in string interpolation.">
2018
<![CDATA[
21-
if ( isset( $_POST['name'] ) ) {
22-
$safe_name = sanitize_text_field( wp_unslash( $_POST['name'] ) );
23-
echo "Hello " . $safe_name;
24-
}
19+
// No superglobal in string interpolation.
2520
]]>
2621
</code>
27-
<code title="Invalid: String interpolation without validation or sanitization.">
22+
<code title="Invalid: Superglobal used in string interpolation.">
2823
<![CDATA[
29-
echo "Hello {$_POST['name']}";
24+
echo "Hello <em>{$_GET['name']}</em>";
3025
]]>
3126
</code>
3227
</code_comparison>
3328
<standard>
3429
<![CDATA[
35-
All superglobal array access must be validated to ensure the key exists before use. This prevents undefined index notices and potential security issues.
30+
Superglobal array keys must be checked for existence before use. Accessing a key that does not exist can lead to unexpected behavior.
3631
]]>
3732
</standard>
3833
<code_comparison>
3934
<code title="Valid: Input is validated before use.">
4035
<![CDATA[
41-
if ( isset( $_POST['name'] ) ) {
42-
$name = sanitize_text_field( wp_unslash( $_POST['name'] ) );
36+
if ( <em>isset( $_POST['name'] )</em> ) {
37+
$name = sanitize_text_field(
38+
wp_unslash( <em>$_POST['name']</em> )
39+
);
4340
}
4441
]]>
4542
</code>
4643
<code title="Invalid: Input used without validation.">
4744
<![CDATA[
48-
$name = sanitize_text_field( wp_unslash( $_POST['name'] ) );
45+
$name = sanitize_text_field(
46+
wp_unslash( <em>$_POST['name']</em> )
47+
);
4948
]]>
5049
</code>
5150
</code_comparison>
5251
<standard>
5352
<![CDATA[
54-
All validated input must be sanitized to remove or escape potentially malicious content before processing or output.
53+
All input must be sanitized to remove potentially malicious content before it is used.
5554
]]>
5655
</standard>
5756
<code_comparison>
58-
<code title="Valid: Input is validated and sanitized.">
57+
<code title="Valid: Input is sanitized.">
5958
<![CDATA[
60-
if ( isset( $_POST['text'] ) ) {
61-
$text = sanitize_text_field( wp_unslash( $_POST['text'] ) );
59+
if ( isset( $_POST['email'] ) ) {
60+
$email = <em>sanitize_email</em>(
61+
wp_unslash( $_POST['email'] )
62+
);
6263
}
6364
]]>
6465
</code>
65-
<code title="Invalid: Input validated but not sanitized.">
66+
<code title="Invalid: Input used without sanitization.">
6667
<![CDATA[
67-
if ( isset( $_POST['text'] ) ) {
68-
$text = wp_unslash( $_POST['text'] );
68+
if ( isset( $_POST['email'] ) ) {
69+
$email = wp_unslash(
70+
<em>$_POST['email']</em>
71+
);
6972
}
7073
]]>
7174
</code>
7275
</code_comparison>
7376
<standard>
7477
<![CDATA[
75-
WordPress automatically adds backslashes to certain superglobals. These must be removed using wp_unslash() or similar functions before sanitization to prevent double-escaping issues.
78+
WordPress adds slashes to $_COOKIE, $_GET, $_POST, $_REQUEST, and $_SERVER elements. These must be passed through an unslashing function before sanitization to ensure the data is processed correctly.
7679
]]>
7780
</standard>
7881
<code_comparison>
7982
<code title="Valid: Input is unslashed before sanitization.">
8083
<![CDATA[
81-
if ( isset( $_POST['data'] ) ) {
82-
$clean = sanitize_text_field( wp_unslash( $_POST['data'] ) );
84+
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
85+
$url = sanitize_url(
86+
<em>wp_unslash</em>( $_SERVER['REQUEST_URI'] )
87+
);
8388
}
8489
]]>
8590
</code>
8691
<code title="Invalid: Missing unslashing before sanitization.">
8792
<![CDATA[
88-
if ( isset( $_POST['data'] ) ) {
89-
$clean = sanitize_text_field( $_POST['data'] );
93+
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
94+
$url = sanitize_url(
95+
<em>$_SERVER['REQUEST_URI']</em>
96+
);
9097
}
9198
]]>
9299
</code>
93100
</code_comparison>
94-
</documentation>
101+
</documentation>

0 commit comments

Comments
 (0)