پاکسازی ورودی
هرگز به ورودی کاربر اعتماد نکنید. همیشه قبل از استفاده تمام داده های ورودی را تأیید و ضدعفونی کنید. XOOPS کلاس MyTextSanitizer را برای پاکسازی ورودی متن و عملکردهای کمکی مختلف برای اعتبار سنجی ارائه می کند.
مستندات مرتبط
Section titled “مستندات مرتبط”- Security-Best-Practices - راهنمای جامع امنیتی
- CSRF-Protection - سیستم توکن و کلاس XoopsSecurity
- SQL-Injection-Prevention - شیوه های امنیتی پایگاه داده
قانون طلایی
Section titled “قانون طلایی”هرگز به ورودی کاربر اعتماد نکنید. همه داده های منابع خارجی باید:
- تأیید شده: بررسی کنید که با فرمت و نوع مورد انتظار مطابقت داشته باشد
- **Sanitized **: حذف یا فرار از شخصیت های بالقوه خطرناک
- Escaped: هنگام خروجی، escape برای زمینه خاص (HTML، JavaScript، SQL)
کلاس MyTextSanitizer
Section titled “کلاس MyTextSanitizer”XOOPS کلاس MyTextSanitizer (که معمولاً با نام مستعار $myts) برای پاکسازی متن ارائه می شود.
دریافت نمونه
Section titled “دریافت نمونه”// Get the singleton instance$myts = MyTextSanitizer::getInstance();پاکسازی متن اولیه
Section titled “پاکسازی متن اولیه”$myts = MyTextSanitizer::getInstance();
// For plain text fields (no HTML allowed)$title = $myts->htmlSpecialChars($_POST['title']);
// This converts:// < to <// > to >// & to &// " to "// ' to 'پردازش محتوای Textarea
Section titled “پردازش محتوای Textarea”روش displayTarea() پردازش جامع متنی را ارائه می دهد:
$myts = MyTextSanitizer::getInstance();
$content = $myts->displayTarea( $_POST['content'], $allowhtml = 0, // 0 = No HTML allowed, 1 = HTML allowed $allowsmiley = 1, // 1 = Smilies enabled $allowxcode = 1, // 1 = XOOPS codes enabled (BBCode) $allowimages = 1, // 1 = Images allowed $allowlinebreak = 1 // 1 = Line breaks converted to <br>);روشهای رایج ضدعفونی
Section titled “روشهای رایج ضدعفونی”$myts = MyTextSanitizer::getInstance();
// HTML special characters escaping$safe_text = $myts->htmlSpecialChars($text);
// Strip slashes if magic quotes are on$text = $myts->stripSlashesGPC($text);
// Convert XOOPS codes (BBCode) to HTML$html = $myts->xoopsCodeDecode($text);
// Convert smileys to images$html = $myts->smiley($text);
// Make clickable links$html = $myts->makeClickable($text);
// Complete text processing for preview$preview = $myts->previewTarea($text, $allowhtml, $allowsmiley, $allowxcode);اعتبار سنجی ورودی
Section titled “اعتبار سنجی ورودی”اعتبارسنجی مقادیر عدد صحیح
Section titled “اعتبارسنجی مقادیر عدد صحیح”// Validate integer ID$id = isset($_REQUEST['id']) ? (int)$_REQUEST['id'] : 0;
if ($id <= 0) { redirect_header('index.php', 3, 'Invalid ID'); exit();}
// Alternative with filter_var$id = filter_var($_REQUEST['id'] ?? 0, FILTER_VALIDATE_INT);if ($id === false || $id <= 0) { redirect_header('index.php', 3, 'Invalid ID'); exit();}اعتبار سنجی آدرس های ایمیل
Section titled “اعتبار سنجی آدرس های ایمیل”$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
if (!$email) { redirect_header('form.php', 3, 'Invalid email address'); exit();}اعتبار سنجی URL ها
Section titled “اعتبار سنجی URL ها”$url = filter_var($_POST['url'], FILTER_VALIDATE_URL);
if (!$url) { redirect_header('form.php', 3, 'Invalid URL'); exit();}
// Additional check for allowed protocols$parsed = parse_url($url);$allowed_schemes = ['http', 'https'];if (!in_array($parsed['scheme'], $allowed_schemes)) { redirect_header('form.php', 3, 'Only HTTP and HTTPS URLs are allowed'); exit();}اعتبارسنجی تاریخ ها
Section titled “اعتبارسنجی تاریخ ها”$date = $_POST['date'] ?? '';
// Validate date format (YYYY-MM-DD)if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) { redirect_header('form.php', 3, 'Invalid date format'); exit();}
// Validate actual date validity$parts = explode('-', $date);if (!checkdate($parts[1], $parts[2], $parts[0])) { redirect_header('form.php', 3, 'Invalid date'); exit();}اعتبارسنجی نام فایل ها
Section titled “اعتبارسنجی نام فایل ها”// Remove all characters except alphanumeric, underscore, and hyphen$filename = preg_replace('/[^a-zA-Z0-9_-]/', '', $_POST['filename']);
// Or use a whitelist approach$allowed_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-';$filename = '';foreach (str_split($_POST['filename']) as $char) { if (strpos($allowed_chars, $char) !== false) { $filename .= $char; }}مدیریت انواع ورودی های مختلف
Section titled “مدیریت انواع ورودی های مختلف”ورودی رشته
Section titled “ورودی رشته”$myts = MyTextSanitizer::getInstance();
// Short text (titles, names)$title = $myts->htmlSpecialChars(trim($_POST['title']));
// Limit lengthif (strlen($title) > 255) { $title = substr($title, 0, 255);}
// Check for empty required fieldsif (empty($title)) { redirect_header('form.php', 3, 'Title is required'); exit();}ورودی عددی
Section titled “ورودی عددی”// Integer$count = (int)$_POST['count'];$count = max(0, min($count, 1000)); // Ensure range 0-1000
// Float$price = (float)$_POST['price'];$price = round($price, 2); // Round to 2 decimal places
// Validate rangeif ($price < 0 || $price > 99999.99) { redirect_header('form.php', 3, 'Invalid price'); exit();}ورودی بولی
Section titled “ورودی بولی”// Checkbox values$is_active = isset($_POST['is_active']) ? 1 : 0;
// Or with explicit value check$is_active = ($_POST['is_active'] ?? '') === '1' ? 1 : 0;ورودی آرایه
Section titled “ورودی آرایه”// Validate array input (e.g., multiple checkboxes)$selected_ids = [];if (isset($_POST['ids']) && is_array($_POST['ids'])) { foreach ($_POST['ids'] as $id) { $clean_id = (int)$id; if ($clean_id > 0) { $selected_ids[] = $clean_id; } }}ورودی Select/Option
Section titled “ورودی Select/Option”// Validate against allowed values$allowed_statuses = ['draft', 'published', 'archived'];$status = $_POST['status'] ?? '';
if (!in_array($status, $allowed_statuses)) { redirect_header('form.php', 3, 'Invalid status'); exit();}درخواست شی (XMF)
Section titled “درخواست شی (XMF)”هنگام استفاده از XMF، کلاس Request مدیریت ورودی تمیزتری را ارائه می دهد:
use XMF\Request;
// Get integer$id = Request::getInt('id', 0);
// Get string$title = Request::getString('title', '');
// Get array$ids = Request::getArray('ids', []);
// Get with method specification$id = Request::getInt('id', 0, 'POST');$search = Request::getString('q', '', 'GET');
// Check request methodif (Request::getMethod() !== 'POST') { redirect_header('form.php', 3, 'Invalid request method'); exit();}ایجاد کلاس اعتبار سنجی
Section titled “ایجاد کلاس اعتبار سنجی”برای فرم های پیچیده، یک کلاس اعتبار سنجی اختصاصی ایجاد کنید:
<?phpnamespace XoopsModules\MyModule;
class Validator{ private $errors = [];
public function validateItem(array $data): bool { $this->errors = [];
// Title validation if (empty($data['title'])) { $this->errors['title'] = 'Title is required'; } elseif (strlen($data['title']) > 255) { $this->errors['title'] = 'Title must be 255 characters or less'; }
// Email validation if (!empty($data['email'])) { if (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) { $this->errors['email'] = 'Invalid email format'; } }
// Status validation $allowed = ['draft', 'published']; if (!in_array($data['status'], $allowed)) { $this->errors['status'] = 'Invalid status'; }
return empty($this->errors); }
public function getErrors(): array { return $this->errors; }
public function getError(string $field): ?string { return $this->errors[$field] ?? null; }}استفاده:
$validator = new Validator();$data = [ 'title' => $_POST['title'], 'email' => $_POST['email'], 'status' => $_POST['status'],];
if (!$validator->validateItem($data)) { $errors = $validator->getErrors(); // Display errors to user}پاکسازی برای ذخیره سازی پایگاه داده
Section titled “پاکسازی برای ذخیره سازی پایگاه داده”هنگام ذخیره داده ها در پایگاه داده:
$myts = MyTextSanitizer::getInstance();
// For storage (will be processed again on display)$title = $myts->addSlashes($_POST['title']);
// Better: Use prepared statements (see SQL Injection Prevention)$sql = "INSERT INTO " . $xoopsDB->prefix('mytable') . " (title) VALUES (?)";$result = $xoopsDB->query($sql, [$_POST['title']]);ضد عفونی برای نمایش
Section titled “ضد عفونی برای نمایش”زمینه های مختلف نیاز به فرار متفاوت دارند:
$myts = MyTextSanitizer::getInstance();
// HTML contextecho $myts->htmlSpecialChars($title);
// Within HTML attributesecho htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
// JavaScript contextecho json_encode($title);
// URL parameterecho urlencode($title);
// Full URLecho htmlspecialchars($url, ENT_QUOTES, 'UTF-8');مشکلات رایج
Section titled “مشکلات رایج”کدگذاری دوگانه
Section titled “کدگذاری دوگانه”مشکل: داده ها چندین بار رمزگذاری می شوند
// Wrong - double encoding$title = $myts->htmlSpecialChars($myts->htmlSpecialChars($_POST['title']));
// Right - encode once, at the appropriate time$title = $_POST['title']; // Store rawecho $myts->htmlSpecialChars($title); // Encode on outputکدگذاری ناسازگار
Section titled “کدگذاری ناسازگار”مشکل: برخی از خروجی ها کدگذاری شده اند، برخی نه
راه حل: همیشه از یک رویکرد ثابت استفاده کنید، ترجیحاً روی خروجی کدگذاری کنید:
// Template assignment$GLOBALS['xoopsTpl']->assign('title', htmlspecialchars($title, ENT_QUOTES, 'UTF-8'));اعتبارسنجی وجود ندارد
Section titled “اعتبارسنجی وجود ندارد”مشکل: فقط ضدعفونی کردن بدون تأیید اعتبار
راه حل: همیشه ابتدا اعتبارسنجی کنید، سپس ضدعفونی کنید:
// First validateif (!preg_match('/^[a-z0-9_]+$/', $_POST['username'])) { redirect_header('form.php', 3, 'Username contains invalid characters'); exit();}
// Then sanitize for storage/display$username = $myts->htmlSpecialChars($_POST['username']);خلاصه بهترین شیوه ها
Section titled “خلاصه بهترین شیوه ها”- از MyTextSanitizer برای پردازش محتوای متنی استفاده کنید
- از filter_var() برای اعتبارسنجی فرمت خاص استفاده کنید
- از نوع ریخته گری برای مقادیر عددی استفاده کنید
- لیست سفید مقادیر مجاز برای ورودی های انتخابی
- قبل از ضدعفونی کردن اعتبارسنجی کنید
- Escape در خروجی، نه در ورودی
- از دستورات آماده شده برای پرس و جوهای پایگاه داده استفاده کنید
- ** کلاس های اعتبارسنجی ** برای فرم های پیچیده ایجاد کنید
- **هرگز به اعتبار سنجی سمت مشتری اعتماد نکنید ** - همیشه سمت سرور را تأیید کنید
#امنیت #ضدعفونی #اعتبارسنجی #xoops #MyTextSanitizer #input-handling