Bỏ qua để đến nội dung

Các phương pháp bảo mật tốt nhất

2.5.x ✅ 4.0.x ✅

Tài liệu này cung cấp các phương pháp thực hành tốt nhất về bảo mật toàn diện cho các nhà phát triển mô-đun XOOPS. Việc làm theo các nguyên tắc này sẽ giúp đảm bảo rằng modules của bạn được an toàn và không gây ra các lỗ hổng trong quá trình cài đặt XOOPS.

Mọi nhà phát triển XOOPS phải tuân theo các nguyên tắc bảo mật cơ bản sau:

  1. Phòng thủ chuyên sâu: Triển khai nhiều lớp kiểm soát bảo mật
  2. Đặc quyền tối thiểu: Chỉ cung cấp các quyền truy cập cần thiết tối thiểu
  3. Xác thực đầu vào: Không bao giờ tin tưởng đầu vào của người dùng
  4. Bảo mật theo mặc định: Bảo mật phải là cấu hình mặc định
  5. Giữ nó đơn giản: Các hệ thống phức tạp khó bảo mật hơn
  • CSRF-Protection - Hệ thống Token và XoopsSecurity class
  • Vệ sinh đầu vào - MyTextSanitizer và xác thực
  • SQL-Injection-Prevention - Thực hành bảo mật cơ sở dữ liệu

Trước khi phát hành mô-đun của bạn, hãy xác minh:

  • Tất cả các dạng token include XOOPS
  • [] Tất cả thông tin đầu vào của người dùng đều được xác thực và vệ sinh
  • [] Tất cả đầu ra đều được thoát đúng cách
  • [] Tất cả các truy vấn cơ sở dữ liệu đều sử dụng các câu lệnh được tham số hóa
  • Tệp uploads được xác thực hợp lệ
  • Việc kiểm tra xác thực và ủy quyền được thực hiện
  • [] Xử lý lỗi không tiết lộ thông tin nhạy cảm
  • [] Cấu hình nhạy cảm được bảo vệ
  • [] Thư viện của bên thứ ba được cập nhật
  • Kiểm tra bảo mật đã được thực hiện
// Check if user is logged in
if (!is_object($GLOBALS['xoopsUser'])) {
redirect_header(XOOPS_URL, 3, _NOPERM);
exit();
}
// Check if user has permission to access this module
if (!$GLOBALS['xoopsUser']->isAdmin($xoopsModule->mid())) {
redirect_header(XOOPS_URL, 3, _NOPERM);
exit();
}
// Check specific permission
$moduleHandler = xoops_getHandler('module');
$module = $moduleHandler->getByDirname('mymodule');
$moduleperm_handler = xoops_getHandler('groupperm');
$groups = $GLOBALS['xoopsUser']->getGroups();
if (!$moduleperm_handler->checkRight('mymodule_view', $item_id, $groups, $module->getVar('mid'))) {
redirect_header(XOOPS_URL, 3, _NOPERM);
exit();
}
// Create permission in install/update function
$gpermHandler = xoops_getHandler('groupperm');
$gpermHandler->deleteByModule($module->getVar('mid'), 'mymodule_view');
// Add permission for all groups
$groups = [XOOPS_GROUP_ADMIN, XOOPS_GROUP_USERS, XOOPS_GROUP_ANONYMOUS];
foreach ($groups as $group_id) {
$gpermHandler->addRight('mymodule_view', 1, $group_id, $module->getVar('mid'));
}

Các phương pháp hay nhất về xử lý phiên

Phần tiêu đề “Các phương pháp hay nhất về xử lý phiên”
  1. Không lưu trữ thông tin nhạy cảm trong phiên
  2. Tạo lại ID phiên sau khi thay đổi đăng nhập/đặc quyền
  3. Xác thực dữ liệu phiên trước khi sử dụng
// Regenerate session ID after login
session_regenerate_id(true);
// Validate session data
if (isset($_SESSION['mymodule_user_id'])) {
$user_id = (int)$_SESSION['mymodule_user_id'];
// Verify user exists in database
}
// After successful login
session_regenerate_id(true);
$_SESSION['mymodule_user_ip'] = $_SERVER['REMOTE_ADDR'];
// On subsequent requests
if ($_SESSION['mymodule_user_ip'] !== $_SERVER['REMOTE_ADDR']) {
// Possible session hijacking attempt
session_destroy();
redirect_header('index.php', 3, 'Session error');
exit();
}
// Check if file was uploaded properly
if (!isset($_FILES['userfile']) || $_FILES['userfile']['error'] != UPLOAD_ERR_OK) {
redirect_header('index.php', 3, 'File upload error');
exit();
}
// Check file size
if ($_FILES['userfile']['size'] > 1000000) { // 1MB limit
redirect_header('index.php', 3, 'File too large');
exit();
}
// Check file type
$allowed_types = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($_FILES['userfile']['type'], $allowed_types)) {
redirect_header('index.php', 3, 'Invalid file type');
exit();
}
// Validate file extension
$filename = $_FILES['userfile']['name'];
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$allowed_extensions = ['jpg', 'jpeg', 'png', 'gif'];
if (!in_array($ext, $allowed_extensions)) {
redirect_header('index.php', 3, 'Invalid file extension');
exit();
}
include_once XOOPS_ROOT_PATH . '/class/uploader.php';
$allowed_mimetypes = ['image/gif', 'image/jpeg', 'image/png'];
$maxsize = 1000000; // 1MB
$maxwidth = 1024;
$maxheight = 768;
$upload_dir = XOOPS_ROOT_PATH . '/uploads/mymodule';
$uploader = new XoopsMediaUploader(
$upload_dir,
$allowed_mimetypes,
$maxsize,
$maxwidth,
$maxheight
);
if ($uploader->fetchMedia('userfile')) {
$uploader->setPrefix('mymodule_');
if ($uploader->upload()) {
$filename = $uploader->getSavedFileName();
// Save filename to database
} else {
echo $uploader->getErrors();
}
} else {
echo $uploader->getErrors();
}

Lưu trữ tệp đã tải lên một cách an toàn

Phần tiêu đề “Lưu trữ tệp đã tải lên một cách an toàn”
// Define upload directory outside web root
$upload_dir = XOOPS_VAR_PATH . '/uploads/mymodule';
// Create directory if it doesn't exist
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0755, true);
}
// Move uploaded file
move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_dir . '/' . $safe_filename);
try {
$result = someFunction();
if (!$result) {
throw new Exception('Operation failed');
}
} catch (Exception $e) {
// Log the error
xoops_error($e->getMessage());
// Display a generic error message to the user
redirect_header('index.php', 3, 'An error occurred. Please try again later.');
exit();
}
// Log security events
xoops_loadLanguage('logger', 'mymodule');
$GLOBALS['xoopsLogger']->addExtra('Security', 'Failed login attempt for user: ' . $username);
// Define configuration path outside web root
$config_path = XOOPS_VAR_PATH . '/configs/mymodule/config.php';
// Load configuration
if (file_exists($config_path)) {
include $config_path;
} else {
// Handle missing configuration
}

Sử dụng .htaccess để bảo vệ các file cấu hình:

# In .htaccess
<Files "config.php">
Order Allow,Deny
Deny from all
</Files>
  1. Chọn thư viện được bảo trì tích cực
  2. Kiểm tra lỗ hổng bảo mật
  3. Xác minh giấy phép của thư viện có tương thích với XOOPS
// Check library version
if (version_compare(LIBRARY_VERSION, '1.2.3', '<')) {
xoops_error('Please update the library to version 1.2.3 or higher');
}
// Load library in a controlled way
function loadLibrary($file)
{
$allowed = ['parser.php', 'formatter.php'];
if (!in_array($file, $allowed)) {
return false;
}
include_once XOOPS_ROOT_PATH . '/modules/mymodule/libraries/' . $file;
return true;
}

Danh sách kiểm tra thủ công1. Kiểm tra tất cả các biểu mẫu có dữ liệu nhập không hợp lệ

Phần tiêu đề “Danh sách kiểm tra thủ công1. Kiểm tra tất cả các biểu mẫu có dữ liệu nhập không hợp lệ”
  1. Cố gắng bỏ qua xác thực và ủy quyền
  2. Kiểm tra chức năng upload file với file độc hại
  3. Kiểm tra lỗ hổng XSS ở tất cả đầu ra
  4. Kiểm tra việc chèn SQL trong tất cả các truy vấn cơ sở dữ liệu

Sử dụng các công cụ tự động để quét các lỗ hổng:

  1. Công cụ phân tích mã tĩnh
  2. Máy quét ứng dụng web
  3. Trình kiểm tra phụ thuộc cho thư viện của bên thứ ba
// For regular HTML content
echo htmlspecialchars($variable, ENT_QUOTES, 'UTF-8');
// Using MyTextSanitizer
$myts = MyTextSanitizer::getInstance();
echo $myts->htmlSpecialChars($variable);
// For data used in JavaScript
echo json_encode($variable);
// For inline JavaScript
echo 'var data = ' . json_encode($variable) . ';';
// For data used in URLs
echo htmlspecialchars(urlencode($variable), ENT_QUOTES, 'UTF-8');
// Assign variables to Smarty template
$GLOBALS['xoopsTpl']->assign('title', htmlspecialchars($title, ENT_QUOTES, 'UTF-8'));
// For HTML content that should be displayed as-is
$GLOBALS['xoopsTpl']->assign('content', $myts->displayTarea($content, 1, 1, 1, 1, 1));

#bảo mật #các phương pháp thực hành tốt nhất #xoops #phát triển mô-đun #xác thực #ủy quyền