Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion sdk/python/src/openlit/guard/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Schema(Guard):
----------
action : str
``"deny"`` (default) or ``"warn"``.
json : bool
json_mode : bool
If ``True``, just verify the output is parseable JSON.
schema : dict, optional
A JSON-schema-like dict to validate against.
Expand Down Expand Up @@ -118,6 +118,9 @@ def evaluate(self, text: str) -> GuardResult:
explanation=f"Output is not valid JSON: {exc}",
)

if self._json_mode:
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
return GuardResult(guard_name=self.name)
Comment on lines +121 to +122

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · String false bypasses schema checks

When json_mode comes from an unparsed configuration value such as the environment string "false", self._json_mode is truthy for the string "false", so evaluate() returns ALLOW without checking the schema and accepts schema-mismatched output.

Validate that json_mode is a boolean at construction, or explicitly parse supported string values before storing it.

Prompt for AI agents
In `sdk/python/src/openlit/guard/schema.py` at lines 109-110:

**String false bypasses schema checks**

When `json_mode` comes from an unparsed configuration value such as the environment string `"false"`, `self._json_mode` is truthy for the string `"false"`, so `evaluate()` returns ALLOW without checking the schema and accepts schema-mismatched output.

Validate that `json_mode` is a boolean at construction, or explicitly parse supported string values before storing it.

Comment on lines +121 to +122

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Invalid JSON passes the guard

When JSON-only mode receives NaN, Infinity, or -Infinity, json.loads accepts these non-standard constants, so evaluate() reaches the JSON-mode ALLOW return and callers see invalid JSON pass the guard.

Reject non-standard constants during JSON parsing before returning ALLOW.

Prompt for AI agents
In `sdk/python/src/openlit/guard/schema.py` at lines 109-110:

**Invalid JSON passes the guard**

When JSON-only mode receives `NaN`, `Infinity`, or `-Infinity`, `json.loads` accepts these non-standard constants, so `evaluate()` reaches the JSON-mode ALLOW return and callers see invalid JSON pass the guard.

Reject non-standard constants during JSON parsing before returning ALLOW.


if self._schema:
err = _validate_json_schema(parsed, self._schema)
if err:
Expand Down
27 changes: 27 additions & 0 deletions sdk/python/tests/test_guard_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,30 @@ def test_type_list_ignores_unknown_names(schema_type, value, action):
result = Schema(schema={"type": schema_type}).evaluate(value)

assert result.action == action

def test_json_mode_allows_parseable_json_even_on_schema_mismatch():
"""json_mode=True skips schema checks once output parses as JSON."""
result = Schema(
action="deny", json_mode=True, schema={"type": "integer"}
).evaluate('"hello"')

assert result.action == GuardAction.ALLOW
assert result.classification == ""


def test_json_mode_still_denies_invalid_json():
"""json_mode=True still rejects output that is not parseable JSON."""
result = Schema(
action="deny", json_mode=True, schema={"type": "integer"}
).evaluate("not json")

assert result.action == GuardAction.DENY
assert result.classification == "invalid_json"


def test_default_mode_still_denies_schema_mismatch():
"""Default json_mode=False keeps schema validation enabled."""
result = Schema(action="deny", schema={"type": "integer"}).evaluate('"hello"')

assert result.action == GuardAction.DENY
assert result.classification == "schema_mismatch"
Loading