T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/main.py:27
- Finding
- Unvalidated Numeric Inputs Can Crash or Disable Automated Monitoring<![CDATA[ ## Vulnerability Details **File Location**: `scripts/main.py`, lines 27–35; related command-line input handling at lines 153–182 **Vulnerability Type**: Improper numeric input validation and unhandled arithmetic exceptions **Risk Level**: Medium ### Vulnerable Code ```python def calculate_remaining_days(pressure: float, daily_consumption: float) -> float: """Calculate remaining days""" if daily_consumption <= 0: return float('inf') return pressure / daily_consumption def calculate_depletion_time(remaining_days: float) -> datetime: """Calculate estimated depletion time""" return get_current_time() + timedelta(days=remaining_days) ``` The command-line arguments are parsed as unrestricted floating-point values: ```python parser.add_argument( "--pressure", "-p", type=float, default=8.0, help="Current tank pressure (MPa), default 8.0" ) parser.add_argument( "--daily-consumption", "-d", type=float, default=1.5, help="Daily consumption rate (MPa/day), default 1.5" ) parser.add_argument( "--alert-days", "-a", type=int, default=2, help="Alert threshold in days, default 2" ) ``` ### Technical Analysis The program does not verify that pressure and consumption values are finite, positive, and within physically reasonable limits. Python's `float()` parser accepts special values such as `nan`, `inf`, and `-inf`. A zero or negative consumption value is converted to positive infinity by `calculate_remaining_days()`. That value is then passed to `timedelta(days=remaining_days)`, which can raise `OverflowError` or another arithmetic conversion exception. Non-finite or excessively large pressure values can produce the same result. Negative pressure and negative alert thresholds are also accepted, potentially producing misleading status calculations rather than a sensor-data validation failure. No exception handler converts these failures into an explicit monitoring-error alert. ### Att ...[truncated 1252 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions - Validate all numeric values immediately after argument parsing. - Use `math.isfinite()` to reject `nan`, positive infinity, and negative infinity. - Require pressure and daily consumption to be positive and within documented physical limits. - Require `alert_days` to be non-negative and impose a reasonable maximum. - Do not translate invalid consumption into infinity; report invalid sensor data explicitly. - Catch `OverflowError`, `ValueError`, and related calculation failures at the command boundary. - Return a dedicated monitoring-failure exit code that automation treats as an urgent operational fault. - Add tests covering zero, negative, non-finite, and excessively large values. Example hardening: ```python import math def validate_inputs(pressure: float, daily_consumption: float, alert_days: int) -> None: if not math.isfinite(pressure) or pressure < 0 or pressure > 20: raise ValueError("Pressure must be finite and between 0 and 20 MPa") if not math.isfinite(daily_consumption) or daily_consumption <= 0: raise ValueError("Daily consumption must be finite and greater than zero") if alert_days < 0 or alert_days > 30: raise ValueError("Alert threshold must be between 0 and 30 days") ``` ]]>
