Fix file states raising UnicodeDecodeError on encoding mismatch#69696
Open
ggiesen wants to merge 3 commits into
Open
Fix file states raising UnicodeDecodeError on encoding mismatch#69696ggiesen wants to merge 3 commits into
ggiesen wants to merge 3 commits into
Conversation
The file.comment, file.append and file.prepend states read the target file as bytes and decode it with the system encoding purely to build a diff. A strict decode raised UnicodeDecodeError and aborted the state when the file contained bytes invalid in that encoding. Decode with errors="replace" since the result is only used for the diff. Fixes saltstack#50903
…tch fix The direct tests call file.comment and file.prepend (the two changed state functions previously untested for this case) with production-shape args (name+regex / name+text, as the state compiler passes from an SLS) against a real file holding a 0xed byte while __salt_system_encoding__ is ascii, pinning the errors="replace" decode fix; both fail with UnicodeDecodeError when the fix is reverted. The inverse test guards against overcorrection: file.append on a cleanly-decodable UTF-8 file must still produce an unmangled diff with no U+FFFD replacement characters, and passes with and without the fix. Claude-Session: https://claude.ai/code/session_01MF2AuQNhBZg4HDt1x6xxCu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
salt/states/file.py append() (and its siblings prepend/comment/blockreplace) read the target file as bytes and decode it with salt_system_encoding using strict error handling, purely to build a diff; a file whose bytes are not valid in the system encoding (e.g. ISO-8859-1 content under an ASCII/C locale) raised UnicodeDecodeError and aborted the state. The fix adds errors="replace" to all seven such decode sites, since the decoded lines only feed difflib. Verified the new unit test PASSES with the fix (1 passed via pytest with shadow confirmed) and FAILS (UnicodeDecodeError) when the fix is temporarily reverted.
What issues does this PR fix or reference?
Fixes #50903
Previous Behavior
See #50903.
New Behavior
Fix file states raising UnicodeDecodeError on encoding mismatch. Validated by a unit test proven to fail on unpatched 3006.x and pass with the fix (confirmed by adversarial review).
Merge requirements satisfied?
Commits signed with GPG?
No