feat: 新增 SaFirewallCheckHookForPathBannedCharacter 禁用字符校验

This commit is contained in:
click33
2025-02-28 05:26:50 +08:00
parent 6c55de0ef3
commit 8f51d1af8d
4 changed files with 90 additions and 6 deletions

View File

@@ -45,6 +45,7 @@ public final class SaFirewallStrategy {
checkHooks.add(SaFirewallCheckHookForWhitePath.instance);
checkHooks.add(SaFirewallCheckHookForBlackPath.instance);
checkHooks.add(SaFirewallCheckHookForPathDangerCharacter.instance);
checkHooks.add(SaFirewallCheckHookForPathBannedCharacter.instance);
checkHooks.add(SaFirewallCheckHookForDirectoryTraversal.instance);
checkHooks.add(SaFirewallCheckHookForHost.instance);
checkHooks.add(SaFirewallCheckHookForHttpMethod.instance);
@@ -52,7 +53,10 @@ public final class SaFirewallStrategy {
checkHooks.add(SaFirewallCheckHookForParameter.instance);
}
// 注册一个防火墙校验 hook
/**
* 注册一个防火墙校验 hook
* @param checkHook /
*/
public void registerCheckHook(SaFirewallCheckHook checkHook) {
SaManager.getLog().info("防火墙校验 hook 注册成功: " + checkHook.getClass());
checkHooks.add(checkHook);

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* 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 cn.dev33.satoken.strategy.hooks;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.context.model.SaResponse;
import cn.dev33.satoken.exception.RequestPathInvalidException;
import cn.dev33.satoken.util.SaFoxUtil;
/**
* 防火墙策略校验钩子函数:请求 path 禁止字符校验
*
* @author click33
* @since 1.41.0
*/
public class SaFirewallCheckHookForPathBannedCharacter implements SaFirewallCheckHook {
/**
* 默认实例
*/
public static SaFirewallCheckHookForPathBannedCharacter instance = new SaFirewallCheckHookForPathBannedCharacter();
/**
* 执行的方法
*
* @param req 请求对象
* @param res 响应对象
* @param extArg 预留扩展参数
*/
@Override
public void execute(SaRequest req, SaResponse res, Object extArg) {
// 非可打印 ASCII 字符检查
String requestPath = req.getRequestPath();
if(SaFoxUtil.hasNonPrintableASCII(requestPath)) {
throw new RequestPathInvalidException("请求 path 包含禁止字符:" + requestPath, requestPath);
}
}
}

View File

@@ -809,4 +809,24 @@ public class SaFoxUtil {
return listX;
}
/**
* 检查字符串是否包含非可打印 ASCII 字符
* @param str /
* @return /
*/
public static boolean hasNonPrintableASCII(String str) {
if (str == null) {
return false;
}
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
// ASCII 范围检查0-31 或 127
if ((c <= 31) || (c == 127)) {
return true;
}
}
return false;
}
}