The Ultimate Guide to Web Security
Welcome to the definitive field guide for Web Application Security. This document synthesizes core concepts across major vulnerability classes, breaking down their mechanics, theoretical impact, and essential defensive strategies.
[!NOTE] This guide is intended for educational purposes and defensive engineering. Understanding how vulnerabilities operate is the first step in building resilient, secure systems.
1. Cross-Site Scripting (XSS)
Cross-Site Scripting (XSS) occurs when an application includes untrusted data in a web page without proper validation or escaping. This allows the execution of malicious scripts in the victim’s browser.
The Mechanics
XSS vulnerabilities are generally categorized into three main types:
| Type | Description |
|---|---|
| Reflected XSS | The malicious payload is reflected off the web application to the victim's browser, typically via a URL parameter. |
| Stored XSS | The payload is permanently stored on the target server (e.g., in a database, forum post, or comment field) and served to users viewing the affected content. |
| DOM-based XSS | The vulnerability exists in the client-side code rather than the server-side code. The DOM is modified by JavaScript using attacker-controlled data. |
Theoretical Example
A vulnerable search page might reflect user input directly into the HTML:
<!-- Vulnerable PHP Code -->
<h1>Search Results for: <?php echo $_GET['query']; ?></h1>
If a user inputs <script>alert(1)</script>, the browser interprets the input as executable JavaScript.
Defensive Remediation
To effectively prevent XSS, developers must adopt a defense-in-depth approach:
- Context-Aware Output Encoding: Always encode untrusted data before inserting it into an HTML document. The type of encoding depends on where the data is placed (HTML body, JavaScript variable, CSS, etc.).
- Content Security Policy (CSP): Implement a strict CSP to restrict the sources from which scripts can be loaded and executed.
- Use Safe APIs: Modern frameworks (like React or Angular) automatically encode output by default. Avoid unsafe sinks like
innerHTMLin vanilla JavaScript.
2. Server-Side Request Forgery (SSRF)
Server-Side Request Forgery (SSRF) is a vulnerability that allows an attacker to induce the server-side application to make HTTP requests to an arbitrary domain of the attacker's choosing.
The Mechanics
In a typical SSRF scenario, the server might try to fetch a remote resource based on a URL provided by the user. If this input is not validated, the server can be tricked into interacting with internal infrastructure (e.g., 127.0.0.1 or 192.168.x.x) or external systems.
Common Targets
- Internal administrative panels.
- Cloud metadata services (e.g.,
http://169.254.169.254/in AWS). - Backend databases or internal APIs not exposed to the public internet.
Theoretical Example
Consider a web application with a "Check Stock" feature that queries a backend API:
POST /product/stock HTTP/1.1
Host: example.com
stockApi=http://stock.backend.internal:8080/check
If the stockApi parameter is modifiable, it could be pointed to the server's loopback interface:
stockApi=http://localhost/admin/delete?username=carlos
Defensive Remediation
- Network Segmentation: Place backend services in isolated networks and restrict egress traffic from web servers.
- Strict Allowlisting: If the application needs to fetch resources, validate the URL against a strict allowlist of permitted domains or IP addresses.
- Disable Unused URL Schemas: Ensure the HTTP client library only supports
http://andhttps://. Disable schemes likefile://,dict://, orgopher://.
3. SQL Injection (SQLi)
SQL Injection allows an attacker to interfere with the queries that an application makes to its database. This can allow unauthorized viewing of data, modification, or even complete system compromise.
The Mechanics
SQLi occurs when user input is unsafely embedded into a database query, altering the query's logic.
// Vulnerable Code Example
$username = $_POST['user'];
$query = "SELECT * FROM users WHERE username = '$username'";
$db->execute($query);
By manipulating the input, the query's structure is altered, bypassing authentication or extracting additional data.
Defensive Remediation
- Prepared Statements (Parameterized Queries): This is the primary defense against SQLi. Prepared statements ensure that the database treats user input as data, not executable code.
- Input Validation: Enforce strict allowlists for input (e.g., ensuring an ID is strictly an integer).
- Principle of Least Privilege: Ensure the database user account used by the application only has the minimum necessary permissions.
4. File Upload Vulnerabilities
Allowing users to upload files can expose a server to significant risks if the files are not properly validated.
The Mechanics
If a web server allows the upload of executable scripts (like .php, .jsp, or .py) and stores them in a directory accessible from the web, an attacker can request the file to execute arbitrary code on the server.
Defensive Remediation
- File Type Verification: Do not rely on the
Content-Typeheader or file extension alone. Verify the file's signature (magic bytes) to ensure it matches the expected type. - Rename Uploaded Files: Generate a random string or UUID for the filename upon upload to prevent attackers from predicting the file path.
- Store Outside the Web Root: Uploaded files should be stored in a directory that is not directly accessible or executable by the web server.
[!IMPORTANT] Security is an ongoing process. Continuous learning, regular auditing, and adherence to secure coding practices are essential to protecting web applications in a constantly evolving threat landscape.