跳到主要内容
uencoder

如何Build 安全 CI/CD Pipeline

Design CI/CD pipeline 具有安全 gates 在每个阶段: 依赖 scanning, SAST, DAST, secret 检测, 和 artifact 签名.

问题

你的 CI/CD pipeline deploys 代码无 安全 checks, allowing vulnerabilities, secrets, 和 malicious dependencies 到 reach production. 你需要integrate 安全 gates 遍及 pipeline 该阻止不安全 builds.

前置条件

  • Understanding 的 CI/CD concepts (构建, 测试, deploy 阶段)
  • 熟悉静态和 动态分析 tools
  • Knowledge 的依赖管理和 supply 链接 risks

步骤

  1. 1. Secure the source and dependencies

    Enforce branch protection: require PR reviews, signed commits, and status checks before merge. On every PR, scan dependencies for known vulnerabilities (CVE database) and license compliance. Use lockfiles (package-lock.json, pnpm-lock.yaml) to pin exact versions and detect supply chain attacks. Verify package signatures and provenance (SLSA levels). Fail the build if critical vulnerabilities are found. Use the config validator and file hash tools to verify integrity.

    输入
    pull request with new dependency
    输出
    scan: 2 CVEs found (1 critical) | status: BLOCKED
  2. 2. Integrate static and dynamic analysis

    Run SAST (Static Application Security Testing) on every commit to detect code-level vulnerabilities: SQL injection, XSS, hardcoded secrets, insecure crypto. Run DAST (Dynamic Application Security Testing) against the staging deployment to find runtime vulnerabilities. Run SCA (Software Composition Analysis) for third-party dependencies. Configure tools to fail the build on critical/high findings. Use the JS linter, SQL validator, and regex tools for code quality checks.

    输入
    source code commit
    输出
    SAST: 0 critical | DAST: 1 high (XSS in /search) | status: BLOCKED
  3. 3. Implement secret scanning and management

    Scan the repository for leaked secrets: API keys, passwords, private keys, tokens. Use pre-commit hooks to prevent secrets from being committed. Store secrets in a secrets manager (Vault, AWS Secrets Manager), injected as environment variables at build/runtime — never in the repo or CI config. Rotate secrets automatically. Use the env parser and hash-compare tools to manage and verify secrets. If a secret is detected, fail the build and alert the security team.

    输入
    commit containing API key
    输出
    secret scan: 1 secret detected (AWS key) | status: BLOCKED | alert sent
  4. 4. Sign artifacts and enforce deployment gates

    Sign build artifacts (containers, binaries) with a build-system signing key (Sigstore/cosign, GPG). Generate SBOM (Software Bill of Materials) for each build. Enforce deployment gates: only signed artifacts from the main branch, passing all security checks, can deploy to production. Use progressive deployment (canary) with automated rollback on error rate spikes. Audit all pipeline steps immutably. Use the file hash and timestamp tools for artifact verification.

    输入
    build artifact: app-v1.2.3.tar
    输出
    signature: cosign sign | SBOM: generated | gate: PASS -> deploy

预期结果

CI/CD pipeline where 每个构建是 scanned 为 vulnerabilities, secrets, 和依赖 risks, 具有 signed artifacts 和部署 gates 该 prevent 不安全代码从 reaching production.

常见错误

Security scans that don't fail the build
Configure scanners to exit non-zero on critical/high findings. A scan that only reports but doesn't block is ineffective. Use quality gates with thresholds.
Storing secrets in CI environment variables or repo
Use a secrets manager (Vault, AWS SM) with short-lived credentials injected at runtime. CI env vars are often visible in logs and to anyone with repo access.
Skipping security scans for speed
Run fast checks (SAST, secret scan) on every PR, slower checks (DAST, full dependency scan) on merge to main or nightly. Never skip for speed — optimize the tools instead.

排障指引

  • If SAST produces too many false positives, tune the rules and establish a baseline of accepted findings, then fail only on new issues.
  • If dependency scanning is slow, use incremental scanning (only changed dependencies) and cache the vulnerability database.
  • For monorepos, run pipeline stages only for affected packages to balance security coverage with build speed.

安全限制

  • CI/CD security gates reduce but do not eliminate risk — zero-days and novel attack vectors may bypass known-pattern scanners.
  • All secret management happens in the secrets manager; the pipeline never logs or stores secrets in plaintext.
  • The pipeline itself is a high-value target — secure the CI system with MFA, least privilege, and audit logging.

相关工具

配置验证器
Config Validate — 纯客户端工具。隐私优先,无服务器往返。
工具
配置差异对比器
Config Diff — 纯客户端工具。隐私优先,无服务器往返。
工具
ENV 解析器
将 .env 风格的 KEY=VALUE 行解析为 JSON。空行与「#」开头的注释会被跳过。其余 dotenv 约定未实现:「export 」前缀会保留在键名中(export PORT=3000 → “export PORT”),值周围的引号也会原样保留。
工具
ENV 转 JSON
Env To Json — 纯客户端工具。隐私优先,无服务器往返。
工具
文件哈希计算器
计算文件内容的简单哈希。
工具
文件信息查看器
获取文件内容信息:大小、行数、词数。
工具
文件差异对比器
两个文件的逐行差异比较。
工具
JS Lint 检查器
检查 JavaScript 常见问题。
工具
JS 验证器
JavaScript 括号配平检查:圆括号、方括号、花括号必须成对。这是启发式检查——含括号的正则字面量(如 /\(/)可能误报 UNCLOSED;并非完整解析器。
工具
SQL 验证器
轻量 SQL 健全性检查:语句必须以 SQL 关键字开头,且括号必须配平。以括号开头的语句(如 (SELECT 1) UNION (SELECT 2))会被拒绝——这不是真正的 SQL 解析器。
工具
JSON 验证器
验证 JSON 并按行列定位语法错误。免费、隐私本地化、100% 客户端运行。
工具
YAML 解析器
YAML Parser — 纯客户端工具。隐私优先,无服务器往返。
工具
正则表达式测试器
测试正则表达式,支持匹配高亮、捕获组和替换预览。免费、隐私本地化、100% 客户端运行。支持全局(g)、忽略大小写(i)、多行(m)、dotAll(s)标志位,匹配在带超时的 Web Worker 中运行以避免灾难性回溯冻结页面。
工具
哈希对比器
对比同一输入的 MD5、SHA-1、SHA-256、SHA-384、SHA-512 哈希。免费、隐私、100% 客户端运行。
工具
时间戳转换器
在 Unix 时间戳和人类可读日期之间转换,显示 UTC 和本地时间。免费、隐私、100% 客户端运行。自动识别秒(10 位)与毫秒(13 位),支持 ISO 8601 等日期字符串解析,适合调试 API 时间戳、日志分析和跨时区时间转换。
工具

相关词条

参考链接