Files
jupyterhub/jsx/src/util/jhapiUtil.js
Min RK abe1136cba Use XSRF tokens for cross-site protections
Removes all Referer checks, which have proven unreliable and have never been particularly strong

We can use XSRF on paths for more robust inter-path protections.

- `_xsrf` is added for forms via hidden input
- xsrf check is additionally applied to GET requests on API endpoints
2023-01-16 09:35:33 +01:00

23 lines
647 B
JavaScript

const jhdata = window.jhdata || {};
const base_url = jhdata.base_url || "/";
const xsrfToken = jhdata.xsrf_token;
export const jhapiRequest = (endpoint, method, data) => {
let api_url = `${base_url}hub/api`;
let suffix = "";
if (xsrfToken) {
// add xsrf token to url parameter
var sep = endpoint.indexOf("?") === -1 ? "?" : "&";
suffix = sep + "_xsrf=" + xsrf_token;
}
return fetch(api_url + endpoint + suffix, {
method: method,
json: true,
headers: {
"Content-Type": "application/json",
Accept: "application/jupyterhub-pagination+json",
},
body: data ? JSON.stringify(data) : null,
});
};