为 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 code <root directory>/CLAUDE.md Anthropic 原生的常设指令格式。指令中必须注明:如果该文件已存在,则追加到该文件中。
    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.