Skip to content

Commit

Permalink
Clarify comment about order of operations in filter.AddRule; improve …
Browse files Browse the repository at this point in the history
…logging for JS rule matches
  • Loading branch information
anfragment committed Dec 18, 2024
1 parent 91e92a2 commit 5d0505d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions internal/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,18 @@ func (f *Filter) ParseAndAddRules(reader io.Reader, filterListName *string, filt

// AddRule adds a new rule to the filter. It returns true if the rule is an exception, false otherwise.
func (f *Filter) AddRule(rule string, filterListName *string, filterListTrusted bool) (isException bool, err error) {
/*
The order of operations is crucial here.
jsRule.RuleRegex also matches scriptlet rules.
Therefore, we must first check for a scriptlet rule match before checking for a JS rule match.
*/
if scriptletRegex.MatchString(rule) {
if err := f.scriptletsInjector.AddRule(rule, filterListTrusted); err != nil {
return false, fmt.Errorf("add scriptlet: %w", err)
}
return false, nil
}
if filterListTrusted && jsrule.RuleRegex.MatchString(rule) {
// The order of operations is crucial here.
// RuleRegex in jsrule also matches scriptlet rules.
// Therefore, we must first check for a scriptlet rule match before testing for a jsrule match.
if err := f.jsRuleInjector.AddRule(rule); err != nil {
return false, fmt.Errorf("add js rule: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/jsrule/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (inj *Injector) AddRule(rule string) error {
func (inj *Injector) Inject(req *http.Request, res *http.Response) error {
hostname := req.URL.Hostname()
scripts := inj.store.Get(hostname)
log.Printf("got %d scripts for %q", len(scripts), hostname)
log.Printf("got %d js rules for %q", len(scripts), hostname)
if len(scripts) == 0 {
return nil
}
Expand Down

0 comments on commit 5d0505d

Please sign in to comment.