Truly independent web browser
LibWebView: Mark downloads as web downloads on macOS
Jelle Raaijmakersbb6d1e58b7a0
diff --git a/Libraries/LibWebView/CMakeLists.txt b/Libraries/LibWebView/CMakeLists.txtindex 99231c7faec..2562d3ac870 100644--- a/Libraries/LibWebView/CMakeLists.txt+++ b/Libraries/LibWebView/CMakeLists.txt@@ -104,8 +104,9 @@ if (APPLE)NotificationBackendMacOS.mmPlatformColorsMacOS.cppProcessReaper.cpp+ QuarantineMacOS.mm)- set_source_files_properties(MicrophoneAuthorizerMacOS.mm NotificationBackendMacOS.mm PROPERTIES COMPILE_FLAGS "-fobjc-arc")+ set_source_files_properties(MicrophoneAuthorizerMacOS.mm NotificationBackendMacOS.mm QuarantineMacOS.mm PROPERTIES COMPILE_FLAGS "-fobjc-arc")elseif (LINUX)list(APPEND SOURCES AudioDaemonSocket.cpp CrashReportPOSIX.cpp)endif()@@ -175,7 +176,7 @@ if (LINUX)endif()if (APPLE)- target_link_libraries(LibWebView PRIVATE "-framework AVFoundation" "-framework Carbon" "-framework UserNotifications")+ target_link_libraries(LibWebView PRIVATE "-framework AVFoundation" "-framework Carbon" "-framework CoreServices" "-framework Foundation" "-framework UserNotifications")endif()if (LINUX OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")diff --git a/Libraries/LibWebView/FileDownloader.cpp b/Libraries/LibWebView/FileDownloader.cppindex aa25e7dcd33..b28d7c2ccc2 100644--- a/Libraries/LibWebView/FileDownloader.cpp+++ b/Libraries/LibWebView/FileDownloader.cpp@@ -32,6 +32,7 @@#include <LibWebView/RequestServerManager.h>#if defined(AK_OS_MACOS)+# include <LibWebView/QuarantineMacOS.h># include <sys/xattr.h>#endif@@ -167,10 +168,16 @@ FileDownloader::~FileDownloader() = default;// The operating system checks a downloaded file before it is first opened, as long as the file carries the mark that// browsers give their downloads.-static void mark_as_downloaded([[maybe_unused]] Core::File& file)+static void mark_as_downloaded([[maybe_unused]] Core::File& file, [[maybe_unused]] LexicalPath const& path, [[maybe_unused]] IsPrivate is_private){#if defined(AK_OS_MACOS)- // NB: These are the quarantine flags other browsers set, and the time of the download in hexadecimal seconds.+ // The quarantine event of a web download stays in a system log after the file is gone, which a private download must+ // not leave behind.+ if (is_private == IsPrivate::No && mark_as_web_download(path.string()))+ return;++ // Without a quarantine event the system tells the user that we created the file, but it does check the file. These+ // are the quarantine flags of a download, and the time of the download in hexadecimal seconds.auto quarantine = ByteString::formatted("0083;{:08x};Ladybird;", UnixDateTime::now().seconds_since_epoch());if (::fsetxattr(file.fd(), "com.apple.quarantine", quarantine.characters(), quarantine.length(), 0, 0) < 0)dbgln("Unable to quarantine a download: {}", Error::from_errno(errno));@@ -963,7 +970,7 @@ u64 FileDownloader::start_download(IsPrivate is_private, URL::URL const& url, Lereturn download_id;}auto file = file_or_error.release_value();- mark_as_downloaded(*file);+ mark_as_downloaded(*file, temporary_destination, is_private);auto active = make<ActiveDownload>(move(file), temporary_destination);active->may_replace_destination = may_replace_destination == MayReplaceDestination::Yes;@@ -1239,7 +1246,7 @@ void FileDownloader::resume_download(u64 id)active->file = file_or_error.release_value();// NB: The file may come from a build that did not mark downloads, or have been created anew by opening it.- mark_as_downloaded(*active->file);+ mark_as_downloaded(*active->file, active->temporary_destination, download->is_private);active->file_offset = {};}diff --git a/Libraries/LibWebView/QuarantineMacOS.h b/Libraries/LibWebView/QuarantineMacOS.hnew file mode 100644index 00000000000..a981896b726--- /dev/null+++ b/Libraries/LibWebView/QuarantineMacOS.h@@ -0,0 +1,15 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/ByteString.h>++namespace WebView {++bool mark_as_web_download(ByteString const& path);++}diff --git a/Libraries/LibWebView/QuarantineMacOS.mm b/Libraries/LibWebView/QuarantineMacOS.mmnew file mode 100644index 00000000000..83a840252e0--- /dev/null+++ b/Libraries/LibWebView/QuarantineMacOS.mm@@ -0,0 +1,34 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/Format.h>+#include <LibWebView/QuarantineMacOS.h>++#import <CoreServices/CoreServices.h>+#import <Foundation/Foundation.h>++namespace WebView {++// The system records a quarantine event for the file, from which it tells the user that the file came from the web.+bool mark_as_web_download(ByteString const& path)+{+ @autoreleasepool {+ auto* url = [NSURL fileURLWithFileSystemRepresentation:path.characters() isDirectory:NO relativeToURL:nil];+ auto* properties = @ {+ (__bridge NSString*)kLSQuarantineAgentNameKey : @"Ladybird",+ (__bridge NSString*)kLSQuarantineTypeKey : (__bridge NSString*)kLSQuarantineTypeWebDownload,+ };++ NSError* error = nil;+ if ([url setResourceValue:properties forKey:NSURLQuarantinePropertiesKey error:&error])+ return true;++ dbgln("Unable to mark {} as a web download: {}", path, error.localizedDescription.UTF8String);+ return false;+ }+}++}diff --git a/Tests/LibWebView/TestFileDownloader.cpp b/Tests/LibWebView/TestFileDownloader.cppindex 6da9a5cd0e8..19794527ac5 100644--- a/Tests/LibWebView/TestFileDownloader.cpp+++ b/Tests/LibWebView/TestFileDownloader.cpp@@ -5,6 +5,7 @@*/#include "HeadlessTest.h"+#include <AK/Array.h>#include <AK/ByteString.h>#include <AK/Mutex.h>#include <AK/ScopeGuard.h>@@ -25,6 +26,10 @@#include <stdlib.h>#include <sys/socket.h>+#if defined(AK_OS_MACOS)+# include <sys/xattr.h>+#endif+namespace {enum class RangeSupport {@@ -337,7 +342,7 @@ ByteBuffer make_body(size_t size)return body;}-ByteString run_download(TestHttpServer& server, StringView destination_directory, StringView file_name, Function<void(u64, DownloadWatcher&)> during_download = {})+ByteString run_download(TestHttpServer& server, StringView destination_directory, StringView file_name, Function<void(u64, DownloadWatcher&)> during_download = {}, WebView::IsPrivate is_private = WebView::IsPrivate::No){auto& downloader = WebView::Application::the().file_downloader();@@ -346,7 +351,7 @@ ByteString run_download(TestHttpServer& server, StringView destination_directoryauto url = URL::Parser::basic_parse(ByteString::formatted("http://127.0.0.1:{}/file", server.port()));VERIFY(url.has_value());- auto download_id = downloader.download_file(WebView::IsPrivate::No, *url, destination);+ auto download_id = downloader.download_file(is_private, *url, destination);watcher.watch(download_id);if (during_download)@@ -369,6 +374,26 @@ void expect_file_matches(StringView path, ReadonlyBytes expected)VERIFY(contents.bytes() == expected);}+#if defined(AK_OS_MACOS)+enum class QuarantineEvent : u8 {+ No,+ Yes,+};++void expect_file_is_quarantined(ByteString const& path, QuarantineEvent event)+{+ Array<char, 256> value {};+ auto size = getxattr(path.characters(), "com.apple.quarantine", value.data(), value.size(), 0, 0);+ VERIFY(size > 0);++ // The fields are the flags, the time, the agent and the identifier of the quarantine event.+ auto fields = StringView { value.data(), static_cast<size_t>(size) }.split_view(';', SplitBehavior::KeepEmpty);+ VERIFY(fields.size() == 4);+ VERIFY(fields[2] == "Ladybird"sv);+ VERIFY(fields[3].is_empty() == (event == QuarantineEvent::No));+}+#endif+void expect_request_can_be_released_from_finish_callback(TestHttpServer& server, size_t expected_body_size){auto url = URL::Parser::basic_parse(ByteString::formatted("http://127.0.0.1:{}/file", server.port()));@@ -544,6 +569,20 @@ TEST_CASE(file_downloader)outln("release request from finish callback");}+#if defined(AK_OS_MACOS)+ {+ auto response_body = make_body(4 * KiB);+ TestHttpServer server { response_body, RangeSupport::No };++ expect_file_is_quarantined(run_download(server, test_directory, "quarantined.bin"sv), QuarantineEvent::Yes);+ outln("quarantine as web download");++ auto private_path = run_download(server, test_directory, "quarantined-private.bin"sv, {}, WebView::IsPrivate::Yes);+ expect_file_is_quarantined(private_path, QuarantineEvent::No);+ outln("quarantine private download without an event");+ }+#endif+{auto response_body = make_body(4 * KiB);TestHttpServer server { response_body, RangeSupport::No };