跳至正文
OpenAgentSkill
浏览已开放 · 目录记录日期 2026-11-21 · 自动更新和账户服务暂时关闭。
安全

harden-gitlab-ci

Harden GitLab CI/CD pipelines for supply-chain security — SHA-pin `include:` and CI/CD components, scope the `CI_JOB_TOKEN` allowlist, protect and mask variables, pin job image digests, and use `id_tokens`/OIDC instead of long-lived secrets. Use when adding or auditing a `.gitlab-ci.yml`, before making a GitLab project public, when a supply-chain review flags CI gaps, or when standardizing pipeline hardening across GitLab projects (gitlab.com or self-hosted). GitLab-specific by design — for GitHub Actions use `harden-github-actions`; Forgejo/Gitea Actions are out of scope.

jrjsmrtn · 已有 AI 审核记录

安装与使用

运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。

npx skills add jrjsmrtn/project-orchestration-skills --skill harden-gitlab-ci

技能说明


name: harden-gitlab-ci description: Harden GitLab CI/CD pipelines for supply-chain security — SHA-pin include: and CI/CD components, scope the CI_JOB_TOKEN allowlist, protect and mask variables, pin job image digests, and use id_tokens/OIDC instead of long-lived secrets. Use when adding or auditing a .gitlab-ci.yml, before making a GitLab project public, when a supply-chain review flags CI gaps, or when standardizing pipeline hardening across GitLab projects (gitlab.com or self-hosted). GitLab-specific by design — for GitHub Actions use harden-github-actions; Forgejo/Gitea Actions are out of scope. metadata: author: "Georges Martin jrjsmrtn@gmail.com" version: "0.1.34" license: MIT

Harden GitLab CI

Harden GitLab CI/CD pipelines against supply-chain attack: pin what runs, minimise what it can reach, and stop long-lived secrets from existing at all.

GitLab-specific by design. Like harden-github-actions, this skill is deliberately bound to one forge. The controls are properties of the GitLab CI execution model — include: resolution, the CI_JOB_TOKEN allowlist, project/group variable scoping, id_tokens — not portable concepts in GitLab syntax. It is a sibling of harden-github-actions, not a translation of it: the two forges differ in what the risks are, not merely in how they are spelled.

When to Use

  • When adding a .gitlab-ci.yml to a project (after setup-git-hooks)
  • When auditing an existing pipeline before making a GitLab project public
  • When a supply-chain review (or the supply-chain skill) flags CI hardening gaps
  • When standardising pipeline hardening across GitLab projects

Not for: GitHub Actions (use harden-github-actions) or Forgejo/Gitea Actions. The controls do not carry over — say so rather than approximating.

Required Inputs

  1. Repository — a GitLab project (auto-detected from git remotes). Self-hosted GitLab is a first-class target, not an exception: authenticate once and every control below is identical.
  2. Project visibility — public/internal or private. Phases 3 and 4 branch on it: public and internal projects expose pipeline artifacts to unauthenticated users, and fork merge requests are the main variable-exfiltration route.
  3. Scope — what to run:
    • audit — report findings against the checklist below; no writes (default)
    • fix — apply the mechanical fixes (pin includes/components/images), leave judgment calls to the maintainer
    • full — audit, then fix after confirmation

The GitLab Threat Model — What Actually Differs

Where the risk sits differs from GitHub Actions; the controls are not one-for-one equivalents:

ConcernGitHub ActionsGitLab CI
Third-party code executionuses: actions/x@ref from the Marketplaceinclude: + CI/CD Catalog components — same class of risk, different keyword
Ambient credentialGITHUB_TOKEN with repo-wide default scopesCI_JOB_TOKEN — defaults to own project only; risk is a widened allowlist
Secret exfiltration surfaceworkflow can read all repo secretsvariable scoping — protected/masked/environment-scoped, plus fork-MR exposure
Provenance / keyless authOIDC via id-token: writeid_tokens: with an aud claim
Runner trustGitHub-hosted vs self-hostedshared vs group/project runners, privileged Docker, shell executor

GitLab has a marketplace-shaped supply chain: the CI/CD Catalog of components. Assuming otherwise — that only GitHub has this problem — skips the pinning discipline entirely. GitLab's own docs (CI/CD components → Best practices) say to "Pin CI/CD components to a specific commit SHA (preferred) or release version tag to ensure the integrity of the component used in a pipeline."

Workflow

Phase 1: Detect Context

  1. Confirm the forge is GitLab and the CLI is authenticated:

    git remote -v
    glab auth status                      # self-hosted: glab auth login --hostname gitlab.internal.example
    

    If the remote is GitHub or Forgejo, stop and point at the right sibling skill.

  2. Locate the pipeline config. glab ci lint defaults to .gitlab-ci.yml in the current directory, but a project may relocate it (Settings → CI/CD → General pipelines → CI/CD configuration file). If it is relocated, cd to its directory or pass its path/URL — a bare lint would silently validate the wrong file, or nothing.

  3. Validate it parses before changing anything:

    glab ci lint                      # or: glab ci lint <path-or-url>
    
  4. Inventory the third-party surface — run these and triage every hit (CONFIG = the config path):

    # Components/includes on a moving target: ~latest, a partial semver, or a branch
    grep -nE '@(~latest|[0-9]+(\.[0-9]+)?$|main|master)' "$CONFIG"
    
    # include: project blocks — then confirm each has a `ref:` pinned to a 40-char SHA.
    # A missing ref: is a finding: it silently defaults to the target project's HEAD.
    grep -nA3 'include:' "$CONFIG" | grep -E 'project:|ref:'
    grep -nE '^\s*ref:\s*(?![0-9a-f]{40}\s*$)' -P "$CONFIG"   # ref: present but not a SHA
    
    # Remote includes — no auth, no ref to pin; each one is a trust-boundary decision
    grep -nE '^\s*-?\s*remote:' "$CONFIG"
    
    # Images/services not pinned by digest (covers `image:` and `- name:` service entries;
    # the negative lookahead is what keeps already-pinned `foo:1@sha256:…` from false-positiving)
    grep -nPE '^\s*-?\s*(image|name):\s*(?!.*@sha256:)' "$CONFIG"
    
    # Deprecated JWTs (see Phase 5) and inline secrets
    grep -nE 'CI_JOB_JWT' "$CONFIG"
    
    # Runner isolation escapes
    grep -nE 'privileged:\s*true|executor:\s*shell' "$CONFIG" .gitlab-runner/*.toml 2>/dev/null
    

    Treat every hit as a finding to justify or fix — not as noise to skim.

Phase 2: Pin the Third-Party Supply Chain

This is the highest-value c

阅读完整来源 ↗