Challenge - 5 Problems
Input Safety Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code snippet?
Consider the following PHP code that tries to validate and sanitize user input. What will it output?
PHP
<?php $input = "<script>alert('XSS');</script>"; $validated = filter_var($input, FILTER_VALIDATE_EMAIL); $sanitized = filter_var($input, FILTER_SANITIZE_EMAIL); echo "Validated: "; var_export($validated); echo "\nSanitized: "; var_export($sanitized); ?>
Attempts:
2 left
💡 Hint
FILTER_VALIDATE_EMAIL returns false if input is not a valid email. FILTER_SANITIZE_EMAIL removes invalid characters.
✗ Incorrect
The input is not a valid email, so validation returns false. Sanitization removes characters invalid in emails, leaving 'scriptalertXSSscript'.
🧠 Conceptual
intermediate1:30remaining
Which statement correctly describes input validation vs sanitization?
Choose the option that best explains the difference between input validation and input sanitization.
Attempts:
2 left
💡 Hint
Think about whether the process changes the input or just checks it.
✗ Incorrect
Validation is about checking if input is acceptable. Sanitization changes input to make it safe.
🔧 Debug
advanced2:30remaining
Why does this PHP code fail to prevent XSS?
This PHP code tries to sanitize user input to prevent XSS attacks. Why does it fail?
PHP
<?php $user_input = "<img src=x onerror=alert(1) />"; $safe_input = filter_var($user_input, FILTER_SANITIZE_STRING); echo $safe_input; ?>
Attempts:
2 left
💡 Hint
Check PHP documentation about FILTER_SANITIZE_STRING.
✗ Incorrect
FILTER_SANITIZE_STRING was deprecated as of PHP 8.1 and removed in PHP 8.3, and it does not remove dangerous HTML attributes like onerror, so XSS can still happen.
📝 Syntax
advanced2:00remaining
Which PHP code snippet correctly validates and sanitizes an email input?
Select the code that first validates an email input and then sanitizes it if valid.
Attempts:
2 left
💡 Hint
Validation should happen before sanitization to confirm input is valid.
✗ Incorrect
Option D validates first, then sanitizes. Option D sanitizes first which may alter input before validation. Option D uses wrong filter in if condition. Option D sanitizes after validation but echoes original input sanitized again.
🚀 Application
expert3:00remaining
How many items are in the resulting array after this PHP input processing?
Given this PHP code that validates and sanitizes an array of user inputs, how many items remain in the final array?
PHP
<?php $inputs = ["john@example.com", "invalid-email", "alice@site.org", "<b>bob@site.com</b>"]; $valid_emails = array_filter($inputs, fn($email) => filter_var($email, FILTER_VALIDATE_EMAIL)); $sanitized_emails = array_map(fn($email) => filter_var($email, FILTER_SANITIZE_EMAIL), $valid_emails); print_r($sanitized_emails); ?>
Attempts:
2 left
💡 Hint
Count how many inputs pass FILTER_VALIDATE_EMAIL before sanitization.
✗ Incorrect
Only "john@example.com" and "alice@site.org" are valid emails. "invalid-email" fails validation. "bob@site.com" fails validation because of tags. So 2 items remain.