Skip to content

Commit eb7a505

Browse files
authored
feat: add IsVerbose field to Rule for detailed debug output (#106)
1 parent 730612c commit eb7a505

File tree

13 files changed

+58
-2
lines changed

13 files changed

+58
-2
lines changed

object/rule.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Rule struct {
3838
Action string `xorm:"varchar(100) notnull" json:"action"`
3939
StatusCode int `xorm:"int notnull" json:"statusCode"`
4040
Reason string `xorm:"varchar(100) notnull" json:"reason"`
41+
IsVerbose bool `xorm:"bool" json:"isVerbose"`
4142
}
4243

4344
func GetGlobalRules() ([]*Rule, error) {

rule/rule.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"net/http"
2020

2121
"github.com/casbin/caswaf/object"
22+
"github.com/casbin/caswaf/util"
2223
)
2324

2425
type Rule interface {
@@ -89,7 +90,10 @@ func CheckRules(ruleIds []string, r *http.Request) (*RuleResult, error) {
8990

9091
// Update reason if rule has custom reason
9192
if result.Action == "Block" || result.Action == "Drop" {
92-
if rule.Reason != "" {
93+
if rule.IsVerbose {
94+
// Add verbose debug info with rule name and triggered expression
95+
result.Reason = util.GenerateVerboseReason(rule.GetId(), result.Reason, rule.Reason)
96+
} else if rule.Reason != "" {
9397
result.Reason = rule.Reason
9498
} else if result.Reason != "" {
9599
result.Reason = fmt.Sprintf("hit rule %s: %s", ruleIds[i], result.Reason)

util/rule.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2024 The casbin Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package util
16+
17+
import "fmt"
18+
19+
// GenerateVerboseReason creates a detailed reason message for verbose mode
20+
func GenerateVerboseReason(ruleId string, expressionReason string, customReason string) string {
21+
verboseReason := fmt.Sprintf("Rule [%s] triggered", ruleId)
22+
if expressionReason != "" {
23+
verboseReason += fmt.Sprintf(" - %s", expressionReason)
24+
}
25+
if customReason != "" {
26+
verboseReason += fmt.Sprintf(" - Custom reason: %s", customReason)
27+
}
28+
return verboseReason
29+
}

web/src/RuleEditPage.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
import React from "react";
16-
import {Button, Card, Col, Input, InputNumber, Row, Select} from "antd";
16+
import {Button, Card, Col, Input, InputNumber, Row, Select, Switch} from "antd";
1717
import * as Setting from "./Setting";
1818
import * as RuleBackend from "./backend/RuleBackend";
1919
import i18next from "i18next";
@@ -216,6 +216,19 @@ class RuleEditPage extends React.Component {
216216
</Col>
217217
</Row>
218218
}
219+
{
220+
<Row style={{marginTop: "20px"}}>
221+
<Col span={2} style={{marginTop: "5px"}}>
222+
{i18next.t("rule:Verbose mode")}:
223+
</Col>
224+
<Col span={22}>
225+
<Switch checked={this.state.rule.isVerbose}
226+
onChange={checked => {
227+
this.updateRuleField("isVerbose", checked);
228+
}} />
229+
</Col>
230+
</Row>
231+
}
219232
</Card>
220233
);
221234
}

web/src/locales/de/data.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"Status code": "Status code",
8787
"Type": "Type",
8888
"Value": "Value",
89+
"Verbose mode": "Verbose mode",
8990
"and": "and",
9091
"begin": "begin",
9192
"contains": "contains",

web/src/locales/en/data.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"Status code": "Status code",
8787
"Type": "Type",
8888
"Value": "Value",
89+
"Verbose mode": "Verbose mode",
8990
"and": "and",
9091
"begin": "begin",
9192
"contains": "contains",

web/src/locales/es/data.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"Status code": "Status code",
8787
"Type": "Type",
8888
"Value": "Value",
89+
"Verbose mode": "Verbose mode",
8990
"and": "and",
9091
"begin": "begin",
9192
"contains": "contains",

web/src/locales/fr/data.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"Status code": "Status code",
8787
"Type": "Type",
8888
"Value": "Value",
89+
"Verbose mode": "Verbose mode",
8990
"and": "and",
9091
"begin": "begin",
9192
"contains": "contains",

web/src/locales/id/data.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"Status code": "Status code",
8787
"Type": "Type",
8888
"Value": "Value",
89+
"Verbose mode": "Verbose mode",
8990
"and": "and",
9091
"begin": "begin",
9192
"contains": "contains",

web/src/locales/ja/data.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"Status code": "Status code",
8787
"Type": "Type",
8888
"Value": "Value",
89+
"Verbose mode": "Verbose mode",
8990
"and": "and",
9091
"begin": "begin",
9192
"contains": "contains",

0 commit comments

Comments
 (0)