Skip to content

Truly independent web browser

LibCore: Add File::open_regular_file_or_directory()

Shannon Boothc7dab7507ae1
diff --git a/Libraries/LibCore/File.h b/Libraries/LibCore/File.h
index 31781584060..d8b0882cc6c 100644
--- a/Libraries/LibCore/File.h
+++ b/Libraries/LibCore/File.h
@@ -52,6 +52,10 @@ public:
static ErrorOr<NonnullOwnPtr<File>> open(StringView filename, OpenMode, mode_t = 0644);
static ErrorOr<NonnullOwnPtr<File>> adopt_fd(int fd, OpenMode, ShouldCloseFileDescriptor = ShouldCloseFileDescriptor::Yes);
+#if !defined(AK_OS_WINDOWS)
+ static ErrorOr<NonnullOwnPtr<File>> open_regular_file_or_directory(StringView canonical_path);
+#endif
+
static ErrorOr<NonnullOwnPtr<File>> standard_input();
static ErrorOr<NonnullOwnPtr<File>> standard_output();
File(File&& other) { operator=(move(other)); }
diff --git a/Libraries/LibCore/FilePosix.cpp b/Libraries/LibCore/FilePosix.cpp
index e5b046d00dc..1f9f32afc9f 100644
--- a/Libraries/LibCore/FilePosix.cpp
+++ b/Libraries/LibCore/FilePosix.cpp
@@ -5,6 +5,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/LexicalPath.h>
+#include <AK/ScopeGuard.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
@@ -133,4 +135,54 @@ ErrorOr<void> File::set_blocking(bool enabled)
return System::set_socket_blocking(fd(), enabled);
}
+ErrorOr<NonnullOwnPtr<File>> File::open_regular_file_or_directory(StringView canonical_path)
+{
+ // Intermediate directories need search permission, not read permission.
+#if defined(O_PATH)
+ static constexpr int directory_options = O_PATH | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC;
+#elif defined(O_SEARCH)
+ static constexpr int directory_options = O_SEARCH | O_NOFOLLOW | O_CLOEXEC;
+#else
+ static constexpr int directory_options = O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC;
+#endif
+ static constexpr int file_options = O_RDONLY | O_NONBLOCK | O_NOFOLLOW | O_CLOEXEC;
+
+ auto is_regular_file_or_directory = [](struct stat const& stat) {
+ return S_ISREG(stat.st_mode) || S_ISDIR(stat.st_mode);
+ };
+
+ LexicalPath lexical_path { canonical_path };
+ if (!lexical_path.is_absolute())
+ return Error::from_errno(EINVAL);
+ auto const& components = lexical_path.parts_view();
+
+ int fd = -1;
+ if (components.is_empty()) {
+ fd = TRY(System::open("/"sv, file_options));
+ } else {
+ int directory_fd = TRY(System::open("/"sv, directory_options));
+ ScopeGuard close_directory = [&] { (void)System::close(directory_fd); };
+ for (size_t i = 0; i + 1 < components.size(); ++i) {
+ auto next_directory_fd = TRY(System::openat(directory_fd, components[i], directory_options));
+ (void)System::close(directory_fd);
+ directory_fd = next_directory_fd;
+ }
+
+ if (!is_regular_file_or_directory(TRY(System::fstatat(directory_fd, components.last(), AT_SYMLINK_NOFOLLOW))))
+ return Error::from_errno(EACCES);
+ fd = TRY(System::openat(directory_fd, components.last(), file_options));
+ }
+
+ auto file_or_error = adopt_fd(fd, OpenMode::Read);
+ if (file_or_error.is_error()) {
+ (void)System::close(fd);
+ return file_or_error.release_error();
+ }
+ auto file = file_or_error.release_value();
+ if (!is_regular_file_or_directory(TRY(file->stat())))
+ return Error::from_errno(EACCES);
+ TRY(file->set_blocking(true));
+ return file;
+}
+
}
diff --git a/Tests/LibCore/CMakeLists.txt b/Tests/LibCore/CMakeLists.txt
index 734036217f2..e9196fd6bda 100644
--- a/Tests/LibCore/CMakeLists.txt
+++ b/Tests/LibCore/CMakeLists.txt
@@ -24,6 +24,7 @@ endif()
if (APPLE OR LINUX)
list(APPEND TEST_SOURCES
+ TestLibCoreFile.cpp
TestLibCoreProcess.cpp
TestLibCoreStandardPaths.cpp
)
diff --git a/Tests/LibCore/TestLibCoreFile.cpp b/Tests/LibCore/TestLibCoreFile.cpp
new file mode 100644
index 00000000000..a5f872268ba
--- /dev/null
+++ b/Tests/LibCore/TestLibCoreFile.cpp
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/LexicalPath.h>
+#include <LibCore/File.h>
+#include <LibCore/System.h>
+#include <LibTest/TemporaryDirectory.h>
+#include <LibTest/TestCase.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+
+namespace {
+
+// The paths given to open_regular_file_or_directory() are canonical, so the temporary directory's links are resolved.
+class CanonicalDirectory {
+public:
+ CanonicalDirectory()
+ {
+ char resolved[PATH_MAX];
+ VERIFY(realpath(m_directory.path().string().characters(), resolved));
+ m_path = resolved;
+ }
+
+ ByteString join(StringView name) const { return LexicalPath::join(m_path, name).string(); }
+ ByteString const& path() const { return m_path; }
+
+ ByteString write_file(StringView name, StringView content) const
+ {
+ auto path = join(name);
+ auto file = MUST(Core::File::open(path, Core::File::OpenMode::Write));
+ MUST(file->write_until_depleted(content));
+ return path;
+ }
+
+private:
+ Test::TemporaryDirectory m_directory { "TestLibCoreFile"sv };
+ ByteString m_path;
+};
+
+}
+
+static void expect_refused(StringView path)
+{
+ if (!Core::File::open_regular_file_or_directory(path).is_error())
+ FAIL(ByteString::formatted("{} was opened", path));
+}
+
+TEST_CASE(regular_files_and_directories_open)
+{
+ CanonicalDirectory directory;
+ auto file_path = directory.write_file("file.txt"sv, "content"sv);
+
+ auto file = TRY_OR_FAIL(Core::File::open_regular_file_or_directory(file_path));
+ auto content = TRY_OR_FAIL(file->read_until_eof());
+ EXPECT_EQ(StringView { content }, "content"sv);
+ EXPECT_EQ(fcntl(file->fd(), F_GETFL) & O_NONBLOCK, 0);
+
+ EXPECT(!Core::File::open_regular_file_or_directory(directory.path()).is_error());
+ EXPECT(!Core::File::open_regular_file_or_directory("/"sv).is_error());
+}
+
+TEST_CASE(directories_on_the_way_need_only_be_searchable)
+{
+ CanonicalDirectory directory;
+ TRY_OR_FAIL(Core::System::mkdir(directory.join("search-only"sv), 0700));
+ auto file_path = directory.write_file("search-only/file.txt"sv, "content"sv);
+ TRY_OR_FAIL(Core::System::chmod(directory.join("search-only"sv), 0100));
+
+ auto file = Core::File::open_regular_file_or_directory(file_path);
+ TRY_OR_FAIL(Core::System::chmod(directory.join("search-only"sv), 0700));
+ EXPECT(!file.is_error());
+}
+
+TEST_CASE(symbolic_links_are_refused)
+{
+ CanonicalDirectory directory;
+ auto file_path = directory.write_file("file.txt"sv, "content"sv);
+
+ auto file_link = directory.join("file-link"sv);
+ TRY_OR_FAIL(Core::System::symlink(file_path, file_link));
+ expect_refused(file_link);
+
+ auto directory_link = directory.join("directory-link"sv);
+ TRY_OR_FAIL(Core::System::symlink(directory.path(), directory_link));
+ expect_refused(LexicalPath::join(directory_link, "file.txt"sv).string());
+}
+
+TEST_CASE(other_kinds_of_file_are_refused)
+{
+ CanonicalDirectory directory;
+ auto fifo_path = directory.join("fifo"sv);
+ VERIFY(mkfifo(fifo_path.characters(), 0600) == 0);
+ expect_refused(fifo_path);
+
+ expect_refused("/dev/null"sv);
+}
+
+TEST_CASE(relative_paths_are_refused)
+{
+ expect_refused("file.txt"sv);
+}