getkirby / kql

Kirby's Query Language API combines the flexibility of Kirby's data structures, the power of GraphQL and the simplicity of REST.
https://getkirby.com
MIT License
145 stars 5 forks source link

[Headless] Question - Is this csrf-token protected? #27

Closed eder13 closed 2 years ago

eder13 commented 3 years ago

Hello,

I would like to use the query language extension to effectively use Kirby as a headless REST CMS (seperated React frontend). The only problem I see is, that this query language is using POST Requests, and afaik kirby uses csrf protection and there seems to be no (safe) way to get the csrf-token on the frontend.

But is it even csrf protected?

PS: CSRF Protection would still be applied, but instead of Kirby I would integrate it into my backend API which would fetch the kirby cms data which is protected via basic auth so that only the backend api has access to kirby.

Thanks in advance! -eder

lukasbestle commented 2 years ago

The KQL endpoint is implemented as an API route. If a session is used for authentication, the API checks for the CSRF token by default. In this case you can use the csrf() helper in your template code to access the token and provide it to your frontend code:

<script>
window.csrf = "<?= csrf() ?>"
</script>

If you want to use KQL via your backend API as a proxy, you can use HTTP Basic auth. For basic auth, no CSRF token is required.