This works now.
I have two pages one works with fetch, but this page rely on webgl, the other page of the site supports older browsers too with jquery and XMLHttpRequest:
var xhr = new XMLHttpRequest();
$.getJSON('/lighttable.json')
But if I woud use csrf token, where should I check the token?
If you’re sending the token via a header, like you do in the opening post of this thread, you can get that token via $kirby->request()->csrf(). Therefore you should be able to check if the submitted token is valid with something like this:
$submittedToken = kirby()->request()->csrf();
if(csrf($submittedToken)) {
//tokens match load and return your json
} else {
//show error
}
PS: csrf tokens only prevent some kind of xss attacks, their purpose is not to prevent people from stealing your data and use it on other sites. Other sites (on the server side) could just request a token from yours and then request the json file with that. Adding a csrf check doesn’t really add anything more to keep your data secret than the default “same origin policy” in browsers does.
Thank you @rasteiner for your reply,
does the header check with fetch or XMLHttpRequest secure anything or could I build a page somewhere else end fetch the data, if one know the json url?
Taki
anyone could write, for example, a php script that requests stuff from your server without you noticing any difference. When doing their request they just have to send the X-Requested-With: fetch header with it and there’s nothing reasonable you can do about that.
You have no guarantee that the headers you receive are “real”. Just like you can’t prevent people from just manually saving the json file to their hard disk by opening a developer console.
if it’s sensible data it should go behind a login, or some other kind of auth method;
if you just don’t want other websites to lazily fetch your json directly in a users browser, you don’t need to do anything because browsers won’t allow that (unless you explicitly allow that kind of resource sharing - I doubt you do).
Edit:
of course, adding something like the csrf check or the header check, while not being a “proper security” measure, could just be enough hassle for people to discourage them from trying further. It really depends on the importance of the data, I guess