mediumMultiple ChoiceObjective-mapped
XSS Prevention: Primary Defenses - Input Validation and Output Encoding
A security architect is designing a web application that handles sensitive user data. To protect against cross-site scripting (XSS) attacks, which of the following should be implemented?
Quick Answer
The correct answer is to implement input validation and output encoding, as these are the primary defenses against cross-site scripting (XSS) attacks. Input validation works by rejecting or sanitizing dangerous characters like angle brackets and ampersands at the point of entry, preventing malicious payloads from ever reaching the application logic. Output encoding, such as HTML entity encoding, then ensures that any user-supplied data rendered in the page is treated as inert text rather than executable code, directly neutralizing the root cause of XSS—untrusted data injected into the DOM. On the CompTIA SecurityX CAS-004 exam, this concept often appears in scenario-based questions where a security architect must choose layered controls; a common trap is selecting only one defense, such as input validation alone, which fails against stored XSS in rich text fields. Remember the mnemonic “V.E.T.”—Validate at Entry, Encode at Template—to reinforce that both layers are required for complete XSS prevention.
⚠ Common exam trap
The CAS-004 exam often tests the distinction between preventive controls (input validation/output encoding) and compensating controls (CSP, WAF, HTTPS), leading candidates to choose CSP or WAF because they are security-specific technologies, even though they do not eliminate the injection vulnerability itself.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Implement input validation and output encoding
Input validation and output encoding are the primary defenses against XSS because they prevent malicious scripts from being interpreted by the browser. Input validation rejects or sanitizes dangerous characters (e.g., <, >, &) at the point of entry, while output encoding (e.g., HTML entity encoding) ensures that any user-supplied data rendered in the page is treated as text, not executable code. This directly addresses the root cause of XSS—untrusted data being injected into the DOM.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Implement Content Security Policy (CSP)
Why it's wrong here
CSP is a defense-in-depth measure that can reduce the impact of XSS but does not prevent it entirely; proper input validation and output encoding are the primary controls.
- ✗
Use HTTPS for all communications
Why it's wrong here
HTTPS protects data in transit but does not prevent XSS attacks.
- ✗
Deploy a Web Application Firewall (WAF)
Why it's wrong here
WAF can detect and block some XSS attempts but is not a primary prevention; it can be bypassed and should not replace secure coding.
Option-by-option analysis
Why each answer is right or wrong
Understanding why wrong answers are wrong — and when they would be correct — is what separates a 750 score from a 900. The CAS-005 exam frequently reuses these exact scenarios with slightly different constraints.
✓Implement input validation and output encodingCorrect answer▾
✗Implement Content Security Policy (CSP)Wrong answer — click to see why▾
Why this is wrong here
CSP is a defense-in-depth measure that can reduce the impact of XSS but does not prevent it entirely; proper input validation and output encoding are the primary controls.
✗Use HTTPS for all communicationsWrong answer — click to see why▾
Why this is wrong here
HTTPS protects data in transit but does not prevent XSS attacks.
✗Deploy a Web Application Firewall (WAF)Wrong answer — click to see why▾
Why this is wrong here
WAF can detect and block some XSS attempts but is not a primary prevention; it can be bypassed and should not replace secure coding.
Analysis generated from the official CAS-005blueprint and verified against question context. The “when correct” sections are what AI assistants cite when candidates ask “what’s the difference between these options?”
Go deeper
Related to this question
About these practice questions
Courseiva writes every CAS-005 question from scratch — 968 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
Same concept, more angles
1 more way this is tested on CAS-005
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A security architect is designing a secure coding standard for a web application. Which of the following should be prioritized to mitigate cross-site scripting (XSS) risks?
medium- A.Input validation
- ✓ B.Output encoding
- C.Secure cookies
- D.Parameterized queries
Why B: Output encoding is the primary defense against cross-site scripting (XSS) because it neutralizes injected scripts by converting special characters (e.g., <, >, &, ") into their HTML entity equivalents (e.g., < > & ") before the data is rendered in the browser. This ensures that user-supplied data is treated as text, not executable code, regardless of how it entered the application. Input validation alone is insufficient because it can be bypassed via alternate encodings or allowed characters that are still dangerous in certain contexts.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This CAS-005 practice question is part of Courseiva's free CompTIA certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the CAS-005 exam.