A contribution note on my first merged open-source pull request.
- Upstream project: msoedov/agentic_security, an open-source agentic LLM vulnerability scanner and AI red-teaming kit
- Pull request: #404
fix(http_spec): make LLMSpec.model_name always return a string - Status: merged on 2026-11-27 by the maintainer, review: "LGTM, thx for the patch"
- Merge commit:
2baacb874572baf9bbabf378d200d44022ff94a7 - Change size: 2 files; a small fix in
agentic_security/http_spec.pyplus a new test filetests/unit/test_model_name.py - Long-form write-up: My First Merged Open-Source PR (Medium)
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.
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.
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.
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.
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
modelset to null, empty string, number, list or object - 1 check that the result can be passed straight into
calculate_costwithout 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 |
- Read the function's contract (its docstring), then try to break it with input an attacker or a careless config could produce.
- Treat the shape of untrusted input as untrusted, not only its values. "Valid JSON" has several shapes.
- Write the failing tests first, so the bug is shown rather than claimed.
- Keep the change to one fix, so review is quick.
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.
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