Skip to content

Change this workflow to not use user-controlled data directly in a run block. #300

@baynezy

Description

@baynezy

To prevent command injection in GitHub Actions workflows, avoid directly interpolating untrusted data into shell commands. Instead, use environment variables to safely pass untrusted data without risk of code execution.

The recommended approach is to assign untrusted data to environment variables using the env key, then reference these variables using standard shell variable syntax ($VARIABLE_NAME or ${VARIABLE_NAME}) rather than GitHub’s expression syntax (${{ …​}}).

Noncompliant code example

The following GitHub Action is vulnerable to command injections as it uses untrusted input directly in a run command:

name: Example

on:
  pull_request:
    branches: [ main ]

jobs:
  main:
    runs-on: ubuntu-latest

    steps:
      - name: Example Step
        run: |
          echo "PR title: ${{ github.event.pull_request.title }}" # Noncompliant

Compliant solution

name: Example

on:
  pull_request:
    branches: [ main ]

jobs:
  main:
    runs-on: ubuntu-latest

    steps:
      - name: Example Step
        env:
          PR_TITLE: ${{ github.event.pull_request.title }}
        run: |
          echo "PR title: $PR_TITLE"

Metadata

Metadata

Assignees

Labels

environmentImprovement, to infra or build environmenttechnical debtAn issue describing some technical debt that needs to be addressed in the futureto doWork that is yet to be started

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions