Repository navigation
fix(workflows): escape lone surrogates in to_json output - #5637
Open
leninworld wants to merge 1 commit into
Open
leninworld wants to merge 1 commit into
leninworld wants to merge 1 commit into
Conversation
`to_json` serializes with ensure_ascii=False, so a surrogate code point
(U+D800..U+DFFF) in a string was emitted raw. `from_json` accepts the
valid JSON text "\ud800" and returns such a string, so
`{{ inputs.p | from_json | to_json }}` produced text that cannot be
encoded as UTF-8: a shell step using it in `run` failed the workflow
with "'utf-8' codec can't encode character '\ud800' ... surrogates not
allowed", and as an `env` value the step failed to start.
Escape only the code points UTF-8 cannot encode (the surrogates) as
JSON \uXXXX sequences, which is what ensure_ascii=True emits for them,
via a UTF-8 backslashreplace round trip. Ordinary non-ASCII text stays
readable and the output stays byte-stable and valid JSON; from_json
round-trips it.
Assisted-by: OpenAI Codex CLI (model: gpt-5.6-sol, autonomous)
Assisted-by: Claude Code (model: Claude Opus 5.5, autonomous)
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the surrogate-safe JSON serialization change, though tests were not independently rerun.
No open issues 🎉
What changed in this PR
This PR makes the workflow expression filter to_json produce UTF-8-safe JSON when values contain surrogate code points, preventing failures when passing serialized values to processes.
Changes:
- Escape surrogate code points while preserving readable Unicode and existing serialization error behavior.
- Add regression tests for surrogates, serialization guarantees, and a real shell-step round trip.
The PR reports before-and-after regression results; tests were not independently rerun here.
| File | Description |
|---|---|
tests/specify_cli/workflows/expressions/test_filters.py |
Adds surrogate regression tests and guards for existing JSON behavior. |
src/specify_cli/workflows/expressions/filters.py |
Escapes surrogates after JSON serialization and documents the behavior. |
Review effort: Balanced (adaptive)
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The workflow expression filter
to_jsonserializes withensure_ascii=False, so a surrogate code point (U+D800..U+DFFF) in a string is written to the output raw.from_jsonaccepts the valid JSON text"\ud800"and returns exactly such a string, so{{ inputs.payload | from_json | to_json }}returns text that cannot be encoded as UTF-8, and anything that hands it to a process fails:On main,
specify workflow run wf-run.yml --input 'payload="\ud800"'stops withWorkflow failed: 'utf-8' codec can't encode character '\ud800' in position 14: surrogates not allowed(exit 1). With the value inenv:instead, the step fails withenv 'PAYLOAD' cannot be encoded for the process environment.This PR escapes only the code points UTF-8 cannot encode, which for a
strare exactly the surrogates, as JSON\uXXXXsequences: thejson.dumpsresult goes throughencode("utf-8", "backslashreplace").decode("utf-8"). That writes\ud800, the same escapeensure_ascii=Trueuses. A raw surrogate can only appear inside a JSON string literal, so the output is still valid JSON,from_jsonturns a lone surrogate back into the same value, and the output stays byte-stable. (Two separate surrogate code points that form a pair come back as the single astral character, as withensure_ascii=True.) Every other non-ASCII character (é, CJK, emoji) is still written as-is, as the docs describe. The existing errors forNaNand non-string keys are unchanged.With this PR both workflows complete: the
run:step writes the 8 bytes"\ud800", and theenv:step writes the same text plus itsprintfnewline.Not changed here: interpolating the raw
from_jsonresult withoutto_jsonstill carries the surrogate (that is the input value itself).docs/workflows/expressions.mdalready says non-ASCII is left as-is, which stays true for every character UTF-8 can encode, so I left the doc alone.Testing
Set up this checkout's
.venvand editable install using the local development guide. Run commands from the checkout root.Tested locally with
.venv/bin/specify --help(Windows PowerShell:.\.venv\Scripts\specify.exe --help)Ran relevant existing tests with
.venv/bin/python -m pytest tests -q(Windows PowerShell:.\.venv\Scripts\python.exe -m pytest tests -q); use affected test paths for focused checks andLC_ALL=en_US.UTF-8on macOSTested with a sample project (if applicable)
New
TestToJsoncases intests/specify_cli/workflows/expressions/test_filters.py(15 cases). 10 fail on main and pass with the fix: a lone high and a lone low surrogate, a surrogate in a list, as a dict value, as a dict key, nested, mixed with ASCII andé, two separate surrogate code points, thefrom_json | to_jsonround trip, and a realWorkflowEngineshell step that writes theto_jsonresult to a file (POSIX only). 5 guards pass on both:é, CJK and an emoji stay unescaped, key order stays sorted, an existing backslash is not double-escaped, andNaN/ non-string keys still raise. Switching toensure_ascii=True, escaping only high surrogates, or replacing surrogates with U+FFFD each makes cases fail.Focused:
tests/specify_cli/workflows,tests/workflowsandtests/test_workflows.py: 3365 passed, 29 skipped.Full suite on macOS: 17332 passed, 635 skipped, 1 failed:
test_version_execution.py::TestWindowsUvToolEnvironment::test_non_windows_uv_tool_dry_run_still_previews_installer, which also fails on main in my environment (unrelated).uvx ruff@0.15.0 check src tests: all checks passed. pre-commit hooks pass on the changed files, and.github/scripts/check_file_helper_migration.py --base HEAD~1 --head HEADpasses.Sample project:
specify init --here --integration claude --script sh --ignore-agent-tools --non-interactive, then the two workflows above withpayload="\ud800", before and after (apayload="é"control completes on both).AI Disclosure
AI disclosure: OpenAI Codex CLI (model: gpt-5.6-sol, autonomous) found the gap in a read-only review of the expression filters and wrote the fix and tests (code generation) from Claude's task description. Claude Code (model: Claude Opus 5.5, autonomous) chose the issue, reproduced it end to end with
specify workflow run, reviewed the change, ran the tests, mutations, lint and hooks, and wrote this description. The commit carriesAssisted-by:trailers.