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

TerminalWidget: Refactor validated_feed to avoid nested conditions #804

Merged
merged 5 commits into from
Nov 10, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions src/Widgets/TerminalWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -525,33 +525,44 @@ namespace Terminal {

// 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")) {

string? warn_text = null;
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");
}
// Do nothing when text is invalid
if (text == null || !text.validate ()) {
return;
}

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

dialog.present ();
}
} else {
// No user interaction because of user's preference or previous selection
if (toplevel.unsafe_ignored ||
!Application.settings.get_boolean ("unsafe-paste-alert")) {

feed_child (text.data);
return;
}

string? warn_text = null;
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");
}

// No user interaction for safe commands
if (warn_text != null) {
ryonakano marked this conversation as resolved.
Show resolved Hide resolved
feed_child (text.data);
return;
}

// Ask user for interaction for unsafe commands
var dialog = new UnsafePasteDialog (toplevel, warn_text, text.strip ());
dialog.response.connect ((res) => {
dialog.destroy ();
if (res == Gtk.ResponseType.ACCEPT) {
feed_child (text.data);
}
}
});

dialog.present ();
}

private void update_theme () {
Expand Down