xdg-desktop-portal File Access in Linux Explained
The xdg-desktop-portal service acts as a secure,
standardized bridge between sandboxed, containerized applications—such
as Flatpaks and Snaps—and the underlying Linux operating system. In
modern Linux environments, applications are frequently isolated from the
host filesystem to enhance security and stability.
xdg-desktop-portal solves the challenge of file sharing by
allowing these isolated applications to request user-authorized access
to specific files via native system dialogs and D-Bus interfaces,
ensuring applications only read or modify files that the user explicitly
selects.
The Containerization Challenge
Traditional Linux applications run with the full permissions of the user executing them, granting them unrestricted access to files in the home directory. Containerized packaging formats like Flatpak and Snap isolate applications inside sandboxes using Linux namespaces, cgroups, and seccomp filters. While this confinement shields sensitive data from untrusted code, it prevents applications from performing routine tasks, such as opening a document or saving an exported image, without breaking the security model.
The Role of xdg-desktop-portal
Instead of giving an application broad access to storage devices, the
desktop environment provides xdg-desktop-portal as a
desktop-agnostic D-Bus service. When a sandboxed application needs to
read or write a file, it calls the
org.freedesktop.portal.FileChooser interface over the D-Bus
session bus.
xdg-desktop-portal routes this request to an
environment-specific backend implementation, such as
xdg-desktop-portal-gtk for GNOME,
xdg-desktop-portal-kde for Plasma, or
xdg-desktop-portal-wlr for wlroots-based compositors. This
modular design guarantees that dialogs match the look, feel, and native
behavior of the host desktop environment.
The File Access Workflow
The process of granting file access follows a distinct, user-driven workflow:
- Request: The containerized application initiates a file open or save operation using standard platform file dialog APIs (such as GTK or Qt), which are configured to use portal backends inside sandboxes.
- Mediation: The request is sent to
xdg-desktop-portal, which triggers the host's backend to render the native file picker outside the application's sandbox. The sandboxed application cannot see or interact with the filesystem during this selection phase. - User Consent: The user navigates the filesystem and selects the target file. This interaction serves as explicit, runtime user authorization.
- Handoff: Once selected, the file picker closes, and the host returns the user's choice to the portal framework.
The Document Portal and Dynamic Permissions
Because the container still lacks direct access to host paths, the
system relies on a companion service called the Document Portal
(xdg-document-portal). The Document Portal uses a virtual
FUSE filesystem mounted inside the container, typically located at
/run/user/$UID/doc/.
When the user selects a file:
- The Document Portal registers the host file and generates a unique, opaque path inside the FUSE mount.
- The containerized app is granted scoped read, write, or grant permissions solely for that specific file path.
- The application interacts with the file through the virtual mount point, while the Document Portal securely proxies modifications back to the real file on the host.
Security and Usability Advantages
By relying on xdg-desktop-portal, Linux achieves the
principle of least privilege without sacrificing user experience.
Developers do not need to request blanket permissions (such as
--filesystem=home) in their package manifests.
Simultaneously, users maintain full control over their personal files,
as no containerized program can quietly inspect or modify host data
without direct user interaction through the native desktop
interface.