Skip to content

Bug: RPC and database calls break on pages cached longer than ~24 hours #176

Closed

Description

@vinnysgreen

Summary

Blockstudio injects its client credentials into the page HTML. Those credentials expire, so a page served from a cache for longer than that starts failing on RPC/database calls. The fix should fetch a fresh token when the page loads, the way islands already avoid embedding an expiring credential in cached HTML.

Current behavior / evidence

  • Rpc::get_client_script() injects a <script id="blockstudio-fn"> into </head> (via the blockstudio/buffer/output filter) containing both wp_create_nonce('wp_rest') and Csrf::generate() as X-WP-Nonce / X-BS-Token.
  • Database::get_client_script() does the same for bs.db(), so this is not RPC-only.
  • Csrf::generate() is wp_hash( wp_nonce_tick() . '|blockstudio_csrf' ), and Csrf::verify() accepts only the current and previous tick. WordPress nonce ticks rotate every 12 hours, so a token baked into cached HTML is valid for 12–24 hours depending on where in the tick it was generated — not a guaranteed day.
  • The WP REST nonce has the same ~24-hour ceiling.

Reproduction

  1. Put a page with a public RPC function (or a db.php schema) behind a page cache with a TTL over 24 hours, or serve a stale cached copy.
  2. Load the cached page after the token has aged out.
  3. Call the public function. It fails with a CSRF error (blockstudio_fn_csrf, and the database equivalent), even though nothing about the site changed.

Functions set to 'open' are unaffected, since they skip the token check.

Expected behavior

A cached page should keep working. Either:

  • bs.fn() / bs.db() fetch a fresh token from a small, uncached, same-origin endpoint on page load (and/or retry once with a fresh token after a 403), or
  • public calls follow the island pattern and validate the request server-side without relying on a credential embedded in cached HTML.

Any fresh-token endpoint must itself be excluded from page caching, as the island fragment endpoint is.

Note on scope

Blockstudio's own static prerender docs say nonce-bearing pages are not prerendered, so this bites mainly external / long-TTL page caches. That's still the common case for anyone with a page cache, which is why this is filed as a bug rather than a docs note.

Activity

  1. dnnsjsk commented on Oct 24, 2026

    @dnnsjsk
    Contributor

    Fixed and released in Blockstudio 7.7.8. RPC, database calls, and realtime polling now obtain fresh visitor credentials lazily outside cached HTML and retry once when those credentials expire, without weakening permission checks. Regression coverage includes cached clients, expired nonce/CSRF recovery, authenticated requests, and rejected cross-origin requests.

    Please purge existing full-page/CDN caches after upgrading so previously cached pages load the updated client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions