Skip to content

Commit 25e9268

Browse files
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. (2e90f38)
1 parent 9e660e1 commit 25e9268

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

ui/opensnitch/dialogs/ruleseditor.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
from opensnitch.database import Database
1717
from opensnitch.database.enums import RuleFields, ConnFields
1818
from opensnitch.version import version
19-
from opensnitch.utils import Message, FileDialog, Icons, NetworkInterfaces
19+
from opensnitch.utils import (
20+
Message,
21+
FileDialog,
22+
Icons,
23+
NetworkInterfaces,
24+
qvalidator
25+
)
2026
from opensnitch.rules import Rule, Rules
2127

2228
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]):
3339
LAN_LABEL = "LAN"
3440
MULTICAST_LABEL = "MULTICAST"
3541

42+
INVALID_RULE_NAME_CHARS = '/'
43+
3644
ADD_RULE = 0
3745
EDIT_RULE = 1
3846
WORK_MODE = ADD_RULE
@@ -55,6 +63,10 @@ def __init__(self, parent=None, _rule=None, appicon=None):
5563
self.setupUi(self)
5664
self.setWindowIcon(appicon)
5765

66+
self.ruleNameValidator = qvalidator.RestrictChars(RulesEditorDialog.INVALID_RULE_NAME_CHARS)
67+
self.ruleNameValidator.result.connect(self._cb_rule_name_validator_result)
68+
self.ruleNameEdit.setValidator(self.ruleNameValidator)
69+
5870
self.buttonBox.setStandardButtons(
5971
QtWidgets.QDialogButtonBox.Help |
6072
QtWidgets.QDialogButtonBox.Reset |
@@ -138,6 +150,16 @@ def showEvent(self, event):
138150
def _bool(self, s):
139151
return s == 'True'
140152

153+
def _cb_rule_name_validator_result(self, result):
154+
if result == QtGui.QValidator.Invalid:
155+
self._set_status_error(
156+
QC.translate("rules",
157+
"Invalid rule name (not allowed characters: '{0}' )".format(RulesEditorDialog.INVALID_RULE_NAME_CHARS)
158+
)
159+
)
160+
else:
161+
self._set_status_message("")
162+
141163
def _cb_accept_clicked(self):
142164
pass
143165

ui/opensnitch/utils/qvalidator.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
from PyQt5 import QtCore, QtGui
3+
4+
class RestrictChars(QtGui.QValidator):
5+
result = QtCore.pyqtSignal(object)
6+
7+
def __init__(self, restricted_chars, *args, **kwargs):
8+
QtGui.QValidator.__init__(self, *args, **kwargs)
9+
self._restricted_chars = restricted_chars
10+
11+
def validate(self, value, pos):
12+
# allow to delete all characters
13+
if len(value) == 0:
14+
return QtGui.QValidator.Intermediate, value, pos
15+
16+
# user can type characters or paste them.
17+
# pos value when pasting can be any number, depending on where did the
18+
# user paste the characters.
19+
for char in self._restricted_chars:
20+
if char in value:
21+
self.result.emit(QtGui.QValidator.Invalid)
22+
return QtGui.QValidator.Invalid, value, pos
23+
24+
self.result.emit(QtGui.QValidator.Acceptable)
25+
return QtGui.QValidator.Acceptable, value, pos

0 commit comments

Comments
 (0)