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, theCI_JOB_TOKENallowlist, project/group variable scoping,id_tokens— not portable concepts in GitLab syntax. It is a sibling ofharden-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.ymlto a project (aftersetup-git-hooks) - When auditing an existing pipeline before making a GitLab project public
- When a supply-chain review (or the
supply-chainskill) 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
- 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.
- 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.
- 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 maintainerfull—audit, thenfixafter confirmation
The GitLab Threat Model — What Actually Differs
Where the risk sits differs from GitHub Actions; the controls are not one-for-one equivalents:
| Concern | GitHub Actions | GitLab CI |
|---|---|---|
| Third-party code execution | uses: actions/x@ref from the Marketplace | include: + CI/CD Catalog components — same class of risk, different keyword |
| Ambient credential | GITHUB_TOKEN with repo-wide default scopes | CI_JOB_TOKEN — defaults to own project only; risk is a widened allowlist |
| Secret exfiltration surface | workflow can read all repo secrets | variable scoping — protected/masked/environment-scoped, plus fork-MR exposure |
| Provenance / keyless auth | OIDC via id-token: write | id_tokens: with an aud claim |
| Runner trust | GitHub-hosted vs self-hosted | shared 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
-
Confirm the forge is GitLab and the CLI is authenticated:
git remote -v glab auth status # self-hosted: glab auth login --hostname gitlab.internal.exampleIf the remote is GitHub or Forgejo, stop and point at the right sibling skill.
-
Locate the pipeline config.
glab ci lintdefaults to.gitlab-ci.ymlin the current directory, but a project may relocate it (Settings → CI/CD → General pipelines → CI/CD configuration file). If it is relocated,cdto its directory or pass its path/URL — a bare lint would silently validate the wrong file, or nothing. -
Validate it parses before changing anything:
glab ci lint # or: glab ci lint <path-or-url> -
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/nullTreat 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
阅读完整来源 ↗