Skip to content

Quote tagged Ansible string substitutions before interpolation - #2483

Open
hakarrr wants to merge 4 commits into
OpenSCAP:mainfrom
hakarrr:fix-1879-ansible-string-values
Open

hakarrr wants to merge 4 commits into
OpenSCAP:mainfrom
hakarrr:fix-1879-ansible-string-values

Conversation

@hakarrr

@hakarrr hakarrr commented Nov 10, 2026

Copy link
Copy Markdown

Fixes #1879.

Quote standalone, explicitly tagged string substitutions (key: !!str <sub/>) in Ansible fixes before the XCCDF value is inserted into the template. This keeps literal values such as |/bin/false from being interpreted as YAML block-scalar syntax. Doing this before interpolation also distinguishes literal quote characters in an XCCDF value from quoting already supplied by the template.

The generated double-quoted scalar escapes quotes, backslashes, tabs, CR/LF and Unicode line breaks. Existing template quoting, embedded substitutions, numeric variables, task block scalars and non-Ansible fixes are left unchanged. This is intentionally not a general YAML-template rewriter.

Regression coverage extends the existing Ansible variable test with 14 literal string cases, prequoted/numeric controls and a shell-fix guard. The new fixture covers pipe/folded indicators, quotes, backslashes, comment/mapping characters, leading/trailing spaces, multiline values, tabs/CR, boolean/numeric strings and Unicode.

Validation on Linux with GCC 11.4 and a native CMake build:

  • ctest --test-dir build -R 'API/XCCDF/(unittests|fix)' -j4 --output-on-failure: 95 passed, 3 skipped, 0 failed.
  • The extended Ansible variable test fails against the original implementation and passes with this patch.
  • The issue's original ssg-rhel9-ds.xml reproduces the YAML parse failure before the change. After the change, its generated 881-task playbook parses as YAML and preserves sysctl_kernel_core_pattern_value as the literal |/bin/false.
  • All 14 regression values round-trip exactly through the generated playbook using a YAML parser.
  • Native executable/library build and git diff --check pass.

Build dependencies were supplied locally without tracked dependency or build-configuration changes. Documentation, Python/Perl bindings and oscap-docker were disabled. The three skipped XCCDF tests cover transformation, offline remediation and platform-specific system information. The full project suite, Ansible execution/ansible-lint, and non-Linux builds were not run. The original datastream's remote OVAL resource was not downloaded.

hakarrr and others added 4 commits November 10, 2026 23:13
Preserve literal XCCDF values before interpolating Ansible YAML. Escape quotes, backslashes and line breaks without changing existing template quoting or other remediation formats.

Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
Extend the existing Ansible variable regression test with special-character fixtures and a shell-format guard.

Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
Cover pipe and folded indicators, quotes, backslashes, comments, colons, whitespace, line breaks, booleans, numbers and Unicode. Include prequoted templates and other fix formats as guards.

Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
Record the expected escaped YAML scalars while preserving numeric values, prequoted strings and task block scalars.

Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
@sonarqubecloud

Copy link
Copy Markdown

@hakarrr

hakarrr commented Nov 13, 2026

Copy link
Copy Markdown
Author

Could a maintainer review this PR and let me know if the approach is wanted? Please point out any changes or additional evidence needed before it can move forward. Thanks.

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.

Pipe characters in XCCDF Values need to be escaped in Ansible Playbooks

1 participant