Zero Capability Checks or Data Validation

It seems only appropriate that I post a terrible code snippet that I myself wrote. This is one piece of the code that I used to create new post types in version 1.0 of my Easy Content Types plugin.

This code was loaded directly into the main plugin file and had zero capability checks. It also doesn’t ever validate any of the data that is getting passed to the database, meaning that this was very, very, very susceptible to SQL injection.

This was fixed a long time ago.

if(isset($_POST['post-type-name'])) 
{	
	if($_POST['label-single'] != '') { $single = $_POST['label-single']; } else { $single = $_POST['post-type-name']; }
	if($_POST['label-plural'] != '') { $plural = $_POST['label-plural']; } else { $plural = $_POST['post-type-name']; }

	// check for checked options
	if($_POST['options-hierarchial']) { $hierarchical = 1; }
	if($_POST['options-post-formats']) { $post_formats = 1; }
	if($_POST['options-archives']) { $archives = 1; }
	if($_POST['options-nav']) { $nav = 1; }
	
	// check for supports options
	if($_POST['options-title']) { $title = 1; }
	if($_POST['options-editor']) { $editor = 1; }
	if($_POST['options-author']) { $author = 1; }
	if($_POST['options-thumbnail']) { $thumbnail = 1; }
	if($_POST['options-excerpt']) { $excerpt = 1; }
	if($_POST['options-custom-fields']) { $fields = 1; }
	if($_POST['options-comments']) { $comments = 1; }
	if($_POST['options-revisions']) { $revisions = 1; }
		
	$add = $wpdb->query("INSERT INTO " . $ecpt_db_name . " SET 
		name='" . strtolower($_POST['post-type-name']) . "',
		singular_name='" . 	$single . "',	
		plural_name='" . 	$plural . "',	
		hierarchical='" . 	$hierarchical . "',	
		post_formats='" . 	$post_formats . "',	
		has_archive='" . 	$archives . "',		
		title='" . 	$title . "',
		editor='" . 	$editor . "',
		author='" . 	$author . "',
		thumbnail='" . 	$thumbnail . "',
		excerpt='" . 	$excerpt . "',
		fields='" . 	$fields . "',
		comments='" . 	$comments . "',
		revisions='" . 	$revisions . "',
		menu_icon='" . 	$_POST['options-icon'] . "'

	;");	
	
	$url = get_bloginfo('wpurl') . '/wp-admin/admin.php?page=easy-content-types/easy-content-types.php?posttypes&post-type-added=1';
	header ("Location: $url");
}

Not sure why this is so bad? Ask and I’ll be happy to elaborate!

Advertisements

6 thoughts on “Zero Capability Checks or Data Validation

    • The primary things are checking whether the current user has permission to access the page and also sanitizing all of the input. Using $wpdb->prepare() is a big one.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s