December 1, 2026
Lab: Reflected XSS into HTML context with nothing encoded
Reflected XSS into HTML Context with Nothing Encoded โ Port Swigger Web Security Academy
By Anusreeramesh
5 min read
Reflected XSS into HTML Context with Nothing Encoded โ Port Swigger Web Security Academy
Introduction
Imagine we are visiting a website and using its search functionality to look for something. You enter a keyword, click the search button, and the website displays your search term back on the results page.
Everything appears normal. But what if the website doesn't properly handle the input provided by users?
Instead of treating the search term as ordinary text, the application directly reflects it into the HTML response without encoding or sanitization. This creates an opportunity for an attacker to inject malicious JavaScript into the page.
This is where Reflected Cross-Site Scripting (XSS) comes into play.
In this PortSwigger Web Security Academy lab, we explore a simple reflected XSS vulnerability in which user-supplied input is returned directly within the HTML response without any encoding. The objective is to identify the vulnerable search functionality and demonstrate that arbitrary JavaScript can execute in the victim's browser by triggering the JavaScript alert() function.
Let's explore how this vulnerability works, identify the injection point, and understand how a simple input-handling mistake can lead to cross-site scripting.
Understanding Reflected Cross-Site Scripting (XSS)
Cross-Site Scripting (XSS) is a client-side injection vulnerability that allows an attacker to inject malicious scripts into web pages viewed by other users.
Reflected XSS occurs when an application receives malicious input through an HTTP request and immediately includes that input in its HTTP response without appropriate output encoding.
Unlike Stored XSS, where malicious scripts are permanently stored on the application server, reflected XSS payloads are processed as part of an individual request and reflected in the response.
The vulnerability becomes exploitable when the browser interprets the reflected input as executable HTML or JavaScript instead of displaying it as plain text.
In this lab, the search functionality acts as the injection point because the application reflects the submitted search term directly into the HTML response.
How Does Reflected XSS Work?
To understand this vulnerability, it is important to understand how a normal web request works.
When a user enters a search term into a website, the browser sends that input to the web server through an HTTP request.
The server processes the request and generates an HTTP response containing the search results.
In a vulnerable application, if the server includes the original user input in the HTML response without proper encoding, the browser may interpret the injected content as executable code.
The process can be understood through the following steps:
- User Input: The user enters a search query containing a JavaScript payload.
2.HTTP Request: The browser sends the search query to the web server.
3.Server Response: The application reflects the submitted input into the HTML response.
4.Browser Interpretation: The browser parses the returned HTML and executes the injected JavaScript.
5.Execution: The JavaScript executes in the context of the vulnerable website.
Step 1: Injecting the XSS Payload
First, I navigated to the lab's search functionality and entered the following JavaScript payload into the search box:
<script>alert(1)</script><script>alert(1)</script>This payload was designed to test whether the application was vulnerable to reflected Cross-Site Scripting (XSS). Since the application does not encode the injected input, the browser can interpret the script tag as executable JavaScript rather than ordinary text.
Technical Analysis of the XSS Payload
The payload used in this lab was:
<script>alert(1)</script><script>alert(1)</script>Let's break down the payload:
<script>
The script tag tells the browser that the content inside it contains JavaScript code.
alert(1)
The alert() function is a built-in JavaScript function that displays a popup dialog in the browser. The value 1 is the message displayed inside the popup.
</script>
This closing tag marks the end of the JavaScript block.
Since the application does not encode the injected HTML tags, the browser interprets the script element as executable JavaScript.
Step 2: Triggering the JavaScript Alert
After entering the payload, I clicked the search button to submit the input. The application reflected the injected script into the HTML response, and the browser executed the JavaScript.
As a result, a JavaScript alert popup displaying 1 appeared on the screen. This confirmed that the search functionality was vulnerable to Reflected Cross-Site Scripting (XSS).
The appearance of the alert popup demonstrates that the injected JavaScript was successfully executed.
This is a basic proof-of-concept payload used to confirm the presence of the reflected XSS vulnerability.
Security Impact of Reflected XSS
Although this lab demonstrates the vulnerability using a simple JavaScript alert popup, the real-world impact of reflected XSS can be more serious.
Depending on the application, browser security controls, and victim interaction, an attacker may potentially:
- Perform actions within a victim's authenticated session.
- Modify webpage content and display misleading information.
- Redirect users to malicious websites.
- Display fake login forms to capture credentials.
- Perform unauthorized actions using the victim's browser privileges.
The actual impact depends on the application's security controls, session management, and the context in which the injected script executes.
This is why reflected XSS should not be considered harmless simply because the proof-of-concept only displays an alert.
Final Result โ Lab Successfully Solved
The reflected Cross-Site Scripting (XSS) vulnerability was successfully identified and exploited using the following JavaScript payload:
<script>alert(1)</script><script>alert(1)</script>Upon submitting the payload through the search functionality, the browser executed the injected JavaScript and displayed an alert popup containing 1.
The successful execution of the payload confirmed the presence of a reflected XSS vulnerability. PortSwigger subsequently marked the lab as Solved, indicating that the challenge objective had been achieved.
Screenshot: Lab Successfully Solved
[Insert your final Solved-status screenshot here.]
Conclusion
This lab provided practical experience in identifying and exploiting a reflected XSS vulnerability. It helped me understand how improperly handled user input can lead to JavaScript execution in a browser.
Even a simple search functionality can introduce a security weakness when user input is reflected into an HTML response without proper encoding.
Completing this challenge strengthened my understanding of Cross-Site Scripting and the importance of secure input handling and context-aware output encoding in web application security.
Lab completed successfully!
Prevention and Mitigation
Developers can reduce the risk of reflected XSS by implementing appropriate security measures.
1. Context-Aware Output Encoding
Encode untrusted user input before inserting it into HTML. The encoding method must match the output context, such as HTML text, HTML attributes, or JavaScript.
2. Input Validation
Validate user input against the expected format and reject unexpected data where appropriate. However, input validation alone is not sufficient to prevent XSS.
3. Secure Coding Practices
Use secure templating engines and frameworks that automatically escape untrusted output. Avoid directly inserting user-controlled data into executable HTML or JavaScript contexts.
4. Content Security Policy (CSP)
Implement a properly configured Content Security Policy to restrict the execution of unauthorized scripts. CSP provides an additional layer of protection but should not replace output encoding.
By following these practices, developers can significantly reduce the likelihood of XSS vulnerabilities in web applications.