在傳統開發流程中,安全檢測(Security Scanning)常常是最後一步。
但這樣的模式存在兩個重大問題:
而在 AI Agent 時代,我們能讓安全融入開發全流程,形成真正的「AI 驅動 DevSecOps」。
Scanner Agent
調用 SAST/Dependency 工具(如 Trivy、Bandit、npm audit)Analyzer Agent
解析掃描結果建立一個 Express 應用程式,用來提供簡單的 API 服務。
import express from "express";
const app = express();
建立一個 /api/scan/result
API,回傳模擬的掃描結果清單,包含漏洞名稱、CVE 編號、風險分數與修補建議。
app.get("/api/scan/result", (req, res) => {
res.json([
{
file: "src/server.js",
vuln: "SQL Injection",
cve: "CVE-2024-56789",
cvss: 9.1,
desc: "User input not sanitized before SQL query execution.",
fix: "Use parameterized queries or ORM."
},
{
file: "package.json",
vuln: "Outdated library",
cve: "CVE-2025-12345",
cvss: 7.3,
desc: "axios <1.6.0 has prototype pollution vulnerability.",
fix: "Upgrade to axios@1.6.0 or above."
}
]);
});
讓伺服器監聽在 3000
端口,啟動後即可在瀏覽器打開 API。
app.listen(3000, () => console.log("Scan API ready: http://localhost:3000"));
載入 .env
的 API 金鑰,建立 Gemini 模型(gemini-2.5-flash),之後會用它來生成 AI 報告。
import fetch from "node-fetch";
import dotenv from "dotenv";
import { GoogleGenerativeAI } from "@google/generative-ai";
dotenv.config();
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });
/api/scan/result
抓取掃描資料async function analyzeSecurityReport() {
const res = await fetch("http://localhost:3000/api/scan/result");
const data = await res.json();
const prompt = `
以下為安全掃描結果,請幫我輸出兩種報告:
1. 技術報告(含 CVE、描述、修補建議)
2. 管理摘要(列出高風險項目、風險程度與修補優先順序)
資料如下:
${JSON.stringify(data, null, 2)}
`;
const aiRes = await model.generateContent(prompt);
console.log("=== AI 安全報告 ===\n");
console.log(aiRes.response.text());
}
analyzeSecurityReport();