Skip to content

fix(renderer): quote env entries in generated compose files - #565

Open
Abhishek9639 wants to merge 1 commit into
ossf:mainfrom
Abhishek9639:fix/compose-env-quoting
Open

Abhishek9639 wants to merge 1 commit into
ossf:mainfrom
Abhishek9639:fix/compose-env-quoting

Conversation

@Abhishek9639

Copy link
Copy Markdown

Summary

Env entries in the build-target and run compose templates were rendered as - KEY=value, so YAML parsed the value before docker compose got it. A value with # lost everything after the #, and a value with : (inline JSON, say) became a mapping that docker compose rejects.

This adds a small yaml_quote filter in renderer.py that lets PyYAML write each entry as a single-line, double-quoted scalar, and uses it for the three loops fed by additional_env: the builder env, the run module env and the builder sidecar passthrough env. The filter is registered in a create_template_env() helper that render_template() now uses, and the template tests build their Jinja environment through it too, so they render with the same filters as production.

In the issue I suggested tojson. I switched because it escapes anything outside the BMP as a surrogate pair (rocket 🚀 becomes "rocket \ud83d\ude80"), and docker compose rejects that. Before this change an emoji in a value worked, so tojson would have traded one breakage for another. PyYAML emits \U0001F680 instead, which both PyYAML and compose read back correctly.

${VAR} references still work, since compose interpolates after parsing.

Not changed here: framework-owned env and volume lines (paths, HARNESS_NAME, TARGET_NAME) are still plain scalars. They'd only break on a --work-dir or harness name containing # or : , so I left them out to keep this focused.

Fixes #548

User Impact

  • User-facing change
  • Internal-only change

additional_env values now reach the containers as written. No config change or migration is needed.

Release Note / Changelog

  • I updated CHANGELOG.md ([Unreleased]) for user-facing changes
  • No changelog entry needed (internal-only refactor/test/chore)

Validation

  • uv run verify: ruff check, ruff format and pyright pass, plus 830 unit tests and 31 libCRS tests.
  • New parametrized tests in test_renderer_build_target.py and test_renderer_run_compose.py cover #, inline JSON, : , double quotes, ${VAR} and a non-BMP character, plus a direct test that yaml_quote keeps long values on one line. Against the old templates the #, JSON and : cases fail; with tojson instead of yaml_quote the non-BMP case fails.
  • End to end on a small local CRS whose builder dumps its env (alpine target, Docker Compose v5.6.1): on main oss-crs build-target fails with services.target_builder.environment.[8]: unexpected type map[string]interface {}. With this branch it builds, and the container sees -max_len=100 #keep, {"temperature": 0.2}, mode: fast and rocket 🚀 exactly, with ${HOME} expanded.
  • For the run template I loaded a rendered run compose with the same values through docker compose config; module and builder sidecar env come through unchanged.

Checklist

  • I followed Conventional Commits
  • I updated docs for behavior/config/CLI changes
  • I added/updated tests for behavior changes
  • I considered backward compatibility and migration impact

Env entries were rendered as `- KEY=value`, so YAML parsed the value
before docker compose saw it. A value containing " #" was cut off at
the "#", and a value containing ": " turned into a mapping that docker
compose rejects.

Add a yaml_quote filter that lets PyYAML emit each entry as a
single-line double-quoted scalar, and use it for the additional_env
loops. tojson would have been shorter, but it escapes characters
outside the BMP as surrogate pairs, which docker compose refuses.

Signed-off-by: Abhishek <abhishekup082@gmail.com>

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.

additional_env values containing " #" or ": " break the generated compose files

1 participant