XSS-Labs

Created by Abhinav Singwal ยท GitHub ยท Linktree

๐Ÿ”น Lab #1 ยท Reflected XSS

GET Parameter Reflection

The page reads the ?q= parameter from the URL and injects it directly into the DOM using innerHTML โ€” NO sanitization.

Goal: Execute JavaScript (pop an alert box) by modifying the URL parameter.

VULNERABLE TARGET (Reflected via innerHTML)
No payload yet. Add ?q=... to the URL.
Current ?q = (none)
Hints (click to expand)

Solution & Explanation

Working Payloads (add to URL after ?q=):

๐Ÿ”ง Why it works:
The application takes the URL parameter ?q=... and uses innerHTML to render it directly. Because there's no escaping or filtering, the browser parses any HTML/JavaScript injected.

// Vulnerable JavaScript code:
const query = new URLSearchParams(location.search).get('q');
document.getElementById('output').innerHTML = query;

How to Fix (Secure Code):

// Method 1: Use textContent instead of innerHTML
const query = new URLSearchParams(location.search).get('q');
document.getElementById('output').textContent = query;

// Method 2: Escape HTML entities
function escapeHtml(str) {
    return str.replace(/[&<>]/g, function(m) {
        if (m === '&') return '&';
        if (m === '<') return '<';
        if (m === '>') return '>';
        return m;
    });
}
document.getElementById('output').innerHTML = escapeHtml(query);

This lab runs entirely in your browser โ€” safe for educational purposes.