<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Compliance on Tarragon</title><link>https://tarrragon.github.io/blog/tags/compliance/</link><description>Recent content in Compliance on Tarragon</description><generator>Hugo -- gohugo.io</generator><language>zh-TW</language><copyright>Tarragon (CC BY 4.0)</copyright><lastBuildDate>Mon, 22 Jun 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://tarrragon.github.io/blog/tags/compliance/index.xml" rel="self" type="application/rss+xml"/><item><title>GDPR 最小化原則的工程落地</title><link>https://tarrragon.github.io/blog/monitoring/07-security-privacy/gdpr-minimization/</link><pubDate>Fri, 19 Jun 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/monitoring/07-security-privacy/gdpr-minimization/</guid><description>&lt;p>GDPR 的資料最小化原則要求「只收集達成特定目的所需的最少資料」。這個法律原則轉譯到監控系統的工程實作，影響三個設計決策：收集什麼欄位、保留多久、誰可以存取。&lt;/p>
&lt;h2 id="資料最小化只收集需要的欄位">資料最小化：只收集需要的欄位&lt;/h2>
&lt;p>資料最小化的工程落地是「每個收集的欄位都要能回答：這個欄位用來做什麼決策？」。如果一個欄位只是「可能有用」但沒有明確的消費場景，就不應該收集。&lt;/p>
&lt;h3 id="正面表列-vs-負面排除">正面表列 vs 負面排除&lt;/h3>
&lt;p>正面表列（allowlist）是列出「收集哪些欄位」— 只收集清單上的欄位，其他全部不收。&lt;/p>
&lt;p>負面排除（denylist）是列出「不收集哪些欄位」— 預設收集所有欄位，排除清單上的。&lt;/p>
&lt;p>GDPR 的精神更接近正面表列 — 每個收集行為需要有正當理由（lawful basis）。工程上的實作方式是：事件 schema 定義哪些欄位是允許的，不在 schema 中的欄位在 collector 端丟棄。&lt;/p>
&lt;h3 id="sdk-端的最小化">SDK 端的最小化&lt;/h3>
&lt;p>SDK 端的最小化更主動 — 在事件產生時就只包含必要的欄位，而非送到 collector 再過濾。&lt;/p>
&lt;p>設計 SDK 的 event API 時，不提供「送任意 key-value」的 free-form API，而是提供結構化的 API：&lt;/p>





&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">// free-form（難以控制收集了什麼）
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">monitor.event(&amp;#39;login&amp;#39;, data: {&amp;#39;email&amp;#39;: email, &amp;#39;ip&amp;#39;: ip, &amp;#39;device&amp;#39;: device, ...})
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">// 結構化（schema 控制收集範圍）
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">monitor.event(&amp;#39;login&amp;#39;, loginMethod: &amp;#39;biometric&amp;#39;, success: true)&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>結構化 API 的參數在 SDK 設計時就決定了收集範圍，code review 時可以檢查「為什麼這個 event 需要這個參數」。&lt;/p>
&lt;h2 id="目的限制收集的資料只用於聲明的目的">目的限制：收集的資料只用於聲明的目的&lt;/h2>
&lt;p>目的限制要求資料只用於收集時聲明的目的。監控系統收集事件的目的通常是 debug 和效能監控 — 如果之後要用同一份資料做行為分析或廣告投放，需要額外的法律基礎（通常是使用者同意）。&lt;/p>
&lt;h3 id="工程落地">工程落地&lt;/h3>
&lt;p>目的限制在工程上的實作是「不同目的的資料分開儲存、分開授權」。&lt;/p>
&lt;p>Debug 用的 error 事件和行為分析用的 event 事件存在不同的儲存位置（不同的 JSONL 檔案或不同的資料庫 table）。Debug 用途的 access 不需要使用者同意（legitimate interest）；行為分析用途的 access 需要使用者同意。&lt;/p>
&lt;p>分開儲存讓「使用者撤回行為分析同意」的工程操作變簡單 — 刪除行為分析的儲存，不影響 debug 儲存。&lt;/p>
&lt;h2 id="儲存限制不保留超過必要期間的資料">儲存限制：不保留超過必要期間的資料&lt;/h2>
&lt;p>儲存限制要求資料只保留達成目的所需的最短期間。監控資料的合理保留期間依用途不同：&lt;/p>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>用途&lt;/th>
 &lt;th>合理保留期間&lt;/th>
 &lt;th>理由&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>Debug&lt;/td>
 &lt;td>30-90 天&lt;/td>
 &lt;td>大部分 bug 在 30 天內被發現和修復&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>效能趨勢&lt;/td>
 &lt;td>6-12 個月&lt;/td>
 &lt;td>季節性趨勢需要至少一年的資料&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>行為分析&lt;/td>
 &lt;td>依同意期間&lt;/td>
 &lt;td>使用者同意到期就刪除&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>合規審計&lt;/td>
 &lt;td>依法規要求（通常 1-7 年）&lt;/td>
 &lt;td>法規指定的最短保留期間&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;h3 id="自動清理">自動清理&lt;/h3>
&lt;p>Collector 的儲存清理應該自動化 — 手動清理依賴人記得執行，最終會被遺忘。&lt;/p>
&lt;p>JSONL 儲存用「一天一檔」的命名（&lt;code>events-2026-06-19.jsonl&lt;/code>），清理腳本每天刪除超過保留期限的檔案。Cron job 或 systemd timer 定期執行。&lt;/p>
&lt;h2 id="下一步路由">下一步路由&lt;/h2>
&lt;ul>
&lt;li>去識別化技術 → &lt;a href="https://tarrragon.github.io/blog/monitoring/07-security-privacy/anonymization-strategy/" data-link-title="去識別化策略" data-link-desc="IP 截斷 / user agent 簡化 / stack trace 路徑清理 / session UUID — 四種去識別化技術的適用場景和實作方式">去識別化策略&lt;/a>&lt;/li>
&lt;li>監控資料洩漏的威脅分析 → &lt;a href="https://tarrragon.github.io/blog/monitoring/07-security-privacy/monitoring-data-threat-model/" data-link-title="監控資料洩漏的 Threat Model" data-link-desc="監控系統本身是攻擊面 — 四個威脅場景（傳輸竊聽 / 儲存入侵 / endpoint 濫用 / 內部越權存取）的風險評估和防護措施">監控資料洩漏的 threat model&lt;/a>&lt;/li>
&lt;li>Collector 的儲存設計 → &lt;a href="https://tarrragon.github.io/blog/monitoring/04-collector/" data-link-title="模組四：Collector 設計" data-link-desc="收 → 驗 → 存 → 查 → 觸發的完整鏈路 — Go 單一 binary、可插拔 Storage Backend、rule engine">模組四 Collector 設計&lt;/a>&lt;/li>
&lt;/ul></description><content:encoded><![CDATA[<p>GDPR 的資料最小化原則要求「只收集達成特定目的所需的最少資料」。這個法律原則轉譯到監控系統的工程實作，影響三個設計決策：收集什麼欄位、保留多久、誰可以存取。</p>
<h2 id="資料最小化只收集需要的欄位">資料最小化：只收集需要的欄位</h2>
<p>資料最小化的工程落地是「每個收集的欄位都要能回答：這個欄位用來做什麼決策？」。如果一個欄位只是「可能有用」但沒有明確的消費場景，就不應該收集。</p>
<h3 id="正面表列-vs-負面排除">正面表列 vs 負面排除</h3>
<p>正面表列（allowlist）是列出「收集哪些欄位」— 只收集清單上的欄位，其他全部不收。</p>
<p>負面排除（denylist）是列出「不收集哪些欄位」— 預設收集所有欄位，排除清單上的。</p>
<p>GDPR 的精神更接近正面表列 — 每個收集行為需要有正當理由（lawful basis）。工程上的實作方式是：事件 schema 定義哪些欄位是允許的，不在 schema 中的欄位在 collector 端丟棄。</p>
<h3 id="sdk-端的最小化">SDK 端的最小化</h3>
<p>SDK 端的最小化更主動 — 在事件產生時就只包含必要的欄位，而非送到 collector 再過濾。</p>
<p>設計 SDK 的 event API 時，不提供「送任意 key-value」的 free-form API，而是提供結構化的 API：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">// free-form（難以控制收集了什麼）
</span></span><span class="line"><span class="ln">2</span><span class="cl">monitor.event(&#39;login&#39;, data: {&#39;email&#39;: email, &#39;ip&#39;: ip, &#39;device&#39;: device, ...})
</span></span><span class="line"><span class="ln">3</span><span class="cl">
</span></span><span class="line"><span class="ln">4</span><span class="cl">// 結構化（schema 控制收集範圍）
</span></span><span class="line"><span class="ln">5</span><span class="cl">monitor.event(&#39;login&#39;, loginMethod: &#39;biometric&#39;, success: true)</span></span></code></pre></div><p>結構化 API 的參數在 SDK 設計時就決定了收集範圍，code review 時可以檢查「為什麼這個 event 需要這個參數」。</p>
<h2 id="目的限制收集的資料只用於聲明的目的">目的限制：收集的資料只用於聲明的目的</h2>
<p>目的限制要求資料只用於收集時聲明的目的。監控系統收集事件的目的通常是 debug 和效能監控 — 如果之後要用同一份資料做行為分析或廣告投放，需要額外的法律基礎（通常是使用者同意）。</p>
<h3 id="工程落地">工程落地</h3>
<p>目的限制在工程上的實作是「不同目的的資料分開儲存、分開授權」。</p>
<p>Debug 用的 error 事件和行為分析用的 event 事件存在不同的儲存位置（不同的 JSONL 檔案或不同的資料庫 table）。Debug 用途的 access 不需要使用者同意（legitimate interest）；行為分析用途的 access 需要使用者同意。</p>
<p>分開儲存讓「使用者撤回行為分析同意」的工程操作變簡單 — 刪除行為分析的儲存，不影響 debug 儲存。</p>
<h2 id="儲存限制不保留超過必要期間的資料">儲存限制：不保留超過必要期間的資料</h2>
<p>儲存限制要求資料只保留達成目的所需的最短期間。監控資料的合理保留期間依用途不同：</p>
<table>
  <thead>
      <tr>
          <th>用途</th>
          <th>合理保留期間</th>
          <th>理由</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Debug</td>
          <td>30-90 天</td>
          <td>大部分 bug 在 30 天內被發現和修復</td>
      </tr>
      <tr>
          <td>效能趨勢</td>
          <td>6-12 個月</td>
          <td>季節性趨勢需要至少一年的資料</td>
      </tr>
      <tr>
          <td>行為分析</td>
          <td>依同意期間</td>
          <td>使用者同意到期就刪除</td>
      </tr>
      <tr>
          <td>合規審計</td>
          <td>依法規要求（通常 1-7 年）</td>
          <td>法規指定的最短保留期間</td>
      </tr>
  </tbody>
</table>
<h3 id="自動清理">自動清理</h3>
<p>Collector 的儲存清理應該自動化 — 手動清理依賴人記得執行，最終會被遺忘。</p>
<p>JSONL 儲存用「一天一檔」的命名（<code>events-2026-06-19.jsonl</code>），清理腳本每天刪除超過保留期限的檔案。Cron job 或 systemd timer 定期執行。</p>
<h2 id="下一步路由">下一步路由</h2>
<ul>
<li>去識別化技術 → <a href="/blog/monitoring/07-security-privacy/anonymization-strategy/" data-link-title="去識別化策略" data-link-desc="IP 截斷 / user agent 簡化 / stack trace 路徑清理 / session UUID — 四種去識別化技術的適用場景和實作方式">去識別化策略</a></li>
<li>監控資料洩漏的威脅分析 → <a href="/blog/monitoring/07-security-privacy/monitoring-data-threat-model/" data-link-title="監控資料洩漏的 Threat Model" data-link-desc="監控系統本身是攻擊面 — 四個威脅場景（傳輸竊聽 / 儲存入侵 / endpoint 濫用 / 內部越權存取）的風險評估和防護措施">監控資料洩漏的 threat model</a></li>
<li>Collector 的儲存設計 → <a href="/blog/monitoring/04-collector/" data-link-title="模組四：Collector 設計" data-link-desc="收 → 驗 → 存 → 查 → 觸發的完整鏈路 — Go 單一 binary、可插拔 Storage Backend、rule engine">模組四 Collector 設計</a></li>
</ul>
]]></content:encoded></item><item><title>AWS CloudHSM</title><link>https://tarrragon.github.io/blog/backend/07-security-data-protection/vendors/cloudhsm/</link><pubDate>Mon, 18 May 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/backend/07-security-data-protection/vendors/cloudhsm/</guid><description>&lt;p>AWS CloudHSM 是 &lt;em>single-tenant dedicated HSM&lt;/em> 服務（FIPS 140-2 Level 3）、客戶獨享一個 HSM cluster、AWS 提供 &lt;em>硬體 + network + provisioning&lt;/em>、客戶自己管 &lt;em>crypto user / partition / key custody / backup&lt;/em>。它跟 &lt;a href="https://tarrragon.github.io/blog/backend/07-security-data-protection/vendors/aws-kms/" data-link-title="AWS KMS" data-link-desc="AWS 原生 key management service、envelope encryption / digital signing / Multi-Region Key、Key Policy &amp;#43; Grant 雙軌授權">AWS KMS&lt;/a> 是 &lt;em>不同信任模型&lt;/em> — KMS 是 multi-tenant managed、AWS 持有 key custody 與 API plane；CloudHSM 上 &lt;em>AWS 看不到 key、也不能 reset Crypto User password&lt;/em>、客戶丟了 credential 等於 key 永久遺失。&lt;/p>
&lt;h2 id="服務定位">服務定位&lt;/h2>
&lt;p>CloudHSM 的核心定位是 &lt;em>把 cryptographic root of trust 放回客戶手上&lt;/em> — 適合金融、政府、醫療這類有資料主權、FIPS 140-2 Level 3、PCI HSM、HIPAA 合規壓力的場景。跟 &lt;a href="https://tarrragon.github.io/blog/backend/07-security-data-protection/vendors/aws-kms/" data-link-title="AWS KMS" data-link-desc="AWS 原生 key management service、envelope encryption / digital signing / Multi-Region Key、Key Policy &amp;#43; Grant 雙軌授權">AWS KMS&lt;/a> 比、KMS 也滿足 FIPS 140-2 Level 3、但 &lt;em>HSM cluster 是 AWS 多租戶共用&lt;/em>、key material 由 AWS-controlled HSM 持有、控制面 API 也是 AWS。CloudHSM 把 HSM cluster 物理隔離給單一客戶、PKCS#11 / JCE / OpenSSL Dynamic Engine 直接打 HSM、AWS 在資料平面 &lt;em>沒有讀 key 的能力&lt;/em>。&lt;/p>
&lt;p>跟 &lt;em>自管 on-prem HSM&lt;/em>（SafeNet / Thales 自架）比、CloudHSM 把硬體採購、機房、network、firmware patch 交還 AWS、客戶只管 key custody 跟 Crypto User policy；代價是不能完全脫離 AWS region。跟 &lt;a href="https://tarrragon.github.io/blog/backend/07-security-data-protection/vendors/hashicorp-vault/" data-link-title="HashiCorp Vault" data-link-desc="Self-hosted secret management 與 dynamic credential / encryption-as-a-service / PKI engine、跨雲跨環境的 secret 控制面">Vault auto-unseal&lt;/a> 整合場景中、CloudHSM 是 &lt;em>Vault master key 的 root custodian&lt;/em> — Vault unseal key 用 CloudHSM 加密、CloudHSM 出事整個 Vault cluster 沒法 unseal、所以可用性設計（cross-AZ cluster、cross-region backup）很關鍵。多數一般 web app / SaaS 用 KMS 即可、不需要 CloudHSM 的物理隔離。&lt;/p></description><content:encoded><![CDATA[<p>AWS CloudHSM 是 <em>single-tenant dedicated HSM</em> 服務（FIPS 140-2 Level 3）、客戶獨享一個 HSM cluster、AWS 提供 <em>硬體 + network + provisioning</em>、客戶自己管 <em>crypto user / partition / key custody / backup</em>。它跟 <a href="/blog/backend/07-security-data-protection/vendors/aws-kms/" data-link-title="AWS KMS" data-link-desc="AWS 原生 key management service、envelope encryption / digital signing / Multi-Region Key、Key Policy &#43; Grant 雙軌授權">AWS KMS</a> 是 <em>不同信任模型</em> — KMS 是 multi-tenant managed、AWS 持有 key custody 與 API plane；CloudHSM 上 <em>AWS 看不到 key、也不能 reset Crypto User password</em>、客戶丟了 credential 等於 key 永久遺失。</p>
<h2 id="服務定位">服務定位</h2>
<p>CloudHSM 的核心定位是 <em>把 cryptographic root of trust 放回客戶手上</em> — 適合金融、政府、醫療這類有資料主權、FIPS 140-2 Level 3、PCI HSM、HIPAA 合規壓力的場景。跟 <a href="/blog/backend/07-security-data-protection/vendors/aws-kms/" data-link-title="AWS KMS" data-link-desc="AWS 原生 key management service、envelope encryption / digital signing / Multi-Region Key、Key Policy &#43; Grant 雙軌授權">AWS KMS</a> 比、KMS 也滿足 FIPS 140-2 Level 3、但 <em>HSM cluster 是 AWS 多租戶共用</em>、key material 由 AWS-controlled HSM 持有、控制面 API 也是 AWS。CloudHSM 把 HSM cluster 物理隔離給單一客戶、PKCS#11 / JCE / OpenSSL Dynamic Engine 直接打 HSM、AWS 在資料平面 <em>沒有讀 key 的能力</em>。</p>
<p>跟 <em>自管 on-prem HSM</em>（SafeNet / Thales 自架）比、CloudHSM 把硬體採購、機房、network、firmware patch 交還 AWS、客戶只管 key custody 跟 Crypto User policy；代價是不能完全脫離 AWS region。跟 <a href="/blog/backend/07-security-data-protection/vendors/hashicorp-vault/" data-link-title="HashiCorp Vault" data-link-desc="Self-hosted secret management 與 dynamic credential / encryption-as-a-service / PKI engine、跨雲跨環境的 secret 控制面">Vault auto-unseal</a> 整合場景中、CloudHSM 是 <em>Vault master key 的 root custodian</em> — Vault unseal key 用 CloudHSM 加密、CloudHSM 出事整個 Vault cluster 沒法 unseal、所以可用性設計（cross-AZ cluster、cross-region backup）很關鍵。多數一般 web app / SaaS 用 KMS 即可、不需要 CloudHSM 的物理隔離。</p>
<h2 id="本章目標">本章目標</h2>
<p>讀完本頁、讀者能判斷：</p>
<ol>
<li>何時需要 CloudHSM 的 dedicated 模型、何時 <a href="/blog/backend/07-security-data-protection/vendors/aws-kms/" data-link-title="AWS KMS" data-link-desc="AWS 原生 key management service、envelope encryption / digital signing / Multi-Region Key、Key Policy &#43; Grant 雙軌授權">AWS KMS</a> 已足夠</li>
<li>CloudHSM cluster 的最低安全 / 可用性需求（cross-AZ、Crypto Officer 分離、Quorum、backup）</li>
<li>Crypto User credential 出事的降級路徑（AWS 不能幫忙、靠 backup + Quorum）</li>
<li>跟 <a href="https://docs.aws.amazon.com/kms/latest/developerguide/custom-key-store-overview.html">KMS Custom Key Store</a> / <a href="/blog/backend/07-security-data-protection/vendors/hashicorp-vault/" data-link-title="HashiCorp Vault" data-link-desc="Self-hosted secret management 與 dynamic credential / encryption-as-a-service / PKI engine、跨雲跨環境的 secret 控制面">Vault auto-unseal</a> 整合的取捨</li>
</ol>
<h2 id="最短判讀路徑">最短判讀路徑</h2>
<p>判斷 CloudHSM deployment 是否健康、最少看四件事：</p>
<ul>
<li><strong>Cluster 拓樸</strong>：production cluster 是否至少 2 個 HSM instance 跨 AZ、cluster 內自動 replicate、單一 AZ 故障時 key 是否仍可用</li>
<li><strong>Crypto User 管理</strong>：Crypto Officer（CO）跟 Crypto User（CU）是否分離、CO password 是否走 break-glass 保管、CU credential 是否走 short-lived 取得 + audit</li>
<li><strong>Quorum-based policy</strong>：高敏 operation（建 CU、改 policy、key export wrapped）是否設 M-of-N approval、避免單一 admin compromise 後 silent abuse</li>
<li><strong>Backup 治理</strong>：automatic 24h backup 跟 manual backup 是否都開、cross-region backup 是否走 explicit copy、restore 流程是否定期演練</li>
</ul>
<p>四件事任一缺失、就是 CloudHSM deployment 待補項目 — 跟 <a href="/blog/backend/knowledge-cards/secret-management/" data-link-title="Secret Management" data-link-desc="說明 token、key、password 與憑證如何保存、輪替與撤銷">secret management</a> 的 evidence 邊界同類。</p>
<h2 id="日常操作與決策形狀">日常操作與決策形狀</h2>
<p><strong>Cluster + HSM Instance 拓樸</strong>：CloudHSM 的部署單位是 <em>cluster</em>、cluster 內可以有 1-N 個 <em>HSM instance</em>。production 場景至少 2 個 HSM instance 跨 AZ、cluster 自動把 key material replicate 在所有 instance 上、單一 AZ 失效不影響 cryptographic operation。跨 region 不自動 replicate — 跨 region DR 要靠 backup copy。</p>
<p><strong>Crypto Officer (CO) vs Crypto User (CU)</strong>：CO 是 cluster 管理員、能建 / 刪 CU、設 policy、做 backup；CU 是真的做 cryptographic operation 的 identity（encrypt / decrypt / sign / verify）。production 必須分離 — CO credential 走 break-glass 保管、CU credential 給 application 使用、application compromise 只影響 CU 邊界、不能改 CO policy。</p>
<p><strong>Quorum-based policy（M-of-N approval）</strong>：CloudHSM 支援把高敏操作（建 CU、改 policy、key export wrapped）綁定 <em>M-of-N CO approval</em>。例如 3-of-5 quorum、單一 CO 即使 credential 外洩也不能單獨建後門 CU、必須拿到另外 2 個 CO 的 signed token。對應 <a href="/blog/backend/07-security-data-protection/red-team/cases/identity-access/microsoft-storm-0558-2023-signing-key-chain/" data-link-title="7.R7.1.5 Microsoft Storm-0558 2023：簽章金鑰鏈與郵件存取" data-link-desc="從簽章金鑰保護失效到雲端郵件存取，拆解身分信任鏈的關鍵控制點">Storm-0558 signing key chain</a> 啟示：高價值 key custodian 的 admin operation 不該是 <em>單人單 token</em>、必須有第二人簽核才能改變信任根。</p>
<p><strong>Backup 治理</strong>：CloudHSM 每 24 小時自動 backup 整個 cluster state（含 key material）、backup 是 AWS-managed encrypted blob、AWS 自己也不能解密、restore 必須在 CloudHSM cluster context 內進行。可手動 backup、可 copy 到其他 region 做 DR。Backup retention 預設 90 天、可延長。Backup 不是 <em>export</em> — 不能把 key material 從 HSM 拿出來看 plaintext。</p>
<p><strong>Key Replication 跨 region</strong>：CloudHSM cluster 綁定單一 AWS region、跨 region 走 <em>backup → copy → restore</em> 流程、不是 active replication。設計 DR 時要算 RTO：restore 一個 cluster 從 backup 大約小時級、不適合 hot failover、應該 <em>primary region 跑、DR region 備好空 cluster + backup copy</em>。</p>
<p><strong>PKCS#11 / JCE / OpenSSL Dynamic Engine 整合</strong>：application 不用 AWS SDK 講 CloudHSM、而是透過 <em>標準 cryptographic API library</em>（PKCS#11 for C/C++、JCE Provider for Java、OpenSSL Dynamic Engine 走 TLS termination）。好處是 <em>application code 用業界標準介面</em>、未來換 HSM 廠也只需要換 library。代價是 client SDK 要裝在 application host、CU credential 要 deploy 到 host、host security baseline 變成 cryptographic boundary 的一部分。</p>
<p><strong>跟 KMS Custom Key Store 整合</strong>：<a href="https://docs.aws.amazon.com/kms/latest/developerguide/custom-key-store-overview.html">KMS Custom Key Store</a> 把 KMS Key 的 <em>backing material 放在 CloudHSM</em>、API 仍透過 KMS（<code>kms:Encrypt</code> / <code>kms:Decrypt</code>）、application code 不需要改。這是 <em>KMS 易用 + HSM dedicated 雙重</em>：保留 KMS 的 IAM policy / key rotation / audit log（CloudTrail）、又得到 single-tenant HSM 的合規屬性。代價是 CloudHSM 失效時、Custom Key Store backing 的 KMS Key 全部不可用、需要監控 cluster health。</p>
<h2 id="核心取捨表">核心取捨表</h2>
<table>
  <thead>
      <tr>
          <th>取捨維度</th>
          <th>AWS CloudHSM</th>
          <th>AWS KMS</th>
          <th>Azure Managed HSM</th>
          <th>Google Cloud HSM</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>部署模型</td>
          <td>Single-tenant dedicated cluster</td>
          <td>Multi-tenant managed</td>
          <td>Single-tenant pool</td>
          <td>HSM-backed Cloud KMS（Protection Level=HSM）</td>
      </tr>
      <tr>
          <td>FIPS 140-2</td>
          <td>Level 3（dedicated）</td>
          <td>Level 3（shared cluster）</td>
          <td>Level 3</td>
          <td>Level 3</td>
      </tr>
      <tr>
          <td>AWS / 雲廠持 key？</td>
          <td>不持（CU credential 客戶獨有）</td>
          <td>持（managed key custody）</td>
          <td>不持（HSM admin 客戶獨有）</td>
          <td>不持 plaintext key material</td>
      </tr>
      <tr>
          <td>整合介面</td>
          <td>PKCS#11 / JCE / OpenSSL</td>
          <td>AWS SDK / CLI / KMS API</td>
          <td>Key Vault SDK / REST</td>
          <td>Cloud KMS API</td>
      </tr>
      <tr>
          <td>Quorum 多人簽核</td>
          <td>內建（M-of-N）</td>
          <td>透過 IAM policy + organization SCP</td>
          <td>RBAC + Privileged Identity Management</td>
          <td>IAM Condition + organization policy</td>
      </tr>
      <tr>
          <td>運維成本</td>
          <td>高 — 自管 CU credential / patch / topology</td>
          <td>低</td>
          <td>中</td>
          <td>低</td>
      </tr>
      <tr>
          <td>合規憑證</td>
          <td>FIPS 140-2 L3 + PCI HSM + Common Criteria</td>
          <td>FIPS 140-2 L3 + PCI DSS</td>
          <td>FIPS 140-2 L3 + Common Criteria</td>
          <td>FIPS 140-2 L3</td>
      </tr>
      <tr>
          <td>適合場景</td>
          <td>金融 / 政府 / 醫療、需要物理隔離 + AWS 不持 key</td>
          <td>一般 AWS-heavy workload、需要 IAM 整合</td>
          <td>Azure-heavy + 合規壓力</td>
          <td>GCP-heavy + 合規壓力</td>
      </tr>
      <tr>
          <td>退場成本</td>
          <td>中 — backup 跨廠不可移植、key 不能 export</td>
          <td>中</td>
          <td>中</td>
          <td>中</td>
      </tr>
  </tbody>
</table>
<p>選 CloudHSM 的核心訴求：<em>合規明文要求 dedicated HSM</em>（PCI HSM、某些國家資料主權法規）、或 <em>trust model 上不接受 AWS 持 key</em>。多數 AWS-heavy workload 用 KMS 即可、加 CloudHSM 反而引入 <em>Crypto User credential 的單點失誤</em>（丟了 = key 永久遺失）。需要 KMS API 但又要 dedicated HSM、走 <a href="https://docs.aws.amazon.com/kms/latest/developerguide/custom-key-store-overview.html">Custom Key Store</a> 是折衷路徑。</p>
<h2 id="進階主題">進階主題</h2>
<p><strong>Quorum Auth 設計</strong>：production 把 Quorum threshold 設為 <em>3-of-5</em> 或 <em>2-of-3</em>、五位 CO 由不同部門 / 不同地理位置持有、避免單一辦公室 / 單一網路同時被攻陷。Quorum token 有 TTL、單次 operation 用完就失效、防止 replay。建議 quarterly 演練：模擬一個 CO 不在、用剩餘 quorum 完成 emergency operation、驗證流程在事故時跑得通。</p>
<p><strong>KMS Custom Key Store 整合決策</strong>：用 Custom Key Store 的關鍵問題是 <em>availability blast radius</em> — KMS Key 出事影響範圍是 <em>使用該 Key 的 AWS service</em>（S3、EBS、RDS encryption）、Custom Key Store backing 失效會讓這些 service 同步斷。設計時做 <em>分層 key strategy</em>：mass volume 的 S3 / EBS 用 AWS-managed KMS Key、高合規敏感的 database / secret 才用 Custom Key Store backing 的 KMS Key、降低單一 cluster 失效的影響面。</p>
<p><strong>Cross-Region Backup</strong>：DR 要把 backup copy 到第二個 region、走 <code>CopyBackupToRegion</code> API、restore 時建空 cluster + 套 backup。整個 RTO 通常數小時、不適合熱備、設計上是 <em>容忍小時級 outage 換到 BCDR 環境</em>、不是 <em>秒級 failover</em>。對應 <a href="/blog/backend/07-security-data-protection/cases/azure-ad-identity-control-plane-2021/" data-link-title="7.C3 Azure AD：2021 Identity Control-plane 事件" data-link-desc="身分控制面事件如何影響多服務信任鏈與回復優先序。">Azure AD Identity Control Plane 2021</a> 對照啟示：身份 / 加密控制面的單點 outage 影響整個 platform、availability 的 topology 設計跟 confidentiality 同等重要。</p>
<p><strong>跟 Vault auto-unseal 整合</strong>：<a href="/blog/backend/07-security-data-protection/vendors/hashicorp-vault/" data-link-title="HashiCorp Vault" data-link-desc="Self-hosted secret management 與 dynamic credential / encryption-as-a-service / PKI engine、跨雲跨環境的 secret 控制面">Vault</a> auto-unseal 可用 CloudHSM 作 master key custodian、走 PKCS#11 plugin、Vault unseal 時呼叫 CloudHSM <code>Unwrap</code> master key。比起 <a href="/blog/backend/07-security-data-protection/vendors/aws-kms/" data-link-title="AWS KMS" data-link-desc="AWS 原生 key management service、envelope encryption / digital signing / Multi-Region Key、Key Policy &#43; Grant 雙軌授權">AWS KMS auto-unseal</a> 多一層 dedicated HSM 保證、適合監管特別嚴的場景。代價是 CloudHSM cluster 失效 → Vault 不能 unseal → 下游所有 secret 拿不到、要設計 break-glass 流程。</p>
<p><strong>合規憑證</strong>：CloudHSM 同時持有 FIPS 140-2 Level 3、PCI HSM、Common Criteria EAL4+ 多個認證、可作金融 PIN block 處理、payment 業者的 HSM 上鏈、政府機敏資料加密的 <em>直接合規承諾</em>、不需要客戶端再做 HSM 認證 audit。</p>
<h2 id="排錯與失敗快速判讀">排錯與失敗快速判讀</h2>
<ul>
<li><strong>Crypto User credential 丟失</strong>：CU password 全公司只有一份、保管人離職 → AWS <em>不能 reset</em>、key material 永久不可用 — CU credential 要走 password manager + 多人持有、CO 有能力 revoke 舊 CU 建新 CU</li>
<li><strong>Cluster 只有單一 HSM instance</strong>：成本省了、單一 instance 故障 cluster 整個失效 — production 強制至少 2 個 instance、跨 AZ</li>
<li><strong>Backup 沒測過 restore</strong>：每天 automatic backup 跑、從未 restore 演練、DR 真要用時發現流程不通 — quarterly 演練 restore 到測試 cluster、驗證 key material 可用</li>
<li><strong>Custom Key Store 沒監控 CloudHSM health</strong>：CloudHSM cluster degraded 時、KMS Custom Key Store 跟著失效、application 看到 KMS 5xx — CloudWatch metric 監 <code>HsmsActive</code> / <code>HsmTemperature</code>、cluster health degrade 立即 alert</li>
<li><strong>PKCS#11 library 版本漂移</strong>：application host 的 client SDK 版本跟 cluster firmware 不相容、cryptographic operation 失敗 — version compatibility matrix 進 deployment pipeline、firmware upgrade 前先測 staging</li>
<li><strong>Quorum CO 全部同地點</strong>：5 個 CO 全在同一個辦公室、辦公室斷網 = quorum 不能組 — CO 跨 region / 跨組織分散</li>
<li><strong>Audit log 沒接 SIEM</strong>：CloudHSM activity 透過 CloudTrail + cluster audit log、沒接 SIEM 就無 forensic — CloudTrail 跟 cluster audit 都 push 到 SIEM（見 <a href="/blog/backend/07-security-data-protection/detection-coverage-and-signal-governance/" data-link-title="7.13 偵測覆蓋率與訊號治理" data-link-desc="定義偵測覆蓋、訊號品質與誤報成本的治理問題">7.13 偵測覆蓋率與訊號治理</a>）</li>
</ul>
<h2 id="何時改走其他服務">何時改走其他服務</h2>
<table>
  <thead>
      <tr>
          <th>需求形狀</th>
          <th>改走</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>一般 AWS workload 加密、無 dedicated 合規</td>
          <td><a href="/blog/backend/07-security-data-protection/vendors/aws-kms/" data-link-title="AWS KMS" data-link-desc="AWS 原生 key management service、envelope encryption / digital signing / Multi-Region Key、Key Policy &#43; Grant 雙軌授權">AWS KMS</a></td>
      </tr>
      <tr>
          <td>Azure-heavy + dedicated HSM 合規需求</td>
          <td>Azure Managed HSM（見上方對照表）</td>
      </tr>
      <tr>
          <td>GCP-heavy + dedicated HSM 合規需求</td>
          <td>Google Cloud HSM（Cloud KMS Protection Level=HSM）</td>
      </tr>
      <tr>
          <td>Secret storage + dynamic credential</td>
          <td><a href="/blog/backend/07-security-data-protection/vendors/hashicorp-vault/" data-link-title="HashiCorp Vault" data-link-desc="Self-hosted secret management 與 dynamic credential / encryption-as-a-service / PKI engine、跨雲跨環境的 secret 控制面">HashiCorp Vault</a> / <a href="/blog/backend/07-security-data-protection/vendors/aws-secrets-manager/" data-link-title="AWS Secrets Manager" data-link-desc="AWS 原生 secret store &#43; 內建 RDS / Redshift rotation Lambda、Resource Policy 跨帳號共享、KMS 加密">AWS Secrets Manager</a></td>
      </tr>
      <tr>
          <td>Certificate / PKI（不是 key custody）</td>
          <td><a href="/blog/backend/07-security-data-protection/vendors/aws-acm/" data-link-title="AWS ACM" data-link-desc="AWS-managed certificate provisioning、DNS validation &#43; auto-renewal、整合 ELB / CloudFront / API Gateway、Private CA 後端">AWS ACM</a> / <a href="/blog/backend/07-security-data-protection/vendors/cert-manager/" data-link-title="cert-manager" data-link-desc="K8s 原生 certificate lifecycle automation、支援 Let&#39;s Encrypt / Vault PKI / Venafi 等多 issuer、auto-renewal &#43; Challenge solver">cert-manager</a></td>
      </tr>
      <tr>
          <td>跨雲 unified key custody</td>
          <td><a href="/blog/backend/07-security-data-protection/vendors/hashicorp-vault/" data-link-title="HashiCorp Vault" data-link-desc="Self-hosted secret management 與 dynamic credential / encryption-as-a-service / PKI engine、跨雲跨環境的 secret 控制面">HashiCorp Vault</a> transit engine（雲廠中立）</td>
      </tr>
      <tr>
          <td>Key rotation 證據鏈</td>
          <td><a href="/blog/backend/07-security-data-protection/credential-rotation-scoped-evidence/" data-link-title="7.27 Credential Rotation with Scoped Evidence 實作示範" data-link-desc="以 webhook/API credential 輪替示範 scope map、證據欄位與回退窗口如何一起設計。">7.5 Credential Rotation Scoped Evidence</a></td>
      </tr>
  </tbody>
</table>
<h2 id="不在本頁內的主題">不在本頁內的主題</h2>
<ul>
<li>CloudHSM 完整 PKCS#11 / JCE API reference</li>
<li>CloudHSM Classic（舊版、已 EOL）的差異</li>
<li>每種合規法規（PCI HSM、HIPAA、FedRAMP）的逐條對應</li>
<li>CloudHSM CLI 跟 <code>cloudhsm_mgmt_util</code> 詳細指令</li>
<li>應用層使用 HSM-bound key 做 TLS termination 的 nginx / Apache 配置細節</li>
</ul>
<h2 id="案例回寫">案例回寫</h2>
<p>CloudHSM 在 07 案例庫沒有直接 vendor-level 事件、以下案例採對照引用：</p>
<table>
  <thead>
      <tr>
          <th>案例</th>
          <th>跟 CloudHSM 的關係（對照）</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><a href="/blog/backend/07-security-data-protection/red-team/cases/identity-access/microsoft-storm-0558-2023-signing-key-chain/" data-link-title="7.R7.1.5 Microsoft Storm-0558 2023：簽章金鑰鏈與郵件存取" data-link-desc="從簽章金鑰保護失效到雲端郵件存取，拆解身分信任鏈的關鍵控制點">Microsoft Storm-0558 Signing Key Chain</a></td>
          <td>核心對照 — CloudHSM 設計 <em>AWS 不持 key + key 不能 export</em> 是 Storm-0558 反設計、攻擊者進 cluster 也搬不走 key material、Quorum policy 阻單一 admin compromise</td>
      </tr>
      <tr>
          <td><a href="/blog/backend/07-security-data-protection/cases/failure-credential-rotation-without-scope/" data-link-title="7.C9 反例：憑證輪替未分 Scope" data-link-desc="憑證輪替若未分域分批，容易造成跨系統連鎖中斷。">Failure: Credential Rotation Without Scope</a></td>
          <td>CloudHSM key rotation 需要應用層配合 key alias 切換、不像 KMS 自動 rotation；scope map 跟雙軌驗證窗口更明顯、PKCS#11 client 散落 host 群時 rotation 要分批</td>
      </tr>
      <tr>
          <td><a href="/blog/backend/07-security-data-protection/cases/azure-ad-identity-control-plane-2021/" data-link-title="7.C3 Azure AD：2021 Identity Control-plane 事件" data-link-desc="身分控制面事件如何影響多服務信任鏈與回復優先序。">Azure AD Identity Control Plane 2021</a></td>
          <td>對照啟示 — HSM cluster 是 single point of compromise、cross-AZ topology + cross-region backup 是 <em>availability</em> 的設計依據、不是 confidentiality</td>
      </tr>
  </tbody>
</table>
<h2 id="下一步路由">下一步路由</h2>
<ul>
<li>上游：<a href="/blog/backend/07-security-data-protection/secrets-and-machine-credential-governance/" data-link-title="7.6 秘密管理與機器憑證治理" data-link-desc="以問題驅動方式整理 secret、token、key 與機器身份治理">7.6 秘密管理與機器憑證治理</a>、<a href="/blog/backend/07-security-data-protection/transport-trust-and-certificate-lifecycle/" data-link-title="7.5 傳輸信任與憑證生命週期" data-link-desc="以問題驅動方式整理傳輸信任鏈、會話完整性與憑證節奏">7.5 傳輸信任與憑證生命週期</a>（HSM 為 CA / signing key 的 FIPS-grade root custodian）、<a href="/blog/backend/07-security-data-protection/detection-coverage-and-signal-governance/" data-link-title="7.13 偵測覆蓋率與訊號治理" data-link-desc="定義偵測覆蓋、訊號品質與誤報成本的治理問題">7.13 偵測覆蓋率與訊號治理</a></li>
<li>平行：<a href="/blog/backend/07-security-data-protection/vendors/aws-kms/" data-link-title="AWS KMS" data-link-desc="AWS 原生 key management service、envelope encryption / digital signing / Multi-Region Key、Key Policy &#43; Grant 雙軌授權">AWS KMS</a>、<a href="/blog/backend/07-security-data-protection/vendors/google-cloud-kms/" data-link-title="Google Cloud KMS" data-link-desc="GCP 原生 key management service、KeyRing / CryptoKey Version 設計、CMEK 整合 &#43; Cloud HSM &#43; External Key Manager">Google Cloud KMS</a>、<a href="/blog/backend/07-security-data-protection/vendors/azure-key-vault/" data-link-title="Azure Key Vault" data-link-desc="Azure 三合一 service（Secret &#43; Key &#43; Certificate）、整合 Managed Identity &#43; Entra ID RBAC、Premium tier 走 HSM">Azure Key Vault</a></li>
<li>整合：<a href="/blog/backend/07-security-data-protection/vendors/hashicorp-vault/" data-link-title="HashiCorp Vault" data-link-desc="Self-hosted secret management 與 dynamic credential / encryption-as-a-service / PKI engine、跨雲跨環境的 secret 控制面">HashiCorp Vault</a>（CloudHSM 作為 Vault auto-unseal master key custodian）</li>
<li>整合：<a href="https://docs.aws.amazon.com/kms/latest/developerguide/custom-key-store-overview.html">KMS Custom Key Store</a>（KMS API + CloudHSM backing 雙重）</li>
<li>跨模組：<a href="/blog/backend/08-incident-response/vendors/" data-link-title="事故處理 Vendor 清單" data-link-desc="規劃 on-call、incident response、status page 與 postmortem 工具的服務頁撰寫順序與判準">8 事故處理 vendor 清單</a>（HSM 失效如何 routing 進 IR 流程）</li>
<li>官方：<a href="https://docs.aws.amazon.com/cloudhsm/">AWS CloudHSM Documentation</a></li>
</ul>
]]></content:encoded></item><item><title>6.6 OWASP LLM Top 10 對照圖</title><link>https://tarrragon.github.io/blog/llm/06-security/owasp-llm-top10-mapping/</link><pubDate>Tue, 12 May 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/llm/06-security/owasp-llm-top10-mapping/</guid><description>&lt;p>模組六前面六章是「個人 dev 視角」的本地 LLM 安全議題、用本 blog 自己的 framing 組織。但企業 / 合規 / vendor audit 場景的共同詞彙是 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/owasp-llm-top10/" data-link-title="OWASP LLM Top 10" data-link-desc="LLM 應用最常見 10 大資安風險的業界共同詞彙、跟模組六本地 dev 視角的 mapping 表">OWASP LLM Top 10&lt;/a>（2023 首發、2025 更新版）。本章把模組六 + 模組四相關章節對照到 OWASP 編號、補出「同議題、不同詞彙」的 mapping、讓讀者跟企業安全 team 溝通時能 align。&lt;/p>
&lt;h2 id="本章目標">本章目標&lt;/h2>
&lt;p>讀完本章後、你應該能：&lt;/p>
&lt;ol>
&lt;li>對照 OWASP LLM Top 10（LLM01-LLM10）跟自己工作流的具體風險。&lt;/li>
&lt;li>看到 enterprise security audit 報告用 OWASP 編號、能 map 到模組六章節找對應 control。&lt;/li>
&lt;li>知道哪些 OWASP 項目模組六完整覆蓋、哪些只覆蓋部分、哪些屬其他模組或 backend/07。&lt;/li>
&lt;/ol>
&lt;h2 id="owasp-llm-top-10-2025">OWASP LLM Top 10 2025&lt;/h2>
&lt;p>OWASP（Open Worldwide Application Security Project）的 LLM 應用安全清單、2025 更新版：&lt;/p>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>編號&lt;/th>
 &lt;th>名稱&lt;/th>
 &lt;th>一句話描述&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>LLM01&lt;/td>
 &lt;td>Prompt Injection&lt;/td>
 &lt;td>惡意指令藏進 LLM 會讀到的內容、間接影響模型行為&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>LLM02&lt;/td>
 &lt;td>Sensitive Information Disclosure&lt;/td>
 &lt;td>LLM 輸出洩漏訓練資料 / system prompt / PII / 機密&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>LLM03&lt;/td>
 &lt;td>Supply Chain&lt;/td>
 &lt;td>模型 / 訓練資料 / 工具 / dependency 供應鏈攻擊&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>LLM04&lt;/td>
 &lt;td>Data and Model Poisoning&lt;/td>
 &lt;td>訓練資料污染、模型行為被植入後門&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>LLM05&lt;/td>
 &lt;td>Improper Output Handling&lt;/td>
 &lt;td>LLM 輸出未驗證直接執行（XSS / SQLi / RCE）&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>LLM06&lt;/td>
 &lt;td>Excessive Agency&lt;/td>
 &lt;td>Agent 工具權限過大、副作用不可控&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>LLM07&lt;/td>
 &lt;td>System Prompt Leakage&lt;/td>
 &lt;td>System prompt 被使用者誘導露出&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>LLM08&lt;/td>
 &lt;td>Vector and Embedding Weaknesses&lt;/td>
 &lt;td>Vector DB / embedding pipeline 的攻擊面&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>LLM09&lt;/td>
 &lt;td>Misinformation&lt;/td>
 &lt;td>Hallucination / 過度信任 LLM 輸出&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>LLM10&lt;/td>
 &lt;td>Unbounded Consumption&lt;/td>
 &lt;td>Resource exhaustion / cost runaway（DoS / 燒錢）&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;blockquote>
&lt;p>&lt;strong>事實查核註&lt;/strong>：OWASP 列表會定期更新（2023 → 2025、未來會有新版）、引用前以 &lt;a href="https://owasp.org/www-project-top-10-for-large-language-model-applications/">OWASP LLM Top 10&lt;/a> 當前版為準。&lt;/p>&lt;/blockquote>
&lt;h2 id="詳細-mapping">詳細 mapping&lt;/h2>
&lt;h3 id="llm01-prompt-injection">LLM01 Prompt Injection&lt;/h3>
&lt;p>&lt;strong>OWASP 範圍&lt;/strong>：使用者輸入 / 外部資料 / RAG retrieved content 中藏指令、影響模型行為。包含 direct injection（user 自己注）跟 indirect injection（內容裡有人塞）。&lt;/p>
&lt;p>&lt;strong>模組六對應&lt;/strong>：&lt;/p>
&lt;ul>
&lt;li>&lt;strong>主章節&lt;/strong>：&lt;a href="https://tarrragon.github.io/blog/llm/06-security/prompt-injection-in-ide/" data-link-title="6.3 IDE 場景的 prompt injection" data-link-desc="個人 dev 場景下 IDE 寫 code 工作流的 prompt injection：codebase 內容、外部文件、剪貼簿作為攻擊面、跟雲端 LLM 場景的差異">6.3 IDE 場景的 prompt injection&lt;/a>&lt;/li>
&lt;li>&lt;strong>覆蓋&lt;/strong>：間接注入（codebase / 第三方依賴 / issue / 剪貼簿 / web fetch）、本地 LLM 跟雲端 LLM 的抵抗能力差異、IDE 場景的具體入口&lt;/li>
&lt;li>&lt;strong>不在 M6 範圍&lt;/strong>：production agent 場景的 prompt injection 後果（資料外洩 / 誤觸 tool）見 &lt;a href="https://tarrragon.github.io/blog/backend/07-security-data-protection/llm-prompt-injection-in-agent/" data-link-title="LLM Agent Prompt Injection 後果治理" data-link-desc="production LLM agent 場景的 prompt injection 後果：tool spec 設計、agent loop 限制、review checkpoint、跟 incident workflow 的接合">backend/07 LLM agent prompt injection&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>個人 dev 場景的最低 control&lt;/strong>：RAG exclude &lt;code>.env&lt;/code> / secrets、tool use 加 confirm（見 &lt;a href="https://tarrragon.github.io/blog/llm/06-security/tool-use-permission-model/" data-link-title="6.2 tool use 與 MCP server 的權限模型" data-link-desc="個人 dev 場景下 tool use / MCP server 的副作用權限：檔案系統 / shell / 網路存取邊界、第三方 MCP 信任、副作用的可逆性">6.2&lt;/a>）、agent loop 設 max steps、untrusted 來源內容明確標記&lt;/p></description><content:encoded><![CDATA[<p>模組六前面六章是「個人 dev 視角」的本地 LLM 安全議題、用本 blog 自己的 framing 組織。但企業 / 合規 / vendor audit 場景的共同詞彙是 <a href="/blog/llm/knowledge-cards/owasp-llm-top10/" data-link-title="OWASP LLM Top 10" data-link-desc="LLM 應用最常見 10 大資安風險的業界共同詞彙、跟模組六本地 dev 視角的 mapping 表">OWASP LLM Top 10</a>（2023 首發、2025 更新版）。本章把模組六 + 模組四相關章節對照到 OWASP 編號、補出「同議題、不同詞彙」的 mapping、讓讀者跟企業安全 team 溝通時能 align。</p>
<h2 id="本章目標">本章目標</h2>
<p>讀完本章後、你應該能：</p>
<ol>
<li>對照 OWASP LLM Top 10（LLM01-LLM10）跟自己工作流的具體風險。</li>
<li>看到 enterprise security audit 報告用 OWASP 編號、能 map 到模組六章節找對應 control。</li>
<li>知道哪些 OWASP 項目模組六完整覆蓋、哪些只覆蓋部分、哪些屬其他模組或 backend/07。</li>
</ol>
<h2 id="owasp-llm-top-10-2025">OWASP LLM Top 10 2025</h2>
<p>OWASP（Open Worldwide Application Security Project）的 LLM 應用安全清單、2025 更新版：</p>
<table>
  <thead>
      <tr>
          <th>編號</th>
          <th>名稱</th>
          <th>一句話描述</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>LLM01</td>
          <td>Prompt Injection</td>
          <td>惡意指令藏進 LLM 會讀到的內容、間接影響模型行為</td>
      </tr>
      <tr>
          <td>LLM02</td>
          <td>Sensitive Information Disclosure</td>
          <td>LLM 輸出洩漏訓練資料 / system prompt / PII / 機密</td>
      </tr>
      <tr>
          <td>LLM03</td>
          <td>Supply Chain</td>
          <td>模型 / 訓練資料 / 工具 / dependency 供應鏈攻擊</td>
      </tr>
      <tr>
          <td>LLM04</td>
          <td>Data and Model Poisoning</td>
          <td>訓練資料污染、模型行為被植入後門</td>
      </tr>
      <tr>
          <td>LLM05</td>
          <td>Improper Output Handling</td>
          <td>LLM 輸出未驗證直接執行（XSS / SQLi / RCE）</td>
      </tr>
      <tr>
          <td>LLM06</td>
          <td>Excessive Agency</td>
          <td>Agent 工具權限過大、副作用不可控</td>
      </tr>
      <tr>
          <td>LLM07</td>
          <td>System Prompt Leakage</td>
          <td>System prompt 被使用者誘導露出</td>
      </tr>
      <tr>
          <td>LLM08</td>
          <td>Vector and Embedding Weaknesses</td>
          <td>Vector DB / embedding pipeline 的攻擊面</td>
      </tr>
      <tr>
          <td>LLM09</td>
          <td>Misinformation</td>
          <td>Hallucination / 過度信任 LLM 輸出</td>
      </tr>
      <tr>
          <td>LLM10</td>
          <td>Unbounded Consumption</td>
          <td>Resource exhaustion / cost runaway（DoS / 燒錢）</td>
      </tr>
  </tbody>
</table>
<blockquote>
<p><strong>事實查核註</strong>：OWASP 列表會定期更新（2023 → 2025、未來會有新版）、引用前以 <a href="https://owasp.org/www-project-top-10-for-large-language-model-applications/">OWASP LLM Top 10</a> 當前版為準。</p></blockquote>
<h2 id="詳細-mapping">詳細 mapping</h2>
<h3 id="llm01-prompt-injection">LLM01 Prompt Injection</h3>
<p><strong>OWASP 範圍</strong>：使用者輸入 / 外部資料 / RAG retrieved content 中藏指令、影響模型行為。包含 direct injection（user 自己注）跟 indirect injection（內容裡有人塞）。</p>
<p><strong>模組六對應</strong>：</p>
<ul>
<li><strong>主章節</strong>：<a href="/blog/llm/06-security/prompt-injection-in-ide/" data-link-title="6.3 IDE 場景的 prompt injection" data-link-desc="個人 dev 場景下 IDE 寫 code 工作流的 prompt injection：codebase 內容、外部文件、剪貼簿作為攻擊面、跟雲端 LLM 場景的差異">6.3 IDE 場景的 prompt injection</a></li>
<li><strong>覆蓋</strong>：間接注入（codebase / 第三方依賴 / issue / 剪貼簿 / web fetch）、本地 LLM 跟雲端 LLM 的抵抗能力差異、IDE 場景的具體入口</li>
<li><strong>不在 M6 範圍</strong>：production agent 場景的 prompt injection 後果（資料外洩 / 誤觸 tool）見 <a href="/blog/backend/07-security-data-protection/llm-prompt-injection-in-agent/" data-link-title="LLM Agent Prompt Injection 後果治理" data-link-desc="production LLM agent 場景的 prompt injection 後果：tool spec 設計、agent loop 限制、review checkpoint、跟 incident workflow 的接合">backend/07 LLM agent prompt injection</a></li>
</ul>
<p><strong>個人 dev 場景的最低 control</strong>：RAG exclude <code>.env</code> / secrets、tool use 加 confirm（見 <a href="/blog/llm/06-security/tool-use-permission-model/" data-link-title="6.2 tool use 與 MCP server 的權限模型" data-link-desc="個人 dev 場景下 tool use / MCP server 的副作用權限：檔案系統 / shell / 網路存取邊界、第三方 MCP 信任、副作用的可逆性">6.2</a>）、agent loop 設 max steps、untrusted 來源內容明確標記</p>
<h3 id="llm02-sensitive-information-disclosure">LLM02 Sensitive Information Disclosure</h3>
<p><strong>OWASP 範圍</strong>：模型輸出洩漏訓練資料、system prompt、PII、商業機密、API key。</p>
<p><strong>模組六對應</strong>：</p>
<ul>
<li><strong>主章節</strong>：<a href="/blog/llm/06-security/cross-cloud-local-data-boundary/" data-link-title="6.4 跨雲端 / 本地的資料邊界" data-link-desc="個人 dev 場景下混用雲端 LLM 跟本地 LLM 時的 prompt 洩漏點：Continue.dev 多 provider 設定、隱私資料流、按敏感度分流的判讀">6.4 跨雲端 / 本地的資料邊界</a></li>
<li><strong>覆蓋</strong>：跨雲端 prompt 邊界、第三方 plugin 偷送 prompt、API key 不放在前端 JS</li>
<li><strong>補充章節</strong>：<a href="/blog/llm/04-applications/static-and-serverless-rag-deployment/" data-link-title="4.16 靜態 / serverless RAG deployment：架構選擇與資安取捨" data-link-desc="沒 backend 的場景怎麼做 RAG：四種 deployment 方案、API key 暴露問題、CORS / abuse / 第三方信任、跟模組六的 routing">4.16 靜態 / serverless RAG 資安</a> 的 API key 暴露段、user query 隱私段</li>
<li><strong>不在 M6 範圍</strong>：企業合規（GDPR / HIPAA / SOC 2）的逐條檢核屬 <a href="/blog/backend/07-security-data-protection/" data-link-title="模組七：資安與資料保護" data-link-desc="以問題驅動方式擴充資安知識網：先定義服務環節問題，再以案例作為觸發式參考">backend/07</a></li>
</ul>
<p><strong>個人 dev 場景的最低 control</strong>：本地敏感任務不送雲端、雲端 model 明確標記、API key 從環境變數讀</p>
<h3 id="llm03-supply-chain">LLM03 Supply Chain</h3>
<p><strong>OWASP 範圍</strong>：模型權重、訓練資料、tokenizer、dependency 套件、MCP server 等的供應鏈風險。</p>
<p><strong>模組六對應</strong>：</p>
<ul>
<li><strong>主章節</strong>：<a href="/blog/llm/06-security/model-supply-chain-trust/" data-link-title="6.0 模型供應鏈與信任邊界" data-link-desc="個人 dev 用本地 LLM 時的模型權重來源信任：GGUF 完整性、Hugging Face / Ollama registry 信任、量化版本污染、檔案完整性檢查">6.0 模型供應鏈與信任邊界</a></li>
<li><strong>覆蓋</strong>：GGUF / HuggingFace / Ollama registry 信任、量化版本污染、權重完整性、MCP server 信任</li>
<li><strong>補充</strong>：<a href="/blog/llm/04-applications/static-and-serverless-rag-deployment/" data-link-title="4.16 靜態 / serverless RAG deployment：架構選擇與資安取捨" data-link-desc="沒 backend 的場景怎麼做 RAG：四種 deployment 方案、API key 暴露問題、CORS / abuse / 第三方信任、跟模組六的 routing">4.16 靜態 RAG 資安</a> 的 client-side LLM 模型 CDN 信任段</li>
<li><strong>不在 M6 範圍</strong>：production 模型 release / SBOM / artifact provenance 屬 <a href="/blog/backend/07-security-data-protection/supply-chain-integrity-and-artifact-trust/" data-link-title="7.12 供應鏈完整性與 Artifact 信任" data-link-desc="定義 build provenance、artifact 信任與交付鏈風險問題">backend/07 supply chain</a></li>
</ul>
<p><strong>個人 dev 場景的最低 control</strong>：選主流作者 / 量化者、下載後 hash 比對、MCP server 跑 sandbox</p>
<h3 id="llm04-data-and-model-poisoning">LLM04 Data and Model Poisoning</h3>
<p><strong>OWASP 範圍</strong>：訓練資料被植入惡意樣本、fine-tune 資料污染、模型行為後門。</p>
<p><strong>模組六對應</strong>：<strong>部分覆蓋</strong></p>
<ul>
<li><strong>覆蓋</strong>：<a href="/blog/llm/06-security/model-supply-chain-trust/" data-link-title="6.0 模型供應鏈與信任邊界" data-link-desc="個人 dev 用本地 LLM 時的模型權重來源信任：GGUF 完整性、Hugging Face / Ollama registry 信任、量化版本污染、檔案完整性檢查">6.0 模型供應鏈</a> 的「量化版本污染」段、選主流作者的 framing</li>
<li><strong>不在 M6 範圍</strong>：自己 train base model 或 large-scale fine-tune 的資料治理屬研究 / production team 範圍、見 <a href="/blog/llm/03-theoretical-foundations/training-pipeline/" data-link-title="3.4 訓練流程：pre-train → SFT → RLHF" data-link-desc="LLM 的三階段訓練：預訓練、指令微調、人類反饋強化學習；各階段目標與最新替代方案">3.4 訓練流程</a> 概念 + <a href="/blog/llm/01-local-llm-services/hands-on/local-fine-tuning/" data-link-title="Hands-on：用 QLoRA 在本機 fine-tune coding 模型" data-link-desc="Apple Silicon Mac / PC 獨立 GPU 上跑 QLoRA fine-tune 的完整流程：環境、資料、訓練、evaluation、合併、部署到 Ollama">1.x hands-on local-fine-tune</a> 的小規模 fine-tune 注意事項</li>
</ul>
<p><strong>個人 dev 場景的最低 control</strong>：個人 dev 多用既有模型、threat model 不涵蓋自訓 base、用主流作者降低 poisoning 風險</p>
<h3 id="llm05-improper-output-handling">LLM05 Improper Output Handling</h3>
<p><strong>OWASP 範圍</strong>：把 LLM 輸出直接餵給下游系統（執行、render、SQL query）、若 LLM 輸出含惡意內容、下游 XSS / SQLi / RCE。</p>
<p><strong>模組六對應</strong>：</p>
<ul>
<li><strong>主章節</strong>：<a href="/blog/llm/06-security/tool-use-permission-model/" data-link-title="6.2 tool use 與 MCP server 的權限模型" data-link-desc="個人 dev 場景下 tool use / MCP server 的副作用權限：檔案系統 / shell / 網路存取邊界、第三方 MCP 信任、副作用的可逆性">6.2 tool use 與 MCP server 的權限模型</a></li>
<li><strong>覆蓋</strong>：tool 副作用範圍 spectrum、可逆性、confirm 機制</li>
<li><strong>補充原理</strong>：<a href="/blog/llm/04-applications/tool-use-principles/" data-link-title="4.3 Tool use 原理：LLM 跟外部世界互動" data-link-desc="Structured output 是 LLM 跨入工程系統的橋、function calling 取捨、為什麼本地小模型 tool use 表現崩潰">4.3 tool use 副作用範圍設計</a></li>
<li><strong>不在 M6 範圍</strong>：web app 場景的 output sanitization、CSP、render escape 屬一般 web 安全 + <a href="/blog/backend/07-security-data-protection/" data-link-title="模組七：資安與資料保護" data-link-desc="以問題驅動方式擴充資安知識網：先定義服務環節問題，再以案例作為觸發式參考">backend/07</a></li>
</ul>
<p><strong>個人 dev 場景的最低 control</strong>：副作用類 tool 加 confirm、shell 命令前 review、git track + diff</p>
<h3 id="llm06-excessive-agency">LLM06 Excessive Agency</h3>
<p><strong>OWASP 範圍</strong>：Agent 工具權限過大、副作用範圍超出需求、agent loop 太自主沒人類審查。</p>
<p><strong>模組六對應</strong>：</p>
<ul>
<li><strong>主章節</strong>：<a href="/blog/llm/06-security/tool-use-permission-model/" data-link-title="6.2 tool use 與 MCP server 的權限模型" data-link-desc="個人 dev 場景下 tool use / MCP server 的副作用權限：檔案系統 / shell / 網路存取邊界、第三方 MCP 信任、副作用的可逆性">6.2 tool use 權限</a> + <a href="/blog/llm/04-applications/agent-architecture/" data-link-title="4.4 Agent 架構原理" data-link-desc="Agent loop 結構、失敗模式、什麼任務適合 vs 不適合、跟人類審查的協作模型">4.4 Agent 跟人類審查協作</a></li>
<li><strong>覆蓋</strong>：sandbox / 白名單 / 副作用可逆性、agent 人類審查 spectrum、coding agent 的 permission boundary（<a href="/blog/llm/01-local-llm-services/hands-on/permission-boundary/" data-link-title="Hands-on：Ollama 改檔案 / 寫程式碼的權限邊界在哪" data-link-desc="四組對照實驗：Ollama 自己沒 FS / shell 權限、wrapper 才有；--dry-run / --confirm / --auto 三檔審查粒度的取捨">hands-on</a>）</li>
<li><strong>補充</strong>：<a href="/blog/llm/04-applications/coding-agent-harness/" data-link-title="4.17 Coding agent harness：scaffold / context engineering / subagent" data-link-desc="Coding agent 的內部設計：scaffold vs harness 分層、context budget 25% 規則、subagent 拓樸、跟 Claude Code / Cursor / Aider 的 mapping">4.17 coding agent harness</a> 的 permission boundary 設計</li>
</ul>
<p><strong>個人 dev 場景的最低 control</strong>：副作用 tool 加 confirm、agent max steps、production-level tool 不放在 dev agent 可達範圍</p>
<h3 id="llm07-system-prompt-leakage">LLM07 System Prompt Leakage</h3>
<p><strong>OWASP 範圍</strong>：使用者透過 prompt engineering 誘導 LLM 露出 system prompt 內容、暴露商業邏輯 / 提示工程 know-how。</p>
<p><strong>模組六對應</strong>：<strong>部分</strong></p>
<ul>
<li><strong>覆蓋</strong>：<a href="/blog/llm/04-applications/coding-agent-harness/" data-link-title="4.17 Coding agent harness：scaffold / context engineering / subagent" data-link-desc="Coding agent 的內部設計：scaffold vs harness 分層、context budget 25% 規則、subagent 拓樸、跟 Claude Code / Cursor / Aider 的 mapping">4.17 coding agent harness</a> 的 scaffold 設計提到 system prompt 是核心元件、但沒專門講 leakage</li>
<li><strong>不在 M6 範圍</strong>：sysprompt leak 主要是 production 商業祕密議題、屬 backend/07 / 各 vendor docs</li>
</ul>
<p><strong>個人 dev 場景的最低 control</strong>：不要把 secret（API key、internal info）寫在 system prompt、敏感邏輯放後端而非 prompt</p>
<h3 id="llm08-vector-and-embedding-weaknesses">LLM08 Vector and Embedding Weaknesses</h3>
<p><strong>OWASP 範圍</strong>：Vector DB 被污染、embedding model 被攻擊、retrieval pipeline 被注入毒文件、跨租戶 vector 污染。</p>
<p><strong>模組六對應</strong>：<strong>部分</strong></p>
<ul>
<li><strong>覆蓋</strong>：<a href="/blog/llm/04-applications/static-and-serverless-rag-deployment/" data-link-title="4.16 靜態 / serverless RAG deployment：架構選擇與資安取捨" data-link-desc="沒 backend 的場景怎麼做 RAG：四種 deployment 方案、API key 暴露問題、CORS / abuse / 第三方信任、跟模組六的 routing">4.16 靜態 RAG 資安</a> 的「第三方 SaaS 信任」段、跨租戶 isolation 議題</li>
<li><strong>補充原理</strong>：<a href="/blog/llm/04-applications/rag-principles/" data-link-title="4.1 RAG 原理：retrieval &#43; augmentation 模式" data-link-desc="為什麼模型需要外掛知識、語意相似 vs 字面相似、chunking 的本質取捨、retrieval 失敗的根本原因">4.1 RAG 原理</a> 的失敗模式、<a href="/blog/llm/04-applications/embedding-model-internals/" data-link-title="4.12 Embedding model 內部：訓練、選型、in-domain fine-tune" data-link-desc="Embedding model 怎麼訓練（contrastive learning &#43; hard negative mining）、怎麼挑（MTEB / 大小 / domain）、何時該自己 fine-tune">4.12 embedding model 內部</a></li>
<li><strong>不在 M6 範圍</strong>：production multi-tenant vector DB 屬 <a href="/blog/backend/07-security-data-protection/llm-multi-tenant-isolation/" data-link-title="LLM 多租戶推論隔離" data-link-desc="production LLM 服務的多租戶隔離：KV cache 不共享、log / model artifact 隔離、跨用戶 prompt 洩漏面">backend/07 多租戶 isolation</a></li>
</ul>
<p><strong>個人 dev 場景的最低 control</strong>：RAG ingestion 加 PII / secret filter、vector DB 選 search-only key、不混跨 user vector</p>
<h3 id="llm09-misinformation">LLM09 Misinformation</h3>
<p><strong>OWASP 範圍</strong>：LLM hallucination 被當真實、使用者過度信任輸出做 critical 決定。</p>
<p><strong>模組六對應</strong>：<strong>跨章節</strong></p>
<ul>
<li><strong>概念基礎</strong>：<a href="/blog/llm/knowledge-cards/hallucination/" data-link-title="Hallucination" data-link-desc="LLM 生成內容看起來合理但事實錯誤、引用不存在的來源、虛構不存在的 entity 的現象">hallucination 卡</a></li>
<li><strong>評估方法</strong>：<a href="/blog/llm/04-applications/benchmarking-and-evaluation/" data-link-title="4.14 Benchmarking 與評估方法論" data-link-desc="判讀 model card benchmark 數字、做自己工作流的 in-house benchmark、量測本地推論速度的完整方法論">4.14 benchmarking</a> + <a href="/blog/llm/04-applications/llm-as-judge/" data-link-title="4.21 LLM-as-Judge 評估方法" data-link-desc="LLM 評估 LLM 的 production eval 方法：rubric design、pairwise / direct scoring、三大 bias 緩解、跟 trace 串接的閉環、calibration">4.21 LLM-as-judge</a></li>
<li><strong>應用層緩解</strong>：<a href="/blog/llm/04-applications/rag-principles/" data-link-title="4.1 RAG 原理：retrieval &#43; augmentation 模式" data-link-desc="為什麼模型需要外掛知識、語意相似 vs 字面相似、chunking 的本質取捨、retrieval 失敗的根本原因">4.1 RAG</a>（給 LLM 外掛真實知識）、<a href="/blog/llm/04-applications/agent-architecture/" data-link-title="4.4 Agent 架構原理" data-link-desc="Agent loop 結構、失敗模式、什麼任務適合 vs 不適合、跟人類審查的協作模型">4.4 agent</a> 的人類審查 spectrum</li>
<li><strong>不在 M6 範圍</strong>：M6 預設 dev 自己驗證輸出、不專章寫</li>
</ul>
<p><strong>個人 dev 場景的最低 control</strong>：critical 任務人類 review、複雜推理用 reasoning model、code 生成必跑 test</p>
<h3 id="llm10-unbounded-consumption">LLM10 Unbounded Consumption</h3>
<p><strong>OWASP 範圍</strong>：Resource exhaustion（context / token / GPU memory 燒爆）、cost runaway（API quota 被偷用 / agent 無限 loop 燒錢）。</p>
<p><strong>模組六對應</strong>：<strong>部分</strong></p>
<ul>
<li><strong>覆蓋</strong>：<a href="/blog/llm/04-applications/static-and-serverless-rag-deployment/" data-link-title="4.16 靜態 / serverless RAG deployment：架構選擇與資安取捨" data-link-desc="沒 backend 的場景怎麼做 RAG：四種 deployment 方案、API key 暴露問題、CORS / abuse / 第三方信任、跟模組六的 routing">4.16 靜態 RAG 資安</a> 的「rate limit / abuse」段、靜態前端被 scrape 後燒 LLM quota 的情境</li>
<li><strong>補充</strong>：<a href="/blog/llm/04-applications/prompt-caching-engineering/" data-link-title="4.18 Prompt caching 工程實務：cost / latency 最大槓桿" data-link-desc="Prompt cache 怎麼運作、cache_control 設計、coding agent 跟 long-context 的 cache pattern、anti-pattern 跟 cache miss 訊號">4.18 prompt caching</a>（<a href="/blog/llm/knowledge-cards/prompt-cache/" data-link-title="Prompt Cache" data-link-desc="重複出現的 prompt prefix 在推論伺服器或 LLM 服務端被 cache、後續 query 跳過 prefill、大幅降 cost 跟 TTFT">Prompt Cache</a>、cost 控制）、<a href="/blog/llm/04-applications/agent-architecture/" data-link-title="4.4 Agent 架構原理" data-link-desc="Agent loop 結構、失敗模式、什麼任務適合 vs 不適合、跟人類審查的協作模型">4.4 agent</a> 的 termination（max steps / cost cap）、<a href="/blog/llm/04-applications/coding-agent-harness/" data-link-title="4.17 Coding agent harness：scaffold / context engineering / subagent" data-link-desc="Coding agent 的內部設計：scaffold vs harness 分層、context budget 25% 規則、subagent 拓樸、跟 Claude Code / Cursor / Aider 的 mapping">4.17 coding agent harness</a> 的 budget management</li>
<li><strong>不在 M6 範圍</strong>：production rate limiting / DDoS 防護屬 <a href="/blog/backend/07-security-data-protection/entrypoint-and-server-protection/" data-link-title="7.3 入口治理與伺服器防護" data-link-desc="以問題驅動方式整理對外入口、管理平面與伺服器邊界">backend/07 entrypoint protection</a></li>
</ul>
<p><strong>個人 dev 場景的最低 control</strong>：agent 設 max_steps / max_cost、API key 不放前端 JS、用 edge function 加 rate limit</p>
<h2 id="速查表">速查表</h2>
<p>按 OWASP 編號排序、給定 OWASP 項目可快速找對應 control 章節：</p>
<table>
  <thead>
      <tr>
          <th>OWASP</th>
          <th>主章節</th>
          <th>補充章節 / 卡片</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>LLM01</td>
          <td><a href="/blog/llm/06-security/prompt-injection-in-ide/" data-link-title="6.3 IDE 場景的 prompt injection" data-link-desc="個人 dev 場景下 IDE 寫 code 工作流的 prompt injection：codebase 內容、外部文件、剪貼簿作為攻擊面、跟雲端 LLM 場景的差異">6.3</a></td>
          <td><a href="/blog/llm/04-applications/agent-architecture/" data-link-title="4.4 Agent 架構原理" data-link-desc="Agent loop 結構、失敗模式、什麼任務適合 vs 不適合、跟人類審查的協作模型">4.4 agent loop</a>、<a href="/blog/llm/01-local-llm-services/hands-on/permission-boundary/" data-link-title="Hands-on：Ollama 改檔案 / 寫程式碼的權限邊界在哪" data-link-desc="四組對照實驗：Ollama 自己沒 FS / shell 權限、wrapper 才有；--dry-run / --confirm / --auto 三檔審查粒度的取捨">hands-on permission-boundary</a></td>
      </tr>
      <tr>
          <td>LLM02</td>
          <td><a href="/blog/llm/06-security/cross-cloud-local-data-boundary/" data-link-title="6.4 跨雲端 / 本地的資料邊界" data-link-desc="個人 dev 場景下混用雲端 LLM 跟本地 LLM 時的 prompt 洩漏點：Continue.dev 多 provider 設定、隱私資料流、按敏感度分流的判讀">6.4</a></td>
          <td><a href="/blog/llm/04-applications/static-and-serverless-rag-deployment/" data-link-title="4.16 靜態 / serverless RAG deployment：架構選擇與資安取捨" data-link-desc="沒 backend 的場景怎麼做 RAG：四種 deployment 方案、API key 暴露問題、CORS / abuse / 第三方信任、跟模組六的 routing">4.16 靜態 RAG</a>、<a href="/blog/llm/00-foundations/privacy-data-flow/" data-link-title="0.7 隱私 / 資安的資料流原理" data-link-desc="從「位置」到「資料流」的思考升級：信任邊界、合約模型、零信任原則套用到 LLM 工作流">0.7</a></td>
      </tr>
      <tr>
          <td>LLM03</td>
          <td><a href="/blog/llm/06-security/model-supply-chain-trust/" data-link-title="6.0 模型供應鏈與信任邊界" data-link-desc="個人 dev 用本地 LLM 時的模型權重來源信任：GGUF 完整性、Hugging Face / Ollama registry 信任、量化版本污染、檔案完整性檢查">6.0</a></td>
          <td><a href="/blog/llm/04-applications/static-and-serverless-rag-deployment/" data-link-title="4.16 靜態 / serverless RAG deployment：架構選擇與資安取捨" data-link-desc="沒 backend 的場景怎麼做 RAG：四種 deployment 方案、API key 暴露問題、CORS / abuse / 第三方信任、跟模組六的 routing">4.16 client-side LLM 段</a></td>
      </tr>
      <tr>
          <td>LLM04</td>
          <td><a href="/blog/llm/06-security/model-supply-chain-trust/" data-link-title="6.0 模型供應鏈與信任邊界" data-link-desc="個人 dev 用本地 LLM 時的模型權重來源信任：GGUF 完整性、Hugging Face / Ollama registry 信任、量化版本污染、檔案完整性檢查">6.0</a> 部分</td>
          <td><a href="/blog/llm/03-theoretical-foundations/training-pipeline/" data-link-title="3.4 訓練流程：pre-train → SFT → RLHF" data-link-desc="LLM 的三階段訓練：預訓練、指令微調、人類反饋強化學習；各階段目標與最新替代方案">3.4 訓練流程</a>、<a href="/blog/llm/01-local-llm-services/hands-on/local-fine-tuning/" data-link-title="Hands-on：用 QLoRA 在本機 fine-tune coding 模型" data-link-desc="Apple Silicon Mac / PC 獨立 GPU 上跑 QLoRA fine-tune 的完整流程：環境、資料、訓練、evaluation、合併、部署到 Ollama">hands-on fine-tune</a></td>
      </tr>
      <tr>
          <td>LLM05</td>
          <td><a href="/blog/llm/06-security/tool-use-permission-model/" data-link-title="6.2 tool use 與 MCP server 的權限模型" data-link-desc="個人 dev 場景下 tool use / MCP server 的副作用權限：檔案系統 / shell / 網路存取邊界、第三方 MCP 信任、副作用的可逆性">6.2</a></td>
          <td><a href="/blog/llm/04-applications/tool-use-principles/" data-link-title="4.3 Tool use 原理：LLM 跟外部世界互動" data-link-desc="Structured output 是 LLM 跨入工程系統的橋、function calling 取捨、為什麼本地小模型 tool use 表現崩潰">4.3 tool use 原理</a></td>
      </tr>
      <tr>
          <td>LLM06</td>
          <td><a href="/blog/llm/06-security/tool-use-permission-model/" data-link-title="6.2 tool use 與 MCP server 的權限模型" data-link-desc="個人 dev 場景下 tool use / MCP server 的副作用權限：檔案系統 / shell / 網路存取邊界、第三方 MCP 信任、副作用的可逆性">6.2</a> + <a href="/blog/llm/04-applications/agent-architecture/" data-link-title="4.4 Agent 架構原理" data-link-desc="Agent loop 結構、失敗模式、什麼任務適合 vs 不適合、跟人類審查的協作模型">4.4</a></td>
          <td><a href="/blog/llm/04-applications/coding-agent-harness/" data-link-title="4.17 Coding agent harness：scaffold / context engineering / subagent" data-link-desc="Coding agent 的內部設計：scaffold vs harness 分層、context budget 25% 規則、subagent 拓樸、跟 Claude Code / Cursor / Aider 的 mapping">4.17 coding agent harness</a>、<a href="/blog/llm/01-local-llm-services/hands-on/permission-boundary/" data-link-title="Hands-on：Ollama 改檔案 / 寫程式碼的權限邊界在哪" data-link-desc="四組對照實驗：Ollama 自己沒 FS / shell 權限、wrapper 才有；--dry-run / --confirm / --auto 三檔審查粒度的取捨">hands-on permission-boundary</a></td>
      </tr>
      <tr>
          <td>LLM07</td>
          <td><a href="/blog/llm/04-applications/coding-agent-harness/" data-link-title="4.17 Coding agent harness：scaffold / context engineering / subagent" data-link-desc="Coding agent 的內部設計：scaffold vs harness 分層、context budget 25% 規則、subagent 拓樸、跟 Claude Code / Cursor / Aider 的 mapping">4.17 scaffold</a> 部分</td>
          <td><a href="/blog/llm/knowledge-cards/system-prompt/" data-link-title="System Prompt" data-link-desc="LLM application 中由開發者預設、不直接顯示給使用者的指令層、定義模型的角色、行為規範、輸出格式">system prompt 卡</a></td>
      </tr>
      <tr>
          <td>LLM08</td>
          <td><a href="/blog/llm/04-applications/static-and-serverless-rag-deployment/" data-link-title="4.16 靜態 / serverless RAG deployment：架構選擇與資安取捨" data-link-desc="沒 backend 的場景怎麼做 RAG：四種 deployment 方案、API key 暴露問題、CORS / abuse / 第三方信任、跟模組六的 routing">4.16 靜態 RAG 資安</a> 部分</td>
          <td><a href="/blog/llm/04-applications/rag-principles/" data-link-title="4.1 RAG 原理：retrieval &#43; augmentation 模式" data-link-desc="為什麼模型需要外掛知識、語意相似 vs 字面相似、chunking 的本質取捨、retrieval 失敗的根本原因">4.1 RAG</a>、<a href="/blog/llm/04-applications/embedding-model-internals/" data-link-title="4.12 Embedding model 內部：訓練、選型、in-domain fine-tune" data-link-desc="Embedding model 怎麼訓練（contrastive learning &#43; hard negative mining）、怎麼挑（MTEB / 大小 / domain）、何時該自己 fine-tune">4.12 embedding</a></td>
      </tr>
      <tr>
          <td>LLM09</td>
          <td><a href="/blog/llm/knowledge-cards/hallucination/" data-link-title="Hallucination" data-link-desc="LLM 生成內容看起來合理但事實錯誤、引用不存在的來源、虛構不存在的 entity 的現象">hallucination 卡</a> + <a href="/blog/llm/04-applications/llm-as-judge/" data-link-title="4.21 LLM-as-Judge 評估方法" data-link-desc="LLM 評估 LLM 的 production eval 方法：rubric design、pairwise / direct scoring、三大 bias 緩解、跟 trace 串接的閉環、calibration">4.21</a></td>
          <td><a href="/blog/llm/04-applications/rag-principles/" data-link-title="4.1 RAG 原理：retrieval &#43; augmentation 模式" data-link-desc="為什麼模型需要外掛知識、語意相似 vs 字面相似、chunking 的本質取捨、retrieval 失敗的根本原因">4.1 RAG</a>、<a href="/blog/llm/04-applications/benchmarking-and-evaluation/" data-link-title="4.14 Benchmarking 與評估方法論" data-link-desc="判讀 model card benchmark 數字、做自己工作流的 in-house benchmark、量測本地推論速度的完整方法論">4.14 benchmarking</a></td>
      </tr>
      <tr>
          <td>LLM10</td>
          <td><a href="/blog/llm/04-applications/static-and-serverless-rag-deployment/" data-link-title="4.16 靜態 / serverless RAG deployment：架構選擇與資安取捨" data-link-desc="沒 backend 的場景怎麼做 RAG：四種 deployment 方案、API key 暴露問題、CORS / abuse / 第三方信任、跟模組六的 routing">4.16 abuse 段</a> + <a href="/blog/llm/04-applications/prompt-caching-engineering/" data-link-title="4.18 Prompt caching 工程實務：cost / latency 最大槓桿" data-link-desc="Prompt cache 怎麼運作、cache_control 設計、coding agent 跟 long-context 的 cache pattern、anti-pattern 跟 cache miss 訊號">4.18 caching</a></td>
          <td><a href="/blog/llm/04-applications/agent-architecture/" data-link-title="4.4 Agent 架構原理" data-link-desc="Agent loop 結構、失敗模式、什麼任務適合 vs 不適合、跟人類審查的協作模型">4.4 termination</a>、<a href="/blog/llm/04-applications/coding-agent-harness/" data-link-title="4.17 Coding agent harness：scaffold / context engineering / subagent" data-link-desc="Coding agent 的內部設計：scaffold vs harness 分層、context budget 25% 規則、subagent 拓樸、跟 Claude Code / Cursor / Aider 的 mapping">4.17 budget</a></td>
      </tr>
  </tbody>
</table>
<h2 id="跟-backend07-的分工再述">跟 backend/07 的分工再述</h2>
<p>模組六是「<strong>個人 dev 視角</strong>」、跟 <a href="/blog/backend/07-security-data-protection/" data-link-title="模組七：資安與資料保護" data-link-desc="以問題驅動方式擴充資安知識網：先定義服務環節問題，再以案例作為觸發式參考">backend 模組七 資安</a> 是分工關係（<a href="/blog/llm/06-security/routing-to-production-security/" data-link-title="6.5 跨進 production 的 routing 中樞" data-link-desc="個人 dev → 團隊 → production LLM 服務的三層演化、跟 backend/07 對應卡片的 routing 清單">6.5 routing-to-production-security</a> 有詳細）：</p>
<table>
  <thead>
      <tr>
          <th>場景</th>
          <th>看哪</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>個人 dev 在自己機器跑、純粹本地</td>
          <td>模組六 + 模組四</td>
      </tr>
      <tr>
          <td>個人 dev 用雲端 API、自己機器跑</td>
          <td>模組六 + 模組四 + <a href="/blog/llm/04-applications/static-and-serverless-rag-deployment/" data-link-title="4.16 靜態 / serverless RAG deployment：架構選擇與資安取捨" data-link-desc="沒 backend 的場景怎麼做 RAG：四種 deployment 方案、API key 暴露問題、CORS / abuse / 第三方信任、跟模組六的 routing">4.16 靜態 RAG 資安</a></td>
      </tr>
      <tr>
          <td>團隊內部部署 LLM、給內部用戶用</td>
          <td>模組六 + <a href="/blog/backend/07-security-data-protection/" data-link-title="模組七：資安與資料保護" data-link-desc="以問題驅動方式擴充資安知識網：先定義服務環節問題，再以案例作為觸發式參考">backend/07 部分</a></td>
      </tr>
      <tr>
          <td>Production multi-tenant LLM 服務</td>
          <td><a href="/blog/backend/07-security-data-protection/" data-link-title="模組七：資安與資料保護" data-link-desc="以問題驅動方式擴充資安知識網：先定義服務環節問題，再以案例作為觸發式參考">backend/07 全部</a>（多租戶 isolation、合規、incident）</td>
      </tr>
  </tbody>
</table>
<p>OWASP LLM Top 10 是兩邊共用詞彙、不限本地或 production。</p>
<h2 id="何時過時--何時不過時">何時過時 / 何時不過時</h2>
<p><strong>不會過時的部分</strong>：</p>
<ul>
<li>OWASP LLM Top 10 作為企業合規溝通共同詞彙的地位</li>
<li>本章 mapping 表的 framing（每個 OWASP 項對應模組六哪章 / 部分覆蓋 / 跨模組）</li>
<li>模組六跟 backend/07 的分工</li>
</ul>
<p><strong>會變的部分</strong>：</p>
<ul>
<li>OWASP 清單本身（2023 → 2025 → 未來新版、項目可能調整）</li>
<li>具體 vendor security audit 的範本（不同 vendor / industry 不同）</li>
<li>跟其他 framework（NIST AI RMF、ISO/IEC 42001）的對照</li>
</ul>
<h2 id="下一步">下一步</h2>
<p>本章是模組六最後一章。production 多租戶服務化資安見 <a href="/blog/backend/07-security-data-protection/" data-link-title="模組七：資安與資料保護" data-link-desc="以問題驅動方式擴充資安知識網：先定義服務環節問題，再以案例作為觸發式參考">backend 模組七</a>。</p>
]]></content:encoded></item><item><title>Cloud Logging 查詢、匯出與合規</title><link>https://tarrragon.github.io/blog/backend/04-observability/vendors/gcp-cloud-operations/cloud-logging-export-compliance/</link><pubDate>Mon, 22 Jun 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/backend/04-observability/vendors/gcp-cloud-operations/cloud-logging-export-compliance/</guid><description>&lt;blockquote>
&lt;p>本文是 &lt;a href="https://tarrragon.github.io/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations&lt;/a> 的 vendor deep article，深化 overview「Cloud Logging 結構化 logs」跟「BigQuery 匯出長期儲存」段。初次接觸 GCP 觀測的讀者建議先讀 &lt;a href="https://tarrragon.github.io/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations 服務頁&lt;/a>。&lt;/p>&lt;/blockquote>
&lt;h2 id="問題情境">問題情境&lt;/h2>
&lt;p>Cloud Logging 對 GCP 服務是預設開啟的 — GKE、Cloud Run、Cloud Functions 的 stdout/stderr 自動進 Cloud Logging，工程師不需要配置就能查。問題出在後續階段：log 量成長後的成本控制（GCP 的 ingestion 計費讓高 volume 服務成本快速累積）、合規需求要求特定 log 保留特定時間（healthcare / fintech 的 7 年留存）、organization-level 的 log 聚合與存取控制（多 project 集中 audit）、以及 PII 在 log 中的遮罩與加密。理解 Cloud Logging 的 router / sink 架構跟 retention bucket 才能從「預設全收」走向「可治理的 log pipeline」。&lt;/p>
&lt;h2 id="核心概念">核心概念&lt;/h2>
&lt;h3 id="log-router-與-sink">Log Router 與 Sink&lt;/h3>
&lt;p>Cloud Logging 的資料流是 &lt;strong>log entry → log router → sink → destination&lt;/strong>。每一筆 log 進入 Cloud Logging 後，log router 根據 inclusion filter 跟 exclusion filter 決定這筆 log 送到哪些 destination。&lt;/p>
&lt;p>&lt;strong>Sink&lt;/strong> 是 log router 的輸出端點。每個 GCP project 預設有兩個 sink：&lt;code>_Required&lt;/code>（admin activity audit log、system event，不可關閉）和 &lt;code>_Default&lt;/code>（其他所有 log、送到 &lt;code>_Default&lt;/code> log bucket、可修改 filter）。工程師可以建立自訂 sink，把符合條件的 log 送到 BigQuery、Cloud Storage、Pub/Sub 或 Splunk。&lt;/p>
&lt;p>&lt;strong>Exclusion filter&lt;/strong> 在 log router 層攔截 — 被排除的 log 不會寫入任何 sink destination，也不計入 ingestion 計費。這是成本控制的第一道防線。&lt;/p>
&lt;p>&lt;strong>Inclusion filter&lt;/strong> 在 sink 層生效 — 只有符合 filter 的 log 會送到該 sink 的 destination。&lt;/p>
&lt;p>路由順序很重要：exclusion filter 先執行（全域攔截），然後 &lt;code>_Required&lt;/code> sink 攔走必留 log，然後 &lt;code>_Default&lt;/code> sink 跟自訂 sink 各自的 inclusion filter 平行執行。一筆 log 可以同時送到多個 sink。&lt;/p>
&lt;h3 id="retention-與-log-bucket">Retention 與 Log Bucket&lt;/h3>
&lt;p>Cloud Logging 的儲存單位是 &lt;strong>log bucket&lt;/strong>。每個 project 預設有兩個 bucket：&lt;/p></description><content:encoded><![CDATA[<blockquote>
<p>本文是 <a href="/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations</a> 的 vendor deep article，深化 overview「Cloud Logging 結構化 logs」跟「BigQuery 匯出長期儲存」段。初次接觸 GCP 觀測的讀者建議先讀 <a href="/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations 服務頁</a>。</p></blockquote>
<h2 id="問題情境">問題情境</h2>
<p>Cloud Logging 對 GCP 服務是預設開啟的 — GKE、Cloud Run、Cloud Functions 的 stdout/stderr 自動進 Cloud Logging，工程師不需要配置就能查。問題出在後續階段：log 量成長後的成本控制（GCP 的 ingestion 計費讓高 volume 服務成本快速累積）、合規需求要求特定 log 保留特定時間（healthcare / fintech 的 7 年留存）、organization-level 的 log 聚合與存取控制（多 project 集中 audit）、以及 PII 在 log 中的遮罩與加密。理解 Cloud Logging 的 router / sink 架構跟 retention bucket 才能從「預設全收」走向「可治理的 log pipeline」。</p>
<h2 id="核心概念">核心概念</h2>
<h3 id="log-router-與-sink">Log Router 與 Sink</h3>
<p>Cloud Logging 的資料流是 <strong>log entry → log router → sink → destination</strong>。每一筆 log 進入 Cloud Logging 後，log router 根據 inclusion filter 跟 exclusion filter 決定這筆 log 送到哪些 destination。</p>
<p><strong>Sink</strong> 是 log router 的輸出端點。每個 GCP project 預設有兩個 sink：<code>_Required</code>（admin activity audit log、system event，不可關閉）和 <code>_Default</code>（其他所有 log、送到 <code>_Default</code> log bucket、可修改 filter）。工程師可以建立自訂 sink，把符合條件的 log 送到 BigQuery、Cloud Storage、Pub/Sub 或 Splunk。</p>
<p><strong>Exclusion filter</strong> 在 log router 層攔截 — 被排除的 log 不會寫入任何 sink destination，也不計入 ingestion 計費。這是成本控制的第一道防線。</p>
<p><strong>Inclusion filter</strong> 在 sink 層生效 — 只有符合 filter 的 log 會送到該 sink 的 destination。</p>
<p>路由順序很重要：exclusion filter 先執行（全域攔截），然後 <code>_Required</code> sink 攔走必留 log，然後 <code>_Default</code> sink 跟自訂 sink 各自的 inclusion filter 平行執行。一筆 log 可以同時送到多個 sink。</p>
<h3 id="retention-與-log-bucket">Retention 與 Log Bucket</h3>
<p>Cloud Logging 的儲存單位是 <strong>log bucket</strong>。每個 project 預設有兩個 bucket：</p>
<ul>
<li><code>_Required</code> bucket：admin activity audit log 跟 system event，保留 400 天，不可刪除或修改 retention</li>
<li><code>_Default</code> bucket：其他所有 log，預設保留 30 天，可調整為 1-3650 天</li>
</ul>
<p>自訂 log bucket 可以設定不同 retention 期。常見用法：把 application log 留 30 天、把 audit log 留 7 年（送到自訂 bucket 或 BigQuery）。</p>
<p>Cloud Logging 的 ingestion 計費跟 storage 計費是分開的。前 50 GiB/month per billing account 的 ingestion 免費；超過後按 ingestion volume 計費。<code>_Required</code> log 的 ingestion 免費。Storage 在 <code>_Default</code> bucket 的前 0.5 GiB 免費，自訂 bucket 按用量計費。</p>
<p>成本治理判讀：高 volume 服務（例如 GKE 的 container stdout）的成本主要來自 ingestion，而非 storage。Exclusion filter 攔掉不需要的 log 是最直接的降成本方式。</p>
<h3 id="查詢語言">查詢語言</h3>
<p>Cloud Logging 的查詢語言用在 Logs Explorer 跟 gcloud CLI：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">resource.type=&#34;k8s_container&#34;
</span></span><span class="line"><span class="ln">2</span><span class="cl">resource.labels.cluster_name=&#34;prod-us-central1&#34;
</span></span><span class="line"><span class="ln">3</span><span class="cl">severity&gt;=ERROR
</span></span><span class="line"><span class="ln">4</span><span class="cl">jsonPayload.order_id=&#34;ord-12345&#34;
</span></span><span class="line"><span class="ln">5</span><span class="cl">timestamp&gt;=&#34;2026-06-22T00:00:00Z&#34;</span></span></code></pre></div><p>語法特點：field path 用 <code>.</code> 分隔、支援 comparison operators（<code>=</code> / <code>!=</code> / <code>&gt;</code> / <code>&gt;=</code> / <code>&lt;</code> / <code>&lt;=</code>）、支援 boolean（<code>AND</code> / <code>OR</code> / <code>NOT</code>）、支援 regex（<code>=~</code> / <code>!~</code>）。</p>
<p>跟 KQL（Elastic）或 LogQL（Loki）相比，Cloud Logging 查詢語言更接近 structured filter 而非 full-text search。Full-text 搜尋要用 <code>textPayload:</code> 或 <code>jsonPayload:</code> prefix。進階分析（aggregation、time bucketing、join）需要匯出到 BigQuery 後用 SQL 做。</p>
<h2 id="配置-step-by-step">配置 step-by-step</h2>
<h3 id="organization-level-log-聚合">Organization-level log 聚合</h3>
<p>多 project 環境下，集中 log 的標準做法是在 organization 或 folder level 建立 aggregated sink：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">gcloud logging sinks create org-audit-sink \
</span></span><span class="line"><span class="ln">2</span><span class="cl">  bigquery.googleapis.com/projects/central-audit/datasets/org_audit_logs \
</span></span><span class="line"><span class="ln">3</span><span class="cl">  --organization=123456789 \
</span></span><span class="line"><span class="ln">4</span><span class="cl">  --include-children \
</span></span><span class="line"><span class="ln">5</span><span class="cl">  --log-filter=&#39;logName:&#34;cloudaudit.googleapis.com&#34;&#39;</span></span></code></pre></div><p><code>--include-children</code> 讓 organization 下所有 project、folder 的符合 log 都送到同一個 BigQuery dataset。Sink 的 service account 需要 destination 的寫入權限（BigQuery Data Editor）。</p>
<p>適用場景：SOC 團隊需要跨 project 的 audit log 查詢、compliance team 需要集中的 data access log 存檔、security team 需要異常 IAM 變更的全域偵測。</p>
<h3 id="data-access-audit-logs-啟用">Data Access Audit Logs 啟用</h3>
<p>GCP 的 audit log 分三類：</p>
<ul>
<li><strong>Admin Activity</strong>：對資源的管理操作（建立 / 刪除 / 修改 IAM）。預設開啟、不可關閉、不計費。</li>
<li><strong>Data Access</strong>：對資源的讀取操作（BigQuery query、GCS read、Cloud SQL connect）。預設關閉（除 BigQuery）、需手動啟用、計費。</li>
<li><strong>System Event</strong>：GCP 系統自動操作。預設開啟、不可關閉、不計費。</li>
</ul>
<p>Data Access audit log 的啟用是 per-service、per-project（或 org level）。啟用後 log 量會大幅增加 — 一個高 QPS 的 Cloud SQL 服務可能每秒產生數百筆 data access log。成本跟 volume 判讀要先做。</p>
<p>建議做法：先對 security-sensitive 服務啟用（IAM / KMS / Cloud SQL / GCS），其他服務按需啟用。用 exclusion filter 精細控制 — 例如只保留 <code>ADMIN_READ</code> 跟 <code>DATA_WRITE</code>、排除 <code>DATA_READ</code>（read 量通常遠大於 write）。</p>
<h3 id="vpc-flow-logs-與-dns-logs-的觀測用途">VPC Flow Logs 與 DNS Logs 的觀測用途</h3>
<p>VPC Flow Logs 記錄每一筆通過 VPC 的網路流量元資料（src/dst IP、port、protocol、bytes、packets）。啟用方式是 per-subnet 設定、支援 sampling rate（100% / 50% / 10%）。</p>
<p>DNS Logs 記錄 VPC 內的 DNS 查詢（query name、response code、source VM）。啟用方式是 per-VPC 或 per-policy 設定。</p>
<p>觀測用途：</p>
<ul>
<li><strong>異常流量偵測</strong>：VPC Flow Logs 送到 BigQuery 後用 SQL 找出異常流量模式（大量對外連線、非預期 port、跨 region 資料傳輸）</li>
<li><strong>網路效能分析</strong>：量測 inter-service latency、跨 AZ 流量比例</li>
<li><strong>安全稽核</strong>：DNS Logs 偵測 DNS tunneling 或 C2 callback</li>
</ul>
<p>成本注意：VPC Flow Logs 在高流量服務上的 ingestion 量非常大。100% sampling + 高 QPS 服務可能每天產生 TB 級 log。建議用 sampling rate 控制、或只對 security-sensitive subnet 啟用 100%。</p>
<h3 id="自建-vs-managed-pipeline-的取捨">自建 vs managed pipeline 的取捨</h3>
<p><a href="/blog/backend/04-observability/cases/cloudflare-internal-observability-architecture/" data-link-title="4.C12 Cloudflare：內部觀測平台的三層能力" data-link-desc="全球 300&#43; edge 節點的觀測架構，把 monitoring、analytics 與 forensics 拆成三個獨立能力層。">Cloudflare 觀測案例</a>展示了自建觀測 pipeline 的理由 — 全球 300+ edge locations、每秒數十億 request 的規模下，SaaS 觀測平台的帳單不合理，自建 pipeline 的 compute 成本反而更低。</p>
<p>但多數團隊的結論是反過來的。GCP 環境下，Cloud Logging 的 managed pipeline（log entry → router → sink → BigQuery / Cloud Storage）幾乎不需要維運人力。自建等價的 pipeline（Fluent Bit → Kafka → Elasticsearch / BigQuery）需要維運 Kafka cluster、Elasticsearch cluster、Fluent Bit DaemonSet 的升級與監控。</p>
<p>判斷分水嶺的兩個維度：</p>
<table>
  <thead>
      <tr>
          <th>維度</th>
          <th>偏向 managed（Cloud Logging）</th>
          <th>偏向自建</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Log volume</td>
          <td>&lt; 1 TB/day</td>
          <td>&gt; 10 TB/day（SaaS ingestion 成本超過自建 compute）</td>
      </tr>
      <tr>
          <td>查詢需求</td>
          <td>Logs Insights + 偶爾 BigQuery</td>
          <td>需要 Elasticsearch 的全文搜尋 + aggregation + visualization</td>
      </tr>
  </tbody>
</table>
<p>1-10 TB/day 的灰色地帶取決於查詢模式 — 如果 Logs Insights 能滿足 90% 的查詢、BigQuery 能處理剩下 10% 的分析，不需要自建。如果團隊需要 Kibana dashboard、Elasticsearch alerting、或跨 cloud 的統一 log backend，自建可能更合理。</p>
<h3 id="healthcare-分層-retention-在-gcp-的實現">Healthcare 分層 retention 在 GCP 的實現</h3>
<p><a href="/blog/backend/04-observability/cases/healthcare-access-traceability-and-retention/" data-link-title="Healthcare：存取可追溯性與保留邊界" data-link-desc="在資料主權限制下，建立可追溯存取證據與分層保留策略。">Healthcare 案例</a>的核心需求是分層 retention — 不同 log 類型有不同的法規留存要求（data access audit log 要 6 年+、application operational log 要 90 天、debug log 要 7 天）。</p>
<p>在 GCP 上用三層架構實現：</p>
<p><strong>Hot 層（Cloud Logging custom bucket）</strong>：application log 保留 90 天、audit log 保留 1 年。設定 custom log bucket + retention。優點是 Logs Explorer 直接可查、延遲低。</p>
<p><strong>Warm 層（BigQuery）</strong>：audit log sink 到 BigQuery dataset，BigQuery 的 partition expiration 設 2 年。需要分析跟 correlation 時用 SQL 查。成本低於 Cloud Logging storage。</p>
<p><strong>Cold 層（Cloud Storage + Object Lifecycle）</strong>：BigQuery 的 scheduled export 或直接 Cloud Logging sink 到 GCS bucket。Object lifecycle rule 把 90 天以上的 object 轉 Nearline / Coldline / Archive class。最終刪除設定在 7 年。</p>
<p>三層各自的 access control 要獨立設定 — cold 層的 GCS bucket 只有 compliance team 有讀取權限，application team 看不到。CMEK 在三層都啟用（Cloud Logging custom bucket 的 CMEK + BigQuery dataset 的 CMEK + GCS bucket 的 CMEK），金鑰由安全團隊集中管理。</p>
<h3 id="pii-治理與-cmek">PII 治理與 CMEK</h3>
<p>Cloud Logging 中的 PII 治理有三層：</p>
<p><strong>第一層：不寫入</strong>。Application 端在 log 之前就遮罩 PII（email → <code>***@***.com</code>、credit card → last 4 digits）。這是最有效的方式，因為一旦寫入 Cloud Logging，即使後續刪除 log entry，在 deletion 前可能已經被 sink 匯出到 BigQuery / GCS。</p>
<p><strong>第二層：log 層過濾</strong>。用 exclusion filter 把含 PII 的 log field 排除（例如排除特定 jsonPayload field）。限制是 Cloud Logging 的 exclusion filter 只能排除整筆 log entry，不能 redact 單一 field。需要 field-level redaction 的話，在 OTel Collector 或 Fluentd 層做 processor 處理、再送到 Cloud Logging。</p>
<p><strong>第三層：加密</strong>。Cloud Logging 預設用 Google-managed encryption。需要自管金鑰的場景（HIPAA / PCI-DSS / 金融監管）用 CMEK（Customer-Managed Encryption Keys）。CMEK 設定在 log bucket 層 — 自訂 log bucket 可以指定 Cloud KMS key。<code>_Default</code> bucket 也可以啟用 CMEK（需要把 <code>_Default</code> bucket 的 region 從 <code>global</code> 改成特定 region）。</p>
<p>存取控制：Cloud Logging 的 IAM role 分 <code>roles/logging.viewer</code>（讀 log）、<code>roles/logging.privateLogViewer</code>（讀含 data access 的 log）、<code>roles/logging.admin</code>（管理 sink / bucket / filter）。Audit log 的存取用 <code>roles/logging.privateLogViewer</code>、不是一般的 <code>roles/logging.viewer</code>。對應 <a href="/blog/backend/07-security-data-protection/audit-trail-and-accountability-boundary/" data-link-title="7.7 稽核追蹤與責任邊界" data-link-desc="以問題驅動方式整理高風險操作追蹤、可回查與責任切分">稽核追蹤與責任邊界</a> 的 GCP 實作。</p>
<h2 id="故障演練與邊界">故障演練與邊界</h2>
<h3 id="exclusion-filter-設太寬重要-log-被丟掉">Exclusion filter 設太寬，重要 log 被丟掉</h3>
<p><strong>觸發條件</strong>：為了降成本建立 exclusion filter，但 filter expression 太寬泛（例如排除整個 severity=INFO），連帶排除了 business-critical 的 info-level log。</p>
<p><strong>表現</strong>：事故時查不到關鍵 log、audit 證據鏈斷裂。因為 exclusion filter 在 ingestion 前執行，被排除的 log 無法回補。</p>
<p><strong>預防</strong>：exclusion filter 建立後先用 <code>gcloud logging read</code> 驗證哪些 log 會被排除。用 Logs Explorer 的 preview 功能確認 filter 不會命中關鍵 log。對 audit log 和 security log 不設 exclusion filter。</p>
<h3 id="bigquery-sink-匯出成本失控">BigQuery sink 匯出成本失控</h3>
<p><strong>觸發條件</strong>：org-level aggregated sink 把所有 log 送到 BigQuery，沒有 inclusion filter 限制。</p>
<p><strong>表現</strong>：BigQuery storage 跟 streaming insert 成本暴增。一個中型 GKE cluster 每天可能產生 100+ GB 的 container log，全部送 BigQuery 的月成本可能超過 Cloud Logging 本身。</p>
<p><strong>修復</strong>：在 sink 加 inclusion filter（只送 audit log 或 error-level log 到 BigQuery）。高 volume 的 application log 送 Cloud Storage（成本更低），需要查詢時用 BigQuery external table 做 federated query。</p>
<h3 id="log-entry-size-超過限制">Log entry size 超過限制</h3>
<p><strong>觸發條件</strong>：application log 寫入超過 256 KB 的單筆 log entry（Cloud Logging 的 per-entry 上限）。</p>
<p><strong>表現</strong>：超過限制的 log entry 被截斷或拒絕寫入。</p>
<p><strong>修復</strong>：application 端控制 log entry size — 大型 payload（request body / response body / stack trace）做 truncation 後再 log。需要完整內容的場景，把 payload 寫到 GCS、log 中只留 GCS URI。</p>
<h2 id="容量與成本">容量與成本</h2>
<table>
  <thead>
      <tr>
          <th>計費項目</th>
          <th>免費額度</th>
          <th>超出後計費</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Ingestion（非 <code>_Required</code>）</td>
          <td>50 GiB/month per billing account</td>
          <td>per GiB ingested</td>
      </tr>
      <tr>
          <td>Storage（<code>_Default</code> bucket）</td>
          <td>0.5 GiB</td>
          <td>per GiB-month</td>
      </tr>
      <tr>
          <td>Storage（custom bucket）</td>
          <td>無免費額度</td>
          <td>per GiB-month</td>
      </tr>
      <tr>
          <td><code>_Required</code> log ingestion</td>
          <td>不計費</td>
          <td>不計費</td>
      </tr>
      <tr>
          <td>BigQuery sink streaming insert</td>
          <td>依 BigQuery 計費</td>
          <td>per GB inserted</td>
      </tr>
  </tbody>
</table>
<p>成本最佳化優先序：</p>
<ol>
<li><strong>Exclusion filter</strong>：攔掉不需要的 log、最直接</li>
<li><strong>降 log level</strong>：application 端把 verbose debug log 關掉</li>
<li><strong>Sampling</strong>：高 QPS 服務的 request log 做 sampling（在 application 端或 OTel Collector 層）</li>
<li><strong>BigQuery sink filter</strong>：只送需要長期分析的 log 到 BigQuery</li>
<li><strong>Cloud Storage sink</strong>：高 volume + 低查詢頻率的 log 送 GCS、按需用 BigQuery external table 查</li>
</ol>
<h2 id="整合與下一步">整合與下一步</h2>
<ul>
<li><a href="/blog/backend/04-observability/vendors/gcp-cloud-operations/" data-link-title="GCP Cloud Operations" data-link-desc="GCP 原生觀測性套件（前 Stackdriver）：Logging / Monitoring / Trace / Profiler">GCP Cloud Operations 服務頁</a>：overview 與日常操作</li>
<li><a href="../cloud-monitoring-mql/">Cloud Monitoring Metrics Model 與 MQL</a>：同 vendor 的 metrics 面</li>
<li><a href="/blog/backend/04-observability/audit-log-governance/" data-link-title="4.12 Audit Log 邊界與 PII 治理" data-link-desc="把稽核訊號從 operational log 拆出、按法規與不變性治理">4.12 Audit Log 邊界與 PII 治理</a>：跨 vendor 的 audit log 治理策略</li>
<li><a href="/blog/backend/04-observability/cases/fintech-audit-evidence-observability/" data-link-title="FinTech：審計證據鏈的可觀測性設計" data-link-desc="把交易與存取事件轉成可回查證據，降低合規審核與事故判讀落差。">4.C1 Fintech audit evidence</a>：審計證據鏈的案例回寫</li>
<li><a href="/blog/backend/04-observability/cases/healthcare-access-traceability-and-retention/" data-link-title="Healthcare：存取可追溯性與保留邊界" data-link-desc="在資料主權限制下，建立可追溯存取證據與分層保留策略。">4.C3 Healthcare retention</a>：長期保留的合規設計</li>
<li><a href="/blog/backend/07-security-data-protection/" data-link-title="模組七：資安與資料保護" data-link-desc="以問題驅動方式擴充資安知識網：先定義服務環節問題，再以案例作為觸發式參考">07 security 模組</a>：data access audit log 的安全面</li>
</ul>
]]></content:encoded></item><item><title>LLM Log 與 PII 治理</title><link>https://tarrragon.github.io/blog/backend/07-security-data-protection/llm-log-and-pii-governance/</link><pubDate>Tue, 12 May 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/backend/07-security-data-protection/llm-log-and-pii-governance/</guid><description>&lt;p>本章的責任是把 LLM 服務的 prompt log / response log / context cache 在累積、儲存、保留、刪除四個階段的 PII 治理拆成可操作的判讀。通用詞彙見 backend &lt;a href="https://tarrragon.github.io/blog/backend/knowledge-cards/pii/" data-link-title="PII" data-link-desc="說明可識別個人的資料如何影響權限、遮罩、保留與稽核">pii&lt;/a>、&lt;a href="https://tarrragon.github.io/blog/backend/knowledge-cards/data-masking/" data-link-title="Data Masking" data-link-desc="說明敏感資料如何在顯示、匯出、log 與測試資料中降低暴露">data-masking&lt;/a>、&lt;a href="https://tarrragon.github.io/blog/backend/knowledge-cards/data-classification/" data-link-title="Data Classification" data-link-desc="說明資料分級如何決定保護、存取、保留與匯出規則">data-classification&lt;/a>、&lt;a href="https://tarrragon.github.io/blog/backend/knowledge-cards/audit-log/" data-link-title="Audit Log" data-link-desc="說明高風險操作如何留下可追溯、可稽核的紀錄">audit-log&lt;/a> 卡；模型輸出虛構 PII 的特殊議題見 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/hallucination/" data-link-title="Hallucination" data-link-desc="LLM 生成內容看起來合理但事實錯誤、引用不存在的來源、虛構不存在的 entity 的現象">hallucination&lt;/a> 卡。一般資料保護跟 masking 流程沿用 &lt;a href="https://tarrragon.github.io/blog/backend/07-security-data-protection/data-protection-and-masking-governance/" data-link-title="7.4 資料保護與遮罩治理" data-link-desc="以問題驅動方式整理資料分級、遮罩、匯出與備份治理">7.4 資料保護與遮罩治理&lt;/a> 跟 &lt;a href="https://tarrragon.github.io/blog/backend/07-security-data-protection/data-residency-deletion-and-evidence-chain/" data-link-title="7.11 資料駐留、刪除與證據鏈" data-link-desc="定義跨區資料駐留、刪除請求與可驗證證據鏈問題">7.8 資料居住地、刪除與證據鏈&lt;/a>、本章聚焦 LLM 場景下的特殊性：prompt 含豐富使用者意圖、response 可能 hallucinate 出 PII、KV cache 跟 context cache 是非典型 log 載體。&lt;/p>
&lt;h2 id="本章寫作邊界">本章寫作邊界&lt;/h2>
&lt;p>本章聚焦 production LLM 服務的 log / cache / context 中的 PII 治理特殊性。個人 dev 場景的隱私資料流見 &lt;a href="https://tarrragon.github.io/blog/llm/00-foundations/privacy-data-flow/" data-link-title="0.7 隱私 / 資安的資料流原理" data-link-desc="從「位置」到「資料流」的思考升級：信任邊界、合約模型、零信任原則套用到 LLM 工作流">0.7 隱私資料流&lt;/a>；通用資料保護見 7.4；資料居住地與刪除證據鏈見 7.8。&lt;/p>
&lt;h2 id="本章-threat-scope">本章 threat scope&lt;/h2>
&lt;p>&lt;strong>In-scope&lt;/strong>：prompt log 累積的 PII、response log 中模型 hallucinate 出的 PII、context cache 跟 KV cache 中的殘留、跨地區資料居住地對應、log 保留期限與刪除證據。&lt;/p>
&lt;p>&lt;strong>Out-of-scope&lt;/strong>（路由到他章）:&lt;/p>
&lt;ul>
&lt;li>通用資料保護與 masking → &lt;a href="../data-protection-and-masking-governance/">7.4 data-protection-and-masking-governance&lt;/a>&lt;/li>
&lt;li>資料居住地與刪除證據鏈 → &lt;a href="../data-residency-deletion-and-evidence-chain/">7.8 data-residency-deletion-and-evidence-chain&lt;/a>&lt;/li>
&lt;li>通用 audit log → 通用 &lt;a href="https://tarrragon.github.io/blog/backend/knowledge-cards/audit-log/" data-link-title="Audit Log" data-link-desc="說明高風險操作如何留下可追溯、可稽核的紀錄">audit-log knowledge-card&lt;/a>&lt;/li>
&lt;li>multi-tenant log 隔離 → &lt;a href="../llm-multi-tenant-isolation/">llm-multi-tenant-isolation&lt;/a>&lt;/li>
&lt;li>偵測訊號 → &lt;a href="../llm-as-service-detection-coverage/">llm-as-service-detection-coverage&lt;/a>&lt;/li>
&lt;/ul>
&lt;h2 id="從本章到實作">從本章到實作&lt;/h2>
&lt;ul>
&lt;li>&lt;strong>Mechanism&lt;/strong>：問題節點表 → knowledge-card。&lt;/li>
&lt;li>&lt;strong>Delivery&lt;/strong>：交接路由 → &lt;code>05-deployment-platform / 08-incident-response&lt;/code>。&lt;/li>
&lt;/ul>
&lt;h2 id="llm-服務的-log-載體">LLM 服務的 log 載體&lt;/h2>
&lt;p>LLM 服務累積的 log / cache 比一般 service 多幾類載體：&lt;/p>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>載體&lt;/th>
 &lt;th>內容&lt;/th>
 &lt;th>隱私敏感度&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>Request log（API 層）&lt;/td>
 &lt;td>endpoint、status、tenant、latency&lt;/td>
 &lt;td>一般、跟普通 API service 一致&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Prompt log&lt;/td>
 &lt;td>完整 prompt 內容（含 system / context / user message）&lt;/td>
 &lt;td>高、含使用者意圖、可能含 PII&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Response log&lt;/td>
 &lt;td>LLM 完整輸出&lt;/td>
 &lt;td>高、可能 hallucinate 出 PII&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Tool call log&lt;/td>
 &lt;td>tool name、arguments、result&lt;/td>
 &lt;td>高、tool 參數可能含 sensitive 內容&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>KV cache&lt;/td>
 &lt;td>推論時的 attention 暫存&lt;/td>
 &lt;td>中、跨 request 殘留可能洩漏&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Context cache / RAG&lt;/td>
 &lt;td>持久化的 context、embedding cache&lt;/td>
 &lt;td>高、含原始文件內容&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Telemetry / metric&lt;/td>
 &lt;td>tokens / cost / model / latency 等聚合&lt;/td>
 &lt;td>一般、用 tenant tag 隔離&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;p>跟一般 service 的差異點：&lt;strong>Prompt log / Response log 是新類別&lt;/strong>、它們含的不是 API meta-data、是使用者實際的「想法 / 內容」、隱私敏感度遠高於一般 API log。&lt;/p></description><content:encoded><![CDATA[<p>本章的責任是把 LLM 服務的 prompt log / response log / context cache 在累積、儲存、保留、刪除四個階段的 PII 治理拆成可操作的判讀。通用詞彙見 backend <a href="/blog/backend/knowledge-cards/pii/" data-link-title="PII" data-link-desc="說明可識別個人的資料如何影響權限、遮罩、保留與稽核">pii</a>、<a href="/blog/backend/knowledge-cards/data-masking/" data-link-title="Data Masking" data-link-desc="說明敏感資料如何在顯示、匯出、log 與測試資料中降低暴露">data-masking</a>、<a href="/blog/backend/knowledge-cards/data-classification/" data-link-title="Data Classification" data-link-desc="說明資料分級如何決定保護、存取、保留與匯出規則">data-classification</a>、<a href="/blog/backend/knowledge-cards/audit-log/" data-link-title="Audit Log" data-link-desc="說明高風險操作如何留下可追溯、可稽核的紀錄">audit-log</a> 卡；模型輸出虛構 PII 的特殊議題見 <a href="/blog/llm/knowledge-cards/hallucination/" data-link-title="Hallucination" data-link-desc="LLM 生成內容看起來合理但事實錯誤、引用不存在的來源、虛構不存在的 entity 的現象">hallucination</a> 卡。一般資料保護跟 masking 流程沿用 <a href="/blog/backend/07-security-data-protection/data-protection-and-masking-governance/" data-link-title="7.4 資料保護與遮罩治理" data-link-desc="以問題驅動方式整理資料分級、遮罩、匯出與備份治理">7.4 資料保護與遮罩治理</a> 跟 <a href="/blog/backend/07-security-data-protection/data-residency-deletion-and-evidence-chain/" data-link-title="7.11 資料駐留、刪除與證據鏈" data-link-desc="定義跨區資料駐留、刪除請求與可驗證證據鏈問題">7.8 資料居住地、刪除與證據鏈</a>、本章聚焦 LLM 場景下的特殊性：prompt 含豐富使用者意圖、response 可能 hallucinate 出 PII、KV cache 跟 context cache 是非典型 log 載體。</p>
<h2 id="本章寫作邊界">本章寫作邊界</h2>
<p>本章聚焦 production LLM 服務的 log / cache / context 中的 PII 治理特殊性。個人 dev 場景的隱私資料流見 <a href="/blog/llm/00-foundations/privacy-data-flow/" data-link-title="0.7 隱私 / 資安的資料流原理" data-link-desc="從「位置」到「資料流」的思考升級：信任邊界、合約模型、零信任原則套用到 LLM 工作流">0.7 隱私資料流</a>；通用資料保護見 7.4；資料居住地與刪除證據鏈見 7.8。</p>
<h2 id="本章-threat-scope">本章 threat scope</h2>
<p><strong>In-scope</strong>：prompt log 累積的 PII、response log 中模型 hallucinate 出的 PII、context cache 跟 KV cache 中的殘留、跨地區資料居住地對應、log 保留期限與刪除證據。</p>
<p><strong>Out-of-scope</strong>（路由到他章）:</p>
<ul>
<li>通用資料保護與 masking → <a href="../data-protection-and-masking-governance/">7.4 data-protection-and-masking-governance</a></li>
<li>資料居住地與刪除證據鏈 → <a href="../data-residency-deletion-and-evidence-chain/">7.8 data-residency-deletion-and-evidence-chain</a></li>
<li>通用 audit log → 通用 <a href="/blog/backend/knowledge-cards/audit-log/" data-link-title="Audit Log" data-link-desc="說明高風險操作如何留下可追溯、可稽核的紀錄">audit-log knowledge-card</a></li>
<li>multi-tenant log 隔離 → <a href="../llm-multi-tenant-isolation/">llm-multi-tenant-isolation</a></li>
<li>偵測訊號 → <a href="../llm-as-service-detection-coverage/">llm-as-service-detection-coverage</a></li>
</ul>
<h2 id="從本章到實作">從本章到實作</h2>
<ul>
<li><strong>Mechanism</strong>：問題節點表 → knowledge-card。</li>
<li><strong>Delivery</strong>：交接路由 → <code>05-deployment-platform / 08-incident-response</code>。</li>
</ul>
<h2 id="llm-服務的-log-載體">LLM 服務的 log 載體</h2>
<p>LLM 服務累積的 log / cache 比一般 service 多幾類載體：</p>
<table>
  <thead>
      <tr>
          <th>載體</th>
          <th>內容</th>
          <th>隱私敏感度</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Request log（API 層）</td>
          <td>endpoint、status、tenant、latency</td>
          <td>一般、跟普通 API service 一致</td>
      </tr>
      <tr>
          <td>Prompt log</td>
          <td>完整 prompt 內容（含 system / context / user message）</td>
          <td>高、含使用者意圖、可能含 PII</td>
      </tr>
      <tr>
          <td>Response log</td>
          <td>LLM 完整輸出</td>
          <td>高、可能 hallucinate 出 PII</td>
      </tr>
      <tr>
          <td>Tool call log</td>
          <td>tool name、arguments、result</td>
          <td>高、tool 參數可能含 sensitive 內容</td>
      </tr>
      <tr>
          <td>KV cache</td>
          <td>推論時的 attention 暫存</td>
          <td>中、跨 request 殘留可能洩漏</td>
      </tr>
      <tr>
          <td>Context cache / RAG</td>
          <td>持久化的 context、embedding cache</td>
          <td>高、含原始文件內容</td>
      </tr>
      <tr>
          <td>Telemetry / metric</td>
          <td>tokens / cost / model / latency 等聚合</td>
          <td>一般、用 tenant tag 隔離</td>
      </tr>
  </tbody>
</table>
<p>跟一般 service 的差異點：<strong>Prompt log / Response log 是新類別</strong>、它們含的不是 API meta-data、是使用者實際的「想法 / 內容」、隱私敏感度遠高於一般 API log。</p>
<h2 id="分析模型">分析模型</h2>
<p>LLM log 治理依四個階段分析：</p>
<ol>
<li><strong>累積階段</strong>：哪些載體會累積什麼內容、累積速率多大。</li>
<li><strong>儲存階段</strong>：儲存位置（DB / S3 / SIEM）、加密、訪問權。</li>
<li><strong>保留階段</strong>：保留期限、保留期內的訪問規則。</li>
<li><strong>刪除階段</strong>：刪除觸發條件、刪除證據鏈、合規對應。</li>
</ol>
<h2 id="判讀流程">判讀流程</h2>
<p>判讀流程的責任是把「LLM 服務的 log」轉成「合規可審計的 log」。</p>
<ol>
<li>先盤點所有 log / cache 載體跟對應內容。</li>
<li>再確認 PII 偵測 / masking 在累積階段是否生效。</li>
<li>接著確認儲存跟訪問權跟一般資料保護一致。</li>
<li>最後確認保留期限跟刪除證據鏈跟資料居住地對齊。</li>
</ol>
<h2 id="問題節點案例觸發式">問題節點（案例觸發式）</h2>
<table>
  <thead>
      <tr>
          <th>問題節點</th>
          <th>判讀訊號</th>
          <th>風險後果</th>
          <th>前置控制面</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Prompt log 含 PII 未 mask</td>
          <td>使用者貼信用卡 / 身分證號、log 完整保留</td>
          <td>隱私洩漏、合規違規（GDPR / HIPAA）</td>
          <td><a href="/blog/backend/07-security-data-protection/data-protection-and-masking-governance/" data-link-title="7.4 資料保護與遮罩治理" data-link-desc="以問題驅動方式整理資料分級、遮罩、匯出與備份治理">data-protection</a></td>
      </tr>
      <tr>
          <td>Response 含 hallucinated PII</td>
          <td>LLM 生成虛構電話 / 地址、log 保留</td>
          <td>模型「虛構」也算 PII 處理、合規範圍</td>
          <td><a href="/blog/backend/07-security-data-protection/data-protection-and-masking-governance/" data-link-title="7.4 資料保護與遮罩治理" data-link-desc="以問題驅動方式整理資料分級、遮罩、匯出與備份治理">data-protection</a></td>
      </tr>
      <tr>
          <td>KV cache 跨 request 殘留 PII</td>
          <td>inference engine 沒清 cache、下個 request 的 dump 看得到</td>
          <td>tenant 間隱私洩漏</td>
          <td><a href="/blog/backend/07-security-data-protection/llm-multi-tenant-isolation/" data-link-title="LLM 多租戶推論隔離" data-link-desc="production LLM 服務的多租戶隔離：KV cache 不共享、log / model artifact 隔離、跨用戶 prompt 洩漏面">llm-multi-tenant-isolation</a></td>
      </tr>
      <tr>
          <td>Context cache 跨 session 重用</td>
          <td>同 user 的 long context cache 被其他 session 共用</td>
          <td>個人 prompt 洩漏到其他 session</td>
          <td><a href="/blog/backend/07-security-data-protection/data-protection-and-masking-governance/" data-link-title="7.4 資料保護與遮罩治理" data-link-desc="以問題驅動方式整理資料分級、遮罩、匯出與備份治理">data-protection</a></td>
      </tr>
      <tr>
          <td>保留期限跟資料居住地不一致</td>
          <td>log 跨地區複製、不同地區保留期限不一</td>
          <td>合規對應失效、刪除無法執行</td>
          <td><a href="/blog/backend/07-security-data-protection/data-residency-deletion-and-evidence-chain/" data-link-title="7.11 資料駐留、刪除與證據鏈" data-link-desc="定義跨區資料駐留、刪除請求與可驗證證據鏈問題">data-residency</a></td>
      </tr>
      <tr>
          <td>刪除證據鏈缺失</td>
          <td>客戶要求刪除、無法證明已刪除所有副本</td>
          <td>合規違規、客戶投訴升級</td>
          <td><a href="/blog/backend/knowledge-cards/audit-log/" data-link-title="Audit Log" data-link-desc="說明高風險操作如何留下可追溯、可稽核的紀錄">audit-log</a></td>
      </tr>
      <tr>
          <td>Vendor 政策跟自家政策衝突</td>
          <td>用雲端 LLM、vendor log 30 天、自家承諾 7 天</td>
          <td>對外承諾無法兌現</td>
          <td><a href="/blog/backend/knowledge-cards/contract/" data-link-title="Boundary Contract" data-link-desc="說明跨邊界約定如何維持相容與可驗證">vendor-contract</a></td>
      </tr>
  </tbody>
</table>
<h2 id="常見風險邊界">常見風險邊界</h2>
<p>風險邊界的責任是界定何時 LLM log 治理已進入高壓狀態。</p>
<ul>
<li>Prompt log 含未 mask 的 PII 時、代表 PII 治理在累積階段失效。</li>
<li>KV cache / context cache 跨 tenant 共用時、代表 isolation 失效（亦見 <a href="/blog/backend/07-security-data-protection/llm-multi-tenant-isolation/" data-link-title="LLM 多租戶推論隔離" data-link-desc="production LLM 服務的多租戶隔離：KV cache 不共享、log / model artifact 隔離、跨用戶 prompt 洩漏面">llm-multi-tenant-isolation</a>）。</li>
<li>log 保留期限跟資料居住地政策不一致時、代表治理流程不收斂。</li>
<li>客戶刪除請求無法產生證據鏈時、代表合規對應失效。</li>
</ul>
<h2 id="llm-場景的特殊判讀">LLM 場景的特殊判讀</h2>
<p>LLM log 治理相對一般資料保護的特殊性：</p>
<ol>
<li><strong>Prompt 跟 Response 比 API log 隱私敏感度高一個量級</strong>：一般 API log 主要記 endpoint / status / latency、prompt log 記的是使用者實際「在問什麼」、Response log 是模型「在說什麼」。</li>
<li><strong>模型 hallucinate 的 PII 也是 PII</strong>：LLM 生成虛構的姓名 / 電話 / 地址、即使不對應真人、也屬於 PII 處理範圍、需要對應的 masking 跟保留政策。</li>
<li><strong>KV cache 是非典型 log 載體</strong>：傳統 log 治理工具不掃 GPU memory / RAM cache、但這些 cache 可能跨 request / 跨 tenant 殘留 PII；需要 inference engine 配合做 cache 清理。</li>
<li><strong>RAG context 是雙向載體</strong>：RAG 既把 corpus 注入 prompt（corpus 中的 PII 進 log）、也把 user query 注入 corpus（user query 變 future retrieval 的對象）；治理範圍要覆蓋雙向。</li>
<li><strong>vendor 政策直接影響合規承諾</strong>：用雲端 LLM 時、vendor 的 log 保留政策（如 30 天 abuse log）直接限制自家對下游客戶能承諾的最短保留期、合約鏈要對齊。</li>
<li><strong>abuse detection 跟 PII 治理的張力</strong>：abuse detection 需要 log prompt（看 abuse pattern）、PII 治理要求 minimize、兩者要在 mask 後 detection 跟全文 detection 中找平衡。</li>
</ol>
<h2 id="防禦設計的核心原則">防禦設計的核心原則</h2>
<ol>
<li><strong>累積階段做 PII detection + masking</strong>：log 寫入前過 PII detector、敏感欄位 mask 或不 log。</li>
<li><strong>儲存階段加密 + 訪問權對齊 IAM</strong>：跟一般敏感資料一致。</li>
<li><strong>保留期限明確 + 自動刪除</strong>：用 policy-driven 自動 lifecycle、不依賴人工。</li>
<li><strong>KV cache / context cache 跨 tenant 清理</strong>：inference engine 配合、tenant boundary 明確。</li>
<li><strong>刪除證據鏈</strong>：客戶刪除請求觸發時、產生 audit trail、能證明已刪除所有副本（包含 backup / log archive）。</li>
<li><strong>vendor 政策對齊</strong>：用雲端 LLM 時、vendor 的條款拉進自家政策一致審視。</li>
</ol>
<h2 id="案例觸發參考">案例觸發參考</h2>
<p>LLM log 治理的公開案例累積中、值得追蹤的方向：</p>
<ul>
<li>大型 LLM vendor 的 log 政策變更引發的合規震盪</li>
<li>模型 hallucinate 出真人 PII 的訴訟案例</li>
<li>KV cache 跨用戶洩漏的 incident 報告</li>
</ul>
<p>LLM-specific 案例累積後會補入 <code>red-team/cases/llm-log-pii/</code>。一般資料保護案例見 <a href="/blog/backend/07-security-data-protection/data-protection-and-masking-governance/" data-link-title="7.4 資料保護與遮罩治理" data-link-desc="以問題驅動方式整理資料分級、遮罩、匯出與備份治理">7.4 data-protection-and-masking-governance</a> 跟 <a href="/blog/backend/07-security-data-protection/data-residency-deletion-and-evidence-chain/" data-link-title="7.11 資料駐留、刪除與證據鏈" data-link-desc="定義跨區資料駐留、刪除請求與可驗證證據鏈問題">7.8 data-residency-deletion-and-evidence-chain</a>。</p>
<blockquote>
<p><strong>事實查核註</strong>：LLM log / PII 議題的具體 incident 跟法律判例累積還在早期、各 vendor 政策跟監管要求依時段快速變化、建議引用前以最新的監管文件（GDPR、CCPA、AI Act 等）跟 vendor 當前政策為準。</p></blockquote>
<h2 id="引用標準">引用標準</h2>
<table>
  <thead>
      <tr>
          <th>標準</th>
          <th>版本 / 年份</th>
          <th>適用場景</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>GDPR</td>
          <td>2016/679</td>
          <td>歐盟 PII 治理</td>
      </tr>
      <tr>
          <td>CCPA / CPRA</td>
          <td>2020 / 2023</td>
          <td>加州 PII 治理</td>
      </tr>
      <tr>
          <td>EU AI Act</td>
          <td>2024</td>
          <td>AI 系統 PII 處理特殊規定</td>
      </tr>
      <tr>
          <td>NIST Privacy Framework</td>
          <td>1.0 (2020)</td>
          <td>隱私治理 reference</td>
      </tr>
      <tr>
          <td>OWASP LLM Top 10</td>
          <td>2025</td>
          <td>LLM06 Sensitive Information Disclosure</td>
      </tr>
  </tbody>
</table>
<p>引用版本與 cadence 規則見 <a href="/blog/report/security-citation-currency-and-precision/" data-link-title="Security 標準引用的時效性與精確度" data-link-desc="資安 citation 跟一般技術引用不同——best practice 時效短（MD5 / SHA-1 / bcrypt 100k / TLS 1.0 都曾是 best practice）、原文常被引用扭曲（conditional → unconditional drift）、版本不標 reader 會套用過時 spec。citation 同時涵蓋外部標準（OWASP / RFC / NIST / CIS）跟內部 citation（knowledge-cards / 跨章引用作為 control-of-record）；後者因無版本號 anchor 反而更易 silent drift / broken。每條 citation 必須附：版本 / 年份、引用句意可回溯、deprecated / superseded 標記、強度參數對應 actor 能力的 review trigger（外部）/ last-checked &#43; sync owner（內部）。">security-citation-currency-and-precision</a>。Last reviewed: 2026-05-12。</p>
<h2 id="下一步路由">下一步路由</h2>
<ul>
<li>通用資料保護：<a href="/blog/backend/07-security-data-protection/data-protection-and-masking-governance/" data-link-title="7.4 資料保護與遮罩治理" data-link-desc="以問題驅動方式整理資料分級、遮罩、匯出與備份治理">7.4 data-protection-and-masking-governance</a></li>
<li>資料居住地與刪除：<a href="/blog/backend/07-security-data-protection/data-residency-deletion-and-evidence-chain/" data-link-title="7.11 資料駐留、刪除與證據鏈" data-link-desc="定義跨區資料駐留、刪除請求與可驗證證據鏈問題">7.8 data-residency-deletion-and-evidence-chain</a></li>
<li>多租戶 isolation：<a href="/blog/backend/07-security-data-protection/llm-multi-tenant-isolation/" data-link-title="LLM 多租戶推論隔離" data-link-desc="production LLM 服務的多租戶隔離：KV cache 不共享、log / model artifact 隔離、跨用戶 prompt 洩漏面">llm-multi-tenant-isolation</a></li>
<li>偵測訊號：<a href="/blog/backend/07-security-data-protection/llm-as-service-detection-coverage/" data-link-title="LLM Service 偵測訊號覆蓋" data-link-desc="production LLM 服務的 detection 訊號設計：tool call 異常模式、prompt injection 觸發徵兆、abuse 跟濫用模式、跟既有 detection-coverage 框架的接合">llm-as-service-detection-coverage</a></li>
<li>事件案例工作流：<a href="/blog/backend/07-security-data-protection/incident-case-to-control-workflow/" data-link-title="7.16 從公開事故到工程 Workflow：案例如何回寫控制面" data-link-desc="建立公開事故如何轉成控制面失效樣式與 workflow 回寫的大綱">7.10 incident-case-to-control-workflow</a></li>
</ul>
]]></content:encoded></item></channel></rss>