fix: CSRF token staleness causing intermittent 403 on POST actions
Root cause: bootstrap.php rotates the CSRF token on every successful POST, but most API endpoints called echo json_encode() directly instead of apiRespond() — so the rotated token was never returned to the client. The next POST from the same page sent the now-invalid old token → 403. Refreshing the page loaded a fresh token, making it work once. Fixes: - assign_ticket.php, watch_ticket.php: switch to apiRespond() - saved_filters.php, user_preferences.php: replace all echo json_encode calls with apiRespond() (19 and 12 call sites respectively) - base.js: both apiFetch() and _apiFetchAuth() now update window.CSRF_TOKEN whenever a response includes a csrf_token field, keeping the client permanently in sync with server-side rotations Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+12
-12
@@ -13,10 +13,10 @@ $prefsModel = new UserPreferencesModel($conn);
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
try {
|
||||
$prefs = $prefsModel->getUserPreferences($userId);
|
||||
echo json_encode(['success' => true, 'preferences' => $prefs]);
|
||||
apiRespond(['success' => true, 'preferences' => $prefs]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'error' => 'Failed to fetch preferences']);
|
||||
apiRespond(['success' => false, 'error' => 'Failed to fetch preferences']);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
@@ -46,10 +46,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
setcookie('ticketsPerPage', $value, ['expires' => time() + (86400 * 365), 'path' => '/', 'httponly' => true, 'secure' => true, 'samesite' => 'Lax']);
|
||||
}
|
||||
}
|
||||
echo json_encode(['success' => true]);
|
||||
apiRespond(['success' => true]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'error' => 'Failed to save preferences']);
|
||||
apiRespond(['success' => false, 'error' => 'Failed to save preferences']);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
@@ -57,7 +57,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
// Single preference: { key, value }
|
||||
if (!isset($data['key']) || !isset($data['value'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'error' => 'Missing key or value']);
|
||||
apiRespond(['success' => false, 'error' => 'Missing key or value']);
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
|
||||
if (!in_array($key, $validKeys)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'error' => 'Invalid preference key']);
|
||||
apiRespond(['success' => false, 'error' => 'Invalid preference key']);
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -78,10 +78,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
setcookie('ticketsPerPage', $value, time() + (86400 * 365), '/');
|
||||
}
|
||||
|
||||
echo json_encode(['success' => $success]);
|
||||
apiRespond(['success' => $success]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'error' => 'Failed to save preference']);
|
||||
apiRespond(['success' => false, 'error' => 'Failed to save preference']);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
@@ -92,20 +92,20 @@ if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
|
||||
|
||||
if (!isset($data['key'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'error' => 'Missing key']);
|
||||
apiRespond(['success' => false, 'error' => 'Missing key']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$success = $prefsModel->deletePreference($userId, $data['key']);
|
||||
echo json_encode(['success' => $success]);
|
||||
apiRespond(['success' => $success]);
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'error' => 'Failed to delete preference']);
|
||||
apiRespond(['success' => false, 'error' => 'Failed to delete preference']);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
// Method not allowed
|
||||
http_response_code(405);
|
||||
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
|
||||
apiRespond(['success' => false, 'error' => 'Method not allowed']);
|
||||
|
||||
Reference in New Issue
Block a user