Web security

Nonce

Definition

A nonce is a value, in principle random and used only once, added to an exchange to prevent it from being replayed or forged. In WordPress, a nonce is a token tied to an action, a user and their session, valid for 12 to 24 hours, mainly used to protect against CSRF attacks.

Nonce, Web security

The word stands for “number used once.” In cryptography, MDN defines it as an arbitrary value used only once in a communication, to prevent an intercepted message from being replayed, to guarantee that a response is fresh or to add randomness to a computation. On the web, however, the term covers two fairly different things that are best kept apart: the “pure” cryptographic nonce and the WordPress nonce.

How nonces are used on the web

  • In protocols: TLS handshakes and many authentication mechanisms include random one-time values so that a recorded conversation cannot simply be replayed.
  • In a Content Security Policy: the server generates a random value for each response, places it in the script-src 'nonce-…' directive and in the nonce attribute of allowed scripts. A strict CSP only runs scripts that carry the right value. MDN stresses that it must change with every response and stay unpredictable.
  • Against forged requests: a secret token added to forms proves that the request really comes from a page on the site, which blocks CSRF attacks. This is the main role of WordPress nonces.

WordPress nonces: not really nonces

The official WordPress documentation admits it: its nonces are neither numbers nor single-use. The wp_create_nonce() function computes a ten-character hash from the requested action, the user ID, their session token and a time window. The computation uses the NONCE_KEY and NONCE_SALT keys from wp-config.php, which is why a unique salt matters.

  • Lifetime: one day by default, split into two 12-hour windows. Depending on when it was created, a nonce therefore stays valid for 12 to 24 hours. The nonce_life filter changes this duration.
  • Reusable: WordPress doesn't check that a nonce has been used only once. It therefore doesn't protect against replay during its validity period.
  • Logged-out visitors: their user ID is 0, so all anonymous visitors can receive the same nonce for the same action.
Five-step diagram: WordPress creates a nonce tied to the action, the user, their session and a 12-hour window; the user submits the form with the token; a request forged on another site arrives without the right token; check_admin_referer or wp_verify_nonce rejects it; current_user_can finally checks the capability before acting

For WordPress: using nonces correctly

For a theme or plugin developer, the rule boils down to three functions and a permission check:

// In the form
wp_nonce_field( 'delete_post_' . $post_id );

// When processing
check_admin_referer( 'delete_post_' . $post_id );
if ( ! current_user_can( 'delete_post', $post_id ) ) {
    wp_die( 'You are not allowed to do this.' );
}
  • check_admin_referer() checks the nonce and the referrer on admin screens, check_ajax_referer() checks only the nonce for AJAX requests, and wp_verify_nonce() covers the other cases.
  • Name the action precisely (including the object ID) rather than using a generic name shared across the whole site.
  • Never rely on the nonce for authorization: the documentation says to protect every function with current_user_can().

Common mistakes

A good share of the plugin vulnerabilities reported each year comes from a missing nonce or a forgotten capability check, as our article on WordPress plugin vulnerabilities by the numbers shows. The typical case: an AJAX action checks the nonce but not the user's permission. Yet a nonce can leak into the code of a public page; if it is the only barrier, a mere subscriber can sometimes trigger an administrator action, which leads to privilege escalation. Our guide to WordPress roles and capabilities explains the logic of least privilege.

Another trap: a page cached for more than 12 to 24 hours serves an expired nonce, and the form fails with the message “The link you followed has expired.” Exclude pages containing forms meant for logged-in users from the cache.

Further reading

  • Article WordPress plugin vulnerabilities by the numbers

Sources

  1. WordPress Common APIs Handbook: Nonces (external link, opens in a new tab)
  2. WordPress Developer Resources: wp_create_nonce() (external link, opens in a new tab)
  3. MDN: Nonce (glossaire) (external link, opens in a new tab)
  4. OWASP: Cross-Site Request Forgery Prevention Cheat Sheet (external link, opens in a new tab)

All definitions

Start free, go Pro when you’re ready

The free version already hides WordPress and moves the login page. Pro adds aliases, two-factor authentication and monitoring.