Commit 87890159 authored by Daniel Rubery's avatar Daniel Rubery Committed by Commit Bot

Add timeout to ZIP analysis

To limit regression in the total analysis time from Safe Browsing,
cutoff ZIP analysis at 10 seconds. This CL also removes a little dead
code from the ZIP analysis.

Bug: 948277
Change-Id: I4ab6d2457398e0d5d63c40f1b801d35eaa52b87d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1598844Reviewed-by: default avatarVarun Khaneja <vakh@chromium.org>
Commit-Queue: Varun Khaneja <vakh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#659982}
parent ca0d21ba
......@@ -16,6 +16,7 @@
#include "base/metrics/histogram_macros.h"
#include "base/numerics/ranges.h"
#include "base/rand_util.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/common/safe_browsing/archive_analyzer_results.h"
#include "chrome/common/safe_browsing/file_type_policies.h"
......@@ -25,9 +26,17 @@
namespace safe_browsing {
namespace zip_analyzer {
namespace {
// The maximum duration of ZIP analysis, in milliseconds.
const int kZipAnalysisTimeoutMs = 10000;
} // namespace
void AnalyzeZipFile(base::File zip_file,
base::File temp_file,
ArchiveAnalyzerResults* results) {
base::Time start_time = base::Time::Now();
zip::ZipReader reader;
if (!reader.OpenFromPlatformFile(zip_file.GetPlatformFile())) {
DVLOG(1) << "Failed to open zip file";
......@@ -44,9 +53,8 @@ void AnalyzeZipFile(base::File zip_file,
return;
}
bool contains_zip = false;
bool timeout = false;
bool advanced = true;
int zip_entry_count = 0;
results->file_count = 0;
results->directory_count = 0;
for (; reader.HasMore(); advanced = reader.AdvanceToNextEntry()) {
......@@ -58,6 +66,11 @@ void AnalyzeZipFile(base::File zip_file,
DVLOG(1) << "Failed to open current entry in zip file";
continue;
}
if (base::Time::Now() - start_time >
base::TimeDelta::FromMilliseconds(kZipAnalysisTimeoutMs)) {
timeout = true;
break;
}
// Clear the |temp_file| between extractions.
temp_file.Seek(base::File::Whence::FROM_BEGIN, 0);
......@@ -69,19 +82,13 @@ void AnalyzeZipFile(base::File zip_file,
writer.file_length(), reader.current_entry_info()->is_encrypted(),
results);
if (FileTypePolicies::GetFileExtension(
reader.current_entry_info()->file_path()) ==
FILE_PATH_LITERAL(".zip"))
contains_zip = true;
zip_entry_count++;
if (reader.current_entry_info()->is_directory())
results->directory_count++;
else
results->file_count++;
}
results->success = true;
results->success = !timeout;
}
} // namespace zip_analyzer
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment