Skip to content

Commit

Permalink
open-uri: prevent a request from being opened in the source app
Browse files Browse the repository at this point in the history
Prevent a request from an application to open a URI from looping back
and sent back to the same application.
  • Loading branch information
andyholmes committed Apr 17, 2024
1 parent b9eb4b6 commit 71d59ad
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/open-uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,9 +743,10 @@ load_handlers_json (void)
}

static void
find_patterned_choices (const char *uri,
GStrv *choices,
guint *choices_len)
find_patterned_choices (const char *source_id,
const char *uri,
GStrv *choices,
guint *choices_len)
{
g_autoptr(GUri) guri = NULL;
g_autoptr(GError) error = NULL;
Expand All @@ -771,6 +772,12 @@ find_patterned_choices (const char *uri,
g_hash_table_iter_init (&iter, apps);
while (g_hash_table_iter_next (&iter, (void **)&app_id, (void **)&handlers))
{
if (g_strcmp0 (source_id, app_id) == 0)
{
g_debug ("Skipping handler for originating app %s", app_id);
continue;
}

if (app_uri_handler_match (handlers, guri))
{
g_debug ("Matching handler for %s (%s)", uri, app_id);
Expand All @@ -785,7 +792,8 @@ find_patterned_choices (const char *uri,
}

static void
find_recommended_choices (const char *uri,
find_recommended_choices (XdpAppInfo *app,
const char *uri,
const char *scheme,
const char *content_type,
char **default_app,
Expand All @@ -801,7 +809,7 @@ find_recommended_choices (const char *uri,

/* TODO: pre-empt the default app, since there are hard-coded scheme overrides
*/
find_patterned_choices (uri, &result, &n_choices);
find_patterned_choices (xdp_app_info_get_id (app), uri, &result, &n_choices);
if (n_choices > 0)
{
*choices = g_steal_pointer (&result);
Expand Down Expand Up @@ -863,7 +871,7 @@ app_info_changed (GAppInfoMonitor *monitor,
scheme = (const char *)g_object_get_data (G_OBJECT (request), "scheme");
content_type = (const char *)g_object_get_data (G_OBJECT (request), "content-type");
uri = (const char *)g_object_get_data (G_OBJECT (request), "uri");
find_recommended_choices (uri, scheme, content_type, &default_app, &choices, &n_choices);
find_recommended_choices (request->app_info, uri, scheme, content_type, &default_app, &choices, &n_choices);

xdp_dbus_impl_app_chooser_call_update_choices (impl,
request->id,
Expand Down Expand Up @@ -1094,7 +1102,7 @@ handle_open_in_thread_func (GTask *task,
g_object_set_data_full (G_OBJECT (request), "content-type", g_strdup (content_type), g_free);

/* collect all the information */
find_recommended_choices (uri, scheme, content_type, &default_app, &choices, &n_choices);
find_recommended_choices (request->app_info, uri, scheme, content_type, &default_app, &choices, &n_choices);
/* it's never NULL, but might be empty (only contain the NULL terminator) */
g_assert (choices != NULL);
if (default_app != NULL && !app_exists (default_app))
Expand Down

0 comments on commit 71d59ad

Please sign in to comment.