Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 2024-06-30 - Missing Input Validation Exposing Stack Traces
**Vulnerability:** The `voltage_divider_calculations` function lacked input validation for edge cases like zero resistance or equal input/output voltages, leading to unhandled `ZeroDivisionError` exceptions. Unhandled exceptions can expose internal application details through stack traces, a common security risk (Information Exposure).
**Learning:** Mathematical operations involving user-provided inputs must explicitly check for boundary conditions (like division by zero) and physically impossible states (like negative resistance) to prevent unhandled exceptions and potential denial of service or information leakage.
**Prevention:** Always validate inputs before using them in calculations. Ensure that user-facing functions fail gracefully with safe, informative error messages (like `ValueError`) rather than propagating system-level exceptions.
13 changes: 13 additions & 0 deletions lib/voltage_divider_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ def voltage_divider_calculations(
ValueError: If there is insufficient data to perform the calculation or too many arguments are provided.
"""

# Input validation
for name, value in [("Vin", vin), ("Vout", vout), ("R1", r1), ("R2", r2)]:
if value is not None and value < 0:
raise ValueError(f"{name} cannot be negative.")

# If Vin is not provided, calculate Vin based on the other values
if vin is None:
# Ensure vout, r1, and r2 are provided to calculate Vin
if vout is None or r1 is None or r2 is None:
raise ValueError(
"Insufficient data to calculate Vin. Provide Vout, R1, and R2."
)
if r2 == 0:
raise ValueError("R2 cannot be zero when calculating Vin.")
# Calculate Vin using the rearranged voltage divider formula: Vin = Vout * (R1 + R2) / R2
return vout * (r1 + r2) / r2

Expand All @@ -43,6 +50,8 @@ def voltage_divider_calculations(
raise ValueError(
"Insufficient data to calculate Vout. Provide Vin, R1, and R2."
)
if r1 + r2 == 0:
raise ValueError("The sum of R1 and R2 cannot be zero.")
# Calculate Vout using the voltage divider formula: Vout = Vin * (R2 / (R1 + R2))
return vin * (r2 / (r1 + r2))

Expand All @@ -53,6 +62,8 @@ def voltage_divider_calculations(
raise ValueError(
"Insufficient data to calculate R1. Provide Vout, Vin, and R2."
)
if vin == vout:
raise ValueError("Vin and Vout cannot be equal when calculating R1.")
# Calculate R1 using the rearranged formula: R1 = (Vout * R2) / (Vin - Vout)
return vout * r2 / (vin - vout)

Expand All @@ -63,6 +74,8 @@ def voltage_divider_calculations(
raise ValueError(
"Insufficient data to calculate R2. Provide Vout, Vin, and R1."
)
if vin == vout:
raise ValueError("Vin and Vout cannot be equal when calculating R2.")
# Calculate R2 using the rearranged formula: R2 = (Vout * R1) / (Vin - Vout)
return vout * r1 / (vin - vout)

Expand Down