From c7d262b3755a9fa256b1e0b56a5d4fedcbe2a287 Mon Sep 17 00:00:00 2001 From: Phaedrus Leeds Date: Thu, 14 Apr 2022 12:58:42 -0700 Subject: [PATCH] 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. --- app/flatpak-builtins-install.c | 4 ++-- app/flatpak-builtins-uninstall.c | 2 +- common/flatpak-utils-private.h | 1 + common/flatpak-utils.c | 18 ++++++++++++++++++ tests/libtest.sh | 1 + 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/flatpak-builtins-install.c b/app/flatpak-builtins-install.c index 9e68cee2..8c263d9e 100644 --- a/app/flatpak-builtins-install.c +++ b/app/flatpak-builtins-install.c @@ -382,7 +382,7 @@ flatpak_builtin_install (int argc, char **argv, GCancellable *cancellable, GErro if (remotes == NULL) return FALSE; - if (strchr (argv[1], '/') != NULL || strchr (argv[1], '.') != NULL) + if (!flatpak_allow_fuzzy_matching (argv[1])) matching_refs_flags = FIND_MATCHING_REFS_FLAGS_NONE; else matching_refs_flags = FIND_MATCHING_REFS_FLAGS_FUZZY; @@ -503,7 +503,7 @@ flatpak_builtin_install (int argc, char **argv, GCancellable *cancellable, GErro g_autoptr(GError) local_error = NULL; FindMatchingRefsFlags matching_refs_flags; - if (strchr (pref, '/') != NULL || strchr (pref, '.') != NULL) + if (!flatpak_allow_fuzzy_matching (pref)) matching_refs_flags = FIND_MATCHING_REFS_FLAGS_NONE; else matching_refs_flags = FIND_MATCHING_REFS_FLAGS_FUZZY; diff --git a/app/flatpak-builtins-uninstall.c b/app/flatpak-builtins-uninstall.c index ca9dcdaf..ec780f33 100644 --- a/app/flatpak-builtins-uninstall.c +++ b/app/flatpak-builtins-uninstall.c @@ -286,7 +286,7 @@ flatpak_builtin_uninstall (int argc, char **argv, GCancellable *cancellable, GEr pref = prefs[j]; - if (strchr (pref, '/') != NULL || strchr (pref, '.') != NULL) + if (!flatpak_allow_fuzzy_matching (pref)) matching_refs_flags = FIND_MATCHING_REFS_FLAGS_NONE; else matching_refs_flags = FIND_MATCHING_REFS_FLAGS_FUZZY; diff --git a/common/flatpak-utils-private.h b/common/flatpak-utils-private.h index d11339d6..70657075 100644 --- a/common/flatpak-utils-private.h +++ b/common/flatpak-utils-private.h @@ -841,6 +841,7 @@ gboolean flatpak_allocate_tmpdir (int tmpdir_dfd, GCancellable *cancellable, GError **error); +gboolean flatpak_allow_fuzzy_matching (const char *term); char * flatpak_prompt (gboolean allow_empty, const char *prompt, diff --git a/common/flatpak-utils.c b/common/flatpak-utils.c index bfafc229..2e96ce06 100644 --- a/common/flatpak-utils.c +++ b/common/flatpak-utils.c @@ -7411,6 +7411,24 @@ flatpak_allocate_tmpdir (int tmpdir_dfd, return TRUE; } +gboolean +flatpak_allow_fuzzy_matching (const char *term) +{ + if (strchr (term, '/') != NULL || strchr (term, '.') != NULL) + return FALSE; + + /* This env var is used by the unit tests and only skips the tty test not the + * check above. + */ + if (g_strcmp0 (g_getenv ("FLATPAK_FORCE_ALLOW_FUZZY_MATCHING"), "1") == 0) + return TRUE; + + if (!isatty (STDIN_FILENO) || !isatty (STDOUT_FILENO)) + return FALSE; + + return TRUE; +} + char * flatpak_prompt (gboolean allow_empty, const char *prompt, ...) diff --git a/tests/libtest.sh b/tests/libtest.sh index e63b9247..965cf667 100644 --- a/tests/libtest.sh +++ b/tests/libtest.sh @@ -99,6 +99,7 @@ export FLATPAK_SYSTEM_HELPER_ON_SESSION=1 export FLATPAK_CONFIG_DIR=${TEST_DATA_DIR}/config export FLATPAK_RUN_DIR=${TEST_DATA_DIR}/run export FLATPAK_FANCY_OUTPUT=0 +export FLATPAK_FORCE_ALLOW_FUZZY_MATCHING=1 export HOME=${TEST_DATA_DIR}/home export XDG_CACHE_HOME=${TEST_DATA_DIR}/home/cache