Summary
SourceCodester Pharmacy Sales and Inventory System 1.0 contains a SQL injection vulnerability in the /ajax.php?action=delete_product endpoint, where the ID parameter is incorporated into database queries without sufficient sanitization.
A remote attacker can manipulate this parameter to inject arbitrary SQL, potentially reading, modifying, or deleting data within the application's database.
Affected context
The vulnerability occurs in the Pharmacy Sales and Inventory System's AJAX handler, specifically for the delete_product action in ajax.php.
It manifests when user-supplied input in the ID parameter is passed directly into SQL statements without robust validation, parameterization, or escaping.
This issue typically affects deployments where the application is exposed over a network and the /ajax.php endpoint is reachable by untrusted users, such as internet-facing or intranet-accessible installations.
Exploit preconditions
- Attack surface: HTTP(S) interface to
/ajax.php?action=delete_product - Required access: Network access to the web application; ability to send crafted requests (authentication may or may not be required depending on deployment configuration)
- Trigger point: Submission of a malicious
IDparameter in a request to the vulnerable endpoint - Why exploitation is possible: The backend logic constructs SQL queries using the
IDvalue without using prepared statements or adequate input sanitization, allowing injected SQL fragments to alter query structure and behavior
Why this happens
The root cause aligns with CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), and is also related to CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component.
The application appears to interpolate the ID parameter directly into a SQL statement (e.g., within a WHERE clause) using string concatenation or similar dynamic query construction.
Because the code treats the ID value as trusted and does not enforce strong typing or use parameterized queries, attacker-controlled characters such as quotes, comment markers, or SQL operators can escape the intended query context and inject additional SQL logic.
Potential impact
- Unauthorized reading of sensitive pharmacy and inventory data (e.g., product, sales, customer, or user records)
- Tampering with or deletion of product and sales entries, affecting data integrity and business operations
- Creation, modification, or deletion of user accounts or roles, depending on database structure
- Escalation to full database compromise, including extraction of hashed passwords or other secrets
- In some environments, potential pivot to underlying host compromise via database-specific features or stored procedures
General mitigation guidance
Effective mitigation focuses on removing the ability for user input to influence SQL structure and reducing implicit trust in client-supplied identifiers:
- Use parameterized queries or prepared statements for all database access, binding the
IDvalue as a typed parameter rather than concatenating it into SQL strings. - Enforce strict server-side validation on identifier parameters: accept only expected formats (e.g., positive integers) and reject or normalize anything else.
- Apply the principle of least privilege to the database account used by the application, limiting it to only the operations strictly needed for product deletion and related functionality.
- Centralize data access patterns behind a data access layer that enforces safe query construction by default and discourages ad hoc SQL building.
- Consider using an ORM or query builder that defaults to safe parameterization, combined with code review standards that prohibit raw concatenated SQL for user-controlled values.
How teams detect issues like this
Detecting this vulnerability and similar issues can be approached at multiple stages:
- Static analysis: Configure rules to flag dynamic SQL construction that incorporates request parameters or other untrusted data without parameterization; scan PHP and database access code for string-concatenated queries.
- Code review: During manual review, pay special attention to controller and AJAX handlers like
ajax.phpwhere request parameters flow directly into database calls; look for patterns of$_GET/$_POST/$_REQUESTusage within SQL strings. - Dynamic testing: Use automated or manual SQL injection tests against endpoints that take identifiers, especially delete/update actions exposed via AJAX; monitor for abnormal error messages or behavior.
- Runtime monitoring: Implement database query logging and anomaly detection to identify unusual query patterns, such as appended comments, tautologies (e.g.,
OR 1=1), or stacked queries when supported by the database. - Policy enforcement: Adopt secure coding guidelines that mandate prepared statements and prohibit inline SQL concatenation with untrusted input; enforce these via pre-commit hooks or CI checks where possible.

