Skip to content

Commit

Permalink
ui: restrict allowed characters in the rule name
Browse files Browse the repository at this point in the history
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.

(2e90f38)
  • Loading branch information
gustavo-iniguez-goya committed Sep 17, 2024
1 parent 9e660e1 commit 25e9268
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
24 changes: 23 additions & 1 deletion ui/opensnitch/dialogs/ruleseditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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
Expand All @@ -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 |
Expand Down Expand Up @@ -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

Expand Down
25 changes: 25 additions & 0 deletions ui/opensnitch/utils/qvalidator.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 25e9268

Please # to comment.