Skip to content

Fix(tool-call-detector): handle non-numeric strings in numeric operators#259

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

Fix(tool-call-detector): handle non-numeric strings in numeric operators#259
Jean-Regis-M wants to merge 1 commit intoGenAI-Security-Project:mainfrom
Jean-Regis-M:patch-21

Conversation

@Jean-Regis-M
Copy link

Summary

Fixes #131: Prevents unhandled ValueError when actual value is a non‑numeric string in numeric operator conditions.

Problem

In _check_condition, numeric operators (gt, gte, lt, lte) directly convert actual and expected to float. If actual is a non‑numeric string like "pending", float(actual) raises ValueError, crashing the detector coroutine. This allows an attacker to cause denial‑of‑detection by sending a single malformed event.

Root Cause

The code lacks exception handling for float conversion. The earlier None guard does not protect against non‑numeric strings. This leads to uncaught ValueError propagating through _check_parameters to check_event, stopping all subsequent event evaluations.

Solution

Wrap the float conversions in a try/except block that catches TypeError and ValueError. On exception, return False (non‑numeric cannot satisfy a numeric condition). The operators are grouped to compute floats once, minimising changes. This preserves existing behaviour for valid numeric inputs.

# Before
if op == "gt":
    return float(actual) > float(expected)
if op == "gte":
    return float(actual) >= float(expected)
if op == "lt":
    return float(actual) < float(expected)
if op == "lte":
    return float(actual) <= float(expected)

# After
if op in ("gt", "gte", "lt", "lte"):
    try:
        actual_f = float(actual)
        expected_f = float(expected)
    except (TypeError, ValueError):
        return False
    if op == "gt":
        return actual_f > expected_f
    if op == "gte":
        return actual_f >= expected_f
    if op == "lt":
        return actual_f < expected_f
    if op == "lte":
        return actual_f <= expected_f

Root cause:
Numeric operators (gt, gte, lt, lte) call float(actual) without try/except,
causing unhandled ValueError when actual is a non-numeric string, crashing the
detector.

Solution:
Wrap float conversions in try/except, return False on conversion error.
Group operators to avoid duplication. This ensures the detector gracefully
handles malformed input and continues processing.

Impact:
No breaking changes. Minimal diff. Improves robustness against invalid input
without affecting valid numeric comparisons.

Signed-off-by: JEAN REGIS <240509606@firat.edu.tr>
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_034_MUST_FIX: Test Case PRM-TOL-019 _check_condition numeric operators raise unhandled ValueError when actual value is a non-numeric string

1 participant