Commit bf5255d2 authored by Min Qin's avatar Min Qin Committed by Commit Bot

Truncate large data URL before storing them in history DB

BUG=851650

Change-Id: I56d69fa06ec030abad513ab61820dfb983f0473e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2530196
Commit-Queue: Min Qin <qinmin@chromium.org>
Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826956}
parent dd7b421c
......@@ -57,6 +57,24 @@
namespace {
// Max data url size to be stored in history DB.
const size_t kMaxDataURLSize = 1024u;
// If there is a data URL at the end of the url chain, truncate it if it is too
// long.
void TruncatedDataUrlAtTheEndIfNeeded(std::vector<GURL>* url_chain) {
if (url_chain->empty())
return;
GURL* url = &url_chain->back();
if (url->SchemeIs(url::kDataScheme)) {
const std::string& data_url = url->spec();
if (data_url.size() > kMaxDataURLSize) {
GURL truncated_url(data_url.substr(0, kMaxDataURLSize));
url->Swap(&truncated_url);
}
}
}
// Per-DownloadItem data. This information does not belong inside DownloadItem,
// and keeping maps in DownloadHistory from DownloadItem to this information is
// error-prone and complicated. Unfortunately, DownloadHistory::removing_*_ and
......@@ -157,6 +175,7 @@ history::DownloadRow GetDownloadRow(download::DownloadItem* item) {
download.by_ext_id = by_ext_id;
download.by_ext_name = by_ext_name;
download.download_slice_info = history::GetHistoryDownloadSliceInfos(*item);
TruncatedDataUrlAtTheEndIfNeeded(&download.url_chain);
return download;
}
......@@ -299,8 +318,10 @@ void DownloadHistory::LoadHistoryDownloads(
history::ToContentDownloadState(row.state);
download::DownloadInterruptReason history_reason =
history::ToContentDownloadInterruptReason(row.interrupt_reason);
std::vector<GURL> url_chain = row.url_chain;
TruncatedDataUrlAtTheEndIfNeeded(&url_chain);
download::DownloadItem* item = notifier_.GetManager()->CreateDownloadItem(
row.guid, loading_id_, row.current_path, row.target_path, row.url_chain,
row.guid, loading_id_, row.current_path, row.target_path, url_chain,
row.referrer_url, row.site_url, row.tab_url, row.tab_referrer_url,
base::nullopt, row.mime_type, row.original_mime_type, row.start_time,
row.end_time, row.etag, row.last_modified, row.received_bytes,
......
......@@ -838,4 +838,31 @@ TEST_F(DownloadHistoryTest, RemoveClearedItemFromHistory) {
ExpectDownloadsRemoved(ids);
}
// Test that large data URL will be truncated before being inserted into
// history.
TEST_F(DownloadHistoryTest, CreateLargeDataURLCompletedItem) {
// Create a fresh item not from download DB
CreateDownloadHistory({});
history::DownloadRow row;
std::string data_url = "data:text/html,";
data_url.append(std::string(2048, 'a'));
InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), data_url.c_str(),
"http://example.com/referrer.html",
download::DownloadItem::IN_PROGRESS, &row);
// Incomplete download will not be inserted into history.
CallOnDownloadCreated(0);
ExpectNoDownloadCreated();
// Completed download should be inserted.
EXPECT_CALL(item(0), IsDone()).WillRepeatedly(Return(true));
EXPECT_CALL(item(0), GetState())
.WillRepeatedly(Return(download::DownloadItem::COMPLETE));
row.state = history::DownloadState::COMPLETE;
data_url.resize(1024);
row.url_chain.back() = GURL(data_url);
item(0).NotifyObserversDownloadUpdated();
ExpectDownloadCreated(row);
}
} // anonymous namespace
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