iT邦幫忙

2025 iThome 鐵人賽

DAY 8
0
Security

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

📍 Day 8-3:資料分級與 DLP:讓機密不再「被模型記住」

  • 分享至 

  • xImage
  •  

—— 模型不會忘記,你才會。


💬 開場:資料進了模型腦,還出得來嗎?

昨天我們把「誰可以做什麼」鎖好(RBAC/ABAC/PBAC)。
今天處理另一個靈魂拷問:資料分級 + DLP

沒分級 = 一律可見;
沒 DLP = 一律可說。

AI 系統若沒有這兩層,就等於把公司知識放在開放式冰箱,大家路過順手拿。


🧭 為什麼要「資料分級」?(Classify before you RAG)

在 LLM/RAG 的世界,資料從「文件」變成「片段(chunk)」與「向量(embedding)」。
分級必須一路跟著資料走:從檔案 → chunk → 向量 → 檢索回傳 → 生成輸出。

標準分級範例(可照產業微調)

等級 說明 典型範例 可見對象 可外傳 DLP 策略
Public 對外公開 官網新聞稿 任何人 最低
Internal 內部使用 一般 SOP、FAQ 全員
Confidential 機敏 客戶名單、報價 需授權
Secret 關鍵/法遵 來源碼、併購文件 少數人 極高

原則:分類在前、授權在中、審計在後


🔐 DLP 在 LLM/RAG 的三個落點

[Ingestion(入庫)] → [Retrieval(檢索)] → [Generation(生成)]
     ↑遮罩/分級        ↑ACL/過濾            ↑輸出審查/重寫

1) Ingestion:入庫前清洗 + 標註

  • PII/敏感欄位 mask / tokenize / hash
  • 為每一個 chunk 加上 metadata:tenant/department/classification/clearance
  • 建立 SBOM for Knowledge(資料來源、hash、時間、責任人)

2) Retrieval:查詢時硬控

  • 依照使用者屬性做 metadata filter(硬條件)
  • 對 query 做 策略匹配:敏感意圖預先拒答或降權
  • 多租戶:永遠加上 tenant_id 條件(不要交給 prompt)

3) Generation:輸出前審查

  • 黑/白名單、regex、NER、embedding-matching 二次檢查
  • 命中則:block / redact / rewrite / ask-for-justification

🧱 實作範例 ①:向量庫分級過濾(查詢層硬控)

{
  "query": "請給我客戶ABC最近合約細節",
  "filter": {
    "must": [
      {"key":"tenant_id", "match":{"value":"${user.tenant}"}},
      {"key":"department", "match":{"value":"${user.department}"}},
      {"key":"classification", "in":["public","internal"]},
      {"key":"clearance", "lte":"${user.clearance}"}
    ]
  },
  "limit": 5
}

Don’t trust the model. Trust your filter.


🧱 實作範例 ②:Ingestion 前置遮罩(Python pseudo)

SENSITIVE_PATTERNS = {
  "email": r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}",
  "phone": r"\b(?:\+?\d{1,3}[ -]?)?(?:\d[ -]?){7,12}\b",
  "cc": r"\b(?:\d[ -]?){13,19}\b"
}

def redact(text):
    for k, pat in SENSITIVE_PATTERNS.items():
        text = re.sub(pat, f"<REDACTED:{k}>", text)
    return text

def prepare_chunks(doc, meta):
    cleaned = redact(doc.text)
    chunks = split(cleaned, max_tokens=512)
    return [{
        "text": c,
        "metadata": {**meta, "classification": classify(c)}
    } for c in chunks]

先遮罩再嵌入,避免把 Secrets 直接送進向量空間。


🧱 實作範例 ③:生成輸出前 DLP 審查(Gateway)

type Verdict = "allow" | "rewrite" | "block" | "ask";

export function dlpGuard(user, prompt, answer) {
  const hits = detectSensitive(answer); // regex + ner + vector match
  if (!hits.length) return { action: "allow", answer };

  // 高敏類型(身份證、卡號、API key…)
  if (hits.some(h => h.severity === "high")) {
    return { action: "block", reason: "contains_secrets" };
  }
  // 中敏→重寫
  const rewritten = rewriteAnonymize(answer);
  return { action: "rewrite", answer: rewritten, hits };
}

DLP 不只擋下內容,更要可解釋 + 可審計


🗂️ Policy 範本(YAML;可供 OPA/Cedar 政策引擎轉換)

version: 1
labels:
  - Public
  - Internal
  - Confidential
  - Secret

rules:
  retrieval:
    deny_if:
      - expr: resource.tenant != user.tenant
      - expr: resource.classification in ["Confidential","Secret"] and user.clearance < 3
  generation:
    block_if:
      - detector: "pii.id_number|pii.credit_card|secret.api_key"
    rewrite_if:
      - detector: "pii.email|pii.phone"
    ask_justification_if:
      - detector: "financial.quote|customer.personally_identifiable"
retention:
  - label: Internal
    days: 180
  - label: Confidential
    days: 90
audit:
  store: "siem://dlp-events"

🧪 衡量指標(不量化 = 不安全)

  • Leak Rate:被 DLP 攔截/改寫的比例(按類型)
  • FPR/FNR:誤攔/漏攔率(抽樣人工校驗)
  • Tenant Boundary Violations:跨租戶命中次數
  • Time-to-Remediate:策略更新到生效的時間
  • Shadow Data Events:未登錄資料源被讀取次數

🎭 工程師小劇場

PM:「我們 RAG 很安全,因為文件都內網。」
你:「嗯,那為什麼昨天 AI 回答了 A 客戶報價 420 萬?」
PM:「因為有同事把報價單貼到聊天室,然後我們有做 chat-to-RAG…」
你:「好,先把聊天資料也分級,感謝。」


✅ 落地檢核清單(可直接丟到任務板)

  • [ ] 建立分級規範(含定義/範例/範圍/責任人)
  • [ ] Ingestion:PII/Secrets 遮罩 → chunk 加 tenant/department/classification/clearance
  • [ ] Retrieval:所有查詢強制 metadata filter(多租戶必備)
  • [ ] Generation:DLP guard(block/rewrite/ask)+審計事件
  • [ ] Dataset SBOM:來源、hash、時間、責任人、版控
  • [ ] 例行紅隊:資料外洩演練(prompt injection + 越權檢索)
  • [ ] 指標儀表板:Leak Rate、FPR/FNR、Boundary Violations

🔮 明日預告:Day 10|Secure RAG 工法

多租戶向量庫隔離、檢索白名單、查詢重寫、防資料拼湊(Reconstruction Attack)。


上一篇
📍 Day 8-2:警戒 AI 代理風險!17 種 MCP 攻擊手法全公開
下一篇
📍 Day 9:Secure RAG 手冊
系列文
AI都上線了,你的資安跟上了嗎?13
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言