Skip to content

About

Contribution note: merged PR #404 to msoedov/agentic_security (LLMSpec.model_name hardening)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

agentic_security PR #404: hardening LLMSpec.model_name

A contribution note on my first merged open-source pull request.

All credit for the project belongs to its maintainer and contributors. This repository only documents my patch; it contains no copy of the upstream code.

Summary

LLMSpec.model_name is documented to return the model field of a JSON request body, or "unknown". The request spec is user-supplied, so the body is untrusted input. The function did not keep its promise in two cases, and one of them could crash a scan during cost accounting.

The bug

Original logic:

try:
    body_json = json.loads(self.body)
    return body_json.get("model", "unknown")
except (json.JSONDecodeError, TypeError):
    return "unknown"

Gap 1: valid JSON that is not an object. json.loads accepts [1, 2], "text", null and 42. None of these has .get, so the call raises AttributeError, which the except clause does not catch. The fuzzer only survived because a caller used getattr(spec, "model_name", "unknown"), which swallows AttributeError by accident.

Gap 2: a model value that is not a string. For {"model": ["a", "b"]} or {"model": null} the old code returned the value unchanged. A list or dict then reaches calculate_cost, where model not in PRICING raises TypeError: unhashable type, and cost accounting fails mid-scan.

The fix

Return "unknown" unless the body is a JSON object whose model is a non-empty string.

try:
    body_json = json.loads(self.body)
except (json.JSONDecodeError, TypeError):
    return "unknown"
if not isinstance(body_json, dict):
    return "unknown"
model = body_json.get("model")
if isinstance(model, str) and model:
    return model
return "unknown"

This is one behavior change. The docstring did not need editing, because the code now matches what it already claimed.

Evidence

The PR added tests/unit/test_model_name.py with 14 tests:

  • 13 parametrized cases: normal name, missing field, empty body, non-JSON, JSON that is not an object (list, string, null, number), and model set to null, empty string, number, list or object
  • 1 check that the result can be passed straight into calculate_cost without raising

Results reported in the PR:

Check Result
New tests on main before the fix 10 failed
New tests with the fix all pass
pytest tests/unit tests/integration 538 passed, 2 skipped
black, flake8 clean

Approach

  1. Read the function's contract (its docstring), then try to break it with input an attacker or a careless config could produce.
  2. Treat the shape of untrusted input as untrusted, not only its values. "Valid JSON" has several shapes.
  3. Write the failing tests first, so the bug is shown rather than claimed.
  4. Keep the change to one fix, so review is quick.

Disclosure

The code reading, the fix and the tests were produced by an AI assistant working under my approval. I decided to contribute, reviewed the change, and chose to open the PR and publish this note.

Other work in review

Further contributions are open on NVIDIA/garak, Skyscanner/cfripper, cisco-ai-defense/mcp-scanner, CERT-Polska/Artemis, projectdiscovery/nuclei-templates and promptfoo/promptfoo. Profile: github.com/fouadbehtaher.

Fouad Taher

About

Contribution note: merged PR #404 to msoedov/agentic_security (LLMSpec.model_name hardening)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors