This case study demonstrates a reflected Cross-Site Scripting vulnerability caused by rendering attacker-controlled input directly into an HTML response.
The vulnerable flow is:
User Input
│
▼
Application Response
│
▼
Browser
│
▼
Input Interpreted as HTML / JavaScript
If the application does not encode output correctly, attacker-controlled content may execute in the security context of the affected website.
The vulnerable implementation reflects the value of:
$_GET['name']directly into the page.
Example:
<p>Welcome, <?php echo $_GET['name']; ?></p>The application does not distinguish between:
User Data
and:
Executable Browser Content
Cross-Site Scripting can potentially enable:
- Session theft
- Client-side content manipulation
- Phishing
- Malicious redirects
- Unauthorized user actions
- Credential theft
- Browser-based social engineering
The exact impact depends on the application's authentication model, browser protections, and session configuration.
The remediated version applies output encoding:
htmlspecialchars(
$name,
ENT_QUOTES,
'UTF-8'
);Conceptually:
Untrusted Input
│
▼
Output Encoding
│
▼
HTML-Safe Text
│
▼
Browser
Special characters are represented as data rather than executable markup.
Output encoding converts characters with special meaning in HTML into safe representations for the current output context.
The browser therefore receives:
Text
rather than:
Executable HTML / JavaScript
when the value is inserted into an HTML text context.
The original coursework repeated the same malicious input after remediation.
Expected behavior:
Script Payload
│
▼
htmlspecialchars()
│
▼
Encoded Output
│
▼
Displayed as Text
│
▼
No JavaScript Execution
The coursework reports that the test payload was rendered as plain text and no JavaScript executed after remediation.
Output encoding is the primary defense for this example.
Additional controls include:
- Context-aware output encoding
- Server-side input validation
- Content Security Policy
- Secure template frameworks
HttpOnlysession cookies- Appropriate
SameSitecookie configuration - Security testing of dynamic output
These controls complement output encoding rather than replace it.
The encoding function must match the context in which data is rendered.
For example:
HTML text
HTML attribute
JavaScript
CSS
URL
may each require different handling.
This example specifically addresses untrusted input placed into an HTML text context.
Never render untrusted input into browser content without applying the correct output encoding for the destination context.
The vulnerable implementation is intentionally insecure and is included only for secure-coding education and authorized laboratory testing.