Tấn công XSS chèn script độc hại qua input của người dùng. Phòng chống bằng:
- Luôn escape output với
htmlspecialchars()chuyển đổi<script>thành<script> - Validate và sanitize input với
filter_var() - Thiết lập Content Security Policy header:
header("Content-Security-Policy: default-src 'self'") - Không bao giờ in trực tiếp dữ liệu người dùng
Ví dụ: echo htmlspecialchars($_GET["search"], ENT_QUOTES, "UTF-8"); hiển thị an toàn. Nguyên tắc vàng: không tin bất cứ gì từ người dùng, escape mọi thứ trước khi hiển thị.
XSS attacks inject malicious scripts via user input. Prevent with:
- Always escape output with
htmlspecialchars()converting<script>to<script> - Validate and sanitize input with
filter_var() - Use content security policy headers:
header("Content-Security-Policy: default-src 'self'") - Never directly output user data
Example: echo htmlspecialchars($_GET["search"], ENT_QUOTES, "UTF-8"); safely displays user input. The golden rule: trust nothing from users, escape everything before displaying.