—— 知道得少一點,犯錯就少很多。
昨天我們把資料分級與 DLP 撐起來了。
今天同樣是 Secure RAG,但改用「實戰手冊」的角度重新整理:
從邊界、到檢索、再到答案,每一步都要能「拒絕」。
關鍵心法:預設拒絕(deny by default)、最小揭露(least revelation)。
tenant/department/classification/clearance
export function rewriteQuery(user, raw) {
const sanitized = redactPII(raw); // 去敏
const intent = classifyIntent(sanitized); // 意圖解析
if (detectReconstruction(user.session, raw)) {
throw new PolicyError("blocked: reconstruction-pattern");
}
return {
q: sanitized,
filters: {
tenant_id: user.tenant,
department: user.department,
classification_in: ["public", "internal"],
clearance_lte: user.clearance,
},
top_k: clamp(intent.topK ?? 4, 1, 5),
min_score: 0.76,
since_days: 365,
};
}
def guarded_search(vec, rq):
hits = vec.search(
text=rq["q"],
top_k=rq["top_k"],
min_score=rq["min_score"],
since_days=rq["since_days"],
metadata_filter=rq["filters"], # 硬條件
)
if len(hits) == 0:
return ask_user_to_clarify("找不到高相似度內容,請補充關鍵詞或指定資料域")
# 來源多樣性
if uniq([h.meta["doc_id"] for h in hits]).__len__() < 2:
hits = diversify_sources(hits)
return hits[: rq["top_k"]]
if (riskWindow.score() > 0.8) {
return respondSummaryOnly("疑似逐步重構敏感資訊,僅提供高階摘要與負責窗口。");
}
type Verdict = "allow" | "rewrite" | "block" | "escalate";
export function guardAnswer(user, finalText) {
const hits = dlpDetect(finalText); // regex + NER + 向量
if (hits.some(h => h.level === "secret")) return { action: "block" };
if (hits.some(h => h.level === "confidential")) {
return { action: "rewrite", text: anonymize(finalText) };
}
return { action: "allow" };
}
retrieval:
deny_if:
- expr: resource.tenant != user.tenant
- expr: resource.classification in ["Confidential","Secret"] and user.clearance < 3
enforce:
top_k_max: 5
min_score: 0.76
since_days: 365
generation:
block_if:
- detector: "secret.api_key|pii.id_number|pii.credit_card"
rewrite_if:
- detector: "pii.email|pii.phone|financial.quote.detail"
require_justification_if:
- topic: "finance.detail|employee.compensation"
PM:客戶想要「完整條列每項目單價與折扣」。
你:那是明細,不是知識。我們提供摘要與區間,要明細走核可流程。
PM:懂了,我去跟客戶溝通。
RAG 的價值不是知道一切,而是知道「可以被知道的那一部分」。
把拒絕權放在檢索前、把節制放在答案後,安全才會成為預設值。
下一篇會把攻擊腳本、評分表、與報告模板一次給齊。