[Xfce4-commits] [apps/xfce4-terminal] 01/01: Initial version of the "Unsafe Paste" dialog

noreply at xfce.org noreply at xfce.org
Mon Mar 4 00:13:07 CET 2019


This is an automated email from the git hooks/post-receive script.

f   2   4   0   4       p   u   s   h   e   d       a       c   o   m   m   i   t       t   o       b   r   a   n   c   h       m   a   s   t   e   r   
   in repository apps/xfce4-terminal.

commit 86d588d51da4374e404c4df04332c4fb5ae9a4a2
Author: Igor <f2404 at yandex.ru>
Date:   Sun Mar 3 18:08:08 2019 -0500

    Initial version of the "Unsafe Paste" dialog
    
    Bug #13252
---
 terminal/terminal-preferences.c |  11 ++++
 terminal/terminal-screen.c      | 110 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 119 insertions(+), 2 deletions(-)

diff --git a/terminal/terminal-preferences.c b/terminal/terminal-preferences.c
index b0d1f08..ce07969 100644
--- a/terminal/terminal-preferences.c
+++ b/terminal/terminal-preferences.c
@@ -115,6 +115,7 @@ enum
   PROP_MISC_NEW_TAB_ADJACENT,
   PROP_MISC_SEARCH_DIALOG_OPACITY,
   PROP_MISC_USE_TAB_KEY_TO_CYCLE_TABS,
+  PROP_MISC_SHOW_UNSAFE_PASTE_DIALOG,
   PROP_SCROLLING_BAR,
   PROP_SCROLLING_LINES,
   PROP_SCROLLING_ON_OUTPUT,
@@ -1081,6 +1082,16 @@ terminal_preferences_class_init (TerminalPreferencesClass *klass)
                             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
 
   /**
+   * TerminalPreferences:misc-show-unsafe-paste-dialog:
+   **/
+  preferences_props[PROP_MISC_SHOW_UNSAFE_PASTE_DIALOG] =
+      g_param_spec_boolean ("misc-show-unsafe-paste-dialog",
+                            NULL,
+                            "MiscShowUnsafePasteDialog",
+                            TRUE,
+                            G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
+  /**
    * TerminalPreferences:scrolling-bar:
    **/
   preferences_props[PROP_SCROLLING_BAR] =
diff --git a/terminal/terminal-screen.c b/terminal/terminal-screen.c
index 0be37e7..6bc3532 100644
--- a/terminal/terminal-screen.c
+++ b/terminal/terminal-screen.c
@@ -160,6 +160,10 @@ static void       terminal_screen_set_custom_command            (TerminalScreen
                                                                  gchar                **command);
 static void       terminal_screen_set_tab_label_color           (TerminalScreen        *screen,
                                                                  const GdkRGBA         *color);
+static GtkWidget* terminal_screen_unsafe_paste_dialog_new       (TerminalScreen        *screen,
+                                                                 const gchar           *text);
+static void       terminal_screen_paste_unsafe_text             (TerminalScreen        *screen,
+                                                                 const gchar           *text);
 
 
 
@@ -1761,6 +1765,86 @@ terminal_screen_set_tab_label_color (TerminalScreen *screen,
 
 
 
+gboolean
+terminal_screen_is_text_unsafe (const gchar *text)
+{
+  return text != NULL && (strstr (text, "sudo") != NULL || strchr (text, '\n') != NULL);
+}
+
+
+
+static GtkWidget*
+terminal_screen_unsafe_paste_dialog_new (TerminalScreen *screen,
+                                         const gchar    *text)
+{
+  GtkWindow     *parent = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (screen)));
+  GtkTextBuffer *buffer = gtk_text_buffer_new (gtk_text_tag_table_new ());
+  GtkWidget     *tv = gtk_text_view_new_with_buffer (buffer);
+  GtkWidget     *sw = gtk_scrolled_window_new (NULL, NULL);
+  GtkWidget     *dialog = xfce_titled_dialog_new_with_buttons (_("Warning: Unsafe Paste"), parent,
+                                                               GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                               _("_Cancel"), GTK_RESPONSE_CANCEL,
+                                                               _("_Paste"), GTK_RESPONSE_YES,
+                                                               NULL);
+
+  xfce_titled_dialog_set_subtitle (XFCE_TITLED_DIALOG (dialog),
+                                   _("Pasting this text to the terminal may be dangerous as it looks like\n"
+                                     "some commands may be executed, potentially involving root access ('sudo')."));
+
+  gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (tv), TRUE);
+  gtk_text_view_set_monospace (GTK_TEXT_VIEW (tv), TRUE);
+  gtk_text_view_set_top_margin (GTK_TEXT_VIEW (tv), 6);
+  gtk_text_view_set_bottom_margin (GTK_TEXT_VIEW (tv), 6);
+  gtk_text_view_set_right_margin (GTK_TEXT_VIEW (tv), 6);
+  gtk_text_view_set_left_margin (GTK_TEXT_VIEW (tv), 6);
+
+  gtk_scrolled_window_set_min_content_width (GTK_SCROLLED_WINDOW (sw), 400);
+  gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (sw), 150);
+
+  gtk_container_add (GTK_CONTAINER (sw), tv);
+  gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), sw);
+
+  gtk_text_buffer_set_text (buffer, text, -1);
+
+  gtk_window_set_focus (GTK_WINDOW (dialog), tv);
+
+  return dialog;
+}
+
+
+
+static void
+terminal_screen_paste_unsafe_text (TerminalScreen *screen,
+                                   const gchar    *text)
+{
+  GtkWidget *dialog = terminal_screen_unsafe_paste_dialog_new (screen, text);
+
+  gtk_widget_show_all (dialog);
+
+  if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
+    {
+      GtkWidget     *content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
+      GtkWidget     *sw = g_list_first (gtk_container_get_children (GTK_CONTAINER (content_area)))->data;
+      GtkTextView   *tv = GTK_TEXT_VIEW (gtk_bin_get_child (GTK_BIN (sw)));
+      GtkTextBuffer *buffer = gtk_text_view_get_buffer (tv);
+      GtkTextIter    start, end;
+      char          *res_text;
+
+      gtk_text_buffer_get_bounds (buffer, &start, &end);
+      res_text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+
+      if (res_text != NULL)
+        {
+          vte_terminal_feed_child (VTE_TERMINAL (screen->terminal), res_text, strlen (res_text));
+          g_free (res_text);
+        }
+    }
+
+  gtk_widget_destroy (dialog);
+}
+
+
+
 #if VTE_CHECK_VERSION (0, 48, 0)
 static void
 terminal_screen_spawn_async_cb (VteTerminal *terminal,
@@ -2384,8 +2468,19 @@ terminal_screen_copy_clipboard_html (TerminalScreen *screen)
 void
 terminal_screen_paste_clipboard (TerminalScreen *screen)
 {
+  gboolean  show_dialog;
+  gchar    *text = gtk_clipboard_wait_for_text (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD));
+
   terminal_return_if_fail (TERMINAL_IS_SCREEN (screen));
-  vte_terminal_paste_clipboard (VTE_TERMINAL (screen->terminal));
+
+  g_object_get (G_OBJECT (screen->preferences), "misc-show-unsafe-paste-dialog", &show_dialog, NULL);
+
+  if (show_dialog && terminal_screen_is_text_unsafe (text))
+    terminal_screen_paste_unsafe_text (screen, text);
+  else
+    vte_terminal_paste_clipboard (VTE_TERMINAL (screen->terminal));
+
+  g_free (text);
 }
 
 
@@ -2402,8 +2497,19 @@ terminal_screen_paste_clipboard (TerminalScreen *screen)
 void
 terminal_screen_paste_primary (TerminalScreen *screen)
 {
+  gboolean  show_dialog;
+  gchar    *text = gtk_clipboard_wait_for_text (gtk_clipboard_get (GDK_SELECTION_PRIMARY));
+
   terminal_return_if_fail (TERMINAL_IS_SCREEN (screen));
-  vte_terminal_paste_primary (VTE_TERMINAL (screen->terminal));
+
+  g_object_get (G_OBJECT (screen->preferences), "misc-show-unsafe-paste-dialog", &show_dialog, NULL);
+
+  if (show_dialog && terminal_screen_is_text_unsafe (text))
+    terminal_screen_paste_unsafe_text (screen, text);
+  else
+    vte_terminal_paste_primary (VTE_TERMINAL (screen->terminal));
+
+  g_free (text);
 }
 
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list