What is Cross-Site Scripting (XSS) Stored – Non-Privileged User to Anyone vulnerability?

When a web application transmits stored strings given by an attacker to a victim’s browser in such a way that the browser executes part of the text as code, a Stored Cross-Site Scripting (XSS) vulnerability occurs. The string includes harmful data and is originally saved on the server, commonly in the database of the application. The malicious material is eventually retrieved and inserted into a web page by the application. As a result, the attacker’s code gets executed within a valid user’s session by the victim’s browser.

Cross-Site Scripting (XSS) is a common and critical web application security vulnerability that occurs when an application fails to properly validate and sanitize user inputs, allowing attackers to inject malicious scripts into web pages viewed by other users. In the context of this article, we will delve into a specific type of XSS vulnerability known as “Stored XSS” and discuss how it can be exploited by non-privileged users to target anyone accessing the affected page. We’ll also provide sample code snippets in various popular programming languages and frameworks to help developers understand and prevent this vulnerability.

What is Stored XSS?

Stored XSS, also known as “persistent XSS,” occurs when an attacker injects malicious code, typically JavaScript, into a web application’s database. This code is then retrieved and executed by other users when they access the compromised page. Unlike “Reflected XSS,” where the payload is reflected off a single request, stored XSS payloads persist on the server, affecting multiple users over time.

Exploiting Stored XSS as a Non-Privileged User

To exploit Stored XSS, a non-privileged user typically follows these steps:

  1. Inject Malicious Code: The attacker, often through a web form or input field, injects malicious JavaScript code. For example:
<script>
  // Malicious code here, such as stealing cookies or defacing the page
</script>
  1. Submission: The attacker submits the input with the injected payload to the application.
  2. Persistence: The application stores the payload in its database, associating it with the attacker’s account or the affected page.
  3. Victim Access: When a victim, who may be any user of the application, accesses the page containing the stored payload, the malicious code executes in the context of the victim’s browser.

Preventing Stored XSS

To prevent Stored XSS vulnerabilities, developers should adopt secure coding practices and input validation techniques. Here are some sample code snippets in various programming languages and frameworks:

JavaScript (Client-Side Sanitization)

// Example using DOMPurify to sanitize user-generated content
const userInput = '<script>alert("Malicious code");</script>';
const sanitizedInput = DOMPurify.sanitize(userInput);
document.getElementById('user-content').innerHTML = sanitizedInput;

PHP (Server-Side Validation)

// Example using htmlentities to sanitize user input before displaying it
$userInput = '<script>alert("Malicious code");</script>';
$cleanedInput = htmlentities($userInput, ENT_QUOTES, 'UTF-8');
echo '<div>' . $cleanedInput . '</div>';

Python (Django Framework)

# Example using Django's escapejs filter to sanitize user input
from django.utils.html import escapejs

user_input = '<script>alert("Malicious code");</script>'
sanitized_input = escapejs(user_input)

Ruby on Rails (ERB Templates)

# Example using Rails' `h` method to escape user input in ERB templates
<%=h user_input %>

Java (Spring Framework)

// Example using Spring's Thymeleaf template engine for HTML encoding
<div th:text="${userInput}"></div>

ASP.NET (C#)

// Example using Razor syntax to encode user input
<div>@Html.Raw(userInput)</div>

React (JSX)

// Example in a React component using JSX to escape user-generated content
function App() {
  const userInput = '<script>alert("Malicious code");</script>';
  return <div>{userInput}</div>;
}

Ruby (Sinatra Framework)

# Example in Sinatra using the `escape_html` helper to sanitize user input
require 'sinatra'
helpers do
  include Rack::Utils
  alias_method :h, :escape_html
end

get '/user-input' do
  user_input = '<script>alert("Malicious code");</script>'
  "<div>#{h(user_input)}</div>"
end

These code samples demonstrate various techniques for preventing Stored XSS vulnerabilities in different programming languages and web frameworks. It’s essential to choose the appropriate method based on the technology stack used in your web application to ensure robust security against XSS attacks.

By employing proper input validation, output encoding, and security libraries, developers can mitigate the risk of Stored XSS vulnerabilities and ensure the safety of their web applications and users.

In conclusion, understanding and addressing Cross-Site Scripting (XSS) Stored vulnerabilities is crucial for web application security. By implementing robust security practices and using appropriate coding techniques, developers can protect their applications from this prevalent threat.

Leave a Comment

Scroll to Top