github khalidabuhakmeh/Htmx.Net 1.13.0
1.13.0: Antiforgery security fixes and htmx 2 hx-delete fix

latest release: 2.0.0-preview.2
3 hours ago

Security and bug-fix release for the htmx 1.x / 2.x line

This release fixes the antiforgery integration (app.MapHtmxAntiforgeryScript(), Html.HtmxAntiforgeryScript(), and HtmxSnippets.AntiforgeryJavaScript) on htmx 2.x, and closes several ways the antiforgery token could leak. All 1.x users should upgrade. htmx 1.x apps benefit too.

dotnet add package Htmx --version 1.13.0
dotnet add package Htmx.TagHelpers --version 1.13.0

The 2.x line of this library (htmx 4) got the matching fixes in 2.0.0-preview.2.


πŸ› Fixed: <form hx-delete> returned 400 Bad Request on htmx 2

When a form already contained the rendered __RequestVerificationToken hidden input (which the ASP.NET Core form tag helper adds automatically), the snippet skipped adding the RequestVerificationToken header. htmx 2 sends DELETE parameters in the query string. ASP.NET Core never reads the antiforgery token from there, so validation failed.

The snippet now always sends the header and removes the form-field copy from the request.

What you need to do: nothing. If you added workarounds such as htmx.config.methodsThatUseUrlParams = ["get"] or a custom htmx:configRequest listener, you can remove them.

πŸ”’ Security: antiforgery token leaked into URLs

On htmx 2, the same DELETE path put the token in the URL (/items/1?__RequestVerificationToken=…). There it ends up in server and proxy logs, browser history, and Referer headers. A <form hx-get> containing a rendered token had the same problem.

The token is now never placed in a URL:

  • Header mode (the ASP.NET Core default): the header is sent, and the form field is removed from the request.
  • Safe methods (GET, HEAD, OPTIONS, TRACE): the token is never sent, and any rendered token field is removed.
  • Form-field mode (you set AntiforgeryOptions.HeaderName = null): for verbs that htmx sends in the URL (htmx.config.methodsThatUseUrlParams, which is ["get", "delete"] in htmx 2), the token is not sent, and the console shows a [htmx.net] Antiforgery token not sent for DELETE… warning.

What you need to do: if you use form-field mode and hx-delete on htmx 2, those requests now fail closed (400 plus a console warning) instead of leaking the token. Fix it in one of these ways:

  • keep a header name configured (the default RequestVerificationToken), or
  • set methodsThatUseUrlParams to ["get"] on the config tag helper: <meta name="htmx-config" methodsThatUseUrlParams='new[] { "get" }' includeAspNetAntiforgeryToken="true" />.

πŸ”’ Security: token sent to other origins

The snippet attached the token to every non-GET request, whatever the target. With htmx 1.x (where selfRequestsOnly defaults to false), or any app that allowed cross-origin requests, the token could be sent to a third-party host. A <base href> could also point relative URLs at another origin.

The snippet now resolves each request URL against document.baseURI (exactly what htmx/XHR does) and only attaches the token when the target is the page's own origin. Rendered token fields are also removed from cross-origin requests.

What you need to do: nothing, unless you intentionally sent the antiforgery token to a different origin (for example, a separate API host). That never worked with ASP.NET Core's cookie-bound tokens anyway. Use a dedicated auth mechanism for cross-origin APIs.

⚠️ Changed: selfRequestsOnly is no longer forced to false

HtmxConfigMetaTagHelper.SelfRequestsOnly was a plain bool, so every <meta name="htmx-config"> rendered "selfRequestsOnly":false. That silently turned off htmx 2's protective default (true). The property is now bool? and is left out of the config unless you set it.

What you need to do:

  • htmx 2 users: cross-origin htmx requests are now blocked by htmx's default, as htmx intends. If your app needs cross-origin htmx requests, opt in explicitly: <meta name="htmx-config" selfRequestsOnly="false" ... />.
  • htmx 1.x users: no change (htmx 1.x defaults to false).
  • C# code that sets SelfRequestsOnly = true/false still compiles. Code that reads it now gets a bool?.

⚠️ Removed: automatic token refresh after hx-boost navigation

The previous snippet parsed the HTML of every boosted response with DOMParser and replaced htmx.config.antiForgery with whatever <meta name="htmx-config"> it found. A response body can't prove that it's a genuine full-page navigation, so attacker-influenced content (fragments, out-of-band swaps) could inject a bogus token configuration. The 2.x line rejected this pattern in its security audit, and it is removed here too.

Antiforgery tokens still refresh on every full page load.

What you need to do: if you use hx-boost and the user's identity changes mid-session (sign-in or sign-out, which rotates the antiforgery token), force a full page reload after that action:

// after a successful sign-in / sign-out handled via a boosted request
Response.Htmx(h => h.Refresh());   // sends HX-Refresh: true

Without this, the next unsafe request after a boosted sign-in fails with 400 until the user reloads the page.

Other changes

  • HEAD, OPTIONS, and TRACE are now treated as safe methods (previously only GET was skipped).
  • The "already registered" guard moved from document.body.attributes to window.__htmxAntiforgery, which boosted body swaps can't wipe.
  • The embedded antiforgerySnippet.min.js is generated with terser@5.36.0. New unit tests check that both embedded scripts contain the security guards, so a stale minified file fails the build.
  • JavaScript/README.md documents the snippet's security properties.
  • The README's antiforgery section was rewritten. The sample app now uses htmx 2.0.11. global.json is pinned to the .NET 10 SDK (matching CI).

Verified

Browser-tested (headless Chromium) on htmx 1.9.12 and htmx 2.0.11, in header mode and form-field mode:

  • POST, PUT, PATCH, and DELETE from hx-* buttons and from <form>s with rendered tokens: all 200, with no token in any URL.
  • Cross-origin targets and <base href> redirection: no token sent.
  • Form-field mode with DELETE on htmx 2: fails closed with a warning.

Full Changelog: 1.12.0...1.13.0

Don't miss a new Htmx.Net release

NewReleases is sending notifications on new releases.