T09 · Insecure Skill Coding Practices
- Location
tool.py:41- Finding
Unvalidated Shipment Quantities Allow Division-by-Zero Failures
- Content
View full analysis
Vulnerability Details
File Location:
tool.py, lines 41-42
Vulnerability Type: Improper input validation leading to an unhandled exception
Risk Level: LowVulnerable Code
python "cost_per_kg": total / product.get("net_weight_kg", 1), "cost_per_unit": total / product.get("quantity_units", 1)Technical Analysis
The calculation uses a fallback value of
1only whennet_weight_kgorquantity_unitsis absent. If either field is explicitly set to zero, Python raises an unhandledZeroDivisionError. Non-numeric values can similarly causeTypeError.Because
calculate_landed_costaccepts a caller-supplied dictionary without validating its structure or values, malformed or adversarial shipment data can interrupt tool execution.Attack Path
- An attacker or untrusted upstream source constructs a shipment object with
product.net_weight_kgorproduct.quantity_unitsset to0. - The object is passed to
calculate_landed_cost. - The function calculates the total cost and attempts to divide it by the zero value.
- Python raises an uncaught
ZeroDivisionError. - The current calculation or dependent Agent workflow terminates unless the caller independently handles the exception.
Example triggering input:
python shipment = { "shipment_id": "TEST-001", "product": { "quantity_units": 0, "net_weight_kg": 0 }, "costs": { "product_cost": 100 } } calculate_landed_cost(shipment)Impact Assessment
Exploitation does not grant additional privileges, expose data, or enable code execution. Its impact is limited to availability: an attacker able to influence shipment input can reliably cause the calculation to fail. Repeated malicious inputs could disrupt workflows that depend on this function, although the reviewed code does not establish a persistent or system-wide denial-of-service condition.
- An attacker or untrusted upstream source constructs a shipment object with
- Remediation
View remediation
Remediation Suggestions
Validate the complete shipment structure before performing arithmetic:
- Require
shipment,product, andcoststo have the expected mapping types. - Require
net_weight_kgandquantity_unitsto be finite numeric values greater than zero. - Require all cost fields to be finite, non-negative numeric values.
- Reject booleans, which Python otherwise treats as integers.
- Return a controlled validation error rather than exposing an unhandled arithmetic exception.
- Add tests for zero, negative, missing, non-numeric, infinite, and NaN values.
Example hardened approach:
python import math def require_positive_number(value, field_name): if isinstance(value, bool) or not isinstance(value, (int, float)): raise ValueError(f"{field_name} must be numeric") if not math.isfinite(value) or value <= 0: raise ValueError(f"{field_name} must be finite and greater than zero") return value net_weight = require_positive_number( product.get("net_weight_kg"), "net_weight_kg" ) quantity = require_positive_number( product.get("quantity_units"), "quantity_units" ) return { "shipment_id": shipment.get("shipment_id"), "landed_cost": total, "cost_per_kg": total / net_weight, "cost_per_unit": total / quantity, }- Require
