在現代開發流程中,CI/CD Pipeline(持續整合 / 持續部署)幾乎是標準配置。
然而,多數團隊只檢查「測試是否通過」與「功能是否正常」,卻忽略了 程式碼安全性。
這意味著:
如果我們在 Pipeline 中嵌入 AI Agent,就能讓它自動扮演「安全守門員」的角色:
server.js
模擬檢查任務agent.js
呼叫 Gemini,判斷安全性模擬一個「有 SQL Injection 風險」的 API,作為被 AI Agent 審查的範例
const express = require("express");
const app = express();
app.get("/user", (req, res) => {
// 這裡有 SQL Injection 風險
const query = "SELECT * FROM users WHERE id = " + req.query.id;
res.send("執行查詢: " + query);
});
app.listen(4000, () => {
console.log("Vulnerable app running at http://localhost:4000");
});
模擬在 CI/CD pipeline 中,收集到的程式碼(或 PR 中的差異),並提供給 AI Agent 分析
/code
,回傳需要審查的程式碼。import express from "express";
import fs from "fs";
const app = express();
app.use(express.json());
// 模擬 CI/CD 收到的程式碼 (例如 PR 內的檔案)
app.get("/code", (req, res) => {
const code = fs.readFileSync("./sample_vulnerable_code.js", "utf-8");
res.send({ code });
});
app.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});
透過 API 取得程式碼,模擬在 CI/CD pipeline 中收集到的檔案。
async function getCode() {
const res = await fetch("http://localhost:3000/code");
const data = await res.json();
return data.code;
}
將程式碼交給 Gemini 模型,請它檢查是否存在漏洞,並提供修補建議。
async function auditCode(code) {
const prompt = `
你是一個程式碼安全審查 AI,請檢查以下程式碼是否存在漏洞。
需求:
1. 標記可能的弱點(例如 SQL Injection, XSS, 弱密碼, 過時套件)
2. 提供修補建議
---
程式碼:
${code}
`;
const result = await model.generateContent(prompt);
console.log("=== AI 審查結果 ===");
console.log(result.response.text());
}
串接前兩步:先抓程式碼,再交給 AI Agent 審查,最後輸出結果。
(async () => {
const code = await getCode();
await auditCode(code);
})();