Fix file upload security, bind_param mismatch, and cookie flags
- upload_attachment.php: derive stored file extension from validated MIME type instead of user-supplied filename, preventing executable extension attacks (e.g. a PHP file renamed to evil.txt would now be stored as .txt) - CustomFieldModel.php: fix bind_param type string in updateDefinition() 'sssssiiiii' (10 chars) → 'sssssiiii' (9 chars) to match 9 SQL placeholders - RateLimitMiddleware.php: replace MD5 with SHA256 for rate limit file hashing - user_preferences.php: add httponly, secure, samesite=Lax flags to ticketsPerPage cookie to prevent XSS/CSRF cookie theft Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -151,10 +151,26 @@ if (!is_dir($ticketDir)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate unique filename
|
// Derive extension from validated MIME type (never from user-supplied filename)
|
||||||
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);
|
// This prevents executable extension attacks (e.g. evil.php disguised as text/plain)
|
||||||
$safeExtension = preg_replace('/[^a-zA-Z0-9]/', '', $extension);
|
$mimeToExt = [
|
||||||
$uniqueFilename = uniqid('att_', true) . ($safeExtension ? '.' . $safeExtension : '');
|
'image/jpeg' => 'jpg', 'image/png' => 'png',
|
||||||
|
'image/gif' => 'gif', 'image/webp' => 'webp',
|
||||||
|
'application/pdf' => 'pdf',
|
||||||
|
'text/plain' => 'txt', 'text/csv' => 'csv',
|
||||||
|
'application/msword' => 'doc',
|
||||||
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
|
||||||
|
'application/vnd.ms-excel' => 'xls',
|
||||||
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
|
||||||
|
'application/zip' => 'zip',
|
||||||
|
'application/x-7z-compressed' => '7z',
|
||||||
|
'application/x-tar' => 'tar',
|
||||||
|
'application/gzip' => 'gz',
|
||||||
|
'application/json' => 'json',
|
||||||
|
'application/xml' => 'xml',
|
||||||
|
];
|
||||||
|
$safeExtension = $mimeToExt[$mimeType] ?? 'bin';
|
||||||
|
$uniqueFilename = uniqid('att_', true) . '.' . $safeExtension;
|
||||||
$targetPath = $ticketDir . '/' . $uniqueFilename;
|
$targetPath = $ticketDir . '/' . $uniqueFilename;
|
||||||
|
|
||||||
// Move uploaded file
|
// Move uploaded file
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
if (!in_array($key, $validKeys)) continue;
|
if (!in_array($key, $validKeys)) continue;
|
||||||
$prefsModel->setPreference($userId, $key, $value);
|
$prefsModel->setPreference($userId, $key, $value);
|
||||||
if ($key === 'rows_per_page') {
|
if ($key === 'rows_per_page') {
|
||||||
setcookie('ticketsPerPage', $value, time() + (86400 * 365), '/');
|
setcookie('ticketsPerPage', $value, ['expires' => time() + (86400 * 365), 'path' => '/', 'httponly' => true, 'secure' => true, 'samesite' => 'Lax']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
echo json_encode(['success' => true]);
|
echo json_encode(['success' => true]);
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ class RateLimitMiddleware {
|
|||||||
$now = time();
|
$now = time();
|
||||||
|
|
||||||
// Create a hash of the IP for the filename (security + filesystem safety)
|
// Create a hash of the IP for the filename (security + filesystem safety)
|
||||||
$ipHash = md5($ip . '_' . $type);
|
$ipHash = hash('sha256', $ip . '_' . $type);
|
||||||
$filePath = self::getRateLimitDir() . '/' . $ipHash . '.json';
|
$filePath = self::getRateLimitDir() . '/' . $ipHash . '.json';
|
||||||
|
|
||||||
// Load existing rate data
|
// Load existing rate data
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ class CustomFieldModel {
|
|||||||
WHERE field_id = ?";
|
WHERE field_id = ?";
|
||||||
|
|
||||||
$stmt = $this->conn->prepare($sql);
|
$stmt = $this->conn->prepare($sql);
|
||||||
$stmt->bind_param('sssssiiiii',
|
$stmt->bind_param('sssssiiii',
|
||||||
$data['field_name'],
|
$data['field_name'],
|
||||||
$data['field_label'],
|
$data['field_label'],
|
||||||
$data['field_type'],
|
$data['field_type'],
|
||||||
|
|||||||
Reference in New Issue
Block a user