Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Remove UTF-8 BOM from config files before parsing #11

Merged
merged 4 commits into from
Apr 28, 2015
Merged
Show file tree
Hide file tree
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
24 changes: 23 additions & 1 deletion lib/sensu/settings/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def load_file(file)
if File.file?(file) && File.readable?(file)
begin
warning("loading config file", :file => file)
contents = IO.read(file)
contents = read_config_file(file)
config = MultiJson.load(contents, :symbolize_keys => true)
merged = deep_merge(@settings, config)
unless @loaded_files.empty?
Expand Down Expand Up @@ -190,10 +190,32 @@ def indifferent_access!
@indifferent_access = true
end

# Read a configuration file and force its encoding to 8-bit
# ASCII, ignoring invalid characters. If there is a UTF-8 BOM,
# it will be removed. Some JSON parsers force ASCII but do not
# remove the UTF-8 BOM if present, causing encoding conversion
# errors. This method is for consistency across MultiJson
# adapters and system platforms.
#
# @param [String] file path to read.
# @return [String] file contents.
def read_config_file(file)
contents = IO.read(file)
if contents.respond_to?(:force_encoding)
encoding = ::Encoding::ASCII_8BIT
contents = contents.force_encoding(encoding)
contents.sub!("\xEF\xBB\xBF".force_encoding(encoding), "")
else
contents.sub!(/^\357\273\277/, "")
end
contents
end

# Deep merge two hashes.
#
# @param [Hash] hash_one to serve as base.
# @param [Hash] hash_two to merge in.
# @return [Hash] deep merged hash.
def deep_merge(hash_one, hash_two)
merged = hash_one.dup
hash_two.each do |key, value|
Expand Down
7 changes: 7 additions & 0 deletions spec/assets/bom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"client": {
"name": "bom",
"address": "utf-8",
"subscriptions": ["encoding"]
}
}
8 changes: 8 additions & 0 deletions spec/loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@
expect(messages).to include("ignoring config file")
end

it "can load settings from a utf-8 encoded file with a bom" do
@loader.load_file(File.join(@assets_dir, "bom.json"))
warnings = @loader.warnings
failures = @loader.validate
expect(warnings.size).to eq(1)
expect(failures.size).to eq(0)
end

it "can load settings from files in a directory" do
@loader.load_directory(@config_dir)
warnings = @loader.warnings
Expand Down