Index: main.php =================================================================== --- main.php (revision 1379242) +++ main.php (working copy) @@ -23,8 +23,11 @@ function website_snapshot_admin_scripts() { wp_enqueue_script('jquery'); - wp_register_script('website_snapshot_main_js', WEBSITE_SNAPSHOT__PLUGIN_URL . 'js/main.js'); + wp_register_script('website_snapshot_main_js', WEBSITE_SNAPSHOT__PLUGIN_URL . 'js/main.js', array('jquery'), '0.2.1-request-guard1'); wp_enqueue_script('website_snapshot_main_js'); + wp_localize_script('website_snapshot_main_js', 'website_snapshot_security', array( + 'nonce' => wp_create_nonce('website_snapshot_request') + )); } /** @@ -132,13 +135,25 @@ // ajax hooks for delete add_action('wp_ajax_delete_snapshot', 'website_snapshot_delete_snapshot'); -add_action('wp_ajax_nopriv_delete_snapshot', 'website_snapshot_delete_snapshot'); /** * website_snapshot_delete_snapshot delete the snapshot with the specified name */ +function website_snapshot_verify_request() { + if (!current_user_can('manage_options')) { + wp_send_json(array('message' => 'Snapshot administration is not permitted'), 403); + return false; + } + if (false === check_ajax_referer('website_snapshot_request', 'security', false)) { + wp_send_json(array('message' => 'Snapshot request verification failed'), 403); + return false; + } + return true; +} + function website_snapshot_delete_snapshot() { - $name = get_sanitize_input_name($_POST['name']); + if (!website_snapshot_verify_request()) return; + $name = get_sanitize_input_name(isset($_POST['name']) ? $_POST['name'] : null); // delete the tar file exec('rm -rf ' . get_home_path() . '/' . $name . '.tar'); @@ -155,13 +170,13 @@ // ajax hooks for create add_action('wp_ajax_add_snapshot', 'website_snapshot_add_snapshot'); -add_action('wp_ajax_nopriv_add_snapshot', 'website_snapshot_add_snapshot'); /** * website_snapshot_add_snapshot insert the new snapshot to the snapshot table */ function website_snapshot_add_snapshot() { - $name = get_sanitize_input_name($_POST['name']); + if (!website_snapshot_verify_request()) return; + $name = get_sanitize_input_name(isset($_POST['name']) ? $_POST['name'] : null); // replace any space by underscore $name = str_replace(' ', '_', $name); @@ -205,7 +220,7 @@ */ function get_sanitize_input_name($name) { check_input_name($name); - return filter_var($name, FILTER_SANITIZE_STRING); + return $name; } /** @@ -212,21 +227,13 @@ * check_input_name check the input name */ function check_input_name($name) { - $isNameEmpty = !@isset($name); - $isNameTooLong = strlen($name) > 200; - - if($isNameEmpty || $isNameTooLong) { + if (!is_string($name) || !preg_match('/\A[A-Za-z0-9][A-Za-z0-9 _-]{0,199}\z/', $name)) { set_error_headers(); - - if($isNameEmpty) { - die(json_encode(array('message' => 'Snapshot name is required'))); - - } else if($isNameTooLong) { - die(json_encode(array('message' => 'Snapshot name is too long'))); - } + die(json_encode(array('message' => 'Snapshot name must be 1-200 ASCII letters, numbers, spaces, underscores or hyphens and start with a letter or number'))); } } + /** * set_error_headers */ @@ -243,7 +250,7 @@ * @return string snapshot url */ function website_snapshot_generate_static_site($name, $permalinks=null) { - $name = esc_html($name); // $name is used in some exec + $name = str_replace(' ', '_', get_sanitize_input_name($name)); $static_site_dir = str_replace('http://', '', get_site_url()); $output_path = plugin_dir_path( __FILE__ ) . 'output/'; $snapshot_path = $output_path . $name; Index: js/main.js =================================================================== --- js/main.js (revision 1379242) +++ js/main.js (working copy) @@ -14,7 +14,8 @@ $errorOutput.hide(); var data = { name: $('#snapshot-name').val(), - action: 'add_snapshot' + action: 'add_snapshot', + security: website_snapshot_security.nonce } if(data.name.length === 0) { @@ -77,7 +78,8 @@ function delete_snapshot() { var data = { name: this.id.split('-snapshot-')[1], - action: 'delete_snapshot' + action: 'delete_snapshot', + security: website_snapshot_security.nonce } $.ajax({