Skip to content

Commit

Permalink
i/p/requestrules: ignore internally duplicate pattern variants when a…
Browse files Browse the repository at this point in the history
…dding rules

Signed-off-by: Oliver Calder <[email protected]>
  • Loading branch information
olivercalder committed Sep 20, 2024
1 parent 7a93e38 commit 09a6c16
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions interfaces/prompting/requestrules/requestrules.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,9 @@ func (rdb *RuleDB) addRulePermissionToTree(rule *Rule, permission string) []prom
switch {
case !exists:
newVariantEntries[variantStr] = newEntry
case conflictingVariantEntry.RuleID == rule.ID:
// Rule has duplicate variant, so ignore it
return

Check warning on line 466 in interfaces/prompting/requestrules/requestrules.go

View check run for this annotation

Codecov / codecov/patch

interfaces/prompting/requestrules/requestrules.go#L464-L466

Added lines #L464 - L466 were not covered by tests
case rdb.isRuleWithIDExpired(conflictingVariantEntry.RuleID, rule.Timestamp):
expiredRules[conflictingVariantEntry.RuleID] = true
newVariantEntries[variantStr] = newEntry
Expand Down
24 changes: 24 additions & 0 deletions interfaces/prompting/requestrules/requestrules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,30 @@ func addRuleFromTemplate(c *C, rdb *requestrules.RuleDB, template *addRuleConten
return rdb.AddRule(partial.User, partial.Snap, partial.Interface, constraints, partial.Outcome, partial.Lifespan, partial.Duration)
}

func (s *requestrulesSuite) TestAddRuleDuplicateVariants(c *C) {
rdb, err := requestrules.New(s.defaultNotifyRule)
c.Assert(err, IsNil)

ruleContents := &addRuleContents{
User: s.defaultUser,
Snap: "nextcloud",
Interface: "home",
PathPattern: "/home/test/{{foo/{bar,baz},123},{123,foo{/bar,/baz}}}",
Permissions: []string{"read"},
Outcome: prompting.OutcomeAllow,
Lifespan: prompting.LifespanForever,
Duration: "",
}

var addedRules []*requestrules.Rule
rule, err := addRuleFromTemplate(c, rdb, ruleContents, ruleContents)
c.Check(err, IsNil)
c.Check(rule, NotNil)
addedRules = append(addedRules, rule)
s.checkWrittenRuleDB(c, addedRules)
s.checkNewNoticesSimple(c, nil, rule)
}

func (s *requestrulesSuite) TestAddRuleErrors(c *C) {
rdb, err := requestrules.New(s.defaultNotifyRule)
c.Assert(err, IsNil)
Expand Down

0 comments on commit 09a6c16

Please sign in to comment.