Explore the essentials of frontend security testing with this quiz focused on Cross-Site Scripting (XSS) and Cross-Site Request Forgery (CSRF). Strengthen your understanding of secure web practices, attack prevention techniques, and essential defensive coding strategies relevant for robust web application security.
Which of the following scenarios best describes a stored XSS vulnerability in a web application comment section?
Explanation: Stored XSS occurs when malicious scripts are permanently stored on the target server, such as in comment fields, and then served to other users without sanitization. Posting a script tag that is later rendered unescaped allows XSS to execute. Typing an incorrect password causing an error message is unrelated to XSS. Loading errors for images are about resource handling, not scripting vulnerabilities. Switching to HTTPS relates to transport security, not XSS risks.
When designing a web form that changes user settings, which security measure is most effective at preventing CSRF attacks?
Explanation: CSRF tokens are unique per session or request and validated by the server, effectively thwarting unauthorized requests crafted by attackers. While complex passwords enhance account security, they do not prevent CSRF. Client-side input validation helps with data quality but not with cross-site requests. CAPTCHAs can reduce automated submissions but are not a widespread CSRF defense mechanism.
To prevent reflected XSS vulnerabilities when displaying user input, what should a developer do before rendering data in HTML?
Explanation: Escaping HTML special characters ensures that user input is displayed as text rather than interpreted as executable code by the browser, which is critical for preventing XSS. Hashing input is important for storing passwords securely but does not address XSS. Web application firewalls can block some threats, yet escaping is a fundamental coding defense. Converting input to lowercase has no effect on script execution risk.
In which scenario is a user most likely to become a victim of a CSRF attack?
Explanation: CSRF attacks exploit authenticated sessions by convincing the browser to send unauthorized requests, often through hidden forms or requests initiated by malicious web pages. Accidental input errors and non-existent pages do not represent CSRF pathways. Simply refreshing a page without new input does not enable any form of attack.
Which security header can help mitigate XSS risks by controlling which sources are allowed to execute scripts, and what is a common mistake in its configuration?
Explanation: The Content-Security-Policy header limits which sources are trusted for scripts, reducing XSS risk. Allowing 'unsafe-inline' unintentionally permits inline scripts and makes the policy ineffective. The Referer header relates to referrer information, and its name error disables its function but does not directly mitigate XSS. Accept-Encoding deals with supported content encodings, not script sources. Strict-Transport-Security enforces HTTPS but does not address script execution or XSS.