-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
453 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2023 The casbin Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package object | ||
|
||
import ( | ||
"github.com/casbin/caswaf/util" | ||
) | ||
|
||
var ruleMap = map[string]*Rule{} | ||
|
||
func InitRuleMap() { | ||
err := refreshRuleMap() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func refreshRuleMap() error { | ||
newRuleMap := map[string]*Rule{} | ||
rules, err := GetGlobalRules() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, rule := range rules { | ||
newRuleMap[util.GetIdFromOwnerAndName(rule.Owner, rule.Name)] = rule | ||
} | ||
|
||
ruleMap = newRuleMap | ||
return nil | ||
} | ||
|
||
func GetRulesByRuleIds(ids []string) []*Rule { | ||
var res []*Rule | ||
for _, id := range ids { | ||
if rule, ok := ruleMap[id]; ok { | ||
res = append(res, rule) | ||
} | ||
} | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package service | ||
|
||
import ( | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/casbin/caswaf/object" | ||
) | ||
|
||
type UaRule struct { | ||
Rule object.Rule | ||
check CheckRule | ||
} | ||
|
||
type CheckRule interface { | ||
checkRule(expressions []*object.Expression, req *http.Request) (string, string, error) | ||
} | ||
|
||
func (r *UaRule) checkRule(expressions []*object.Expression, req *http.Request) (string, string, error) { | ||
userAgent := req.UserAgent() | ||
for _, expression := range expressions { | ||
ua := expression.Value | ||
switch expression.Operator { | ||
case "contains": | ||
if strings.Contains(userAgent, ua) { | ||
return r.Rule.Action, r.Rule.Reason, nil | ||
} | ||
case "does not contain": | ||
if !strings.Contains(userAgent, ua) { | ||
return r.Rule.Action, r.Rule.Reason, nil | ||
} | ||
case "equals": | ||
if userAgent == ua { | ||
return r.Rule.Action, r.Rule.Reason, nil | ||
} | ||
case "does not equal": | ||
if strings.Compare(userAgent, ua) != 0 { | ||
return r.Rule.Action, r.Rule.Reason, nil | ||
} | ||
case "match": | ||
// regex match | ||
isMatched, err := regexp.MatchString(ua, userAgent) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
if isMatched { | ||
return r.Rule.Action, r.Rule.Reason, nil | ||
} | ||
} | ||
} | ||
return "", "", nil | ||
} |
Oops, something went wrong.