Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/String.h>
#include <LibCore/System.h>
#include <LibSandbox/Sandbox.h>
#include <RequestServer/ResourceSubstitutionMap.h>
#include <RequestServer/Sandbox.h>
namespace RequestServer {
ErrorOr<void> apply_sandbox(StringView mach_server_name, Vector<ByteString> const& certificates, Optional<StringView> disk_cache_directory)
{
TRY(Sandbox::configure_runtime());
Vector<Sandbox::SeatbeltPath> paths;
auto executable_path = TRY(Core::System::current_executable_path());
TRY(Sandbox::add_seatbelt_path_if_exists(paths, executable_path, Sandbox::SeatbeltPath::Access::ReadOnly));
// The helpers read their own application bundle, for example when CoreFoundation looks up the main bundle.
if (auto bundle = Sandbox::application_bundle_for_executable(executable_path); bundle.has_value())
TRY(Sandbox::add_seatbelt_path_if_exists(paths, *bundle, Sandbox::SeatbeltPath::Access::ReadOnly));
TRY(Sandbox::add_seatbelt_path_if_exists(paths, "/etc/hosts"sv, Sandbox::SeatbeltPath::Access::ReadOnly));
TRY(Sandbox::add_seatbelt_path_if_exists(paths, "/etc/resolv.conf"sv, Sandbox::SeatbeltPath::Access::ReadOnly));
TRY(Sandbox::add_seatbelt_path_if_exists(paths, "/private/etc/hosts"sv, Sandbox::SeatbeltPath::Access::ReadOnly));
TRY(Sandbox::add_seatbelt_path_if_exists(paths, "/private/etc/resolv.conf"sv, Sandbox::SeatbeltPath::Access::ReadOnly));
TRY(Sandbox::add_seatbelt_path_if_exists(paths, "/private/etc/ssl"sv, Sandbox::SeatbeltPath::Access::ReadOnly));
TRY(Sandbox::add_seatbelt_path_if_exists(paths, "/Library/Preferences/com.apple.networkd.plist"sv, Sandbox::SeatbeltPath::Access::ReadOnly));
for (auto const& certificate : certificates)
TRY(Sandbox::add_seatbelt_path_if_exists(paths, certificate, Sandbox::SeatbeltPath::Access::ReadOnly));
if (g_resource_substitution_map) {
TRY(g_resource_substitution_map->for_each_substitution([&](auto const& substitution) -> ErrorOr<void> {
TRY(Sandbox::add_seatbelt_path_if_exists(paths, substitution.file_path, Sandbox::SeatbeltPath::Access::ReadOnly));
return {};
}));
}
if (disk_cache_directory.has_value())
TRY(Sandbox::add_seatbelt_path_if_exists(paths, *disk_cache_directory, Sandbox::SeatbeltPath::Access::ReadWrite));
return Sandbox::apply_macos_sandbox({
.paths = paths.span(),
.network_access = Sandbox::NetworkAccess::Allowed,
.mach_server_name = mach_server_name,
});
}
}