Skip to content

Commit ae638cb

Browse files
committed
file_chunk: add stricter checks for broken meta files
Signed-off-by: Shizuo Fujita <[email protected]>
1 parent 08ea1d8 commit ae638cb

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

lib/fluent/plugin/buffer/file_chunk.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,17 @@ def restore_metadata(bindata)
219219
# old type of restore
220220
data = Fluent::MessagePackFactory.msgpack_unpacker(symbolize_keys: true).feed(bindata).read rescue {}
221221
end
222+
raise FileChunkError, "invalid meta data" if data.nil? || !data.is_a?(Hash)
223+
raise FileChunkError, "invalid unique_id" unless data[:id]
224+
raise FileChunkError, "invalid created_at" unless data[:c].to_i > 0
225+
raise FileChunkError, "invalid modified_at" unless data[:m].to_i > 0
222226

223227
now = Fluent::Clock.real_now
224228

225-
@unique_id = data[:id] || self.class.unique_id_from_path(@path) || @unique_id
229+
@unique_id = data[:id]
226230
@size = data[:s] || 0
227-
@created_at = data.fetch(:c, now.to_i)
228-
@modified_at = data.fetch(:m, now.to_i)
231+
@created_at = data[:c]
232+
@modified_at = data[:m]
229233

230234
@metadata.timekey = data[:timekey]
231235
@metadata.tag = data[:tag]

test/plugin/test_buf_file.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,32 @@ def compare_log(plugin, msg)
13201320
compare_log(@p, 'enqueued meta file is broken')
13211321
assert { not File.exist?(p2) }
13221322
assert { File.exist?("#{@bufdir}/backup/worker0/#{@id_output}/#{@d.dump_unique_id_hex(c2id)}.log") }
1323+
1324+
# broken id, c, m fields in meta data
1325+
c3id, p3 = create_first_chunk('q')
1326+
metadata = File.read(p3 + '.meta')
1327+
File.open(p3 + '.meta', 'wb') { |f| f.write(metadata[0..6] + "\0" * (metadata.size - 6)) } # create enqueued broken meta file
1328+
1329+
Fluent::SystemConfig.overwrite_system_config('root_dir' => @bufdir) do
1330+
@p.start
1331+
end
1332+
1333+
compare_log(@p, 'enqueued meta file is broken')
1334+
assert { not File.exist?(p3) }
1335+
assert { File.exist?("#{@bufdir}/backup/worker0/#{@id_output}/#{@d.dump_unique_id_hex(c3id)}.log") }
1336+
1337+
# truncate meta data
1338+
c4id, p4 = create_first_chunk('q')
1339+
metadata = File.read(p4 + '.meta')
1340+
File.open(p4 + '.meta', 'wb') { |f| f.write(metadata[0..-2]) } # create enqueued broken meta file with last byte truncated
1341+
1342+
Fluent::SystemConfig.overwrite_system_config('root_dir' => @bufdir) do
1343+
@p.start
1344+
end
1345+
1346+
compare_log(@p, 'enqueued meta file is broken')
1347+
assert { not File.exist?(p4) }
1348+
assert { File.exist?("#{@bufdir}/backup/worker0/#{@id_output}/#{@d.dump_unique_id_hex(c4id)}.log") }
13231349
end
13241350

13251351
test '#resume throws away broken chunk with disable_chunk_backup' do

0 commit comments

Comments
 (0)