Summary
SourceCodester Hospitals Patient Records Management System 1.0 contains a SQL injection vulnerability in the patient history save functionality, where the ID parameter is not safely handled before being used in database queries.
A remote attacker can manipulate this parameter to inject arbitrary SQL, which may lead to unauthorized access, modification, or disclosure of patient records when the vulnerable endpoint is reachable.
Affected context
The issue occurs in the /classes/Master.php?f=save_patient_history endpoint responsible for saving patient history records.
It affects deployments where user-controlled input (specifically the ID argument) is passed into SQL queries without robust validation or parameterization.
Any environment that exposes this functionality over a network, such as internet-facing or intranet hospital deployments, can be at risk if no protective controls (e.g., prepared statements, strict input validation, or WAF rules) are in place.
Exploit preconditions
- Attack surface: HTTP(S) endpoint
/classes/Master.php?f=save_patient_history - Required access: Ability to send web requests to the affected application; authentication may or may not be required depending on local configuration
- Trigger point: Supplying crafted values in the
IDparameter (e.g., via query string or POST data) that are subsequently embedded into SQL queries - Why exploitation is possible: The application dynamically constructs SQL statements using user input without proper parameter binding or escaping, allowing attacker-controlled content to alter query structure
Why this happens
The underlying bug is improper construction of SQL statements from untrusted input, mapping directly to CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection').
The ID parameter is likely concatenated into a SQL query string or interpolated directly, without using prepared statements, bound parameters, or rigorous input sanitization.
Additionally, this fits the broader pattern described by CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component, because data intended as simple values is treated as part of a command for the downstream database engine.
This insecure pattern allows an attacker to switch from providing data to providing executable query fragments.
Potential impact
- Unauthorized reading of patient records and related medical data
- Modification or deletion of patient history entries, potentially impacting clinical decisions
- Extraction of user account data, including password hashes or session-related information
- Escalation of privileges within the application via manipulation of authentication or authorization tables
- Potential pivot to underlying database server features (e.g., file read/write or command execution) depending on configuration
General mitigation guidance
Effective mitigation focuses on removing direct string concatenation and treating all external input as untrusted:
- Use parameterized queries or prepared statements for every database interaction involving user input, including the
IDfield - Apply strict server-side validation and type enforcement (e.g., ensuring IDs are numeric and within expected ranges) before touching the database layer
- Centralize query construction in a data access layer that enforces safe patterns by default
- Implement least-privilege database accounts so the application can only access the minimum required tables and operations
- Add defense-in-depth controls such as input normalization and, where appropriate, a web application firewall (WAF) with rules for SQL injection patterns
How teams detect issues like this
Detection typically combines code-centric review with runtime monitoring:
- Static analysis rules that flag dynamic SQL construction, string concatenation with
$_GET,$_POST, or similar request variables, and APIs known for unsafe query building - Manual code review focusing on controller and model methods handling patient histories, validating that every query uses bound parameters
- Central logging of database errors and suspicious query patterns (e.g., presence of tautologies, UNION clauses, or stacked statements), coupled with anomaly detection
- CI/CD integration that runs automated security scanning tools against the codebase and, optionally, DAST scanners against test instances to identify injectable parameters

