Skip to content

FIX: reject non-finite score_value strings in numeric response handler - #2459

Merged
Roman Lutz (romanlutz) merged 3 commits into
microsoft:mainfrom
feiiiiii5:fix/nan-score-validation
Aug 22, 2026
Merged

FIX: reject non-finite score_value strings in numeric response handler#2459
Roman Lutz (romanlutz) merged 3 commits into
microsoft:mainfrom
feiiiiii5:fix/nan-score-validation

Conversation

@feiiiiii5

Copy link
Copy Markdown
Contributor

Root Cause

JsonSchemaResponseHandler with numeric_value=True validated the score value with a bare float(...) cast. Python's float grammar accepts "nan", "inf", "-inf" (case-insensitive), so these strings passed as valid numeric scores.

Downstream, float-scale aggregation clamps with max(0.0, min(1.0, x)). Every comparison against NaN is False, so min(1.0, nan) returns 1.0 — a NaN judge response is silently clamped to the maximum harm value instead of surfacing an error.

Fix

Treat non-finite parsed values as invalid scoring responses via math.isfinite, raising InvalidJsonException consistent with the existing non-numeric branch.

Fixes #2458

Test

  • New parametrized regression test test_json_schema_response_handler_rejects_non_finite_numeric_values covering the five accepted spellings (nan, NaN, inf, -inf, Infinity) — fails on main, passes with this change.
  • Verified locally pre-fix (all five accepted) and post-fix (all rejected; valid "0.75" still parses).

Diff Scope

  • pyrit/score/response_handler.py: + finite check in the numeric branch (+ import math)
  • tests/unit/score/test_response_handler.py: + regression test

Development was AI-assisted with human review of all changes.

fei (feiiiiii5) and others added 2 commits August 22, 2026 16:24
JsonSchemaResponseHandler with numeric_value=True validated the value
with a bare float() cast, so "nan", "inf" and "-inf" strings passed as
valid scores. Float-scale aggregation then clamps with
max(0.0, min(1.0, x)); since every comparison against NaN is False,
min(1.0, nan) returns 1.0 and a NaN judge response silently aggregates
toward the maximum harm score instead of surfacing an error.

Treat non-finite parsed values as invalid scoring responses via
math.isfinite. Regression tests cover the five accepted spellings.

Fixes microsoft#2458

Signed-off-by: fei <204683769+feiiiiii5@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: baa441b0-0b2f-4c62-b230-df65bc4adc6c
@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Aug 22, 2026
Merged via the queue into microsoft:main with commit 76b321f Aug 22, 2026
54 checks passed
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.

Non-finite score_value strings ('nan'/'inf') pass numeric validation and inflate aggregated float-scale scores

3 participants