Skip to content

fix(workflows): escape lone surrogates in to_json output - #5637

Open
leninworld wants to merge 1 commit into
github:mainfrom
leninworld:fix/to-json-surrogates
Open

leninworld wants to merge 1 commit into
github:mainfrom
leninworld:fix/to-json-surrogates

Conversation

@leninworld

Copy link
Copy Markdown
Contributor

Description

The workflow expression filter to_json serializes with ensure_ascii=False, so a surrogate code point (U+D800..U+DFFF) in a string is written to the output raw. from_json accepts 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:

steps:
  - id: emit
    type: shell
    run: "printf '%s' '{{ inputs.payload | from_json | to_json }}' > out2.json"

On main, specify workflow run wf-run.yml --input 'payload="\ud800"' stops with Workflow failed: 'utf-8' codec can't encode character '\ud800' in position 14: surrogates not allowed (exit 1). With the value in env: instead, the step fails with env 'PAYLOAD' cannot be encoded for the process environment.

This PR escapes only the code points UTF-8 cannot encode, which for a str are exactly the surrogates, as JSON \uXXXX sequences: the json.dumps result goes through encode("utf-8", "backslashreplace").decode("utf-8"). That writes \ud800, the same escape ensure_ascii=True uses. A raw surrogate can only appear inside a JSON string literal, so the output is still valid JSON, from_json turns 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 with ensure_ascii=True.) Every other non-ASCII character (é, CJK, emoji) is still written as-is, as the docs describe. The existing errors for NaN and non-string keys are unchanged.

With this PR both workflows complete: the run: step writes the 8 bytes "\ud800", and the env: step writes the same text plus its printf newline.

Not changed here: interpolating the raw from_json result without to_json still carries the surrogate (that is the input value itself). docs/workflows/expressions.md already 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 .venv and 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 and LC_ALL=en_US.UTF-8 on macOS

  • Tested with a sample project (if applicable)

  • New TestToJson cases in tests/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, the from_json | to_json round trip, and a real WorkflowEngine shell step that writes the to_json result 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, and NaN / non-string keys still raise. Switching to ensure_ascii=True, escaping only high surrogates, or replacing surrogates with U+FFFD each makes cases fail.

  • Focused: tests/specify_cli/workflows, tests/workflows and tests/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 HEAD passes.

  • Sample project: specify init --here --integration claude --script sh --ignore-agent-tools --non-interactive, then the two workflows above with payload="\ud800", before and after (a payload="é" control completes on both).

AI Disclosure

  • I did not use AI assistance for this contribution
  • I did use AI assistance (fill in the disclosure below)

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 carries Assisted-by: trailers.

`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)
@leninworld
leninworld requested a review from mnriem as a code owner November 30, 2026 15:21
Copilot AI adaptive review requested due to automatic review settings November 30, 2026 15:21

Copilot AI left a comment

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.

🟢 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants