Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for unsafe drop #794

Merged
merged 11 commits into from
Nov 7, 2024
42 changes: 17 additions & 25 deletions src/Widgets/TerminalWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -519,46 +519,41 @@ 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 ()) {
var stripped_text = text.strip ();
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 ();
ryonakano marked this conversation as resolved.
Show resolved Hide resolved

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, stripped_text);
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;
ryonakano marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
feed_child (text.data);
}

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

private void update_theme () {
Expand Down Expand Up @@ -804,11 +799,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