KScript docs
EN

Sign-in: KScript.Auth

Forms and CSRF

A web route whose file imports KScript.Auth checks a CSRF token on every POST, PUT, PATCH and DELETE. That stops another site from submitting a form as someone who is signed in. Put the token in each form with {! @csrf_field() }, in a page or a component:

<!-- _components/sign-out-button.kscript -->
<import package="KScript.Web" />
<import package="KScript.Auth" />

<type name="SignOutButton" extends="Component">
  <fn name="render" value="```html
      &lt;form method=&quot;post&quot; action=&quot;/logout&quot;>
        {! @csrf_field() }
        &lt;button>Sign out&lt;/button>
      &lt;/form>
      ```" />
</type>
// _components/sign-out-button.kscript
import KScript.Web
import KScript.Auth

type SignOutButton extends Component {
  fn render() => ```html
    <form method="post" action="/logout">
      {! @csrf_field() }
      <button>Sign out</button>
    </form>
    ```
}

Scripts in the page send @csrf_token() as an X-CSRF-Token header instead. A live component's actions need no field: each one carries a proof signed with the session's CSRF token. A request without the right token gets 419 Page Expired. API routes aren't checked, since they use tokens rather than cookies, and neither is a request with a bearer token and no cookie. Set http.auth.csrf to false to turn the check off.

Edit this page on GitHub