Summary
A reflected Cross-Site Scripting (XSS) vulnerability in SourceCodester Sales and Inventory System 1.0 allows untrusted data from the msg URL parameter to be rendered directly in the browser without proper sanitization or encoding.
By luring a user into visiting a crafted URL, an attacker can inject arbitrary JavaScript or HTML into the application's pages, enabling session theft, credential harvesting, or other browser-based attacks under the victim's context.
Affected context
This vulnerability occurs in the web UI of the SourceCodester Sales and Inventory System 1.0, specifically in add_stock.php, where the msg parameter from the query string is reflected back into the page response.
The issue is relevant when:
- User-controlled input from the
msgparameter is included in the HTML response - No output encoding or HTML/JavaScript escaping is applied
- The page is accessed in a standard web browser with an active authenticated session or sensitive application state
Exploit preconditions
- Attack surface: HTTP(S) interface of the Sales and Inventory System, particularly requests to
add_stock.php. - Required access: Ability to send a crafted URL to a victim (e.g., via email, chat, or another web page). The attacker does not need direct access to the server.
- User interaction: A victim must click or otherwise load the maliciously crafted URL in their browser.
- Trigger point: The application reflects the
msgparameter value into the HTML response without proper sanitization or output encoding. - Why exploitation is possible: Untrusted input is inserted into the response in a context where it is interpreted as active HTML/JavaScript, violating output encoding and context separation best practices.
Why this happens
The underlying bug is a classic reflected XSS pattern, mapped to CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting').
Specifically, the application:
- Accepts data from the
msgquery parameter - Directly embeds that data into the HTML response generated by
add_stock.php - Fails to properly encode or sanitize the content for the specific output context (e.g., HTML body, attribute, or script block)
This allows an attacker to craft msg values containing HTML and script constructs that the browser then executes in the origin of the Sales and Inventory System application.
Potential impact
- Theft of session identifiers, potentially allowing account hijacking for currently logged-in users
- Execution of arbitrary JavaScript in the victim's browser within the application origin
- Unauthorized actions performed on behalf of the victim (e.g., manipulating inventory, changing records) via forged requests
- Injection of misleading UI elements or phishing forms inside the trusted application page
- Browser-side exfiltration of sensitive data rendered in the affected page or accessible via same-origin APIs
General mitigation guidance
Effective mitigation typically focuses on robust, context-aware output encoding and strict handling of user-supplied data:
- Apply output encoding/escaping appropriate for the context (HTML, attribute, JavaScript, URL) before rendering any user-controlled data such as
msg. - Prefer server-side frameworks or templating engines that automatically escape variables by default, and avoid manually concatenating raw input into HTML.
- Normalize and validate input to restrict values for parameters like
msgto expected formats (e.g., fixed status codes, whitelisted message keys instead of arbitrary free text). - Avoid reflecting arbitrary user input in responses; instead, use predefined, server-side messages mapped from safe identifiers.
- Consider implementing defense-in-depth mechanisms such as a Content Security Policy (CSP) to limit the impact of potential residual XSS bugs.
How teams detect issues like this
Detection efforts can focus on both code-level review and runtime testing:
- Static analysis: Use rules that flag direct inclusion of
$_GET,$_POST, or other superglobal variables in HTML output without passing through an encoding or sanitization function. - Code review: Look for echo/print statements or template variables in
add_stock.php(and similar files) that incorporate themsgparameter or other user-controlled data without escaping. - Security testing: Perform targeted input fuzzing on query parameters; automated scanners can identify reflected XSS by injecting test payloads and analyzing the rendered response.
- Security policies and CI checks: Enforce guidelines that disallow raw HTML construction with unsanitized input and integrate automated SAST/DAST tools into the development pipeline to surface XSS patterns early.

