Skip to content

Commit d2af7eb

Browse files
committed
escape values starting with +, -, and @
1 parent 8346200 commit d2af7eb

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/logstash/outputs/csv.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ def get_value(name, event)
6666

6767
private
6868
def escape_csv(val)
69-
(spreadsheet_safe && val.is_a?(String) && val.start_with?("=")) ? "'#{val}" : val
69+
(spreadsheet_safe && val.is_a?(String) && val.start_with?(/[=+\-@]/)) ? "'#{val}" : val
7070
end
7171
end # class LogStash::Outputs::CSV

spec/outputs/csv_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,24 @@
139139
let(:event_data) do
140140
{
141141
"f1" => "1+1",
142-
"f2" => "=1+1"
142+
"f2" => "=1+1",
143+
"f3" => "+1+1",
144+
"f4" => "-1+1",
145+
"f5" => "@1+1"
143146
}
144147
end
145148
let(:events) do
146149
[ LogStash::Event.new(event_data) ]
147150
end
148151

149-
let(:options) { { "path" => tmpfile, "fields" => ["f1", "f2"] } }
152+
let(:options) { { "path" => tmpfile, "fields" => ["f1", "f2", "f3", "f4", "f5"] } }
150153
it "escapes them correctly" do
151154
expect(csv_output.size).to eq(1)
152155
expect(csv_output[0][0]).to eq("1+1")
153156
expect(csv_output[0][1]).to eq("'=1+1")
157+
expect(csv_output[0][2]).to eq("'+1+1")
158+
expect(csv_output[0][3]).to eq("'-1+1")
159+
expect(csv_output[0][4]).to eq("'@1+1")
154160
end
155161

156162
context "when escaping is turned off" do

0 commit comments

Comments
 (0)