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

Tests: add unsafe paste test #818

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions po/POTFILES
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
src/Application.vala
src/MainWindow.vala
src/Utils.vala
src/Dialogs/ColorPreferencesDialog.vala
src/Dialogs/ForegroundProcessDialog.vala
src/Dialogs/UnsafePasteDialog.vala
Expand Down
24 changes: 24 additions & 0 deletions src/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,30 @@ namespace Terminal.Utils {
return uri;
}

/**
* Checks a string for possible unsafe contents before pasting
*
* @param clipboard contents containing terminal commands
*
* @param return localized explanation of risk
*
* @return true if safe, false if unsafe.
*/
public bool is_safe_paste (string text, out string msg) {
if ("\n" in text || "&" in text || "|" in text || ";" in text ) {
msg = _("The pasted text may contain multiple commands");
return false;
}

if ("sudo " in text || "doas " in text || "run0 " in text || "pkexec " in text || "su " in text) {
msg = _("The pasted text may be trying to gain administrative access");
return false;
}

msg = null;
return true;
}

public string? escape_uri (string uri, bool allow_utf8 = true, bool allow_single_quote = true) {
// We only want to allow '#' in appropriate position for fragment identifier, i.e. after the last directory separator.
var placeholder = "::::::";
Expand Down
9 changes: 2 additions & 7 deletions src/Widgets/TerminalWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -571,15 +571,10 @@ namespace Terminal {
}


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

// No user interaction for safe commands
if (warn_text == null) {
if (Utils.is_safe_paste (text, out warn_text)) {
feed_child (text.data);
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ test(
protocol: 'tap',
depends: test_schemas
)

subdir('tests')
34 changes: 34 additions & 0 deletions src/tests/UnsafePasteTest.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2024 elementary, Inc. (https://elementary.io)
*/

private void main (string[] args) {
Test.init (ref args);

Test.add_func ("/valid", () => {
string msg;

// false positive for su
assert (Terminal.Utils.is_safe_paste ("suspend", out msg));
});

Test.add_func ("/invalid", () => {
string msg;

// Elevated permissions
assert (!Terminal.Utils.is_safe_paste ("doas pacman -Syu", out msg));
assert (!Terminal.Utils.is_safe_paste ("pkexec visudo", out msg));
assert (!Terminal.Utils.is_safe_paste ("run0 --nice=19 my-task-with-low-priority", out msg));
assert (!Terminal.Utils.is_safe_paste ("su username", out msg));
assert (!Terminal.Utils.is_safe_paste ("sudo apt autoremove", out msg));

// Multi-line commands
assert (!Terminal.Utils.is_safe_paste ("\n", out msg));
assert (!Terminal.Utils.is_safe_paste ("&", out msg));
assert (!Terminal.Utils.is_safe_paste ("|", out msg));
assert (!Terminal.Utils.is_safe_paste (";", out msg));
});

Test.run ();
}
12 changes: 12 additions & 0 deletions src/tests/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
unsafe_paste_test = executable(
'UnsafePasteTest',
'UnsafePasteTest.vala',
meson.project_source_root() / 'src' / 'Utils.vala',
dependencies: [
glib_dep,
gtk_dep
],
install: false
)

test('UnsafePaste Test', unsafe_paste_test)
Loading