Skip to content

fix(python-cli): reject lone surrogates in JSON input before HTTP encoding - #13389

Merged
kodjima33 merged 1 commit into
BasedHardware:mainfrom
gianluca-disanto:fix-python-cli-json-surrogates
Sep 10, 2026
Merged

kodjima33 merged 1 commit into
BasedHardware:mainfrom
gianluca-disanto:fix-python-cli-json-surrogates

Conversation

@gianluca-disanto

@gianluca-disanto gianluca-disanto commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor
  • Validate strings and dictionary keys in load_json_input() to ensure UTF-8 encodability.
  • Reject lone surrogates at input parsing rather than crashing with unhandled UnicodeEncodeError inside httpx.
  • Add tests/test_json_input_unicode.py covering root values, dictionary keys, nested structures, and byte streams.

Fixes #13382

## What changed and why
`load_json_input` previously accepted JSON containing lone surrogate escape sequences (e.g. `\ud800`). When later serialized by `httpx` for HTTP

transmission, an unhandled UnicodeEncodeError was raised. Validating decoded strings ensures invalid Unicode is rejected as a ValueError at the CLI
boundary.

## Product invariants affected
none

## How it was verified
Ran `pytest tests/test_json_input_unicode.py tests/test_json_input_commands.py`. 42/42 tests pass.

## Tests
`sdks/python-cli/tests/test_json_input_unicode.py`

## Failure class (fixes)
Failure-Class: none

Review in cubic

…oding

- Validate decoded strings and dict keys in load_json_input for UTF-8 encodability.
- Prevent unhandled UnicodeEncodeError inside httpx request serializer when lone surrogates are supplied.
- Add focused test suite in test_json_input_unicode.py covering root, keys, nested structures, and byte inputs.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@Git-on-my-level

Copy link
Copy Markdown
Member

Thanks for the clean fix and the thorough test matrix — this closes a real gap: json.loads happily decodes lone surrogates that httpx's JSON encoder later chokes on.

sdks/python-cli/omi_cli/json_input.py — the post-parse walk UTF-8-checks every string (root scalars, array elements, and both keys and values of objects), and the resulting UnicodeEncodeError is a ValueError subclass, so both call sites keep working unchanged: commands/local.py::call maps it to the existing --args-json must be valid JSON usage error and commands/conversation.py::from-segments to Invalid JSON in <file>. The existing non-finite-number rejection is untouched, and the docstring updates read accurately.

sdks/python-cli/tests/test_json_input_unicode.py — good coverage: surrogate at root/value/key/nested positions, str and utf-8/16/32 byte inputs, a regression guard for the non-finite rejection, and a round-trip through a real httpx.Request. I ran this file against the PR head in a clean environment (pytest 9.1.1, httpx 0.28.1): 20/20 pass.

One heads-up: the Python CLI workflow is awaiting first-time-contributor workflow approval, so repo CI hasn't executed the suite for this PR yet — approving the run before merge will give the full matrix. Leaving the merge call to a maintainer.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added python positive-signal Automation verified a genuine fix/quality contribution labels Sep 10, 2026

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

fix(python-cli): rejects lone Unicode surrogates in load_json_input before httpx encoding, preventing unhandled UnicodeEncodeError. Confirmed not yet on main. Fixes #13382, scoped diff, tests added. Confidence 5/5.

@kodjima33
kodjima33 merged commit 7eda78e into BasedHardware:main Sep 10, 2026
1 check passed
@gianluca-disanto
gianluca-disanto deleted the fix-python-cli-json-surrogates branch September 10, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

positive-signal Automation verified a genuine fix/quality contribution python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python CLI JSON input accepts lone surrogates that fail HTTP encoding

3 participants