Summary
A business logic flaw in the Pharmacy Product Management System's stock entry workflow allows negative quantities to be submitted via the txtqty parameter in add-stock.php.
When abused, this causes the system to reduce inventory instead of increasing it, leading to corrupted stock counts and potential denial of service by artificially depleting available stock.
Affected context
The issue affects the stock management functionality of SourceCodester Pharmacy Product Management System 1.0, specifically when users add stock through add-stock.php.
The vulnerability is present where the application trusts the txtqty parameter as-is, assumes it represents a positive quantity to add, and does not enforce domain rules preventing negative or nonsensical values.
Any deployment where users or integrated systems can submit crafted requests to the stock update endpoint may be exposed to inconsistent inventory states.
Exploit preconditions
- Attack surface: HTTP interface handling stock addition (e.g.,
add-stock.php). - Required access: Ability to submit or intercept and modify stock addition requests (typically authenticated user with stock-management privileges, but could be lower-privileged if access is misconfigured).
- Trigger point: Submission of a stock addition form or API request where
txtqtyis set to a negative number or otherwise invalid quantity. - Why exploitation is possible: The business logic trusts the client-supplied quantity and does not enforce domain constraints (such as non-negative, minimum, or maximum bounds), allowing negative values to be processed as valid inputs that directly alter inventory in an unintended way.
Why this happens
The core issue is a mismatch between intended business rules and the actual implemented logic for stock updates.
Instead of validating that txtqty represents a positive quantity to be added, the application accepts any numeric value and applies it directly to the inventory count.
This reflects CWE-1284: Improper Validation of Specified Quantity in Input, compounded by CWE-20: Improper Input Validation, because the system does not constrain user-controlled values to the domain-appropriate range.
At an abstract level, the stock adjustment routine treats user input as authoritative arithmetic without enforcing business invariants (e.g., "add stock must increase inventory"), allowing users to drive internal state transitions that violate the intended workflow.
Potential impact
- Inventory counts may become negative or otherwise inconsistent with real-world stock.
- Attackers could artificially deplete stock records, making critical medications appear unavailable.
- Operational staff may be unable to rely on the system for ordering, dispensing, or auditing, causing workflow disruption.
- Application-level denial of service could occur if essential products are shown as out of stock, halting normal pharmacy operations.
- Financial and reporting inaccuracies may arise due to corrupted inventory metrics.
General mitigation guidance
Effective mitigation centers on enforcing explicit business rules around quantity handling and input trust:
- Validate that any quantity-related field (such as
txtqty) is within an acceptable domain: numeric, non-negative, and consistent with the action being performed (e.g., add vs. remove stock). - Separate business operations conceptually (e.g., "add stock" vs. "subtract stock") and ensure each path enforces the correct sign and bounds for quantity changes before applying them to inventory.
- Introduce server-side business rule checks that treat client input as a request rather than a command, rejecting or normalizing values that would violate invariants (such as driving inventory below zero or flipping operation semantics).
- Use central validation and domain model APIs for inventory updates instead of directly manipulating counts in multiple places, so constraints are applied consistently.
- Apply least-privilege principles to stock-modifying operations, limiting who or what can trigger inventory adjustments and adding approval or logging for sensitive changes.
How teams detect issues like this
Organizations can detect similar business logic flaws through a mix of review and testing:
- Static and manual review: Examine inventory update routines for direct arithmetic on user-supplied quantities without range checks or domain constraints; look for forms or endpoints where quantity parameters are trusted.
- Business logic testing: Conduct negative testing of stock operations by attempting to submit negative, zero, or unusually large quantities and observing inventory behavior.
- Automated checks: Configure application security testing tools or custom linting rules to flag quantity fields that bypass centralized validation layers.
- Runtime monitoring: Add audit logging around inventory changes (who changed what, when, and by how much) and monitor for suspicious patterns such as frequent large negative adjustments or stock levels dropping below expected thresholds.

