github khalidabuhakmeh/Htmx.Net 2.0.0-preview.2
2.0.0-preview.2: Antiforgery token leak fixes (htmx 4)

pre-release3 hours ago

Security fix for the htmx 4 antiforgery integration (pre-release)

This preview fixes antiforgery token leaks in the htmx 4 antiforgery snippet (app.MapHtmxAntiforgeryScript(), Html.HtmxAntiforgeryScript(), HtmxSnippets). Everyone on 2.0.0-preview.1 should upgrade.

dotnet add package Htmx --version 2.0.0-preview.2
dotnet add package Htmx.TagHelpers --version 2.0.0-preview.2

Still on htmx 1.x/2.x? The equivalent fixes shipped in 1.13.0.


Background: why the token could leak

When htmx 4 fires htmx:config:request, it has already gathered every form value into ctx.request.body, for every HTTP method. That includes the hidden __RequestVerificationToken input that ASP.NET Core's form tag helper renders automatically. After the event, htmx moves the values for any method matching /GET|DELETE/ into the URL query string. The preview.1 snippet didn't account for either fact.

πŸ”’ Security: token placed in URLs

Scenario preview.1 preview.2
<form hx-delete> with a rendered token (header mode) 200, but token also in the URL 200, token in header only
<form hx-get> with a rendered token token in the URL token removed
hx-delete in form-field mode (HeaderName = null) snippet added the token to the URL, still 400 token not sent, console warning
Custom hx-method matching /GET|DELETE/ (e.g. XGET) in form-field mode token in the URL token not sent, console warning

Tokens in URLs end up in server and proxy logs, browser history, and Referer headers.

Behavior now:

  • Header mode (the ASP.NET Core default, RequestVerificationToken): the header is always sent, and the form-field copy is removed from the request.
  • Safe methods (GET, HEAD, OPTIONS, TRACE): no token is sent, and any rendered token field is removed.
  • Form-field mode: for any method htmx sends as URL parameters, the token is not sent, and the console logs [htmx.net] Antiforgery token not sent for DELETE….

πŸ”’ Security: rendered token sent to other origins

The same-origin guard (which resolves the target against document.baseURI) correctly kept the snippet from adding the token to cross-origin requests. But it returned early and left a rendered token field in the request. With htmx.config.mode = "cors", or a <base href> pointing elsewhere, that token went to the other origin in the body (POST) or the URL (GET/DELETE). The field is now removed from every cross-origin request.

βœ… What you need to do

  • Header mode (the default): nothing. Your requests behave as before, minus the leaks.
  • Form-field mode with hx-delete: these requests now fail closed (400 plus a console warning) instead of putting the token in the URL. Configure an antiforgery header name. It's the ASP.NET Core default, so just don't set AntiforgeryOptions.HeaderName = null. You can also switch the action to hx-post.
  • Workarounds: if you added custom htmx:config:request listeners to strip the token, you can remove them.

Other

  • JavaScript/README.md security notes now describe the strip behavior and the form-field-mode rules.
  • Added E2E (Playwright, real htmx 4.0.0) coverage for: header-mode form DELETE (no token in the URL), form-field-mode DELETE (fails closed), cross-origin forms with rendered tokens, and hx-get forms.
  • The docs site's antiforgery guide is updated for both 1.13.0 and 2.0.0-preview.2.

Everything else from 2.0.0-preview.1 still applies: htmx 4 only, net8.0/net10.0, hxn-* URL attributes. See the migration guide.

Full Changelog: 2.0.0-preview.1...2.0.0-preview.2

Don't miss a new Htmx.Net release

NewReleases is sending notifications on new releases.