[epiphany] Detect Snap sandbox in addition to Flatpak



commit af86b96ba187f67417d389ba8c6ce722d2dc0742
Author: Phaedrus Leeds <mwleeds protonmail com>
Date:   Fri Dec 3 13:47:07 2021 -0800

    Detect Snap sandbox in addition to Flatpak
    
    Snaps use the same portals as Flatpak, so we should detect whether we're
    running as a Snap when deciding whether to use portals or disable
    features that are impossible in these different but similar sandboxing
    technologies.
    
    This is mostly academic because the Snap of Epiphany appears to be
    outdated, but it is more correct so let's do it.
    
    Part-of: <https://gitlab.gnome.org/GNOME/epiphany/-/merge_requests/1041>

 embed/ephy-about-handler.c              |  2 +-
 embed/ephy-download.c                   |  4 ++--
 lib/ephy-file-helpers.c                 | 24 ++++++++++-----------
 lib/ephy-flatpak-utils.c                | 37 ++++++++++++++++++++++++---------
 lib/ephy-flatpak-utils.h                |  2 +-
 src/ephy-header-bar.c                   |  2 +-
 src/ephy-window.c                       |  6 +++---
 src/popup-commands.c                    |  2 +-
 src/preferences/prefs-appearance-page.c |  4 ++--
 src/preferences/prefs-general-page.c    |  2 +-
 src/window-commands.c                   |  4 ++--
 tests/ephy-file-helpers-test.c          |  8 +++----
 12 files changed, 57 insertions(+), 40 deletions(-)
---
diff --git a/embed/ephy-about-handler.c b/embed/ephy-about-handler.c
index fde68cb1f..4c794f526 100644
--- a/embed/ephy-about-handler.c
+++ b/embed/ephy-about-handler.c
@@ -558,7 +558,7 @@ ephy_about_handler_handle_request (EphyAboutHandler       *handler,
     handled = ephy_about_handler_handle_memory (handler, request);
   else if (!g_strcmp0 (path, "epiphany"))
     handled = ephy_about_handler_handle_epiphany (handler, request);
-  else if (!g_strcmp0 (path, "applications") && !ephy_is_running_inside_flatpak ())
+  else if (!g_strcmp0 (path, "applications") && !ephy_is_running_inside_sandbox ())
     handled = ephy_about_handler_handle_applications (handler, request);
   else if (!g_strcmp0 (path, "newtab"))
     handled = ephy_about_handler_handle_newtab (handler, request);
diff --git a/embed/ephy-download.c b/embed/ephy-download.c
index 6f982ead2..3705fe677 100644
--- a/embed/ephy-download.c
+++ b/embed/ephy-download.c
@@ -443,7 +443,7 @@ ephy_download_do_download_action (EphyDownload           *download,
   switch ((action ? action : download->action)) {
     case EPHY_DOWNLOAD_ACTION_BROWSE_TO:
       LOG ("ephy_download_do_download_action: browse_to");
-      /* Must not use this action type under flatpak! */
+      /* Must not use this action type under sandbox! */
       ret = ephy_file_browse_to (destination, user_time);
       break;
     case EPHY_DOWNLOAD_ACTION_OPEN:
@@ -945,7 +945,7 @@ ephy_download_new (WebKitDownload *download)
                            G_CALLBACK (download_decide_destination_cb),
                            ephy_download, 0);
 
-  if (!ephy_is_running_inside_flatpak () && g_settings_get_boolean (EPHY_SETTINGS_WEB, 
EPHY_PREFS_WEB_ASK_ON_DOWNLOAD)) {
+  if (!ephy_is_running_inside_sandbox () && g_settings_get_boolean (EPHY_SETTINGS_WEB, 
EPHY_PREFS_WEB_ASK_ON_DOWNLOAD)) {
     g_signal_connect (ephy_download, "filename-suggested",
                       G_CALLBACK (filename_suggested_cb),
                       NULL);
diff --git a/lib/ephy-file-helpers.c b/lib/ephy-file-helpers.c
index ea4eab061..d1ad88e26 100644
--- a/lib/ephy-file-helpers.c
+++ b/lib/ephy-file-helpers.c
@@ -124,7 +124,7 @@ ephy_file_download_dir (void)
  * Returns a proper downloads destination by checking the
  * EPHY_PREFS_STATE_DOWNLOAD_DIR GSettings key and following this logic:
  *
- *  - Under flatpak, always use the XDG downloads directory
+ *  - Under sandbox, always use the XDG downloads directory
  *
  *  - An absolute path: considered user-set, use this value directly.
  *
@@ -142,7 +142,7 @@ ephy_file_get_downloads_dir (void)
   g_autofree char *download_dir = g_settings_get_string (EPHY_SETTINGS_STATE,
                                                          EPHY_PREFS_STATE_DOWNLOAD_DIR);
 
-  if (ephy_is_running_inside_flatpak ())
+  if (ephy_is_running_inside_sandbox ())
     return ephy_file_download_dir ();
 
   if (g_strcmp0 (download_dir, "Desktop") == 0)
@@ -551,10 +551,10 @@ launch_application (GAppInfo *app,
   GdkScreen *screen;
   gboolean res;
 
-  /* This is impossible to implement inside flatpak. Higher layers must
+  /* This is impossible to implement inside sandbox. Higher layers must
    * ensure we don't get here.
    */
-  g_assert (!ephy_is_running_inside_flatpak ());
+  g_assert (!ephy_is_running_inside_sandbox ());
 
   display = gdk_display_get_default ();
   screen = gdk_screen_get_default ();
@@ -587,11 +587,11 @@ ephy_file_launch_desktop_file (const char                   *filename,
 {
   g_autoptr (GDesktopAppInfo) app = NULL;
 
-  /* This is impossible to implement inside flatpak. Higher layers must
+  /* This is impossible to implement inside sandbox. Higher layers must
    * ensure we don't get here.
    */
   g_assert (tag == EPHY_FILE_HELPERS_I_UNDERSTAND_I_MUST_NOT_USE_THIS_FUNCTION_UNDER_FLATPAK);
-  g_assert (!ephy_is_running_inside_flatpak ());
+  g_assert (!ephy_is_running_inside_sandbox ());
 
   app = g_desktop_app_info_new (filename);
 
@@ -641,11 +641,11 @@ ephy_file_launch_handler (GFile   *file,
 
   g_assert (file != NULL);
 
-  /* Launch via URI handler only under flatpak, because this way loses
+  /* Launch via URI handler only under sandbox, because this way loses
    * focus stealing prevention. There's no other way to open a file
-   * under flatpak, and focus stealing prevention becomes the
+   * under sandbox, and focus stealing prevention becomes the
    * responsibility of the portal in this case anyway. */
-  if (ephy_is_running_inside_flatpak ())
+  if (ephy_is_running_inside_sandbox ())
     return launch_via_uri_handler (file);
 
   app = g_file_query_default_handler (file, NULL, &error);
@@ -673,11 +673,11 @@ open_in_default_handler (const char                   *uri,
   g_autoptr (GError) error = NULL;
   GList uris;
 
-  /* This is impossible to implement inside flatpak. Higher layers must
+  /* This is impossible to implement inside sandbox. Higher layers must
    * ensure we don't get here.
    */
   g_assert (tag == EPHY_FILE_HELPERS_I_UNDERSTAND_I_MUST_NOT_USE_THIS_FUNCTION_UNDER_FLATPAK);
-  g_assert (!ephy_is_running_inside_flatpak ());
+  g_assert (!ephy_is_running_inside_sandbox ());
 
   context = gdk_display_get_app_launch_context (screen ? gdk_screen_get_display (screen) : 
gdk_display_get_default ());
   gdk_app_launch_context_set_screen (context, screen);
@@ -726,7 +726,7 @@ ephy_file_browse_to (GFile   *file,
 {
   g_autofree char *uri = g_file_get_uri (file);
 
-  if (ephy_is_running_inside_flatpak ()) {
+  if (ephy_is_running_inside_sandbox ()) {
     ephy_open_directory_via_flatpak_portal (uri);
     return TRUE;
   }
diff --git a/lib/ephy-flatpak-utils.c b/lib/ephy-flatpak-utils.c
index 818af95f4..6fc9a5dec 100644
--- a/lib/ephy-flatpak-utils.c
+++ b/lib/ephy-flatpak-utils.c
@@ -45,24 +45,41 @@ ephy_flatpak_utils_set_is_web_process_extension (void)
   is_web_process = TRUE;
 }
 
-gboolean
+static gboolean
 ephy_is_running_inside_flatpak (void)
 {
-  static _Thread_local gboolean decided = FALSE;
-  static _Thread_local gboolean under_flatpak = FALSE;
-
-  if (decided)
-    return under_flatpak;
-
   /* This function cannot be used in the web process extension, because WebKit
    * creates a .flatpak-info in its web process sandbox even when we are not
    * running under flatpak. It would always return TRUE.
    */
   g_assert (!is_web_process);
 
-  under_flatpak = g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS);
-  decided = TRUE;
-  return under_flatpak;
+  return g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS);
+}
+
+static gboolean
+ephy_is_running_inside_snap (void)
+{
+  /* The "SNAP" environment variable is not unlikely to be set for/by something other
+   * than Snap, so check a couple of additional variables to avoid false positives.
+   * See: https://snapcraft.io/docs/environment-variables
+   */
+  return g_getenv ("SNAP") && g_getenv ("SNAP_NAME") && g_getenv ("SNAP_REVISION");
+}
+
+static gpointer
+get_inside_sandbox (gpointer user_data)
+{
+  return GINT_TO_POINTER (ephy_is_running_inside_flatpak () || ephy_is_running_inside_snap ());
+}
+
+/* FIXME: Use https://github.com/flatpak/libportal/pull/63 */
+gboolean
+ephy_is_running_inside_sandbox (void)
+{
+  static GOnce inside_sandbox = G_ONCE_INIT;
+
+  return GPOINTER_TO_INT (g_once (&inside_sandbox, get_inside_sandbox, NULL));
 }
 
 #if USE_LIBPORTAL
diff --git a/lib/ephy-flatpak-utils.h b/lib/ephy-flatpak-utils.h
index 6ea9557bc..15b85ac9b 100644
--- a/lib/ephy-flatpak-utils.h
+++ b/lib/ephy-flatpak-utils.h
@@ -24,7 +24,7 @@
 
 void     ephy_flatpak_utils_set_is_web_process_extension (void);
 
-gboolean ephy_is_running_inside_flatpak                  (void);
+gboolean ephy_is_running_inside_sandbox                  (void);
 
 void     ephy_open_uri_via_flatpak_portal                (const char *uri);
 
diff --git a/src/ephy-header-bar.c b/src/ephy-header-bar.c
index 85ed3356c..285288189 100644
--- a/src/ephy-header-bar.c
+++ b/src/ephy-header-bar.c
@@ -308,7 +308,7 @@ ephy_header_bar_constructed (GObject *object)
     gtk_widget_destroy (GTK_WIDGET (gtk_builder_get_object (builder, "firefox-sync-separator")));
     gtk_widget_destroy (GTK_WIDGET (gtk_builder_get_object (builder, "firefox-sync-button")));
     gtk_widget_destroy (GTK_WIDGET (gtk_builder_get_object (builder, "import-export-menu")));
-  } else if (ephy_is_running_inside_flatpak ()) {
+  } else if (ephy_is_running_inside_sandbox ()) {
     gtk_widget_destroy (GTK_WIDGET (gtk_builder_get_object (builder, "run-in-background-separator")));
     gtk_widget_destroy (GTK_WIDGET (gtk_builder_get_object (builder, "run-in-background-button")));
     gtk_widget_destroy (GTK_WIDGET (gtk_builder_get_object (builder, "save-as-application-separator")));
diff --git a/src/ephy-window.c b/src/ephy-window.c
index 2866b76e5..ab8caedf5 100644
--- a/src/ephy-window.c
+++ b/src/ephy-window.c
@@ -1742,7 +1742,7 @@ populate_context_menu (WebKitWebView       *web_view,
       add_action_to_context_menu (context_menu, popup_action_group,
                                   "view-image", window);
 
-    if (!ephy_is_running_inside_flatpak ())
+    if (!ephy_is_running_inside_sandbox ())
       add_action_to_context_menu (context_menu, popup_action_group,
                                   "set-image-as-background", window);
   }
@@ -2126,7 +2126,7 @@ decide_navigation_policy (WebKitWebView            *web_view,
       if (ephy_web_application_is_uri_allowed (uri)) {
         gtk_widget_show (GTK_WIDGET (window));
       } else {
-        /* We can't get here under flatpak because this code only
+        /* We can't get here under sandbox because this code only
          * executes in web app mode.
          */
         ephy_file_open_uri_in_default_browser (uri, GDK_CURRENT_TIME,
@@ -2145,7 +2145,7 @@ decide_navigation_policy (WebKitWebView            *web_view,
       if (ephy_web_application_is_uri_allowed (uri))
         return accept_navigation_policy_decision (window, decision, uri);
 
-      /* We can't get here under flatpak because this code only
+      /* We can't get here under sandbox because this code only
        * executes in web app mode.
        */
       ephy_file_open_uri_in_default_browser (uri, GDK_CURRENT_TIME,
diff --git a/src/popup-commands.c b/src/popup-commands.c
index dc3ccfbbc..2f321a190 100644
--- a/src/popup-commands.c
+++ b/src/popup-commands.c
@@ -278,7 +278,7 @@ popup_cmd_set_image_as_background (GSimpleAction *action,
   char *dest_uri, *dest, *base, *base_converted;
   EphyDownload *download;
 
-  if (ephy_is_running_inside_flatpak ())
+  if (ephy_is_running_inside_sandbox ())
     return;
 
   hit_test_result = ephy_window_get_context_event (EPHY_WINDOW (user_data));
diff --git a/src/preferences/prefs-appearance-page.c b/src/preferences/prefs-appearance-page.c
index 42193af00..1f3a43aaf 100644
--- a/src/preferences/prefs-appearance-page.c
+++ b/src/preferences/prefs-appearance-page.c
@@ -194,7 +194,7 @@ css_file_created_cb (GObject      *source,
   if (stream == NULL && !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
     g_warning ("Failed to create %s: %s", g_file_get_path (file), error->message);
   else {
-    if (ephy_is_running_inside_flatpak ()) {
+    if (ephy_is_running_inside_sandbox ()) {
       g_autofree char *uri = g_file_get_uri (file);
       ephy_open_uri_via_flatpak_portal (uri);
     } else {
@@ -229,7 +229,7 @@ js_file_created_cb (GObject      *source,
   if (stream == NULL && !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
     g_warning ("Failed to create %s: %s", g_file_get_path (file), error->message);
   else {
-    if (ephy_is_running_inside_flatpak ()) {
+    if (ephy_is_running_inside_sandbox ()) {
       g_autofree char *uri = g_file_get_uri (file);
       ephy_open_uri_via_flatpak_portal (uri);
     } else {
diff --git a/src/preferences/prefs-general-page.c b/src/preferences/prefs-general-page.c
index 4bae44c72..dc4517f88 100644
--- a/src/preferences/prefs-general-page.c
+++ b/src/preferences/prefs-general-page.c
@@ -1211,7 +1211,7 @@ setup_general_page (PrefsGeneralPage *general_page)
   /* ======================================================================== */
   /* ========================== Downloads =================================== */
   /* ======================================================================== */
-  if (ephy_is_running_inside_flatpak ())
+  if (ephy_is_running_inside_sandbox ())
     gtk_widget_hide (general_page->download_box);
   else
     create_download_path_button (general_page);
diff --git a/src/window-commands.c b/src/window-commands.c
index d101f0d93..8b138257d 100644
--- a/src/window-commands.c
+++ b/src/window-commands.c
@@ -901,7 +901,7 @@ window_cmd_show_shortcuts (GSimpleAction *action,
     builder = gtk_builder_new_from_resource ("/org/gnome/epiphany/gtk/shortcuts-dialog.ui");
     shortcuts_window = GTK_WIDGET (gtk_builder_get_object (builder, "shortcuts-dialog"));
 
-    if (ephy_is_running_inside_flatpak ())
+    if (ephy_is_running_inside_sandbox ())
       gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (builder, "shortcuts-web-apps-group")));
 
     if (gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL) {
@@ -1905,7 +1905,7 @@ window_cmd_save_as_application (GSimpleAction *action,
   char *markup;
   char *escaped_address;
 
-  if (ephy_is_running_inside_flatpak ())
+  if (ephy_is_running_inside_sandbox ())
     return;
 
   embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (window));
diff --git a/tests/ephy-file-helpers-test.c b/tests/ephy-file-helpers-test.c
index b3139b67e..a591e2b58 100644
--- a/tests/ephy-file-helpers-test.c
+++ b/tests/ephy-file-helpers-test.c
@@ -102,7 +102,7 @@ typedef struct {
   const char *key_value;
   const char *expected;
   GUserDirectory user_dir;
-  gboolean in_flatpak;
+  gboolean in_sandbox;
 } DownloadsDirTest;
 
 static const DownloadsDirTest downloads_tests[] = {
@@ -118,7 +118,7 @@ static void
 test_ephy_file_get_downloads_dir (void)
 {
   guint i;
-  gboolean in_flatpak = ephy_is_running_inside_flatpak ();
+  gboolean in_sandbox = ephy_is_running_inside_sandbox ();
 
   ephy_file_helpers_init (NULL, EPHY_FILE_HELPERS_PRIVATE_PROFILE, NULL);
 
@@ -129,7 +129,7 @@ test_ephy_file_get_downloads_dir (void)
 
     test = downloads_tests[i];
 
-    if (in_flatpak && !test.in_flatpak)
+    if (in_sandbox && !test.in_sandbox)
       continue;
 
     if (test.expected != NULL)
@@ -348,7 +348,7 @@ main (int   argc,
   g_test_add_func ("/lib/ephy-file-helpers/get_downloads_dir",
                    test_ephy_file_get_downloads_dir);
 
-  if (!ephy_is_running_inside_flatpak ())
+  if (!ephy_is_running_inside_sandbox ())
     g_test_add_func ("/lib/ephy-file-helpers/create_delete_dir",
                      test_ephy_file_create_delete_dir);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]