Stored XSS: A Security Risk in Web Applications

Listen to this Post

When a web application allows users to construct URLs freely, it opens the door to potential XSS vulnerabilities. An attacker could input javascript:*, and if the server does not properly validate this input, it could lead to script execution.

Best Practices to Mitigate Stored XSS Risks

✅ Never Trust Client Input

  • If the input contains javascript:, data:, or similar schemes, the API should return a `401 Unauthorized` or an appropriate error response.

✅ Enforce Strict Input Validation

  • If user-provided URLs are necessary, apply two layers of validation:
    🔹 Full URL Validation → Ensure the format follows `https://example.com`
    🔹 Path Validation → Restrict input to predefined relative paths like `/any/any`
  • If the origin of a full URL is not in the approved whitelist, return an error or 401 Unauthorized.

✅ Use a Backend Proxy to Manage File URLs
– Instead of allowing direct client input, route file access through a secure backend proxy that enforces validation and sanitization.

You Should Know:

1. Input Sanitization with Linux Commands

To filter malicious inputs, use `sed` or `grep` to detect and block dangerous patterns:

echo "$user_input" | grep -q -E 'javascript:|data:' && echo "Malicious input detected!"

#### **2. Automating URL Validation with Python**

import re

def validate_url(url):
if re.match(r'^https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', url):
return True
return False

#### **3. Secure Proxy Setup with Nginx**

Configure Nginx to act as a proxy and enforce URL whitelisting:

location /proxy/ {
proxy_pass https://trusted-domain.com/;
if ($args ~* "javascript:|data:") { return 403; }
}

#### **4. Browser Security Headers**

Add these HTTP headers to prevent XSS:

Content-Security-Policy: default-src 'self'; script-src 'none'
X-XSS-Protection: 1; mode=block

#### **5. JavaScript Sanitization**

Use DOMPurify to clean HTML inputs:

const clean = DOMPurify.sanitize(user_input);

### **What Undercode Say:**

Stored XSS remains a critical threat due to improper input handling. Developers must enforce strict validation, sanitize inputs, and use secure proxies. Linux commands like `grep` and `sed` help in filtering malicious patterns, while tools like DOMPurify and Nginx enhance security. Always implement CSP headers and automate URL checks to minimize risks.

### **Expected Output:**

A secure web application that blocks malicious inputs, validates URLs strictly, and prevents XSS attacks through proper backend checks and security headers.

**Reference:**

References:

Reported By: Muhamad Rizki – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image