Truly independent web browser
LibWebView: Delete unsent crash reports after 30 days
Andreas Kling608f22146193
diff --git a/Libraries/LibWebView/CrashReportStore.cpp b/Libraries/LibWebView/CrashReportStore.cppindex 5368ce1c64f..7e30e8fca5d 100644--- a/Libraries/LibWebView/CrashReportStore.cpp+++ b/Libraries/LibWebView/CrashReportStore.cpp@@ -84,7 +84,7 @@ static constexpr auto pending_suffix = ".pending"sv;static constexpr off_t maximum_report_size = 1048576;static constexpr size_t retained_report_count = 20;static constexpr auto hard_cutoff_date = UnixDateTime::from_unix_time_parts(2026, 10, 2, 20, 0, 0, 0);-static constexpr auto maximum_pending_report_age = AK::Duration::from_seconds(14ll * 24 * 60 * 60);+static constexpr auto maximum_unsent_report_age = AK::Duration::from_seconds(30ll * 24 * 60 * 60);static NeverDestroyed<ByteString> s_browser_pending_path;static NeverDestroyed<OwnPtr<CrashReport>> s_browser_crash_report;@@ -245,7 +245,7 @@ ErrorOr<Vector<ByteString>> CrashReportStore::pending_report_names(UnixDateTime{// Runs on every launch, so this only lists names instead of reading every report.auto directory = TRY(open_report_directory(m_directory));- auto earliest_time = max(hard_cutoff_date, now - maximum_pending_report_age);+ auto earliest_time = max(hard_cutoff_date, now - maximum_unsent_report_age);Vector<ByteString> names;Core::DirIterator iterator(directory.path().string(), Core::DirIterator::SkipDots);@@ -266,6 +266,31 @@ ErrorOr<Vector<ByteString>> CrashReportStore::pending_report_names(UnixDateTimereturn names;}+static void remove_reports_older_than(Core::Directory const& directory, UnixDateTime earliest_time)+{+ Core::DirIterator iterator(directory.path().string(), Core::DirIterator::SkipDots);+ while (iterator.has_next()) {+ auto name = iterator.next_path();+ if (!CrashReportStore::is_saved_report_name(name))+ continue;+ struct stat status {};+ if (!is_owned_regular_file_at(directory, name, status))+ continue;+ if (UnixDateTime::from_unix_timespec(modified_time(status)) < earliest_time)+ (void)unlinkat(directory.fd(), name.characters(), 0);+ }+}++ErrorOr<void> CrashReportStore::remove_expired_reports(UnixDateTime now) const+{+ auto directory = TRY(open_report_directory(m_directory));+ auto earliest_time = now - maximum_unsent_report_age;+ remove_reports_older_than(directory, earliest_time);+ if (auto seen_directory = TRY(open_seen_directory_if_exists(directory)); seen_directory.has_value())+ remove_reports_older_than(*seen_directory, earliest_time);+ return {};+}+bool CrashReportStore::has_pending_reports(UnixDateTime now) const{auto names = pending_report_names(now);@@ -405,6 +430,8 @@ ErrorOr<void> CrashReportStore::initialize_browser_crash_handler()// process installs a handler of its own. One unreadable file must not prevent that.if (auto result = recover_pending_reports(); result.is_error())warnln("Could not recover browser crash reports: {}", result.error());+ if (auto result = remove_expired_reports(); result.is_error())+ warnln("Could not remove expired crash reports: {}", result.error());ByteString pending_name;auto file = TRY(create_unique_file(directory, pending_prefix, pending_suffix, pending_name));@@ -463,6 +490,7 @@ ErrorOr<ByteString> CrashReportStore::store_report(ProcessType, StringView, Unixreturn Error::from_string_literal("Crash reports are not supported on this platform yet");}+ErrorOr<void> CrashReportStore::remove_expired_reports(UnixDateTime) const { return {}; }ErrorOr<size_t> CrashReportStore::recover_pending_reports() const { return 0; }ErrorOr<void> CrashReportStore::initialize_browser_crash_handler()diff --git a/Libraries/LibWebView/CrashReportStore.h b/Libraries/LibWebView/CrashReportStore.hindex e759ee0f0eb..98abb811259 100644--- a/Libraries/LibWebView/CrashReportStore.h+++ b/Libraries/LibWebView/CrashReportStore.h@@ -34,8 +34,8 @@ public:ErrorOr<SavedReport> saved_report(ByteString const& name) const;- // The reports waiting to be reviewed, newest first. A report of a crash from before the hard cutoff- // date (October 2, 2026 20:00 UTC) or more than 14 days before `now` stays on disk but is never offered.+ // The reports waiting to be reviewed, newest first. A report of a crash from before the hard cutoff date+ // (October 2, 2026 20:00 UTC) or more than 30 days before `now` is never pending.ErrorOr<Vector<ByteString>> pending_report_names(UnixDateTime now = UnixDateTime::now()) const;bool has_pending_reports(UnixDateTime now = UnixDateTime::now()) const;@@ -43,6 +43,9 @@ public:ErrorOr<void> mark_seen(ByteString const& name) const;ErrorOr<void> remove_sent_report(ByteString const& name) const;+ // Reports that are still unsent 30 days after their crash are deleted, whether they were seen or not.+ ErrorOr<void> remove_expired_reports(UnixDateTime now = UnixDateTime::now()) const;+ErrorOr<ByteString> store_report(ProcessType, StringView text, UnixDateTime crashed_at) const;ErrorOr<size_t> recover_pending_reports() const;diff --git a/Tests/LibWebView/TestCrashReportStore.cpp b/Tests/LibWebView/TestCrashReportStore.cppindex d70f91bb577..d5ed094005e 100644--- a/Tests/LibWebView/TestCrashReportStore.cpp+++ b/Tests/LibWebView/TestCrashReportStore.cpp@@ -173,7 +173,7 @@ TEST_CASE(a_report_from_before_the_hard_cutoff_date_is_not_pending)EXPECT_EQ(directory_entries().size(), 2u);}-TEST_CASE(a_report_older_than_14_days_is_not_pending)+TEST_CASE(a_report_older_than_30_days_is_not_pending){cleanup();ScopeGuard guard = cleanup;@@ -184,15 +184,37 @@ TEST_CASE(a_report_older_than_14_days_is_not_pending)write_file(fresh, "Recent enough\n"sv, UnixDateTime::from_unix_time_parts(2026, 10, 5, 12, 0, 0, 0));auto store = test_store();- auto in_two_weeks = UnixDateTime::from_unix_time_parts(2026, 10, 19, 12, 0, 0, 0);- EXPECT_EQ(MUST(store.pending_report_names(in_two_weeks)), Vector<ByteString> { fresh });- EXPECT(store.has_pending_reports(in_two_weeks));+ auto in_30_days = UnixDateTime::from_unix_time_parts(2026, 11, 4, 12, 0, 0, 0);+ EXPECT_EQ(MUST(store.pending_report_names(in_30_days)), Vector<ByteString> { fresh });+ EXPECT(store.has_pending_reports(in_30_days));- auto later = in_two_weeks + AK::Duration::from_seconds(1);+ auto later = in_30_days + AK::Duration::from_seconds(1);EXPECT(MUST(store.pending_report_names(later)).is_empty());EXPECT(!store.has_pending_reports(later));}+TEST_CASE(unsent_reports_are_deleted_after_30_days)+{+ cleanup();+ ScopeGuard guard = cleanup;++ auto stale = report_name("2026-10-05T11-59-59Z"sv);+ auto stale_and_seen = report_name("2026-10-05T11-59-58Z"sv);+ auto fresh = report_name("2026-10-05T12-00-00Z"sv, "Browser"sv);+ auto fresh_and_seen = report_name("2026-10-05T12-00-01Z"sv);+ write_file(stale, "Too old\n"sv, UnixDateTime::from_unix_time_parts(2026, 10, 5, 11, 59, 59, 0));+ write_file(stale_and_seen, "Too old\n"sv, UnixDateTime::from_unix_time_parts(2026, 10, 5, 11, 59, 58, 0));+ write_file(fresh, "Recent enough\n"sv, UnixDateTime::from_unix_time_parts(2026, 10, 5, 12, 0, 0, 0));+ write_file(fresh_and_seen, "Recent enough\n"sv, UnixDateTime::from_unix_time_parts(2026, 10, 5, 12, 0, 1, 0));+ auto store = test_store();+ MUST(store.mark_seen(stale_and_seen));+ MUST(store.mark_seen(fresh_and_seen));++ MUST(store.remove_expired_reports(UnixDateTime::from_unix_time_parts(2026, 11, 4, 12, 0, 0, 0)));+ EXPECT_EQ(directory_entries(), Vector<ByteString> { fresh });+ EXPECT_EQ(directory_entries("Seen"sv), Vector<ByteString> { fresh_and_seen });+}+TEST_CASE(a_stored_report_is_as_old_as_its_crash){cleanup();@@ -203,8 +225,8 @@ TEST_CASE(a_stored_report_is_as_old_as_its_crash)auto crashed_at = UnixDateTime::from_unix_time_parts(2026, 10, 3, 0, 0, 0, 0);auto name = MUST(store.store_report(WebView::ProcessType::Browser, "A report\n"sv, crashed_at));- EXPECT_EQ(MUST(store.pending_report_names(crashed_at + AK::Duration::from_seconds(14 * 24 * 60 * 60))), Vector<ByteString> { name });- EXPECT(MUST(store.pending_report_names(crashed_at + AK::Duration::from_seconds(14 * 24 * 60 * 60 + 1))).is_empty());+ EXPECT_EQ(MUST(store.pending_report_names(crashed_at + AK::Duration::from_seconds(30 * 24 * 60 * 60))), Vector<ByteString> { name });+ EXPECT(MUST(store.pending_report_names(crashed_at + AK::Duration::from_seconds(30 * 24 * 60 * 60 + 1))).is_empty());}TEST_CASE(a_missing_report_cannot_be_moved)