Skip to content

Commit 6ebf263

Browse files
committed
Fixed: Vault warns when valid JSON contains special character sequences
This partially reverts commit 2e0ee1b Fixes chef#415 Signed-off-by: Joseph Larionov <[email protected]>
1 parent 11d5fc5 commit 6ebf263

File tree

2 files changed

+21
-49
lines changed

2 files changed

+21
-49
lines changed

lib/chef/knife/mixin/helper.rb

-38
Original file line numberDiff line numberDiff line change
@@ -39,48 +39,10 @@ def values_from_file(file)
3939
end
4040

4141
def values_from_json(json)
42-
validate_json(json)
4342
JSON.parse(json)
4443
rescue JSON::ParserError
4544
raise JSON::ParserError, "#{json} is not valid JSON!"
4645
end
47-
48-
# I/P: json string
49-
# Raises `InvalidValue` if any of the json's values contain non-printable characters.
50-
def validate_json(json)
51-
begin
52-
parsed_json = JSON.parse(json)
53-
rescue JSON::ParserError
54-
raise ChefVault::Exceptions::InvalidValue, "#{json} is not valid JSON!"
55-
end
56-
57-
check_value(parsed_json) # Start checking from the root of the parsed JSON
58-
end
59-
60-
def check_value(value, parent_key = nil)
61-
if value.is_a?(Array)
62-
value.each { |item| check_value(item, parent_key) }
63-
elsif value.is_a?(Hash)
64-
value.each do |key, nested_value|
65-
next if key == "password" # Skip the password key
66-
67-
check_value(nested_value, key)
68-
end
69-
else
70-
unless printable?(value.to_s)
71-
msg = "Value '#{value}' of key '#{parent_key}' contains non-printable characters."
72-
ChefVault::Log.warn(msg)
73-
end
74-
end
75-
end
76-
77-
# I/P: String
78-
# O/P: true/false
79-
# returns true if string is free of non-printable characters (escape sequences)
80-
# this returns false for whitespace escape sequences as well, e.g. \n\t
81-
def printable?(string)
82-
!/[[:^print:]]/.match?(string) # Returns true if the string is printable
83-
end
8446
end
8547
end
8648
end

spec/chef/helper_spec.rb

+21-11
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,34 @@
44
RSpec.describe ChefVault::Mixin::Helper do
55
include ChefVault::Mixin::Helper
66

7-
let(:json) { { "username": "root" } }
8-
let(:json_data) { '{"username": "root", "password": "abcabc"}' }
9-
let(:buggy_json_data) { '{"username": "root", "password": "abc\abc"' }
7+
before do
8+
allow(ChefVault::Log).to receive(:warn)
9+
end
10+
11+
let(:json_base) { { "username": "root" } }
12+
let(:valid_json) { '{"username": "root", "password": "abcabc"}' }
13+
let(:malformed_json) { '{"username": ' }
14+
let(:valid_json_with_special_character) { { "Data": "Null->\u0000<-Byte" } }
1015

11-
describe "#validate_json" do
12-
it "Raises InvalidValue Exception when invalid data provided" do
13-
expect { validate_json(buggy_json_data) }.to raise_error(ChefVault::Exceptions::InvalidValue)
16+
describe "#values_from_json" do
17+
it "should raise Exception when invalid JSON provided" do
18+
expect { values_from_json(malformed_json) }.to raise_error(JSON::ParserError)
1419
end
1520

16-
it "Not to raise error if valid data provided" do
17-
expect { validate_json(json_data) }.to_not raise_error
21+
it "should not to raise error if valid data provided" do
22+
expect { values_from_json(valid_json) }.to_not raise_error
1823
end
1924

20-
it "not to raise error if data consist of tab/new line OR space" do
25+
it "should not to raise error if data consist of tab/new line OR space" do
2126
%w{abc\tabc abc\nabc}.each do |pass|
22-
json_data_with_slash = json.merge("password": pass)
23-
expect { validate_json(json_data_with_slash.to_json) }.to_not raise_error
27+
json_data_with_slash = json_base.merge("password": pass)
28+
expect { values_from_json(json_data_with_slash.to_json) }.to_not raise_error
2429
end
2530
end
31+
32+
it 'should not warn or fail when JSON contains special characters' do
33+
expect(ChefVault::Log).to_not receive(:warn)
34+
expect { values_from_json(valid_json_with_special_character.to_json) }.to_not raise_error
35+
end
2636
end
2737
end

0 commit comments

Comments
 (0)