Lint / PHP (phpcs PSR-12) (push) Successful in 38s
Lint / JS (eslint) (push) Successful in 15s
Lint / PHP requirements (version + extensions) (push) Successful in 38s
Lint / Notify on failure (push) Skipped
Security / PHP Security (semgrep) (push) Successful in 1m27s
Lint / Deploy (push) Successful in 2s
api/bootstrap.php's centralized CSRF handling echoes CsrfMiddleware::getToken() on a 403 rejection specifically so lt.api's client-side resync (assets/js/base.js) can recover once window.CSRF_TOKEN goes stale (token expiry, or a write in another tab rotating the shared session-scoped token). 12 endpoints duplicate CsrfMiddleware::validateToken() inline instead of routing through bootstrap.php, and their 403 body omitted csrf_token entirely — custom_fields.php, clone_ticket.php, delete_comment.php, delete_attachment.php, bulk_operation.php, generate_api_key.php, manage_templates.php, manage_recurring.php, revoke_api_key.php, manage_workflows.php, ticket_dependencies.php, and upload_attachment.php. Once a client's token drifted out of sync, the next write to any of these 12 endpoints returned a 403 with no way to self-heal — every subsequent write to any endpoint kept failing until a manual reload, since the resync mechanism was only wired up on a minority of the app's write surface. Took the minimal fix the issue names as sufficient (add 'csrf_token' => CsrfMiddleware::getToken() to each rejection body) rather than restructuring all 12 through bootstrap.php, to avoid behavioral risk from rewiring each endpoint's differing auth/bootstrapping. generate_api_key.php and revoke_api_key.php threw a generic Exception for this case (swallowed into a plain error-message response with no room for extra fields), so those two now short-circuit with a direct JSON response instead, matching the other 10. Verified end-to-end against real running endpoints with a real session and real MariaDB: sent a wrong CSRF token to one endpoint of each response shape (plain json_encode, ResponseHelper::error, and the formerly exception-based path) and confirmed all three now return the current valid csrf_token in the 403 body. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lhz7pGMaoTfL5sdYS5XiKv
143 lines
3.9 KiB
PHP
143 lines
3.9 KiB
PHP
<?php
|
|
|
|
// API endpoint for revoking API keys (Admin only)
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 0);
|
|
|
|
// Apply rate limiting
|
|
require_once dirname(__DIR__) . '/middleware/RateLimitMiddleware.php';
|
|
RateLimitMiddleware::apply('api');
|
|
|
|
ob_start();
|
|
|
|
try {
|
|
// Load config
|
|
require_once dirname(__DIR__) . '/config/config.php';
|
|
require_once dirname(__DIR__) . '/helpers/Database.php';
|
|
|
|
// Load models
|
|
require_once dirname(__DIR__) . '/models/ApiKeyModel.php';
|
|
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
|
|
|
|
// Check authentication via session
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
if (!isset($_SESSION['user']) || !isset($_SESSION['user']['user_id'])) {
|
|
http_response_code(401);
|
|
throw new Exception("Authentication required");
|
|
}
|
|
|
|
// Check admin privileges
|
|
if (!isset($_SESSION['user']['is_admin']) || !$_SESSION['user']['is_admin']) {
|
|
http_response_code(403);
|
|
throw new Exception("Admin privileges required");
|
|
}
|
|
|
|
// CSRF Protection
|
|
require_once dirname(__DIR__) . '/middleware/CsrfMiddleware.php';
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$csrfToken = $_SERVER['HTTP_X_CSRF_TOKEN'] ?? '';
|
|
if (!CsrfMiddleware::validateToken($csrfToken)) {
|
|
ob_end_clean();
|
|
http_response_code(403);
|
|
header('Content-Type: application/json');
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Invalid CSRF token',
|
|
'csrf_token' => CsrfMiddleware::getToken()
|
|
]);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Only allow POST
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
throw new Exception("Method not allowed");
|
|
}
|
|
|
|
// Get request data
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
if (!$input) {
|
|
http_response_code(400);
|
|
throw new Exception("Invalid request data");
|
|
}
|
|
|
|
$keyId = (int)($input['key_id'] ?? 0);
|
|
|
|
if ($keyId <= 0) {
|
|
http_response_code(400);
|
|
throw new Exception("Valid key ID is required");
|
|
}
|
|
|
|
// Use centralized database connection
|
|
$conn = Database::getConnection();
|
|
|
|
// Get key info for audit log
|
|
$apiKeyModel = new ApiKeyModel($conn);
|
|
$keyInfo = $apiKeyModel->getKeyById($keyId);
|
|
|
|
if (!$keyInfo) {
|
|
http_response_code(404);
|
|
throw new Exception("API key not found");
|
|
}
|
|
|
|
if (!$keyInfo['is_active']) {
|
|
http_response_code(409);
|
|
throw new Exception("API key is already revoked");
|
|
}
|
|
|
|
// Revoke the key
|
|
$success = $apiKeyModel->revokeKey($keyId);
|
|
|
|
if (!$success) {
|
|
http_response_code(500);
|
|
throw new Exception("Failed to revoke API key");
|
|
}
|
|
|
|
// Log the action
|
|
$auditLog = new AuditLogModel($conn);
|
|
$auditLog->log(
|
|
$_SESSION['user']['user_id'],
|
|
'revoke',
|
|
'api_key',
|
|
$keyId,
|
|
['key_name' => $keyInfo['key_name'], 'key_prefix' => $keyInfo['key_prefix']]
|
|
);
|
|
|
|
// Clear output buffer
|
|
ob_end_clean();
|
|
|
|
// Return success
|
|
header('Content-Type: application/json');
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'API key revoked successfully'
|
|
]);
|
|
} catch (Exception $e) {
|
|
ob_end_clean();
|
|
header('Content-Type: application/json');
|
|
|
|
// Preserve any specific status set before the throw (401/403/404/409/...);
|
|
// only fall back to 500 when nothing more specific was set.
|
|
$code = http_response_code();
|
|
if (!is_int($code) || $code < 400) {
|
|
$code = 500;
|
|
}
|
|
http_response_code($code);
|
|
|
|
if ($code >= 500) {
|
|
error_log("Revoke API key error: " . $e->getMessage());
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'An internal error occurred'
|
|
]);
|
|
} else {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|