编辑自定义控制规则¶
1. 接口描述¶
接口请求路径:POST /prod-api/waf/wafv3/custom/rules/modifyCustomRule
modifyCustomRule 同时用于创建与编辑。创建时省略 customRuleId(或不传 / 为 null),服务端将新增规则并返回新规则 ID;编辑时必传已有 customRuleId。匹配条件、运算符与动作枚举见 查询条件参数。ruleClass 为 default 的定制规则不可编辑。
Token 获取方式见 密钥鉴权。
2. 输入参数¶
以下请求参数列表仅列出了接口请求参数和部分公共参数,完整公共参数列表见 公共请求参数。
| 参数名称 | 必选 | 类型 | 描述 |
|---|---|---|---|
| wafId | 是 | Long | 所属策略组 ID 示例值: 22 |
| ruleMessage | 是 | String | 规则名称,最长 30 字符;不可含 \、'、"示例值: sql注入攻击 包含 |
| status | 是 | String | 启用状态,支持以下值:enable:启用disable:关闭示例值: enable |
| rules | 是 | Object | 规则内容,见 CustomRuleVO |
| customRuleId | 否 | Long | 创建时省略或为 null;传入已有 ID 则为编辑示例值: 61 |
| sort | 否 | Integer | 排序权重 |
3. 示例¶
示例1 创建拦截规则¶
输入示例¶
{
"ruleMessage": "sql注入攻击 包含",
"status": "enable",
"wafId": 22,
"rules": {
"conditions": [
[
{
"target": "CHECK_REQUEST_HEADERS_NAMES",
"operator": "INT_EQ",
"name": "APP",
"values": ["0"]
},
{
"target": "IP_SRC",
"operator": "IP_IN_LIST",
"name": "IP_SRC",
"values": ["52"]
}
]
],
"action": {
"action": "DENY",
"parameters": [
{ "name": "log", "values": ["log"] }
]
}
}
}
代码调用¶
请将 {登录域名}、token 替换为实际值。
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class ModifyCustomRuleTest {
public static void main(String[] args) throws Exception {
String url = "https://{登录域名}/prod-api/waf/wafv3/custom/rules/modifyCustomRule";
String token = "eyJhbGciOiJIUzUxMiJ9.xxxxxx";
String requestBody = "{"
+ "\"ruleMessage\":\"sql注入攻击 包含\","
+ "\"status\":\"enable\","
+ "\"wafId\":22,"
+ "\"rules\":{"
+ "\"conditions\":[[{"
+ "\"target\":\"IP_SRC\",\"operator\":\"IP_IN_LIST\","
+ "\"name\":\"IP_SRC\",\"values\":[\"52\"]"
+ "}]],"
+ "\"action\":{"
+ "\"action\":\"DENY\","
+ "\"parameters\":[{\"name\":\"log\",\"values\":[\"log\"]}]"
+ "}"
+ "}"
+ "}";
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Authorization", "Bearer " + token);
httpPost.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
int statusCode = response.getStatusLine().getStatusCode();
String body = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("Status Code: " + statusCode);
System.out.println("Body: " + body);
}
}
}
}
package main
import (
"bytes"
"fmt"
"io"
"net/http"
)
func main() {
url := "https://{登录域名}/prod-api/waf/wafv3/custom/rules/modifyCustomRule"
token := "eyJhbGciOiJIUzUxMiJ9.xxxxxx"
requestBody := []byte(`{
"ruleMessage": "sql注入攻击 包含",
"status": "enable",
"wafId": 22,
"rules": {
"conditions": [[{
"target": "IP_SRC",
"operator": "IP_IN_LIST",
"name": "IP_SRC",
"values": ["52"]
}]],
"action": {
"action": "DENY",
"parameters": [
{"name": "log", "values": ["log"]}
]
}
}
}`)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(requestBody))
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println("Body:", string(body))
}
输出示例¶
示例2 编辑已有规则¶
编辑时必传 customRuleId,建议带回已有 conditionId:
{
"customRuleId": 61,
"ruleMessage": "sql注入攻击 包含",
"status": "enable",
"wafId": 22,
"rules": {
"conditions": [
[
{
"conditionId": 120000456,
"target": "CHECK_REQUEST_HEADERS_NAMES",
"operator": "INT_EQ",
"name": "APP",
"values": ["0"]
},
{
"conditionId": 120000457,
"target": "IP_SRC",
"operator": "IP_IN_LIST",
"name": "IP_SRC",
"values": ["52"]
}
]
],
"action": {
"action": "DENY",
"parameters": [
{ "name": "log", "values": ["log"] }
]
}
}
}
4. 输出参数¶
| 参数名称 | 类型 | 描述 |
|---|---|---|
| code | Integer | 状态码。示例值:200 |
| msg | String | 提示信息。示例值:成功 |
| data | Long | 保存后的 customRuleId(新建为新 ID,编辑为原 ID)。示例值:61 |
5. 错误码¶
| HTTP 状态码 / 业务提示 | 描述 |
|---|---|
| 200 | 成功 |
| 401 | 未鉴权或 Token 无效 |
| 403 | 无权限 |
| 规则描述不能为空 | ruleMessage 为空 |
| 执行动作不能为空 | rules.action 为空 |
| 规则描述中不能包含特殊字符 | ruleMessage 含 \、' 或 " |
| 失败,定制规则无法编辑 | ruleClass 为 default |
| 状态[status]只能为disable或enable | status 取值非法 |
| [wafId]不能为空 | 未传 wafId |
| 规则名称[ruleMessage]不能超过30位 | 名称超长 |
| 规则状态[status]不能为空 | 未传 status |
| 请输入至少一个条件 | conditions 为空 |
| 只能输入一个条件集合 | 外层条件组超过 1 组 |
| 无效的target | target 非法 |
| 无效的Operator | operator 非法 |
| 值不能为空 | 条件值为空 |
| 规则ID不能为空 | 编辑场景缺少规则 ID |
| 更新失败,请确认提交信息是否正确 | 持久化失败 |
| 未定义相关数据列表,请先创建相关数据列表后再操作 | 数据集合 ID 无效 |
| 字段…的匹配内容… | 条件值校验失败(长度、重复、枚举等) |
6. 数据模型¶
CustomRuleVO¶
| 参数名称 | 类型 | 描述 |
|---|---|---|
| action | Object | 命中后动作 |
| action.action | String | 动作编码,支持以下值:DENY:拦截LOG:仅记录不拦截SKIP_RULES:跳过规则WHITE_RULE_IDS:加白规则 IDREDIRECT:重定向示例值: DENY |
| action.parameters | Array | 动作参数;每项为 { "name", "values" } |
| action.parameters[].name | String | 参数键。常见:log、url、statusCode、remainRules、limitRules、custodyRules |
| action.parameters[].values | Array of String | 参数值列表 |
| conditions | Array | 条件组二维数组;当前仅支持 1 个外层组,组内为与关系 |
| conditions[][].target | String | 匹配维度,与 查询条件参数 一致。示例值:IP_SRC |
| conditions[][].operator | String | 运算符,如 EQ、IP_IN_LIST、INT_EQ |
| conditions[][].name | String | 维度名或子字段名(如请求头名);常与 target 相同 |
| conditions[][].values | Array of String | 比较取值;数据集合填 dataListId 字符串 |
| conditions[][].conditionId | Long | 编辑时建议带回;新建条件可省略 |
常用动作参数示例:
DENY/LOG/WHITE_RULE_IDS:通常仅log(log/nolog)REDIRECT:log+url+statusCode(301/302/303/307)SKIP_RULES:log,以及可选remainRules、limitRules、custodyRules(values一般填与name相同的编码表示启用)
SKIP_RULES 示例: