-
-
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.
Merge remote-tracking branch 'origin/master' into fix/refresh
- Loading branch information
Showing
18 changed files
with
804 additions
and
217 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2024 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 rule | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/casbin/caswaf/object" | ||
) | ||
|
||
type Rule interface { | ||
checkRule(expressions []*object.Expression, req *http.Request) (bool, string, string, error) | ||
} | ||
|
||
func CheckRules(wafRuleIds []string, r *http.Request) (string, string, error) { | ||
rules := object.GetRulesByRuleIds(wafRuleIds) | ||
for _, rule := range rules { | ||
var ruleObj Rule | ||
switch rule.Type { | ||
case "User-Agent": | ||
ruleObj = &UaRule{} | ||
case "IP": | ||
ruleObj = &IpRule{} | ||
case "WAF": | ||
ruleObj = &WafRule{} | ||
default: | ||
return "", "", fmt.Errorf("unknown rule type: %s for rule: %s", rule.Type, rule.GetId()) | ||
} | ||
|
||
isHit, action, reason, err := ruleObj.checkRule(rule.Expressions, r) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
if action == "" { | ||
action = rule.Action | ||
} | ||
if isHit { | ||
if action == "Block" || action == "Drop" { | ||
if rule.Reason != "" { | ||
reason = rule.Reason | ||
} | ||
return action, reason, nil | ||
} else if action == "Allow" { | ||
return action, reason, nil | ||
} else { | ||
return "", "", fmt.Errorf("unknown rule action: %s for rule: %s", action, rule.GetId()) | ||
} | ||
} | ||
} | ||
|
||
return "", "", nil | ||
} |
Oops, something went wrong.