Skip to content

Conversation

@nikhil2611
Copy link
Collaborator

Summary

This PR fixes a security vulnerability (CHEF-3854) where YAML.load_stream() was used to check for multi-document YAML files, allowing arbitrary code execution via deserialization attacks.

Issue

CHEF-3854 - Chef/Knife YAML Insecure Deserialization through YAML.load_stream

Changes

  • Modified: knife/lib/chef/knife/yaml_convert.rb - Replaced YAML.load_stream() with safe regex pattern matching
  • Modified: lib/chef/recipe.rb - Replaced YAML.load_stream() with safe regex pattern matching

Security Impact

Fix: Replaced with yaml_contents.scan(/^---\s*$/).length which simply counts document separator patterns without any deserialization, eliminating the attack vector.

Tests & Coverage

Changed lines: 4; Existing tests verify multi-document detection still works correctly.

File Method/Block Change Type Test Coverage
knife/lib/chef/knife/yaml_convert.rb Multi-document check (line 54-56) Security fix Existing tests verify behavior
lib/chef/recipe.rb from_yaml_file (line 86-88) Security fix spec/unit/recipe_spec.rb lines 579-596

Functionality Preserved

  • ✅ Multi-document YAML detection still works (counts --- separators)
  • ✅ Error messages unchanged
  • ✅ All legitimate YAML files process normally
  • ✅ Backward compatible - no breaking changes
  • ❌ Malicious YAML payloads now rejected (intended security improvement)

Risk & Mitigations

Risk Classification: Low

Rationale: Localized change to YAML parsing validation logic. The actual parsing already used YAML.safe_load() which is secure - we only fixed the pre-check that was vulnerable.

Mitigation: Change only affects the multi-document detection mechanism, not the core YAML parsing functionality.

Rollback Strategy: git revert 0cb36ea0ec

AI Assistance

This work was completed with AI assistance following Progress AI policies.

DCO

All commits include Developer Certificate of Origin sign-off.

…(CHEF-3854)

Replaced insecure YAML.load_stream calls with safe text pattern matching
to prevent arbitrary code execution via YAML deserialization attacks.

The vulnerability allowed malicious YAML files with deserialization gadgets

Changes:
- knife/lib/chef/knife/yaml_convert.rb: Count '---' separators with regex
- lib/chef/recipe.rb: Count '---' separators with regex

This maintains all existing functionality (multi-document detection,
error messages) while eliminating the deserialization attack vector.
The actual YAML parsing already uses YAML.safe_load() which is secure.

Issue: CHEF-3854
Signed-off-by: nikhil2611 <nikhilgupta2102@gmail.com>
Signed-off-by: nikhil2611 <nikhilgupta2102@gmail.com>
@sonarqubecloud
Copy link

@nikhil2611 nikhil2611 marked this pull request as ready for review November 25, 2025 06:09
@nikhil2611 nikhil2611 requested review from a team and jaymzh as code owners November 25, 2025 06:09
Copy link
Collaborator

@jaymzh jaymzh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make changes on main and backport, per standard policy. Do not change chef-18 directly unless the bug only affects Chef 18.

@nikhil2611 nikhil2611 changed the title fix(security): Replace YAML.load_stream to prevent deserialization attacks (CHEF-3854) backport of #15477- fix(security): Replace YAML.load_stream to prevent deserialization attacks (CHEF-3854) Nov 26, 2025
@nikhil2611
Copy link
Collaborator Author

nikhil2611 commented Nov 26, 2025

Please make changes on main and backport, per standard policy. Do not change chef-18 directly unless the bug only affects Chef 18.

Sure @jaymzh , I created a PR against main - #15477 and this one is a backport.

@nikhil2611
Copy link
Collaborator Author

Hey @jaymzh , We added this security fix in chef-18 to get the Knife security update released, so we can ship the latest Knife version in Chef Workstation.
Could you please help unblock this PR and merge the changes into chef-18?

Copy link
Collaborator

@jaymzh jaymzh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

Would be really silly if we fixed a security issue that then regressed in 19. :)

@johnmccrae johnmccrae merged commit 893e91f into chef-18 Dec 1, 2025
64 of 65 checks passed
@johnmccrae johnmccrae deleted the nikhil/CHEF-3854-fix-vulnerability branch December 1, 2025 17:14
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.

5 participants