Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Keeping it Clean: Sanitizing, Validating, and E...
Search
Robin Cornett
May 03, 2016
Technology
120
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Keeping it Clean: Sanitizing, Validating, and Escaping in WordPress
Robin Cornett
May 03, 2016
More Decks by Robin Cornett
See All by Robin Cornett
Accessibility Matters to You, Too
robincornett
0
430
Do You Even Need a Page Builder Anymore?
robincornett
0
33
Choosing and Managing WordPress Plugins
robincornett
0
190
The Genesis Framework Meets the Block Editor
robincornett
0
56
Mailchimp and Your Blog
robincornett
0
220
An Introduction to Chrome Dev Tools
robincornett
0
350
Front Pages and Home Pages and Posts Pages, Oh My!
robincornett
0
110
Extending WordPress with Custom Content Types
robincornett
0
260
What's New in WordPress 4.6
robincornett
0
41
Other Decks in Technology
See All in Technology
SRE依存からの脱却 運用を開 発チームへ移す、 フルサイ クル開 発体制の実践
joooee0000
0
3.1k
AI Driven AI Governance
pict3
0
480
OPENLOGI Company Profile for engineer
hr01
1
74k
最適な自走を最小限の支援で — M&Aで拡大する組織で少人数SREが挑んだ1年 / SRE NEXT 2026
genda
0
1.4k
Genie Ontologyは銀の弾丸かを考える / Is Genie Ontology a Silver Bullet?
nttcom
0
380
凡エンジニアがこの先生きのこるためには。〜TypeScript完全に理解したい〜
alchemy1115
2
310
AI時代の闇と光
tatsuya1970
0
100
AIと共生する開発者プラットフォーム:バクラクのモノレポ×マイクロサービス基盤
sakajunquality
2
3.7k
はじめてのWDM
miyukichi_ospf
1
150
「最後に責任を取るのはチーム」— 人間のPRレビューを最小化してアップデートしたメンタルモデル
jnishime_dresscode
0
860
ソニー銀行におけるビジネスアジリティ向上のためのクラウドシフト戦略
srenext
0
680
発表と総括 / Presentations and Summary
ks91
PRO
0
140
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
230
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
470
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
390
Testing 201, or: Great Expectations
jmmastey
46
8.2k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
How to make the Groovebox
asonas
2
2.3k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
190
How to train your dragon (web standard)
notwaldorf
97
6.7k
Between Models and Reality
mayunak
4
370
Transcript
KEEPING IT CLEAN Sanitizing, Validating, and Escaping in WordPress
ASSUME THAT EVERYONE AROUND YOU IS LYING (or clueless, confused,
lost, can’t/won’t read, crazy)
FOUR STAGES OF DATA SAFETY Sanitization Validation Conditions Escaping Universal
Actions Specific Actions INPUT OUTPUT
INPUT: SANITIZE FOR SANITY Save the Right Kind of Data
DISPLAY FEATURED IMAGE FOR GENESIS
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. …
BE RUTHLESS When you have a range of data that
can be entered, make sure you sanitize it.
LITTLE BOBBY TABLES source: http:/ /xkcd.com/327/
SANITIZE Make sure what’s saved to the database is the
correct format.
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()
ALSO CONSIDER: (bool) (int) intval() array_map() htmlspecialchars_decode() stripslashes() esc_url_raw( $url,
(array) $protocols = null )
INPUT: VALIDATE FOR LOGIC Make Sure the Data Makes Sense
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.
BUT DOES IT MAKE SENSE? If a number needs to
fall within a certain range, make sure it does.
BUT DOES IT MAKE SENSE?
OUTPUT: IT DEPENDS Use Conditionals To Make Smart Decisions
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
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
OUTPUT: ESCAPE ALL THE THINGS
None
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/
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 )
BE RUTHLESS Escape data as much as possible on output
to avoid XSS and malformed HTML.
SOURCES https:/ /css-tricks.com/introduction-to-wordpress-front-end-security-escaping- the-things/ https:/ /vip.wordpress.com/2014/06/20/the-importance-of-escaping-all-the- things/ https:/ /codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data https:/
/ninjaforms.com/mr-mrs-null-form-validation-story/ http:/ /codex.wordpress.org/Conditional_Tags
THANK YOU robincornett.com @robincornett