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.
This commit is contained in:
Phaedrus Leeds
2022-04-14 12:58:42 -07:00
committed by Simon McVittie
parent 5acb4ee7e4
commit c7d262b375
5 changed files with 23 additions and 3 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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,

View File

@@ -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, ...)

View File

@@ -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