fix: only add CSRF body field on POST requests, not GET

This commit is contained in:
Claus Lohmar 2026-07-24 10:51:20 +00:00
parent 037791bbd5
commit 78cae8dea4

View file

@ -23,12 +23,13 @@ function showToast(message, type) {
async function apiFetch(path, opts) { async function apiFetch(path, opts) {
opts = opts || {}; opts = opts || {};
opts.headers = opts.headers || {}; opts.headers = opts.headers || {};
// Send CSRF token both as header AND in POST body (Zoraxy validates via form field) var method = (opts.method || 'GET').toUpperCase();
var isPost = (method === 'POST' || method === 'PUT' || method === 'PATCH');
if (csrfToken && csrfToken !== '{{.csrfToken}}') { if (csrfToken && csrfToken !== '{{.csrfToken}}') {
opts.headers['X-Zoraxy-Csrf'] = csrfToken; opts.headers['X-Zoraxy-Csrf'] = csrfToken;
opts.headers['X-CSRF-Token'] = csrfToken; // For POST requests, include token in body (Zoraxy validates via form field)
// For POST requests, also include in body if (isPost) {
if (!opts.method || opts.method.toUpperCase() === 'POST') {
if (opts.body && typeof opts.body === 'object') { if (opts.body && typeof opts.body === 'object') {
opts.body.csrfToken = csrfToken; opts.body.csrfToken = csrfToken;
} else { } else {
@ -45,7 +46,6 @@ async function apiFetch(path, opts) {
try { try {
var data = JSON.parse(text); var data = JSON.parse(text);
} catch (e) { } catch (e) {
// Zoraxy may intercept error responses and return HTML
throw new Error('Unexpected response (status ' + res.status + '). Check plugin is running.'); throw new Error('Unexpected response (status ' + res.status + '). Check plugin is running.');
} }
if (!res.ok) { if (!res.ok) {