iT邦幫忙

2025 iThome 鐵人賽

DAY 9
0
Security

AI都上線了,你的資安跟上了嗎?系列 第 12

📍 Day 9:Secure RAG 手冊

  • 分享至 

  • xImage
  •  

—— 知道得少一點,犯錯就少很多。


🔎 前言:同一個 RAG,不同的風險曲線

昨天我們把資料分級與 DLP 撐起來了。
今天同樣是 Secure RAG,但改用「實戰手冊」的角度重新整理:
從邊界、到檢索、再到答案,每一步都要能「拒絕」。

關鍵心法:預設拒絕(deny by default)最小揭露(least revelation)


🧭 威脅模型(Threat Model)

  • 越權檢索:能看到跨租戶/跨部門的內容
  • 拼湊外洩:多輪細問,重組出敏感明細
  • 檢索投毒:惡意文件被索引,帶出錯誤或敏感答案
  • Prompt Injection:被文件或提問誘導調用危險工具
  • 答案外溢:模型在輸出階段沒有被二次審核

🧱 最小可行安全(MVS)策略

  1. 多租戶隔離:每個 tenant 獨立 namespace / index
  2. 檢索強制 Filter:永遠帶上 tenant/department/classification/clearance
  3. Query Rewriter:去敏 + 意圖解析 + 風險偵測
  4. Top‑K/相似度/時間窗 上限:避免過量檢索與舊文回溯
  5. Answer Guard:DLP + 匿名化重寫 + 告警
  6. 審計:Blocked/Violation/重寫事件都進 SIEM

🧰 控制點一覽(10 條硬規則)

  1. No direct vector calls:前端不得直接打向量庫
  2. Metadata is law:ACL 僅在檢索層生效(非 Prompt)
  3. Allowlisted collections:只能查註冊過的集合
  4. Query budget:每次/每會話檢索 token & 次數上限
  5. Source diversity:不同文件來源的下限(例如 ≥2)
  6. Recency rule:僅近 N 天(依資料域配置)
  7. Min score:相似度低於門檻則要求澄清
  8. De‑PII:查詢、原文與輸出都做 PII 掃描
  9. High‑risk ask justification:敏感主題須用途說明
  10. Two‑person rule:產出含敏感欄位需要雙人核可

🧩 檢索前「重寫與去敏」示意(TypeScript)

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,
  };
}

🧲 檢索守門(Python pseudo)

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"]]

🧵 防資料拼湊(Reconstruction)策略

  • 滑動視窗分數:連續命中同一文件/段落提高風險分數
  • 摘要替代:高風險時僅回傳摘要/統計/區間值
  • 節流 + 再認證:觸發門檻後,要求用途說明或核可
if (riskWindow.score() > 0.8) {
  return respondSummaryOnly("疑似逐步重構敏感資訊,僅提供高階摘要與負責窗口。");
}

🧪 輸出守門(Answer Guard)

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" };
}

🧾 Policy 片段(YAML → 可轉 OPA/Cedar)

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"

📊 觀測指標(SLO for Secure RAG)

  • Sensitive‑hit rate(被 DLP 觸發率,依等級)
  • Boundary violations(越租戶/越等級)
  • Reconstruction alerts(每日/每使用者分佈)
  • Clarification ratio(澄清率,過高代表索引或 query rewrite 需優化)
  • MTTPU(Mean Time To Policy Update)策略到位速度

🧪 紅隊測試清單(可直接開跑)

  • [ ] 逐步細問同一合約明細(測重構防線)
  • [ ] 跨部門/跨租戶關鍵字(測 metadata filter)
  • [ ] 放入惡意 PDF/頁籤注入(測 prompt injection)
  • [ ] 低相似度釣魚查詢(測 min_score + 澄清流程)
  • [ ] 舊資料回溯(測 since_days 限制)

🎭 工程師小劇場

PM:客戶想要「完整條列每項目單價與折扣」。
你:那是明細,不是知識。我們提供摘要與區間,要明細走核可流程。
PM:懂了,我去跟客戶溝通。


✅ 收斂

RAG 的價值不是知道一切,而是知道「可以被知道的那一部分」。
把拒絕權放在檢索前、把節制放在答案後,安全才會成為預設值。


🔮 明日預告:Day 11|攻擊演練:Prompt Injection × RAG 越權(紅隊腳本)

下一篇會把攻擊腳本、評分表、與報告模板一次給齊。


上一篇
📍 Day 8-3:資料分級與 DLP:讓機密不再「被模型記住」
下一篇
📍 Day 10:紅隊腳本:Prompt Injection × RAG 越權
系列文
AI都上線了,你的資安跟上了嗎?13
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言