Skip to content

Commit 6c05569

Browse files
karmiestolfo
authored andcommitted
[EXT] Sanitize filename in the backup extension
1 parent 4104412 commit 6c05569

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

elasticsearch-extensions/lib/elasticsearch/extensions/backup.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,19 @@ def __perform_single
182182
while r = client.scroll(scroll_id: r['_scroll_id'], scroll: scroll) and not r['hits']['hits'].empty? do
183183
r['hits']['hits'].each do |hit|
184184
FileUtils.mkdir_p "#{path.join hit['_index'], hit['_type']}"
185-
File.open("#{path.join hit['_index'], hit['_type'], hit['_id']}.json", 'w') do |file|
185+
File.open("#{path.join hit['_index'], hit['_type'], __sanitize_filename(hit['_id'])}.json", 'w') do |file|
186186
file.write MultiJson.dump(hit)
187187
end
188188
end
189189
end
190190
end
191+
192+
def __sanitize_filename name
193+
name
194+
.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�")
195+
.strip
196+
.tr("\u{202E}%$|:;/\t\r\n\\", "-")
197+
end
191198
end
192199
end
193200
end

elasticsearch-extensions/test/backup/unit/backup_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,9 @@ class Elasticsearch::Extensions::BackupTest < Elasticsearch::Test::UnitTestCase
123123

124124
@subject.__perform_single
125125
end
126+
127+
should "sanitize filename" do
128+
assert_equal "foo-bar-baz", @subject.__sanitize_filename("foo/bar\nbaz")
129+
end
126130
end
127131
end

0 commit comments

Comments
 (0)