Skip to content

Commit ad937ca

Browse files
committed
ui: fixed false name conflict after changing a rule's action
- When the action of an auto-named rule was changed (e.g. reject to allow), the rule was renamed (reject-xxx -> allow-xxx), but the editor kept tracking the old name. Saving the rule again then wrongly reported "There's already a rule with this name", as it compared the renamed rule against itself. - Track the saved rule name instead of the one typed in the field, and stop mutating the rule object while deleting the old name. - Added a regression test. ref: #1291
1 parent 3655b23 commit ad937ca

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

ui/opensnitch/dialogs/ruleseditor/dialog.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,11 @@ def cb_save_clicked(self):
271271
if self._old_rule_name is not None and self._old_rule_name != self.rule.name:
272272
self.delete_rule()
273273

274-
self._old_rule_name = rule_name
274+
# use the saved rule name, not the one typed in the field: save_rule()
275+
# may rename the rule (e.g. when the action of an auto-named rule
276+
# changes). Otherwise _old_rule_name would lag behind and the next
277+
# save would wrongly report a name conflict with the just saved rule.
278+
self._old_rule_name = self.rule.name
275279

276280
# after adding a new rule, we enter into EDIT mode, to allow further
277281
# changes without closing the dialog.
@@ -388,8 +392,6 @@ def delete_rule(self):
388392
# if the rule name has changed, we need to remove the old one
389393
if self._old_rule_name != self.rule.name:
390394
node = nodes.get_node_addr(self)
391-
old_rule = self.rule
392-
old_rule.name = self._old_rule_name
393395
if self.nodeApplyAllCheck.isChecked():
394396
nid, noti = self._nodes.delete_rule(rule_name=self._old_rule_name, addr=None, callback=self._notification_callback)
395397
self.notifications_sent[nid] = noti

ui/tests/dialogs/test_ruleseditor.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,47 @@ def handle_dialog():
286286
records = self.rd._db.get_rule("www.test-renamed.com", node_addr)
287287
assert records.next() == True
288288

289+
def test_change_action_of_autonamed_rule(self, qtbot):
290+
""" Regression: changing the action of an auto-named rule
291+
(reject-xxx -> allow-xxx) renames it. The name tracking lagged
292+
behind, so saving the rule a second time wrongly reported a name
293+
conflict with the just renamed rule.
294+
"""
295+
qtbot.addWidget(self.rd)
296+
node = re_nodes.get_node_addr(self.rd)
297+
# start from a clean state for the names used here
298+
self.rd._db.delete_rule("reject-rename-host", node)
299+
self.rd._db.delete_rule("allow-rename-host", node)
300+
301+
re_constants.WORK_MODE = re_constants.ADD_RULE
302+
re_utils.reset_state(self.rd)
303+
self.rd.statusLabel.setText("")
304+
self.rd.ruleNameEdit.setText("reject-rename-host")
305+
self.rd.dstHostCheck.setChecked(True)
306+
self.rd.dstHostLine.setText("rename.example.com")
307+
self.rd.actionRejectRadio.setChecked(True)
308+
309+
qtbot.mouseClick(self.rd.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Save), QtCore.Qt.MouseButton.LeftButton)
310+
assert self.rd.statusLabel.text() == ""
311+
assert self.rd._old_rule_name == "reject-rename-host"
312+
313+
# change the action: the rule gets renamed to allow-rename-host
314+
self.rd.actionAllowRadio.setChecked(True)
315+
qtbot.mouseClick(self.rd.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Save), QtCore.Qt.MouseButton.LeftButton)
316+
317+
assert self.rd.statusLabel.text() == ""
318+
assert self.rd.ruleNameEdit.text() == "allow-rename-host"
319+
# name tracking must follow the rename, not lag on the old name
320+
assert self.rd._old_rule_name == "allow-rename-host"
321+
assert self.rd.rule.name == "allow-rename-host"
322+
assert self.rd._db.get_rule("reject-rename-host", node).next() == False
323+
assert self.rd._db.get_rule("allow-rename-host", node).next() == True
324+
325+
# saving again must not report a name conflict with itself
326+
qtbot.mouseClick(self.rd.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Save), QtCore.Qt.MouseButton.LeftButton)
327+
assert self.rd.statusLabel.text() == ""
328+
assert self.rd._db.get_rule("allow-rename-host", node).next() == True
329+
289330
def test_durations(self, qtbot):
290331
""" Test adding new rule with action "deny".
291332
"""

0 commit comments

Comments
 (0)