Fix API security: dependency/visibility leaks, authz, CSRF, comment spoofing
- ticket_dependencies.php: pass current user id/groups/is_admin into the visibility-filtered DependencyModel methods; drop (int) casts that stripped leading zeros from varchar ticket_ids - update_ticket.php: authorize visibility changes (admin or creator only); enforce requires_comment transitions server-side (400 + requires_comment flag so the client can prompt-and-retry); return proper 401/400/403 - add_comment.php: take commenter name from the session not the client (anti-spoofing); validate parent_comment_id belongs to the ticket; reject empty comments; pass ticket visibility to notifications so non-public comment bodies aren't leaked - add_comment/update_comment/bulk_operation: validate CSRF for all state-changing methods, not just POST - bootstrap.php: return the current CSRF token on rejection and never rotate it on a rejected request, so a desynced client can auto-recover - correct auth->401 and validation->400 status codes across these endpoints Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -80,6 +80,9 @@ if (!isset($_SESSION['user']) || !isset($_SESSION['user']['user_id'])) {
|
||||
|
||||
$userId = $_SESSION['user']['user_id'];
|
||||
$currentUser = $_SESSION['user'];
|
||||
$isAdmin = $currentUser['is_admin'] ?? false;
|
||||
// users.groups is a comma-separated string; the dependency model expects an array.
|
||||
$userGroups = array_values(array_filter(array_map('trim', explode(',', $currentUser['groups'] ?? ''))));
|
||||
|
||||
// CSRF Protection for POST/DELETE
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' || $_SERVER['REQUEST_METHOD'] === 'DELETE') {
|
||||
@@ -121,14 +124,14 @@ try {
|
||||
}
|
||||
|
||||
// Verify user can access this ticket
|
||||
$ticket = $ticketModel->getTicketById((int)$ticketId);
|
||||
$ticket = $ticketModel->getTicketById($ticketId);
|
||||
if (!$ticket || !$ticketModel->canUserAccessTicket($ticket, $currentUser)) {
|
||||
ResponseHelper::notFound('Ticket not found');
|
||||
}
|
||||
|
||||
try {
|
||||
$dependencies = $dependencyModel->getDependencies($ticketId);
|
||||
$dependents = $dependencyModel->getDependentTickets($ticketId);
|
||||
$dependencies = $dependencyModel->getDependencies($ticketId, $userId, $userGroups, $isAdmin);
|
||||
$dependents = $dependencyModel->getDependentTickets($ticketId, $userId, $userGroups, $isAdmin);
|
||||
} catch (Exception $e) {
|
||||
error_log('Query error in ticket_dependencies.php GET: ' . $e->getMessage());
|
||||
ResponseHelper::serverError('Failed to retrieve dependencies');
|
||||
@@ -157,11 +160,11 @@ try {
|
||||
}
|
||||
|
||||
// Verify user can access both tickets before creating dependency
|
||||
$srcTicket = $ticketModel->getTicketById((int)$ticketId);
|
||||
$srcTicket = $ticketModel->getTicketById($ticketId);
|
||||
if (!$srcTicket || !$ticketModel->canUserAccessTicket($srcTicket, $currentUser)) {
|
||||
ResponseHelper::notFound('Ticket not found');
|
||||
}
|
||||
$tgtTicket = $ticketModel->getTicketById((int)$dependsOnId);
|
||||
$tgtTicket = $ticketModel->getTicketById($dependsOnId);
|
||||
if (!$tgtTicket || !$ticketModel->canUserAccessTicket($tgtTicket, $currentUser)) {
|
||||
ResponseHelper::notFound('Target ticket not found');
|
||||
}
|
||||
@@ -205,7 +208,7 @@ try {
|
||||
}
|
||||
|
||||
// Verify user can access the source ticket
|
||||
$srcTicket = $ticketModel->getTicketById((int)$ticketId);
|
||||
$srcTicket = $ticketModel->getTicketById($ticketId);
|
||||
if (!$srcTicket || !$ticketModel->canUserAccessTicket($srcTicket, $currentUser)) {
|
||||
ResponseHelper::notFound('Ticket not found');
|
||||
}
|
||||
@@ -235,7 +238,7 @@ try {
|
||||
ResponseHelper::notFound('Dependency not found');
|
||||
}
|
||||
|
||||
$depTicket = $ticketModel->getTicketById((int)$depRow['ticket_id']);
|
||||
$depTicket = $ticketModel->getTicketById($depRow['ticket_id']);
|
||||
if (!$depTicket || !$ticketModel->canUserAccessTicket($depTicket, $currentUser)) {
|
||||
ResponseHelper::forbidden('Access denied');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user