CORS for htmx

medium4 checks

CORS decides which origins may read your API responses. A wildcard turns an authenticated API into a public one.

What htmx must have

  • Access-Control-Allow-Origin limited to known origins
  • No wildcard combined with credentials
  • No reflection of arbitrary Origin headers
  • Preflight responses only allow the methods actually used

How to fix it

  1. 1Replace wildcards with an explicit allowlist
  2. 2Validate Origin before echoing it and reject unknown origins early

htmx context

HTML-over-the-wire library that swaps fragments returned by the server.

Server responses become DOM: sanitise HTML fragments and require CSRF tokens on every hx-post.