Skip to content

Truly independent web browser

LibWebView: Check a renderer's local file before opening it

Shannon Booth522d140afbad
diff --git a/Libraries/LibWebView/Utilities.cpp b/Libraries/LibWebView/Utilities.cpp
index 80101186c1b..900027ff165 100644
--- a/Libraries/LibWebView/Utilities.cpp
+++ b/Libraries/LibWebView/Utilities.cpp
@@ -20,6 +20,10 @@
# include <AK/Windows.h>
#endif
+#if defined(AK_OS_LINUX)
+# include <linux/magic.h>
+#endif
+
namespace WebView {
ErrorOr<Web::HTML::SelectedFile> create_selected_file(ByteString const& file_path)
@@ -48,17 +52,26 @@ ErrorOr<IPC::File> open_local_file_for_renderer(ByteString const& path)
return Error::from_errno(EACCES);
return IPC::File::adopt_file(move(file));
#else
- auto fd = TRY(Core::System::open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
- auto file = IPC::File::adopt_fd(fd);
+ auto canonical_path = TRY(FileSystem::real_path(path));
+
+# if defined(AK_OS_MACOS)
+ // A /dev/fd entry opens a descriptor of the UI process, which reads as whatever file is behind it. Only the path
+ // says it's on devfs, so this is checked before the open.
+ auto path_file_system = TRY(Core::System::statfs(canonical_path));
+ if (StringView { path_file_system.f_fstypename, strnlen(path_file_system.f_fstypename, sizeof(path_file_system.f_fstypename)) } == "devfs"sv)
+ return Error::from_errno(EACCES);
+# endif
+
+ auto file = TRY(Core::File::open_regular_file_or_directory(canonical_path));
- auto stat = TRY(Core::System::fstat(fd));
- if (!S_ISREG(stat.st_mode) && !S_ISDIR(stat.st_mode))
+# if defined(AK_OS_LINUX)
+ // procfs and sysfs can be mounted anywhere and expose information about the opening process.
+ auto file_system = TRY(Core::System::fstatfs(file->fd()));
+ if (file_system.f_type == PROC_SUPER_MAGIC || file_system.f_type == SYSFS_MAGIC)
return Error::from_errno(EACCES);
+# endif
- // The renderer reads the file as it would any other, so it gets a blocking descriptor.
- auto flags = TRY(Core::System::fcntl(fd, F_GETFL));
- TRY(Core::System::fcntl(fd, F_SETFL, flags & ~O_NONBLOCK));
- return file;
+ return IPC::File::adopt_file(move(file));
#endif
}
diff --git a/Libraries/LibWebView/Utilities.h b/Libraries/LibWebView/Utilities.h
index f611c46d5e0..251d20991c4 100644
--- a/Libraries/LibWebView/Utilities.h
+++ b/Libraries/LibWebView/Utilities.h
@@ -21,8 +21,9 @@ namespace WebView {
WEBVIEW_API ErrorOr<Web::HTML::SelectedFile> create_selected_file(ByteString const&);
-// Opens a local file a renderer asked for, read-only. Only regular files and directories are opened, and the open
-// never blocks, so a path naming a FIFO or a device cannot stall the UI process.
+// Opens a local file a renderer asked for, read-only, once its symbolic links are resolved. Anything but a regular file
+// or a directory is refused, as Core::File::open_regular_file_or_directory() refuses it, and so is a file on devfs on
+// macOS, or a process information or sysfs file on Linux.
WEBVIEW_API ErrorOr<IPC::File> open_local_file_for_renderer(ByteString const& path);
WEBVIEW_API ErrorOr<JsonObject> read_json_file(ByteString const& path);
diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txt
index 9e4de5a4437..854a2f55274 100644
--- a/Tests/LibWebView/CMakeLists.txt
+++ b/Tests/LibWebView/CMakeLists.txt
@@ -108,6 +108,7 @@ if (APPLE OR LINUX)
TestCrashReportReview.cpp
TestCrashReportStore.cpp
TestFontService.cpp
+ TestOpenLocalFileForRenderer.cpp
TestRendererFontService.cpp
)
endif()
diff --git a/Tests/LibWebView/TestOpenLocalFileForRenderer.cpp b/Tests/LibWebView/TestOpenLocalFileForRenderer.cpp
new file mode 100644
index 00000000000..50ddcda2cb2
--- /dev/null
+++ b/Tests/LibWebView/TestOpenLocalFileForRenderer.cpp
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/LexicalPath.h>
+#include <AK/ScopeGuard.h>
+#include <LibCore/File.h>
+#include <LibCore/System.h>
+#include <LibTest/TemporaryDirectory.h>
+#include <LibTest/TestCase.h>
+#include <LibWebView/Utilities.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+static ByteString write_file(Test::TemporaryDirectory const& directory, StringView name, StringView content)
+{
+ auto path = directory.path().append(name).string();
+ auto file = MUST(Core::File::open(path, Core::File::OpenMode::Write));
+ MUST(file->write_until_depleted(content));
+ return path;
+}
+
+static void expect_refused(ByteString const& path)
+{
+ if (!WebView::open_local_file_for_renderer(path).is_error())
+ FAIL(ByteString::formatted("{} was opened", path));
+}
+
+TEST_CASE(regular_files_and_directories_open)
+{
+ Test::TemporaryDirectory directory { "TestOpenLocalFileForRenderer"sv };
+ auto file_path = write_file(directory, "file.txt"sv, "content"sv);
+
+ TRY_OR_FAIL(WebView::open_local_file_for_renderer(file_path));
+ TRY_OR_FAIL(WebView::open_local_file_for_renderer(directory.path().string()));
+
+ auto link_path = directory.path().append("link.txt"sv).string();
+ TRY_OR_FAIL(Core::System::symlink(file_path, link_path));
+ auto linked = TRY_OR_FAIL(WebView::open_local_file_for_renderer(link_path));
+ auto linked_file = TRY_OR_FAIL(Core::File::adopt_fd(linked.take_fd(), Core::File::OpenMode::Read));
+ auto content = TRY_OR_FAIL(linked_file->read_until_eof());
+ EXPECT_EQ(StringView { content }, "content"sv);
+}
+
+TEST_CASE(other_kinds_of_file_are_refused)
+{
+ Test::TemporaryDirectory directory { "TestOpenLocalFileForRenderer"sv };
+
+ auto fifo_path = directory.path().append("fifo"sv).string();
+ VERIFY(mkfifo(fifo_path.characters(), 0600) == 0);
+ expect_refused(fifo_path);
+
+ auto link_path = directory.path().append("link"sv).string();
+ TRY_OR_FAIL(Core::System::symlink(fifo_path, link_path));
+ expect_refused(link_path);
+
+ expect_refused("/dev/null"sv);
+}
+
+#if defined(AK_OS_MACOS)
+// A /dev/fd entry opens a descriptor of this process, which reads as the file behind it. realpath() turns /dev/fd/N into
+// /dev/fd/ and the file's name, so the file is named after its descriptor.
+TEST_CASE(descriptor_entries_are_refused)
+{
+ static constexpr int descriptor = 900;
+ // macOS starts a process with a limit of 256 descriptors.
+ TRY_OR_FAIL(Core::System::set_resource_limits(RLIMIT_NOFILE, 65536));
+ Test::TemporaryDirectory directory { "TestOpenLocalFileForRenderer"sv };
+ auto file = TRY_OR_FAIL(Core::File::open(write_file(directory, ByteString::number(descriptor), "content"sv), Core::File::OpenMode::Read));
+ VERIFY(dup2(file->fd(), descriptor) == descriptor);
+ ScopeGuard close_descriptor = [] { close(descriptor); };
+ expect_refused(ByteString::formatted("/dev/fd/{}", descriptor));
+}
+#endif
+
+#if defined(AK_OS_LINUX)
+TEST_CASE(process_and_system_information_is_refused)
+{
+ expect_refused("/proc/self/maps"sv);
+ expect_refused("/proc/self/environ"sv);
+ expect_refused("/sys/kernel/mm/transparent_hugepage/enabled"sv);
+}
+#endif