mateclaw/mateclaw-server/src/main/java/vip/mate/tool/guard/DangerousPattern.java
matevip 579d60125b Initial commit: MateClaw — Java + Vue 3 AI Assistant System
Full-stack AI assistant built on Spring AI Alibaba.
Features: ReAct Agent, Plan-and-Execute, MCP Protocol, Multi-Model, Multi-Channel.

Apache-2.0 License
2026-04-04 19:03:49 +08:00

34 lines
878 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package vip.mate.tool.guard;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
/**
* 危险操作匹配模式
*
* @param regex 正则表达式
* @param category 威胁类别(如 filesystem_destroy, sql_destroy
* @param reason 拦截原因说明
*/
public record DangerousPattern(
String regex,
String category,
String reason
) {
private static final Map<String, Pattern> COMPILED_CACHE = new ConcurrentHashMap<>();
/**
* 检查输入是否匹配该危险模式
*/
public boolean matches(String input) {
if (input == null || input.isEmpty()) {
return false;
}
Pattern p = COMPILED_CACHE.computeIfAbsent(regex,
r -> Pattern.compile(r, Pattern.CASE_INSENSITIVE));
return p.matcher(input).find();
}
}