客户支持聊天机器人不应回应有关暴力犯罪或仇恨言论的提示。此自定义规则会阻止请求,并返回您的应用可解析并展示给用户的 JSON 响应。
-
当传入请求匹配时:
字段 运算符 值 LLM Unsafe topic categories is in S1: Violent CrimesS10: Hate使用编辑器时的表达式:
(any(cf.llm.prompt.unsafe_topic_categories[*] in {"S1" "S10"})) -
操作:Block
-
响应类型:Custom JSON
-
响应体:
{ "error": "content_policy", "message": "Your message could not be processed because it touches on a topic outside this assistant's scope. Please rephrase your question." }
您的应用可检查非 200 响应并向用户显示 message 字段,从而保持对话体验,而不是展示原始拦截页面。
此规则将 AI Security for Apps 的注入分数与 Bot Management 以及请求的国家/地区结合,聚焦高置信度的自动化攻击。与单独使用任一信号相比,这种分层方法可显著减少误报。
-
当传入请求匹配时:
在编辑器中输入以下表达式:
(cf.llm.prompt.injection_score lt 25 and cf.bot_management.score lt 10 and ip.geoip.country ne "US") -
操作:Block
该规则同时针对满足以下条件的请求:
- 可能是提示注入尝试(分数低于 25)。
- 来自自动化工具而非真实浏览器(bot 分数低于 10)。
- 来源于美国以外——请将国家/地区代码调整为与用户所在地匹配。
任一单独信号都可能产生误报。合在一起,它们能识别与自动化提示注入攻击密切相关的模式。
金融服务应用合法地处理来自内部代理的信用卡和银行账号,但应阻止来自外部用户的这些 PII 类型。此规则使用请求的自治系统号 (ASN) 来区分内部流量与公共流量。
-
当传入请求匹配时:
在编辑器中输入以下表达式:
(any(cf.llm.prompt.pii_categories[*] in {"CREDIT_CARD" "US_BANK_NUMBER" "IBAN_CODE"}) and ip.src.asnum ne 13335)将
13335替换为您组织的 ASN。 -
操作:Block
-
响应类型:Custom JSON
-
响应体:
{ "error": "pii_blocked", "message": "Financial account information cannot be submitted from external networks. If you are an internal agent, connect to the corporate network and try again." }
位于企业网络(通过 ASN 识别)的内部代理可将金融 PII 提交给 AI 助手作为工作流程的一部分,而外部用户会被阻止。您还可以结合 Access service token 或 mTLS 进一步细化,以实现更强的身份验证。
当 WAF 规则阻止请求时,Cloudflare 将拦截响应发回您的应用——而非最终用户。您的应用需要处理该响应并决定展示什么。若没有错误处理,用户可能会看到原始 HTML 错误页面或损坏的 UI。
以下两件事可帮助保持体验顺畅。
定义一个友好的默认消息,在应用收到非成功响应时显示。这与拦截规则的配置方式无关——包括默认的 Cloudflare 拦截页面,该页面返回的 HTML 会破坏基于 JSON 的聊天 UI。
// Define a user-friendly fallback message. This is what the user will see
// any time the request is blocked or something unexpected happens.
const FALLBACK = "Sorry, I can't process that request. Please try rephrasing.";
const resp = await fetch("/api/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ prompt: userMessage }),
});
// If the response is not 2xx, show the fallback instead of trying to parse
// the body. This safely handles the default Cloudflare block page (which is
// HTML) without breaking your UI.
if (!resp.ok) {
await resp.text(); // consume the body so the connection is released
showError(FALLBACK);
return;
}
const data = await resp.json();
showMessage(data.message);若需要更多控制,请将拦截规则配置为自定义 JSON 响应——例如 { "message": "That question is outside this assistant's scope." }。您的应用随后可解析响应,并在可用时显示自定义消息,不可用时回退到默认消息。
const FALLBACK = "Sorry, I can't process that request. Please try rephrasing.";
const resp = await fetch("/api/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ prompt: userMessage }),
});
if (!resp.ok) {
// Check the content type to determine if the response contains a custom
// JSON error from your WAF rule, or something else (like the default
// Cloudflare HTML block page, or a DDoS / Bot Management challenge).
const ct = (resp.headers.get("content-type") || "").toLowerCase();
if (ct.includes("application/json")) {
// The WAF returned your custom JSON response. Parse it and show the
// message you configured in the rule. Fall back to the default if the
// field is missing or empty.
const data = await resp.json();
showError(data.message || FALLBACK);
} else {
// The response is not JSON — most likely the default Cloudflare HTML
// block page. Discard the body and show the friendly fallback.
await resp.text();
showError(FALLBACK);
}
return;
}
const data = await resp.json();
showMessage(data.message);