Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Keeping it Clean: Sanitizing, Validating, and E...

Keeping it Clean: Sanitizing, Validating, and Escaping in WordPress

Robin Cornett

May 03, 2016
Tweet

More Decks by Robin Cornett

Other Decks in Technology

Transcript

  1. MANAGE YOUR EXPECTATIONS If a value is supposed to be

    a number, make sure it’s saved as a number. If it’s supposed to be an image ID, make sure it’s a number. If it’s supposed to be a URL, make sure it’s a URL. …
  2. BE RUTHLESS When you have a range of data that

    can be entered, make sure you sanitize it.
  3. CORE SANITIZING FUNCTIONS Example functions: sanitize_email() sanitize_file_name() sanitize_html_class() sanitize_key() sanitize_meta()

    sanitize_mime_type() sanitize_option() sanitize_sql_orderby() sanitize_text_field() sanitize_title() sanitize_title_for_query() sanitize_title_with_dashes() sanitize_user()
  4. BE RUTHLESS Follow the whitelist philosophy with data validation, and

    only allow the user to input data of your expected type. If it's not the proper type, discard it.
  5. BUT DOES IT MAKE SENSE? If a number needs to

    fall within a certain range, make sure it does.
  6. MAYBE I WILL, MAYBE I WON’T Use both WordPress conditionals

    and your own to make sure you’re only printing data when you can or should. http:/ /codex.wordpress.org/Con ditional_Tags
  7. USE ALL THE TOOLS YOU CAN Make sure you’re debugging:

    define( 'WP_DEBUG', true ); define( 'SCRIPT_DEBUG', true ); Use plugins like Query Monitor, Debug Bar, or Hookr
  8. ESCAPE ALL THE THINGS “Escaping changes possibly evil content into

    safe content.” source: https:/ /css-tricks.com/introduction-to-wordpress-front-end-security-escaping-the-things/
  9. ESCAPING FUNCTIONS •intval( $int ) or (int) $int •absint( $int

    ) •wp_kses( (string) $fragment, (array) $allowed_html, (array) $protocols = null ) •wp_rel_nofollow( (string) $html ) •wp_kses_allowed_html( (string) $context ) •esc_html( $text ) •esc_html__() •esc_html_e() •esc_textarea() •sanitize_text_field() •esc_attr( $text ) •esc_attr__() •esc_attr_e() •esc_js( $text ) •esc_url( $url, (array) $protocols = null ) •esc_url_raw( $url, (array) $protocols = null ) •urlencode( $scalar ) •urlencode_deep( $array ) •validate_file( (string) $filename, (array) $allowed_files = "" ) •wp_redirect($location, $status = 302) •wp_safe_redirect($location, $status = 302) •sanitize_title( $title ) •sanitize_user( $username, $strict = false ) •balanceTags( $html ) or force_balance_tags( $html ) •tag_escape( $html_tag_name ) •sanitize_html_class( $class, $fallback ) •is_email( $email_address )