AWS Security Agent 全仓代码扫描——让安全漏洞无处藏身
代码安全这事,团队里没人爱干。每次发版前跑一遍 SAST 工具,报出 200 个 finding,90% 是误报,剩下 10% 还得手动翻代码确认。搞了两三次之后,大家就开始跳过安全检查了。
亚马逊云科技上周发布了 AWS Security Agent 的全仓代码扫描能力(preview)。跟传统工具不一样的地方在于:它扫的是整个仓库的上下文,不是逐文件做模式匹配。而且发现漏洞后直接给你生成修复代码,精确到文件和行号。
我拿了个 Node.js + Python 混合的内部项目试了一下,记录过程。
传统扫描 vs 全仓上下文扫描
先搞清楚区别:
| 对比 | 传统 SAST | Security Agent 全仓扫描 |
|---|---|---|
| 分析单位 | 单个文件 | 整个仓库 |
| 上下文理解 | 无(纯模式匹配) | 跨文件数据流追踪 |
| 误报率 | 高(50-90%) | 低(上下文过滤) |
| 修复建议 | "建议使用参数化查询" | 直接出 patch(文件+行号) |
| 运行时间 | 几秒-几分钟 | 几分钟-十几分钟 |
传统工具最大的问题是没有上下文。比如它看到一个函数接收 user input,就报 SQL injection。但实际上这个 input 在调用链上游已经做了 sanitize——工具不看上游代码,只看当前文件,所以误报。
Security Agent 全仓扫描会追踪跨文件的数据流。它知道 input 从哪来、中间经过了哪些处理、最终到了哪。这样能过滤掉大量误报。
开通步骤
前置条件
你需要已经启用了 AWS Security Agent(如果之前用过 CodeGuru Security 或 Amazon Inspector 代码扫描,大概率已经有了)。
配置仓库连接
# 创建代码仓库关联(以 GitHub 为例)
aws securityagent create-repository-association \
--repository "Owner=my-org,Name=my-backend,ProviderType=GitHub" \
--tags "Team=backend,Env=prod"
触发全仓扫描
# 手动触发一次全仓扫描
aws securityagent create-code-scan \
--repository-association-arn "arn:aws:securityagent:us-east-1:123456789012:association:xxxxx" \
--scan-type FULL_REPOSITORY \
--analysis-types "Security,Quality,Secrets"
参数说明:
scan-type:FULL_REPOSITORY是全仓扫描,INCREMENTAL是增量(PR 触发时用)analysis-types: 可选 Security(安全漏洞)、Quality(代码质量)、Secrets(硬编码密钥)
查看扫描结果
# 获取扫描状态
aws securityagent describe-code-scan \
--scan-arn "arn:aws:securityagent:us-east-1:123456789012:scan:xxxxx"
# 列出发现的问题
aws securityagent list-findings \
--repository-association-arn "arn:aws:securityagent:us-east-1:123456789012:association:xxxxx" \
--max-results 50
实际扫描结果
我的测试项目:Node.js 后端 + Python 数据处理 + Terraform IaC,总计约 8 万行代码。
扫描耗时:6 分 42 秒。
发现结果:
| 严重程度 | 数量 | 有修复建议 |
|---|---|---|
| Critical | 2 | 2 (100%) |
| High | 5 | 5 (100%) |
| Medium | 12 | 10 (83%) |
| Low | 8 | 3 (38%) |
| Info | 15 | 0 |
关键发现举例:
Critical #1:SQL Injection(跨文件追踪)
{
"type": "SQL_INJECTION",
"severity": "CRITICAL",
"filePath": "src/api/orders.js",
"startLine": 45,
"endLine": 47,
"description": "User input from request.query.filter flows through utils/queryBuilder.js:12 into raw SQL query without parameterization",
"dataFlow": [
"src/routes/orders.js:23 → req.query.filter",
"src/api/orders.js:30 → buildQuery(filter)",
"src/utils/queryBuilder.js:12 → string concatenation",
"src/api/orders.js:45 → db.query(rawSql)"
]
}
注意 dataFlow 字段——它追踪了 input 从路由层 → API 层 → 工具函数 → 数据库查询的完整链路。传统工具只会在 orders.js:45 报一个 "potential SQL injection",不会告诉你数据从哪来的。
自动生成的修复代码
{
"remediation": {
"filePath": "src/utils/queryBuilder.js",
"startLine": 10,
"endLine": 15,
"suggestedFix": "// Before:\nfunction buildQuery(filter) {\n return `SELECT * FROM orders WHERE status = '${filter}'`;\n}\n\n// After:\nfunction buildQuery(filter) {\n return { text: 'SELECT * FROM orders WHERE status = $1', values: [filter] };\n}",
"description": "Use parameterized query to prevent SQL injection. The caller at orders.js:45 should be updated to pass the query object to db.query()."
}
}
修复建议精确到行号,而且给出了 before/after 对比。直接 copy 过去改就行。
CI/CD 集成
在 PR 流程中集成增量扫描:
GitHub Actions 示例
name: Security Scan
on:
pull_request:
branches: [main]
jobs:
security-scan:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-arn: arn:aws:iam::123456789012:role/SecurityAgentCI
aws-region: us-east-1
- name: Run incremental scan
run: |
SCAN_ARN=$(aws securityagent create-code-scan \
--repository-association-arn "$REPO_ARN" \
--scan-type INCREMENTAL \
--source-commit "${{ github.event.pull_request.head.sha }}" \
--output text --query 'scanArn')
# 等待扫描完成
aws securityagent wait scan-completed --scan-arn "$SCAN_ARN"
# 获取结果
CRITICAL=$(aws securityagent list-findings \
--scan-arn "$SCAN_ARN" \
--severity CRITICAL \
--query 'length(findings)' --output text)
if [ "$CRITICAL" -gt "0" ]; then
echo "::error::Found $CRITICAL critical security issues"
exit 1
fi
增量扫描只分析 PR 变更的文件及其依赖链,速度比全仓快很多(通常 1-2 分钟)。
对比传统方案的实际体验
之前我们用的是传统 SAST 工具组合。同一个项目:
| 指标 | 旧方案 | Security Agent |
|---|---|---|
| 扫描时间 | 3m 20s | 6m 42s |
| 报告 findings | 187 | 42 |
| 实际有效 | ~20 | ~35 |
| 有修复代码 | 0 | 20 |
| 开发者修复耗时 | 平均 2h/个 | 平均 20min/个 |
扫描时间长了一倍,但误报少了 80%+。开发者不用在 200 个 finding 里大海捞针了。加上自动修复代码,每个 finding 的处理时间从 2 小时降到 20 分钟。
当前限制(Preview 阶段)
坦白说几个限制:
- 语言支持:目前支持 Java、Python、JavaScript/TypeScript、Go、C#。其他语言后续补
- 仓库大小:超过 100 万行代码的仓库扫描时间可能超过 30 分钟
- 修复建议覆盖率:Critical/High 基本 100%,Medium 约 80%,Low 不到 40%
- IaC 扫描:Terraform/CloudFormation 支持有限,主要强在应用代码
费用
Preview 期间,现有 AWS Security Agent 客户免费使用。GA 后的定价还没公布,预计按扫描次数或代码行数计费。
小结
Security Agent 全仓扫描的核心价值:减少误报 + 自动修复。
以前安全扫描是"制造工作"——报一堆 finding 让开发者自己去查。现在是"解决问题"——告诉你哪有洞、数据怎么流过来的、怎么修。
如果你的团队安全扫描形同虚设(大家都跳过),这个工具值得试。至少让"修安全 bug"这件事不再那么痛苦。

浙公网安备 33010602011771号