Commit Graph

489 Commits

Author SHA1 Message Date
Owen W. Taylor
bd846c389f oci-registry: Move some helper functions to flatpak-utils.c
In order to reuse in flatpak-oci-signatures.c, move:

 local_open_file => flatpak_open_file_at
 local_load_file => flatpak_load_file_at
2025-12-08 19:09:56 +00:00
Georgia Garcia
f61d931da8 common: Fix running_under_sudo check by checking euid
Sudo can be used in several ways other than calling a command with the
root user. For example, one can use -u to run the command as the
specified user, or -g to specify a primary group to run the command
as.

Flatpak adds a check when --user is used to prevent an installation in
the root's directory, for example, but it does it by only checking if
sudo was used. As stated previously, it does not necessarily imply
root, so this patch explicitly checks if the command is being run with
the root user.

Fixes: https://github.com/flatpak/flatpak/issues/5979
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
2025-10-28 13:17:13 +00:00
bbhtt
0bf531f44a dir: Add basic OS info to Flatpak-Os-Info header when pulling objects
Fixes https://github.com/flatpak/flatpak/issues/5549
2025-10-13 18:52:55 +00:00
Alexander Larsson
5c12cd83a1 tests: Add support for adding internal tests
If the internal_tests option is enabled we build some internal tests
into the binary. These are added to the tests we run in testlibrary.

This is not intended to be enabled in production, as it adds size to
the real binary, but is useful for CI and development.
2025-10-13 18:31:33 +00:00
joj
f53cef0041 Enable VA-API extension for Intel GPUs if either i915 or xe is loaded
Xe module supports the discrete and new integrated GPUs (the Arc series) and
the i915 supports the older Intel integrated GPUs (Intel HD).

Closes: https://github.com/flatpak/flatpak/issues/5248
2025-09-10 22:44:52 +00:00
Simon McVittie
5d62a6d80b utils: Add flatpak_is_debugging()
This can be used to disable code paths that assemble relatively
"expensive" debug information when debugging is not enabled.
It's activated by `flatpak -v -v`.

With a sufficiently modern GLib version, it also activates for
`G_MESSAGES_DEBUG=all` or `G_MESSAGES_DEBUG=flatpak`.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-08-23 08:39:00 -03:00
Simon McVittie
f711ffc0e3 utils: Be more const-correct
For historical reasons C string literals are officially of type `char *`,
but if we build with -Wwrite-strings, they are `const char *` as they
should be.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-08-22 15:17:13 -03:00
Simon McVittie
87360c96e0 utils: Move remaining direct ostree dependencies to repo-utils
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-07-09 17:12:55 -03:00
Simon McVittie
97cddd6e48 utils: Move more repository functionality to repo-utils
This further reduces circular dependencies: utils no longer has a
circular dependency with repo-utils or xml-utils.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-07-09 17:12:55 -03:00
Simon McVittie
3271f9c25d utils: Export flatpak_get_compat_arch()
This will allow its caller to be moved into repo-utils.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-07-09 17:12:55 -03:00
Simon McVittie
1b85a2c090 utils: Move more summary parsing into repo-utils
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-07-09 17:12:55 -03:00
Simon McVittie
06970e015f utils: Move more repository functionality into repo-utils
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-07-09 17:12:55 -03:00
Simon McVittie
31590889f8 ref-utils: Move flatpak_get_arch_for_ref() to here
The declaration was already in flatpak-ref-utils-private.h.

Fixes: 5dae1fc6 "Break out ref helper functions to separate file"
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-07-09 17:12:55 -03:00
Simon McVittie
c98a7c024f utils: Move OstreeRepo configuration accessors to a new translation unit
This is a step towards removing the libostree dependency from
flatpak-utils, which should be one of the lowest-level components.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-07-09 17:12:55 -03:00
Simon McVittie
14db9d48cf common: Break out the parts of flatpak-utils that deal with FlatpakDir
This breaks the circular dependency between flatpak-utils and flatpak-dir.
There is still a circular dependency between flatpak-dir and
flatpak-dir-utils, but I don't want to make flatpak-dir even larger.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-07-09 17:12:55 -03:00
Simon McVittie
3c82620bab repo-utils: New header for some implementation details of a repository
This will reduce circular dependencies involving FlatpakDir.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-07-09 17:12:55 -03:00
Simon McVittie
89f8f3767a common: Move OCI registry manipulation into FlatpakOciRegistry
This is a step towards making flatpak-utils conceptually "smaller"
than all other translation units, with no dependencies beyond GLib and
libglnx. In particular, consolidating all the OCI registry manipulation
into one place means we can build other translation units without
libarchive.

This would also be a step towards being able to provide a build-time
option to build a libostree-only version of Flatpak without the OCI
feature or the direct libarchive dependency, if someone wanted to
implement that.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-07-09 17:12:55 -03:00
Simon McVittie
722fec4581 utils: Include flatpak-metadata-private.h instead of -run-private.h
This avoids a circular dependency between -run and -utils.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-07-09 17:12:55 -03:00
Simon McVittie
6e81bc6f28 common: Move functions for dealing with AppStream XML to their own file
This seems like an obvious set of functionality to be grouped together
in service of the wider goal of making flatpak-utils smaller and more
manageable.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-05-03 13:21:29 +01:00
Simon McVittie
a01371ce02 common: Move flatpak_context_get_allowed_exports to FlatpakContext
This allows us to break a circular dependency between utils and context.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-05-03 13:21:29 +01:00
Simon McVittie
a96834bb32 populate_commit_data_cache: Don't leak a floating GVariant
var_variant_dup_to_gvariant() returns a floating GVariant, and
g_variant_get_child_value() won't sink it, so we need to free it.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-15 11:06:30 +00:00
Simon McVittie
fd56fb85f8 populate_commit_data_cache: Don't leak child value
g_variant_get_child_value() returns a non-floating reference, so
g_variant_builder_add() will not sink it.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-15 11:06:30 +00:00
Hubert Figuière
6e3cc82af3 Fix memory leaks
Build with address sanitizer and run the tests

Signed-off-by: Hubert Figuière <hub@figuiere.net>
2024-02-13 08:20:57 -06:00
Simon McVittie
9bf3620aef common: Move locale utils to their own small translation unit
This reduces the size of flatpak-utils and flatpak-dir, which are both
too large.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-11-14 18:39:22 +00:00
Dor Askayo
2804cd4116 utils: Add flatpak_parse_env_block()
This function can be used to parse environment variable blocks in
"env -0" format from a buffer. It performs a few format checks during
its parsing and returns an error if an issue is found.

When successful, it returns a string array containing each individual
environment variable parsed from the buffer, in the same format as
the return value of g_get_environ().

The implementation of this function is based on
flatpak_context_parse_env_block().
2023-10-27 17:09:52 +01:00
Simon McVittie
5009857b5a Use g_steal_fd()
This was new in GLib 2.70, but libglnx now provides a backport, so we
can use it unconditionally.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-09-04 13:25:32 +01:00
Simon McVittie
ae2579637a app: Move terminal-related utility functions from common into app
These functions are to do with being an interactive, terminal-oriented
CLI/TUI, so it would be inappropriate for library code in libflatpak
to call them, and it would also be inappropriate for daemons like the
session and system helpers to call them.

In fact all calls to these were already isolated to app/, so we can
easily move the terminal-related utilities themselves into app/.

As well as shrinking libflatpak, this makes it obvious that the system
helper does not actually need to call flatpak_disable_fancy_output():
it does not link any code that would be affected by that API call.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-07-03 20:19:00 +02:00
Simon McVittie
f6cf1b2bbb common: Remove unused functions
Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-07-03 20:07:57 +02:00
Simon McVittie
5522d19f4f common: Move flatpak_is_app_runtime_or_appstream_ref to ref-utils
This matches where it was declared.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-07-03 20:07:57 +02:00
Simon McVittie
ba556fb98c glib-backports: Move flatpak_utils_ascii_string_to_unsigned to here
Currently this is used unconditionally, even if GLib is new enough.
That will be changed in a subsequent commit; no functional change
intended in this one.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-05-17 11:35:44 +01:00
Simon McVittie
a0a3734f18 common: Move direct backports from GLib to a new translation unit
flatpak-utils.c is getting quite large, and is a mixture of code with
and without non-GLib dependencies, making it inconvenient to reuse in
other projects (like Steam's pressure-vessel) or link into Flatpak
services that don't need all of our dependencies (json-glib, appstream,
ostree). One obvious piece of low-hanging fruit for reducing the size
of this file is to move the GLib backports into their own
translation unit.

Sort them by GLib version, so that when we increase our GLib
dependency it's easy to delete the ones that are no longer applicable.

No functional changes intended in this commit.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-05-17 11:35:44 +01:00
Jan Alexander Steffens (heftig)
c9cbdf5179 Set size of file info for symlinks to 0
`ostree_raw_file_to_content_stream` will try to read it, causing a
critical warning with GLib 2.76 causing tests to fail.
2023-03-17 13:27:21 +00:00
Ryan Gonzalez
7fe63f2e8f Reject paths given to --filesystem/--persist with special characters
There isn't much in the way of legit reasons for this, but it's a
potential security footgun when displaying the text.

CVE-2023-28101, GHSA-h43h-fwqx-mpp8

Signed-off-by: Ryan Gonzalez <ryan.gonzalez@collabora.com>
Co-authored-by: Simon McVittie <smcv@collabora.com>
2023-03-16 09:54:14 +00:00
Ryan Gonzalez
6cac99dafe Ensure special characters in permissions and metadata are escaped
This prevents someone from placing special characters in order to
manipulate the appearance of the permissions list.

CVE-2023-28101, GHSA-h43h-fwqx-mpp8

Signed-off-by: Ryan Gonzalez <ryan.gonzalez@collabora.com>
2023-03-16 09:54:14 +00:00
Patrick Griffis
bb730ef8b8 Revert "Add support for files generated by appstreamcli compose (#5277)"
This reverts commit 88f7ecd000.
2023-02-04 12:30:15 -06:00
Luna
88f7ecd000 Add support for files generated by appstreamcli compose (#5277)
Co-authored-by: Bartłomiej Piotrowski <b@bpiotrowski.pl>
Co-authored-by: Jamie Murphy <hello@itsjamie.dev>
2023-02-04 10:40:07 -06:00
Forest
01910ad12f utils: Unmap the old summary.idx file before trying to replace it
Exporting to an existing repo on a Samba filesystem failed with EACCES
when libglnx called renameat() to replace the old summary.idx file.

    error: renameat: Permission denied

This occurred even when the user had appropriate permissions to the file
and its ancestor directories. The problem was that flatpak had mapped the
old file into memory for reading, and still held a reference to that mapping
when attempting to replace the underlying file. Apparently this works on some
filesystems, but not on cifs.

We therefore release the memory mapping before replacing the underlying file.

Fixes #5257

Co-authored-by: Patrick <tingping@tingping.se>
2023-01-22 19:21:36 -06:00
Simon McVittie
e69afb1986 portal: Factor out flatpak_bwrap_is_unprivileged()
We can use this for other features that rely on having a non-setuid
version of bubblewrap.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-12-19 19:15:23 +00:00
Simon McVittie
c2c034733e common: Make flatpak_debug2() use the same domain as g_debug()
Now that we are logging `flatpak -v` messages with log level INFO,
and printing INFO messages in the same way as DEBUG, we can reserve
log level DEBUG for `flatpak -v -v` messages. This means we no longer
need a weird secondary debug domain.

There is a very small behaviour change here: G_MESSAGES_DEBUG=flatpak
is now similar to `flatpak -v -v` (previously `flatpak -v`), and
G_MESSAGES_DEBUG=flatpak2 no longer has any effect. This seems more in
line with what would be expected from a GLib-based application.

In flatpak(1) and the system helper, this does not change behaviour
other than that: the same messages are logged by `-v` and by `-v -v`
as before.

In daemons that do not implement `-v -v` (the OCI authenticator, portal
and session helper), it continues to be necessary to use
G_MESSAGES_DEBUG to see flatpak_debug2() messages.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-12-15 16:45:35 +00:00
Simon McVittie
07a6541f06 common: Use g_info() for messages that will be shown by flatpak -v
This brings us one step closer to being able to stop using the flatpak2
log domain for messages that are exclusive to `flatpak -v -v`.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-12-15 16:45:35 +00:00
Simon McVittie
1eed25617c Replace calls to g_memdup() with g_memdup2()
g_memdup() is subject to an integer overflow on 64-bit machines if the
object being copied is larger than UINT_MAX bytes. I suspect none of
these objects can actually be that large in practice, but it's easier
to replace all the calls than it is to assess whether we need to
replace them.

A backport in libglnx is used on systems where GLib is older than 2.68.x.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-12-12 19:15:52 +00:00
Phaedrus Leeds
600e18567c Add a vim modeline and .editorconfig
To make indentation work with less effort. The modeline was copied from
libostree with minor modification and the .editorconfig from GLib.

The advantage of having both a modeline and an editorconfig is we can
work out of the box on more editor setups, and the modeline allows us to
specify the style with a lot more fine grained control.
2022-08-22 19:48:10 -07:00
Phaedrus Leeds
53a0b528bf Add DeploySideloadCollectionID flatpakref/flatpakrepo key
When Flatpak's P2P updates support was replaced with the "sideloading"
implementation in 1.7.1, a new server side repo config key
"deploy-sideload-collection-id" was added which gets set when you pass
"--deploy-sideload-collection-id" to "flatpak build-update-repo", and
has the effect of setting "xa.deploy-collection-id" in the repo metadata
that is pulled by clients, which itself causes a collection id to be set
on the remote for clients using Flatpak >= 1.7.1.

This commit adds an analogous key in flatpakref and flatpakrepo files,
so the collection id can be set when the remote is configured, rather
than later on when the repo metadata is pulled and acted upon. As before
with DeployCollectionID, it has no difference in function compared to
DeployCollectionID or CollectionID and the only difference is which
Flatpak versions are affected.

It would've been better if this were added in 1.7.1 when the sideload
support was added, but alas here we are.

(Also update the docs and unit tests)
2022-08-16 10:37:36 +02:00
Phaedrus Leeds
bf37034663 common: Add missing error codes to GDBusErrorEntry array
These were mistakenly only added to flatpak-error.h
2022-07-25 16:58:32 -04:00
Phaedrus Leeds
e739773056 app: List apps that use a runtime extension when it's EOL
Currently if a runtime extension, e.g.
org.freedesktop.Platform.html5-codecs//18.08 is used by a runtime
org.kde.Platform//5.12 which itself is used by one or more apps, when we
print a message to the user about html5-codecs being EOL, we don't find
any apps using it and don't print any. Fix this by including apps that
indirectly use a runtime extension in the "Applications using this
runtime:" list.

In a later commit we can re-use the helper function added here to add a
confirmation dialog if the user tries to remove a runtime extension
that's being used; currently we just let them remove it.

This is limited to only looking in the current flatpak installation, so
a per-user app using a system-wide runtime extension would not be found.

This is implemented using in-memory caches because otherwise it is
horribly slow; see
https://github.com/flatpak/flatpak/pull/4835#discussion_r876425289

Helps: #3531
2022-07-21 14:32:45 -05:00
Debarshi Ray
50835cccfd utils: Avoid passing a potentially NULL pointer to strlen(3) 2022-06-03 10:53:03 +02:00
Alexander Larsson
3a2755d7fd build-update: Only add the specified summary-arches to the compat summary
We support the "summary-arches" variable to limit which arches goes
into the compat summary file. However, its currently always adding in
the compat arches of whatever arches you list. This means we can't
e.g. keep compat summary support for aarch64, but not for armv7, which
is a problem as we're nearing the 10MB summary size limit of ostree
for old clients.

So, just keep the exact arches listed. If you want to keep compat
arches, you need to explicitly list them.
2022-05-05 15:55:33 +02:00
Phaedrus Leeds
c7d262b375 app: Disable fuzzy matching if not on a tty
As discussed in #4848, this disables fuzzy matching entirely if stdin or
stdout is not a tty, meaning that something like "flatpak install
firefox" would be treated as incorrect syntax, since this syntax is
intended for interactive CLI use. Even before this commit, "flatpak
install firefox" would error out if run without a tty, since we don't
automatically choose a matching app ID even if there is only one match.
However "flatpak install -y firefox" could work before, but won't any
more. People should be specifying the full app ID in any context other
than a tty.

This commit also introduces a new env var so the unit tests can continue
to check the fuzzy matching behavior, despite them being run without a
tty.
2022-04-21 20:53:46 +01:00
Simon McVittie
c68f31d86a build: Consistently include libglnx header as "libglnx.h"
Recent Meson versions have warnings if you add the subprojects
directory as an include path, because the way Meson wants to consume
subprojects is by the subproject's build system producing a Meson
dependency object that encapsulates its include directory. Flatpak
doesn't have a Meson build system yet, but I'm working on that.

libglnx seems to be set up to have the libglnx directory be its include
path instead: for example, ostree (by the author of libglnx) already
uses "libglnx.h" or <libglnx.h> everywhere. Do the same here.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-04-11 10:32:34 +02:00
Phaedrus Leeds
ecafded3b1 dir: Rewrite dynamic launcher entries on deploy
Fixes https://github.com/flatpak/flatpak/issues/4703
2022-03-29 17:24:03 -07:00