Skip to content

Commit

Permalink
Fixed problem escaping string in JSON
Browse files Browse the repository at this point in the history
Signed-off-by: sanga17 <sausekar@msystechnologies.com>
  • Loading branch information
sanga1794 committed Aug 4, 2020
1 parent ba503e6 commit 283c819
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/chef-vault/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@ class IdMismatch < Exceptions

class V1Format < Exceptions
end

class InvalidValue < Exceptions
end
end
end
30 changes: 30 additions & 0 deletions lib/chef/knife/mixin/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def set_mode(mode)
def merge_values(json, file)
values = {}
values.merge!(values_from_file(file)) if file
validate_json(json)
values.merge!(values_from_json(json)) if json

values
Expand All @@ -43,6 +44,35 @@ def values_from_json(json)
rescue JSON::ParserError
raise JSON::ParserError, "#{json} is not valid JSON!"
end

# I/P: json string
# It checks wheather it contains any non-printable character present or not,
# If present then raises error of InvalidValue
def validate_json(json)
begin
evaled_json = eval(json)
rescue StandardError => e
puts e.message
end

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

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."
raise ChefVault::Exceptions::InvalidValue, msg
end
end
end

# I/P: String
# O/P: true/false
# returns true if string is free of non-printable characters (escape sequences)
# this returns false for whitespace escape sequences as well, e.g. \n\t
def printable?(string)
return false if string =~ /[^[:print:]]/
true
end
end
end
end

0 comments on commit 283c819

Please sign in to comment.