Skip to content

fix: validate risk_level in update_vendor_risk to prevent mixed-case values#220

Open
Jean-Regis-M wants to merge 1 commit intoGenAI-Security-Project:mainfrom
Jean-Regis-M:patch-12
Open

fix: validate risk_level in update_vendor_risk to prevent mixed-case values#220
Jean-Regis-M wants to merge 1 commit intoGenAI-Security-Project:mainfrom
Jean-Regis-M:patch-12

Conversation

@Jean-Regis-M
Copy link

Summary

Fixes #182 by adding input validation to update_vendor_risk so that only exactly "low", "medium", or "high" are accepted.

Problem

Currently, update_vendor_risk accepts any string as risk_level and stores it without validation. This allows mixed‑case values like "Medium" to be saved. Downstream logic that compares risk_level == "medium" fails because the stored value is "Medium", breaking fraud assessment and risk escalation.

Root Cause

The function lacks input validation. The risk_level parameter is passed directly to the repository without checking against the allowed set defined in the docstring. This is a validation gap.

Solution

Add a membership check at the start of the function:

  • Define VALID_RISK_LEVELS = {"low", "medium", "high"}
  • If risk_level is not in the set, raise ValueError with a clear message.

This ensures only the documented values are persisted, and any deviation fails fast.

Impact

  • No breaking changes: all valid calls continue to work.
  • Minimal diff: only 4 lines added.
  • Improved correctness: invalid inputs are rejected instead of silently corrupting data.
  • Prevents future bugs where case variations cause logic errors.

Testing

  • Verified with the existing unit test (test_fraud_upd_013_mixed_case_risk_level_accepted_without_validation) which now passes.
  • Manually tested with:
    • "medium" → succeeds
    • "Medium" → raises ValueError
    • "MEDIUM" → raises ValueError
    • "" → raises ValueError
    • None → raises ValueError
    • 123 → raises ValueError

Checklist

  • Change is minimal and isolated
  • Backward compatible
  • Follows existing error handling pattern (ValueError)
  • No side effects

Root cause:
Missing input validation allowed mixed-case strings like "Medium" to be
persisted, breaking downstream comparisons expecting exact "medium".

Solution:
Add membership check against VALID_RISK_LEVELS = {"low", "medium", "high"}
at function entry. Invalid values now raise ValueError.

Impact:
Minimal change, no side effects for valid inputs, prevents silent data
corruption, aligns with documented behavior.


Signed-off-by: JEAN REGIS <240509606@firat.edu.tr>
@Jean-Regis-M Jean-Regis-M marked this pull request as ready for review March 17, 2026 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug_071_EVALUATE: Test Case FRAUD-UPD-013: update_vendor_risk accepts "Medium" (mixed case) as risk_level

1 participant