From 25e9268171a1e851dd891c5f026bc5475db8766a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20I=C3=B1iguez=20Goia?= Date: Wed, 18 Sep 2024 01:13:33 +0200 Subject: [PATCH] ui: restrict allowed characters in the rule name Since the name of the rule is used for the file name on the disk, certain characters caused issues when saving the rule, like '/'. Now if the user types or pastes '/' in the name field, a warning is displayed, indicating that some characters are not allowed. (2e90f3832d7a7d45d5eb66c697c8150c4489a3e4) --- ui/opensnitch/dialogs/ruleseditor.py | 24 +++++++++++++++++++++++- ui/opensnitch/utils/qvalidator.py | 25 +++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 ui/opensnitch/utils/qvalidator.py diff --git a/ui/opensnitch/dialogs/ruleseditor.py b/ui/opensnitch/dialogs/ruleseditor.py index b35a048e42..9c513a8c8f 100644 --- a/ui/opensnitch/dialogs/ruleseditor.py +++ b/ui/opensnitch/dialogs/ruleseditor.py @@ -16,7 +16,13 @@ from opensnitch.database import Database from opensnitch.database.enums import RuleFields, ConnFields from opensnitch.version import version -from opensnitch.utils import Message, FileDialog, Icons, NetworkInterfaces +from opensnitch.utils import ( + Message, + FileDialog, + Icons, + NetworkInterfaces, + qvalidator +) from opensnitch.rules import Rule, Rules DIALOG_UI_PATH = "%s/../res/ruleseditor.ui" % os.path.dirname(sys.modules[__name__].__file__) @@ -33,6 +39,8 @@ class RulesEditorDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]): LAN_LABEL = "LAN" MULTICAST_LABEL = "MULTICAST" + INVALID_RULE_NAME_CHARS = '/' + ADD_RULE = 0 EDIT_RULE = 1 WORK_MODE = ADD_RULE @@ -55,6 +63,10 @@ def __init__(self, parent=None, _rule=None, appicon=None): self.setupUi(self) self.setWindowIcon(appicon) + self.ruleNameValidator = qvalidator.RestrictChars(RulesEditorDialog.INVALID_RULE_NAME_CHARS) + self.ruleNameValidator.result.connect(self._cb_rule_name_validator_result) + self.ruleNameEdit.setValidator(self.ruleNameValidator) + self.buttonBox.setStandardButtons( QtWidgets.QDialogButtonBox.Help | QtWidgets.QDialogButtonBox.Reset | @@ -138,6 +150,16 @@ def showEvent(self, event): def _bool(self, s): return s == 'True' + def _cb_rule_name_validator_result(self, result): + if result == QtGui.QValidator.Invalid: + self._set_status_error( + QC.translate("rules", + "Invalid rule name (not allowed characters: '{0}' )".format(RulesEditorDialog.INVALID_RULE_NAME_CHARS) + ) + ) + else: + self._set_status_message("") + def _cb_accept_clicked(self): pass diff --git a/ui/opensnitch/utils/qvalidator.py b/ui/opensnitch/utils/qvalidator.py new file mode 100644 index 0000000000..881d4e6daf --- /dev/null +++ b/ui/opensnitch/utils/qvalidator.py @@ -0,0 +1,25 @@ + +from PyQt5 import QtCore, QtGui + +class RestrictChars(QtGui.QValidator): + result = QtCore.pyqtSignal(object) + + def __init__(self, restricted_chars, *args, **kwargs): + QtGui.QValidator.__init__(self, *args, **kwargs) + self._restricted_chars = restricted_chars + + def validate(self, value, pos): + # allow to delete all characters + if len(value) == 0: + return QtGui.QValidator.Intermediate, value, pos + + # user can type characters or paste them. + # pos value when pasting can be any number, depending on where did the + # user paste the characters. + for char in self._restricted_chars: + if char in value: + self.result.emit(QtGui.QValidator.Invalid) + return QtGui.QValidator.Invalid, value, pos + + self.result.emit(QtGui.QValidator.Acceptable) + return QtGui.QValidator.Acceptable, value, pos