為 AI 程式碼代理程式生成安全護欄

運用 AppScan on Cloud 中的 AI 聊天機器人生成一組量身打造的安全護欄,供 IDE 內的開發人員使用。護欄可確保 AI 代理程式生成的任何程式碼都不會為應用程式帶來漏洞。

若要生成護欄:
  1. 在 AppScan on Cloud 中,開啟應用程式。
  2. 在「應用程式」頁面上,按一下 AI 助理圖示 ()。
  3. 其中之一:
    • 按一下「協助我為 IDE 的 LLM 建立安全指示檔案」,或
    • 自行撰寫提示。

      例如,「協助我為我的應用程式建立安全護欄」。

  4. 確認您想為其建立護欄的應用程式是目前的應用程式。
  5. 回應 AI 助理中的四個主要問題。
    1. 您使用的是何種語言、框架和執行時期版本?
    2. 此應用程式是否會連線至資料庫或執行系統層級指令?如果是,會是哪個資料庫和驅動程式 / ORM?
    3. 該應用程式會如何處理鑑別和授權?
    4. 該應用程式是否包含任何我應該明確建立護欄的高風險功能?

    如果您的回應不夠清楚,AI 助理會提出釐清問題。

    AI 助理會生成一份安全原則,您可以將該原則另存為 Markdown 檔案,也可以將其複製到 IDE 內的相關文件中。

  6. 根據用於建立程式碼的目標代理程式,將原則新增至下列位置:
    目標代理程式 儲存庫位置和所需檔案名稱 備註 / 要求
    游標 <root directory>/.cursorrules 或者,該檔案可以位於 .cursor/rules/;然而,根檔案最適合階段 1 MVP 的簡便性。
    Claude 程式碼 <root directory>/CLAUDE.md Ansulpic 的原生常設指令格式。指示必須註明,如果此檔案已存在,便應附加到該檔案。
    GitHub CoPilot <root directory>/copilot-instructions.md 在每個 Copilot 聊天階段作業開始時自動解析。
    Windsurf (Cascade) <root directory>/.windsurfrules 需要簡單的 Markdown 標頭以確保 Cascade 的記憶層予以鎖定。
    通用撤回 <root directory>/AGENTS.md 由多個開放原始碼代理程式框架支援的新興跨相容生態系統標準。

輸出範例

# AI Coding Security Policy — FastAPI / Python 3.12

> Security policies for AI coding assistants in this repository.
> Save as `.appscan/policy.md` in the repo root.

## Anti-Hallucination Rules

- NEVER invent library names, package names, framework APIs, middleware, or configuration flags not present in this stack.
- NEVER suggest packages you cannot confirm are real and actively maintained for Python 3.12.
- If uncertain whether a function or package exists in Python 3.12 — say so explicitly.
- NEVER fabricate security controls or framework features.

## Security Baseline

- NEVER disable or bypass FastAPI, Starlette, or server security controls for convenience.
- NEVER hardcode credentials, API keys, JWT secrets, or connection strings in source code or version-controlled files.
- ALWAYS validate and sanitize all external input at the API boundary using explicit request models and strict typing.
- NEVER silently swallow security-related exceptions.
- REQUIRED: apply least privilege to service accounts, MongoDB users, and any backing services.
- REQUIRED: use Python 3.12 standard-library security-safe primitives where available; prefer `secrets` over `random` for security-sensitive values.

## NoSQL Injection Prevention

- NEVER pass user input as MongoDB query operators, filters, or document keys without strict validation.
- MUST validate all query inputs are scalar values unless a specific structured schema is explicitly required.
- REQUIRED: reject objects containing `$`-prefixed keys or unsafe operator injection from client input.
- REQUIRED: use ODM / typed access-layer methods exclusively for query construction.
- NEVER use `$where` with user input; use explicit field matching only.

## JWT Security

- NEVER accept tokens without verifying the cryptographic signature server-side.
- MUST allowlist signing algorithms such as `RS256` or `ES256`; NEVER permit `alg: "none"`.
- REQUIRED: validate `exp`, `iss`, `aud`, and `nbf` on every protected request.
- NEVER trust client-decoded JWT claims without server-side re-verification.
- NEVER store tokens in browser-accessible storage; prefer `HttpOnly`, `Secure` cookies when feasible.
- REQUIRED: centralize token validation in FastAPI dependencies or middleware and enforce authorization on every protected route.

## Sensitive Data / PII Handling

- TREAT all PII as high-sensitivity data and collect only fields required for the business purpose.
- NEVER log PII, authentication tokens, or raw request bodies containing personal data.
- REQUIRED: mask or redact PII in application logs, error messages, traces, and monitoring events.
- REQUIRED: encrypt PII in transit and at rest using approved platform controls.
- NEVER return unnecessary PII fields in API responses; use response models to minimize exposure.
- REQUIRED: enforce server-side authorization checks before any read, update, export, or deletion of PII.
- REQUIRED: define retention and deletion behavior for PII; NEVER keep personal data indefinitely by default.

## Supply Chain Control

- MUST pin or constrain security-critical dependency versions; avoid unbounded ranges.
- NEVER introduce dependencies with known unpatched CVEs without security review.
- REQUIRED: run vulnerability scanning in CI using tools appropriate for Python dependencies.