-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxss.php
More file actions
69 lines (57 loc) · 2.1 KB
/
xss.php
File metadata and controls
69 lines (57 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* XSS Protection & Input Validation Helper
*
* Poskytuje funkce pro sanitizaci vstupů a ochranu proti XSS útokům.
*
* Sanitizační funkce:
* - cleanHtml() - Escapuje HTML speciální znaky pro bezpečný výstup
* - cleanJs() - Sanitizace pro JavaScript kontext
* - cleanUrl() - URL encoding
* - sanitizeDescription() - Whitelist HTML tagů pro challenge popisy (povoleny pouze bezpečné tagy)
* - sanitizeInput() - Základní očištění vstupu (trim, stripslashes)
*
* Validační funkce:
* - validateUsername() - Regex validace: a-z, A-Z, 0-9, _, délka 3-20 znaků
* - validateEmail() - Email formát validace
* - validateFlagFormat() - FLAG{...} formát s alfanumerickými znaky
*
* Automatická ochrana:
* - Odstranění event handlerů (onclick, onerror, atd.)
* - Blokování javascript: protokolu v odkazech
*/
function cleanHtml($str) {
return htmlspecialchars($str, ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
function cleanJs($str) {
return json_encode($str, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);
}
function cleanUrl($str) {
return urlencode($str);
}
function sanitizeDescription($html) {
$allowedTags = '<p><br><strong><b><em><i><code><pre><a><h3><h4><ul><ol><li><div><span><hr>';
$html = strip_tags($html, $allowedTags);
// Dodatečné ošetření - odstranit event handlery (onclick, onerror, atd.)
$html = preg_replace('/\s*on\w+\s*=\s*["\'][^"\']*["\']/i', '', $html);
// Odstranit javascript: protokol z odkazů
$html = preg_replace('/href\s*=\s*["\']javascript:[^"\']*["\']/i', 'href="#"', $html);
return $html;
}
function validateUsername($username) {
return (bool) preg_match('/^[a-zA-Z0-9_]{3,20}$/', $username);
}
function validateEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
function sanitizeInput($input) {
$input = trim($input);
$input = stripslashes($input);
return $input;
}
function validateFlagFormat($flag) {
return (bool) preg_match('/^FLAG\{[a-zA-Z0-9_]+\}$/', $flag);
}
function safeErrorMessage($message) {
return cleanHtml($message);
}