-
-
Notifications
You must be signed in to change notification settings - Fork 518
Security/ValidatedSanitizedInput: add XML documentation #2698
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
Open
rodrigoprimo
wants to merge
3
commits into
WordPress:develop
Choose a base branch
from
rodrigoprimo:docs-validated-sanitized-input
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
WordPress/Docs/Security/ValidatedSanitizedInputStandard.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| <?xml version="1.0"?> | ||
| <documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd" | ||
| title="Validated Sanitized Input" | ||
| > | ||
| <standard> | ||
| <![CDATA[ | ||
| 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. Failing to do so can lead to security vulnerabilities such as SQL injection and CSRF attacks. | ||
| ]]> | ||
| </standard> | ||
| <standard> | ||
| <![CDATA[ | ||
| Superglobal array keys must be checked for existence before use. Accessing a key that does not exist can lead to unexpected behavior. | ||
| ]]> | ||
| </standard> | ||
| <code_comparison> | ||
| <code title="Valid: Input is validated before use."> | ||
| <![CDATA[ | ||
| if ( <em>isset( $_POST['name'] )</em> ) { | ||
| $name = sanitize_text_field( | ||
| wp_unslash( $_POST['name'] ) | ||
| ); | ||
| } | ||
| ]]> | ||
| </code> | ||
| <code title="Invalid: Input used without validation."> | ||
| <![CDATA[ | ||
| $name = sanitize_text_field( | ||
| wp_unslash( <em>$_POST['name']</em> ) | ||
| ); | ||
| ]]> | ||
| </code> | ||
| </code_comparison> | ||
| <standard> | ||
| <![CDATA[ | ||
| All input must be sanitized to remove potentially malicious content before it is used. | ||
| ]]> | ||
| </standard> | ||
| <code_comparison> | ||
| <code title="Valid: Input is sanitized."> | ||
| <![CDATA[ | ||
| if ( isset( $_FILES['upload']['name'] ) ) { | ||
| $filename = <em>sanitize_file_name</em>( | ||
| $_FILES['upload']['name'] | ||
| ); | ||
| } | ||
|
|
||
| if ( isset( $_POST['email'] ) ) { | ||
| $email = <em>sanitize_email</em>( | ||
| wp_unslash( $_POST['email'] ) | ||
| ); | ||
| } | ||
| ]]> | ||
| </code> | ||
| <code title="Invalid: Input used without sanitization."> | ||
| <![CDATA[ | ||
| if ( isset( $_FILES['upload']['name'] ) ) { | ||
| $filename = <em>$_FILES['upload']['name']</em>; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| if ( isset( $_POST['email'] ) ) { | ||
| $email = wp_unslash( | ||
| <em>$_POST['email']</em> | ||
| ); | ||
| } | ||
| ]]> | ||
| </code> | ||
| </code_comparison> | ||
| <standard> | ||
| <![CDATA[ | ||
| 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. | ||
| ]]> | ||
| </standard> | ||
| <code_comparison> | ||
| <code title="Valid: Input is unslashed before sanitization."> | ||
| <![CDATA[ | ||
| if ( isset( $_SERVER['REQUEST_URI'] ) ) { | ||
| $url = sanitize_url( | ||
| <em>wp_unslash</em>( $_SERVER['REQUEST_URI'] ) | ||
| ); | ||
| } | ||
| ]]> | ||
| </code> | ||
| <code title="Invalid: Missing unslashing before sanitization."> | ||
| <![CDATA[ | ||
| if ( isset( $_SERVER['REQUEST_URI'] ) ) { | ||
| $url = sanitize_url( | ||
| <em>$_SERVER['REQUEST_URI']</em> | ||
| ); | ||
| } | ||
| ]]> | ||
| </code> | ||
| </code_comparison> | ||
| <standard> | ||
| <![CDATA[ | ||
| 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. | ||
| ]]> | ||
| </standard> | ||
| <code_comparison> | ||
| <code title="Valid: Use a sanitized variable in the string."> | ||
| <![CDATA[ | ||
| if ( isset( $_GET['name'] ) ) { | ||
| $name = sanitize_text_field( | ||
| wp_unslash( $_GET['name'] ) | ||
| ); | ||
| echo "Hello <em>$name</em>"; | ||
| } | ||
| ]]> | ||
| </code> | ||
| <code title="Invalid: Superglobal used in string interpolation."> | ||
| <![CDATA[ | ||
| echo "Hello <em>{$_GET['name']}</em>"; | ||
| ]]> | ||
| </code> | ||
| </code_comparison> | ||
| </documentation> | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not a must-have, but it may simplify the example code a little to use
$_ENVinstead (which doesn't need unslashing) ?Or maybe having an example with both
$_POSTas well as$_ENVwould help users ?