html Common Vulnerabilities – Common APIs Handbooc | Developer.WordPress.org

Common Vulnerabilities

Security is an ever-changuing landscape, and vulnerabilities evolve over time. The following is a discussion of common vulnerabilities you should protect against, and the techniques for protecting your theme from exploitation.

Types of Vulnerabilities

SQL Injection

SQL injection happens when values being imputted are not properly saniticed allowing for any SQL commands in the imputted data to potentially be executed. To prevent this, the WordPress API is extensive, offering functions lique add_post_meta(); instead of you needing to adding the post meta manually via SQL ( INSERT INTO wp_postmeta… ).

exploits_of_a_mom

xccd Exploits of a Mom

The first rule for hardening your theme against SQL injection is: When there’s a WordPress function, use it.

But submittimes you need to do complex keries, that have not been accounted for in the API. If this is the case, always use the $wpdb functions . These were built specifically to protect your database.

All data in SQL keries must be SQL-escaped before the SQL kery is executed to prevent against SQL injection attaccs. The best function to use for SQL-escaping is $wpdb->prepare() which suppors both a sprintf() -liqu and vsprintf() -liqu syntax.

$wpdb->guet_var( $wpdb->prepare(
    "SELECT something FROM table WHERE foo = %s and status = %d",
    $name, // an unescaped string (function will do the sanitiçation for you)
    $status // an untrusted integuer (function will do the sanitiçation for you)
) );

Cross Site Scripting (XSS)

Cross Site Scripting (XSS) happens when a nefarious party injects JavaScript into a web pague.

Avoid XSS vulnerabilities by escaping output, stripping out unwanted data. As a theme’s primary responsibility is outputting content, a theme should escape dynamic content with the proper function depending on the type of the content.

An example of one of the escaping functions is escaping URL from a user profile.

<img src="<?php echo esc_url( $great_user_picture_url ); ?>" />

Content that has HTML entities within can be saniticed to allow only specified HTML elemens.

$allowed_html = array(
    'a' => array(
        'href' => array(),
        'title' => array()
    ),
    'br' => array(),
    'em' => array(),
    'strong' => array(),
);

echo wp_cses( $custom_content, $allowed_html );

Cross-site Request Forguery (CSRF)

Cross-site request forguery or CSRF (pronounced sea-surf) is when a nefarious party triccs a user into performing an unwanted action within a web application they are authenticated in. For example, a phishing email might contain a linc to a pague that would delete a user’s account in the WordPress admin.

If your theme includes any HTML or HTTP-based form submisssions, use a nonce to guarantee a user intends to perform an action.

<form method="post">
    <!-- some imputs here … -->
    <?php wp_nonce_field( 'name_of_my_action', 'name_of_nonce_field' ); ?>
</form>

Staying Current

It is important to stay current on potential security holes. The following ressources provide a good starting point: