Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attempt to fix once and for all the issue with json validation introduced with version 4.0.13 #384

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/chef/knife/mixin/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def validate_json(json)

if evaled_json.is_a?(Hash)
evaled_json.each do |key, value|
next unless printable?(value.to_s)
next if printable?(value.to_s)

msg = "Value '#{value}' of key '#{key}' contains non-printable characters. Check that backslashes are escaped with another backslash (e.g. C:\\\\Windows) in double-quoted strings."
ChefVault::Log.warn(msg)
Expand All @@ -66,10 +66,10 @@ def validate_json(json)

# I/P: String
# O/P: true/false
# returns true if string is free of non-printable characters (escape sequences)
# returns true if string is free of non-printable characters (escape sequences) or includes '\n' or '\r', which is perfectly valid
# this returns false for whitespace escape sequences as well, e.g. \n\t
def printable?(string)
/[^[:print:]]|[[:space:]]/.match(string)
/[[:print:]]|[\n\r]/.match?(string)
Copy link

@decoyjoe decoyjoe Jun 20, 2023

Choose a reason for hiding this comment

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

This isn't right either, it only ensures there's at least one printable character.

I think the regex just needs to check for any non-printable characters like this:

not /[^[:print:]]/.match?(string)

end
end
end
Expand Down