Skip to content
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

WP.ORG: Data Must be Sanitized, Escaped, and Validated #611

Closed
mauteri opened this issue Mar 18, 2024 · 0 comments · Fixed by #644
Closed

WP.ORG: Data Must be Sanitized, Escaped, and Validated #611

mauteri opened this issue Mar 18, 2024 · 0 comments · Fixed by #644
Assignees

Comments

@mauteri
Copy link
Contributor

mauteri commented Mar 18, 2024

When you include POST/GET/REQUEST/FILE calls in your plugin, it's important to sanitize, validate, and escape them. The goal here is to prevent a user from accidentally sending trash data through the system, as well as protecting them from potential security issues.

SANITIZE: Data that is input (either by a user or automatically) must be sanitized as soon as possible. This lessens the possibility of XSS vulnerabilities and MITM attacks where posted data is subverted.

VALIDATE: All data should be validated, no matter what. Even when you sanitize, remember that you don’t want someone putting in ‘dog’ when the only valid values are numbers.

ESCAPE: Data that is output must be escaped properly when it is echo'd, so it can't hijack admin screens. There are many esc_*() functions you can use to make sure you don't show people the wrong data.

To help you with this, WordPress comes with a number of sanitization and escaping functions. You can read about those here:

https://developer.wordpress.org/apis/security/sanitizing/
https://developer.wordpress.org/apis/security/escaping/

Remember: You must use the most appropriate functions for the context. If you’re sanitizing email, use sanitize_email(), if you’re outputting HTML, use wp_kses_post(), and so on.

An easy mantra here is this:

Sanitize earlyEscape LateAlways Validate

Clean everything, check everything, escape everything, and never trust the users to always have input sane data. After all, users come from all walks of life.

Example(s) from your plugin:
21_21-26-52_gatherpress/includes/core/classes/class-user.php:103 ! wp_verify_nonce( filter_input( INPUT_POST, 'wpnonce' ), 'update-user' . $user_id )
-----> filter_input(INPUT_POST, '_wpnonce')

Note: When checking a nonce using wp_verify_nonce you will need to sanitize the input using wp_unslash AND sanitize_text_field , this is because this function is pluggable, and extenders should not trust its input values.

Example:
if ( ! isset( $_POST['prefix_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash ( $_POST['prefix_nonce'] ) ) , 'prefix_nonce' ) )

Example(s) from your plugin:
21_21-26-52_gatherpress/includes/core/classes/class-user.php:103 ! wp_verify_nonce( filter_input( INPUT_POST, 'wpnonce' ), 'update-user' . $user_id )

Note: When using functions like filter_var , filter_var_array , filter_input and/or filter_input_array you will need to set the FILTER parameter to any kind of filter that sanitizes the input.

Leaving the filter parameter empty, PHP by default will apply the filter "FILTER_DEFAULT" which is not sanitizing at all.

Example:
$post_id = filter_input(INPUT_GET, 'post_id', FILTER_SANITIZE_NUMBER_INT);

Example(s) from your plugin:
21_21-26-52_gatherpress/includes/core/classes/class-user.php:103 ! wp_verify_nonce( filter_input( INPUT_POST, 'wpnonce' ), 'update-user' . $user_id )
-----> filter_input(INPUT_POST, '_wpnonce')

@mauteri mauteri converted this from a draft issue Mar 18, 2024
@mauteri mauteri self-assigned this Apr 17, 2024
@mauteri mauteri mentioned this issue Apr 17, 2024
4 tasks
@mauteri mauteri moved this from Next Release to QA Review in GatherPress Project Apr 17, 2024
@github-project-automation github-project-automation bot moved this from QA Review to Done in GatherPress Project Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging a pull request may close this issue.

1 participant