इसे छोड़कर कंटेंट पर जाएं

सुरक्षा दिशानिर्देश

यह दस्तावेज़ XOOPS विकास के लिए सुरक्षा सर्वोत्तम प्रथाओं की रूपरेखा तैयार करता है, जिसमें इनपुट सत्यापन, आउटपुट एन्कोडिंग, प्रमाणीकरण, प्राधिकरण और सामान्य वेब कमजोरियों के खिलाफ सुरक्षा शामिल है।

सुरक्षा सिद्धांत

Section titled “सुरक्षा सिद्धांत”
flowchart TB
subgraph "Defense in Depth"
A[Input Validation] --> B[Authentication]
B --> C[Authorization]
C --> D[Data Sanitization]
D --> E[Output Encoding]
E --> F[Audit Logging]
end

स्वच्छता का अनुरोध करें

Section titled “स्वच्छता का अनुरोध करें”
use Xoops\Core\Request;
// Always use typed getters
$id = Request::getInt('id', 0, 'GET');
$name = Request::getString('name', '', 'POST');
$email = Request::getEmail('email', '', 'POST');
$url = Request::getUrl('website', '', 'POST');
// Never use raw $_GET/$_POST/$_REQUEST
// Bad: $id = $_GET['id'];
// Good: $id = Request::getInt('id', 0, 'GET');
// Validate before use
if ($id <= 0) {
throw new InvalidArgumentException('Invalid ID');
}
if (!preg_match('/^[a-zA-Z0-9_]{3,50}$/', $username)) {
throw new InvalidArgumentException('Invalid username format');
}
// Use whitelist validation for enums
$allowedStatuses = ['draft', 'published', 'archived'];
if (!in_array($status, $allowedStatuses, true)) {
throw new InvalidArgumentException('Invalid status');
}

SQL इंजेक्शन रोकथाम

Section titled “SQL इंजेक्शन रोकथाम”

पैरामीटरयुक्त क्वेरीज़ का उपयोग करें

Section titled “पैरामीटरयुक्त क्वेरीज़ का उपयोग करें”
// GOOD: Parameterized query
$sql = "SELECT * FROM {$xoopsDB->prefix('users')} WHERE uid = ?";
$result = $xoopsDB->query($sql, [$userId]);
// BAD: String concatenation (vulnerable!)
// $sql = "SELECT * FROM users WHERE uid = " . $userId;

Criteria ऑब्जेक्ट का उपयोग करना

Section titled “Criteria ऑब्जेक्ट का उपयोग करना”
use Criteria;
use CriteriaCompo;
$criteria = new CriteriaCompo();
$criteria->add(new Criteria('status', 'published'));
$criteria->add(new Criteria('uid', $userId, '='));
$criteria->add(new Criteria('created', time() - 86400, '>'));
$articles = $articleHandler->getObjects($criteria);

एक्सएसएस रोकथाम

Section titled “एक्सएसएस रोकथाम”

आउटपुट एन्कोडिंग

Section titled “आउटपुट एन्कोडिंग”
use Xoops\Core\Text\Sanitizer;
// HTML context
$safeName = htmlspecialchars($userName, ENT_QUOTES, 'UTF-8');
// In templates (auto-escaped)
{$userName|escape}
// For rich content
$sanitizer = Sanitizer::getInstance();
$safeContent = $sanitizer->sanitizeForDisplay($content);

सामग्री सुरक्षा नीति

Section titled “सामग्री सुरक्षा नीति”
// Set CSP headers
header("Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'");

टोकन कार्यान्वयन

Section titled “टोकन कार्यान्वयन”
// Generate token
use Xoops\Core\Security;
$token = Security::createToken();
// In form
echo '<input type="hidden" name="XOOPS_TOKEN_REQUEST" value="' . $token . '">';
// Verify on submission
if (!Security::checkToken()) {
die('Security token mismatch');
}

XoopsForm का उपयोग करना

Section titled “XoopsForm का उपयोग करना”
// Automatically adds CSRF token
$form = new XoopsThemeForm('Edit Article', 'articleform', 'save.php');
$form->addElement(new XoopsFormHiddenToken());

पासवर्ड प्रबंधन

Section titled “पासवर्ड प्रबंधन”
// Hash passwords (PHP 5.5+)
$hashedPassword = password_hash($plainPassword, PASSWORD_ARGON2ID);
// Verify passwords
if (password_verify($plainPassword, $storedHash)) {
// Password correct
}
// Check if rehash needed
if (password_needs_rehash($storedHash, PASSWORD_ARGON2ID)) {
$newHash = password_hash($plainPassword, PASSWORD_ARGON2ID);
// Update stored hash
}
// Regenerate session ID after login
session_regenerate_id(true);
// Set secure session cookie options
ini_set('session.cookie_httponly', 1);
ini_set('session.cookie_secure', 1);
ini_set('session.cookie_samesite', 'Lax');
// Check module admin
if (!$xoopsUser || !$xoopsUser->isAdmin($xoopsModule->mid())) {
redirect_header('index.php', 3, 'Access denied');
}
// Check group permissions
$grouppermHandler = xoops_getHandler('groupperm');
$groups = $xoopsUser ? $xoopsUser->getGroups() : [XOOPS_GROUP_ANONYMOUS];
if (!$grouppermHandler->checkRight('view_item', $itemId, $groups, $moduleId)) {
throw new AccessDeniedException('Permission denied');
}

भूमिका-आधारित पहुंच

Section titled “भूमिका-आधारित पहुंच”
class PermissionChecker
{
public function canEdit(Article $article, ?XoopsUser $user): bool
{
if (!$user) {
return false;
}
// Admin can edit anything
if ($user->isAdmin()) {
return true;
}
// Author can edit their own
if ($article->getAuthorId() === $user->uid()) {
return true;
}
// Check editor permission
return $this->hasPermission($user, 'article_edit');
}
}

फ़ाइल अपलोड सुरक्षा

Section titled “फ़ाइल अपलोड सुरक्षा”
class SecureUploader
{
private array $allowedMimeTypes = [
'image/jpeg',
'image/png',
'image/gif'
];
private array $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
public function validate(array $file): bool
{
// Check file size
if ($file['size'] > 2 * 1024 * 1024) {
throw new FileTooLargeException();
}
// Verify MIME type
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->file($file['tmp_name']);
if (!in_array($mimeType, $this->allowedMimeTypes, true)) {
throw new InvalidFileTypeException();
}
// Check extension
$extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if (!in_array($extension, $this->allowedExtensions, true)) {
throw new InvalidFileTypeException();
}
// Generate safe filename
return true;
}
public function generateSafeFilename(string $original): string
{
$extension = strtolower(pathinfo($original, PATHINFO_EXTENSION));
return bin2hex(random_bytes(16)) . '.' . $extension;
}
}
class SecurityLogger
{
public function logAuthAttempt(string $username, bool $success, string $ip): void
{
$data = [
'username' => $username,
'success' => $success,
'ip' => $ip,
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
'timestamp' => time()
];
// Log to database or file
$this->log('auth', $data);
}
public function logSensitiveAction(int $userId, string $action, array $context): void
{
$data = [
'user_id' => $userId,
'action' => $action,
'context' => json_encode($context),
'ip' => $_SERVER['REMOTE_ADDR'],
'timestamp' => time()
];
$this->log('audit', $data);
}
}

सुरक्षा शीर्षलेख

Section titled “सुरक्षा शीर्षलेख”
// Recommended security headers
header('X-Content-Type-Options: nosniff');
header('X-Frame-Options: SAMEORIGIN');
header('X-XSS-Protection: 1; mode=block');
header('Referrer-Policy: strict-origin-when-cross-origin');
header('Permissions-Policy: geolocation=(), microphone=(), camera=()');
// HSTS (only for HTTPS sites)
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
header('Strict-Transport-Security: max-age=31536000; includeSubDomains');
}
class RateLimiter
{
public function check(string $key, int $maxAttempts, int $windowSeconds): bool
{
$cacheKey = 'rate_limit:' . $key;
$attempts = (int) $this->cache->get($cacheKey, 0);
if ($attempts >= $maxAttempts) {
return false; // Rate limited
}
$this->cache->increment($cacheKey, 1, $windowSeconds);
return true;
}
}
// Usage
$limiter = new RateLimiter();
if (!$limiter->check('login:' . $ip, 5, 300)) {
throw new TooManyRequestsException('Too many login attempts');
}

सुरक्षा जाँच सूची

Section titled “सुरक्षा जाँच सूची”
  • सभी उपयोगकर्ता इनपुट को मान्य और स्वच्छ किया गया
  • सभी डेटाबेस परिचालनों के लिए पैरामीटरयुक्त प्रश्न
  • सभी उपयोगकर्ता-जनित सामग्री के लिए आउटपुट एन्कोडिंग
  • CSRF सभी राज्य बदलने वाले फॉर्म पर टोकन
  • सुरक्षित पासवर्ड हैशिंग (आर्गन2आईडी)
  • सत्र सुरक्षा कॉन्फ़िगर की गई
  • फ़ाइल अपलोड सत्यापन
  • सुरक्षा हेडर सेट
  • दर सीमित लागू किया गया
  • ऑडिट लॉगिंग सक्षम
  • त्रुटि संदेश संवेदनशील जानकारी लीक नहीं करते हैं

संबंधित दस्तावेज़ीकरण

Section titled “संबंधित दस्तावेज़ीकरण”
  • प्रमाणीकरण प्रणाली
  • अनुमति प्रणाली
  • इनपुट सत्यापन