Skip to content

Commit 2ed5735

Browse files
marcelsafinCopilot
andcommitted
fix(integrations): escape control characters in goose recipe YAML renderer
YAML forbids C0 control characters (except tab and newline) and DEL in every scalar form, and a bare CR acts as a line break inside a block scalar. _render_yaml wrote the body verbatim into a |2 literal block scalar, so such bodies produced recipes the YAML parser rejects. Detect block-scalar-unsafe characters and fall back to an escaped double-quoted scalar via yaml.safe_dump, mirroring the TOML renderer's fallback strategy from github#3341. Fixes github#3382 Co-authored-by: Copilot <[email protected]>
1 parent 1be4299 commit 2ed5735

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎src/specify_cli/integrations/base.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,11 @@ def setup(
11221122
# YamlIntegration — YAML-format agents (Goose)
11231123
# ---------------------------------------------------------------------------
11241124

1125+
# Characters a YAML literal block scalar cannot carry: C0 controls other
1126+
# than tab/LF (a bare CR acts as a line break inside the scalar), and DEL.
1127+
_YAML_BLOCK_SCALAR_UNSAFE = re.compile(r"[\x00-\x08\x0b-\x1f\x7f]")
1128+
1129+
11251130
class YamlIntegration(IntegrationBase):
11261131
"""Concrete base for integrations that use YAML recipe format.
11271132
@@ -1240,6 +1245,23 @@ def _render_yaml(cls, title: str, description: str, body: str, source_id: str) -
12401245
default_flow_style=False,
12411246
).strip()
12421247

1248+
# YAML forbids C0 control characters (except tab and newline) and
1249+
# DEL in every scalar form, and a bare CR acts as a line break
1250+
# inside a block scalar. A literal block scalar emits such bytes
1251+
# verbatim, producing a recipe the YAML parser rejects, so fall
1252+
# back to an escaped double-quoted scalar for those bodies.
1253+
if _YAML_BLOCK_SCALAR_UNSAFE.search(body):
1254+
prompt_yaml = yaml.safe_dump(
1255+
{"prompt": body}, allow_unicode=True, default_style='"', width=float("inf")
1256+
).strip()
1257+
lines = [
1258+
header_yaml,
1259+
prompt_yaml,
1260+
"",
1261+
f"# Source: {source_id}",
1262+
]
1263+
return "\n".join(lines) + "\n"
1264+
12431265
# Indent the body for YAML block scalar. Use an explicit indentation
12441266
# indicator ("|2") rather than a bare "|": YAML infers a plain block
12451267
# scalar's indentation from its first non-empty line, so a body whose

‎tests/integrations/test_integration_base_yaml.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,29 @@ def test_yaml_prompt_with_indented_first_line_stays_valid(self):
201201
parsed = yaml.safe_load("\n".join(yaml_lines))
202202
assert parsed["prompt"].rstrip("\n") == body
203203

204+
def test_yaml_prompt_with_control_characters_stays_valid(self):
205+
"""A body containing control characters must still produce parseable YAML.
206+
207+
YAML forbids C0 control characters (except tab and newline) and DEL
208+
in every scalar form; a literal block scalar emits them verbatim,
209+
so the generated recipe failed to load. The renderer falls back to
210+
an escaped double-quoted scalar for such bodies."""
211+
for ch in ("\x08", "\x0c", "\x1b", "\x7f"):
212+
body = f"before{ch}after\nsecond line"
213+
rendered = YamlIntegration._render_yaml("Title", "Desc", body, "src")
214+
parsed = yaml.safe_load(rendered)
215+
assert parsed["prompt"].rstrip("\n") == body, f"char {ch!r} round-trip"
216+
217+
def test_yaml_prompt_with_bare_carriage_return_stays_valid(self):
218+
"""A bare CR (not part of CRLF) must not break the generated YAML.
219+
220+
Inside a block scalar a lone \r acts as a line break, corrupting
221+
the document structure."""
222+
body = "line1\rstill line1\nline2"
223+
rendered = YamlIntegration._render_yaml("Title", "Desc", body, "src")
224+
parsed = yaml.safe_load(rendered)
225+
assert parsed["prompt"].rstrip("\n") == body
226+
204227
def test_plan_command_has_no_context_placeholder(self, tmp_path):
205228
"""The generated plan command must not carry a context-file placeholder.
206229

0 commit comments

Comments
 (0)