Skip to content

Commit

Permalink
Check for unsafe drop (#794)
Browse files Browse the repository at this point in the history
* Combine confirm paste and confirm drop

* Handle unsafe paste alert off/ignored

* Revert inadvertent change to text stripping

* Lose unnecessary single-use var

* Lose unnecessary return

---------

Co-authored-by: Jeremy Wootten <[email protected]>
Co-authored-by: Ryo Nakano <[email protected]>
  • Loading branch information
3 people authored Nov 7, 2024
1 parent 4d593d9 commit 28fd418
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions src/Widgets/TerminalWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -519,46 +519,39 @@ namespace Terminal {

protected override void paste_clipboard () {
clipboard.request_text ((clipboard, text) => {
if (text == null) {
return;
}

if (!text.validate ()) {
warning ("Dropping invalid UTF-8 paste");
return;
}
validated_feed (text);
});
}

// Check pasted and dropped text before feeding to child;
private void validated_feed (string? text) {
if (text != null && text.validate ()) {
unowned var toplevel = (MainWindow) get_toplevel ();
if (!toplevel.unsafe_ignored &&
Application.settings.get_boolean ("unsafe-paste-alert")) {

if (!toplevel.unsafe_ignored && Application.settings.get_boolean ("unsafe-paste-alert")) {
string? warn_text = null;
text._strip ();

if ("\n" in text) {
warn_text = _("The pasted text may contain multiple commands");
} else if ("sudo" in text || "doas" in text) {
warn_text = _("The pasted text may be trying to gain administrative access");
}

if (warn_text != null) {
var dialog = new UnsafePasteDialog (toplevel, warn_text, text);
var dialog = new UnsafePasteDialog (toplevel, warn_text, text.strip ());
dialog.response.connect ((res) => {
dialog.destroy ();
if (res == Gtk.ResponseType.ACCEPT) {
remember_command_start_position ();
base.paste_clipboard ();
feed_child (text.data);
}

dialog.destroy ();
});

dialog.present ();
return;
}
} else {
feed_child (text.data);
}

remember_command_start_position ();
base.paste_clipboard ();
});
}
}

private void update_theme () {
Expand Down Expand Up @@ -804,11 +797,8 @@ namespace Terminal {

case DropTargets.STRING:
case DropTargets.TEXT:
var data = selection_data.get_text ();
if (data != null) {
this.feed_child (data.data);
}

var text = selection_data.get_text ();
validated_feed (text);
break;
}
}
Expand Down

0 comments on commit 28fd418

Please sign in to comment.