Goal
Find and fix typical vulnerabilities in WooCommerce code.
Key concepts
- Nonce — CSRF protection:
wp_nonce_field()/check_ajax_referer()/ a header for REST - Permissions —
current_user_can( 'manage_woocommerce' ),edit_shop_orders; a nonce doesn't replace a permission check - IDOR: a customer sees only their own orders —
$order->get_customer_id() === get_current_user_id() - Sanitize on input, escape on output (
esc_html,esc_attr,esc_url,wp_kses_post) - SQL only through
$wpdb->prepare() - Secrets (API keys, webhook secret) — not in code and not in public options
Diagram
flowchart LR
IN["Request"] --> N{"Nonce ok?"}
N -- no --> X["403"]
N -- yes --> CAP{"Permission / owner?"}
CAP -- no --> X
CAP -- yes --> SAN["Sanitize<br/>and validate"]
SAN --> ACT["Action through CRUD<br/>or $wpdb->prepare"]
ACT --> ESC["Escaped output"]
Code
Code review: find 5 problems.
<?php
add_action( 'wp_ajax_my_order_note', function () {
$order = wc_get_order( $_POST['order_id'] );
$order->add_order_note( $_POST['note'] );
global $wpdb;
$wpdb->query( "UPDATE {$wpdb->prefix}my_log SET note = '{$_POST['note']}' WHERE order_id = {$_POST['order_id']}" );
echo 'Saved: ' . $_POST['note'];
wp_die();
} );
Fixed:
<?php
add_action( 'wp_ajax_my_order_note', static function () {
check_ajax_referer( 'my_order_note' ); // 1. CSRF
$order_id = absint( $_POST['order_id'] ?? 0 );
$note = sanitize_textarea_field( wp_unslash( $_POST['note'] ?? '' ) );
$order = wc_get_order( $order_id );
if ( ! $order || (int) $order->get_customer_id() !== get_current_user_id() ) {
wp_send_json_error( null, 403 ); // 2. IDOR / permissions
}
$order->add_order_note( $note );
global $wpdb;
$wpdb->update( "{$wpdb->prefix}my_log", [ 'note' => $note ], [ 'order_id' => $order_id ] ); // 3. SQL injection
wp_send_json_success( [ 'message' => esc_html( $note ) ] ); // 4. XSS, 5. a proper response
} );
Common mistakes
| Symptom | Cause | Fix |
|---|---|---|
| A customer sees someone else's order by ID | No owner check (IDOR) | Compare get_customer_id() or check the order key |
| There is a nonce, but the attack worked | A nonce without a permission check | Nonce + capability / owner |
| XSS in the orders admin | Meta printed without escaping | esc_html() on output |
Weak and strong answer
Question: How do you protect an AJAX handler that changes an order?
Weak answer. Add a nonce.
Strong answer. Four layers: a nonce against CSRF, a check that the user has the right to this exact order (the owner or
edit_shop_orders), sanitizing and validating the input, escaping the output. Changes go through CRUD, SQL only throughprepareor$wpdb->update. A nonce on its own neither authenticates nor authorizes.
Why the second is stronger: The candidate tells CSRF from authorization and knows IDOR — a typical store vulnerability.
Practice
Review the snippet above yourself before you look at the fix. In the terminal open the same file and run PHPCS with the security sniffs: compare what it found with your list.
Then find every wp_ajax_ and register_rest_route in your plugin and check each one against the checklist: nonce, permissions, sanitizing, escaping, SQL.
Interview Questions
No. A nonce protects against CSRF — a forged request on behalf of the user. Whether the user has the right to the action is checked by current_user_can() or an owner check of the resource.
Insecure Direct Object Reference: a customer changes the order_id in a request and gets someone else's order. The protection is a check that the order belongs to the current user, or access by the order key.
Sanitize on input, before saving. Escape on output, in the output context (HTML, attribute, URL). One doesn't replace the other.