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
<form method="post" action="/logout">
{! @csrf_field() }
<button>Sign out</button>
</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.