AI Hints
CI/CD with GitHub Actions · Lesson 107 of 138

Script Injection

Untrusted text in run.

⏱ 2 min read▶ Video at 2:04:45🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Some values in a workflow come from strangers. A pull request title, an issue comment, a branch name. If you put them straight into a run script, a stranger can write shell commands that your workflow runs.

Script Injection
From the video: Script Injection
  • Titles, comments, branch names
  • Written by strangers
  • Pasted into your script

The unsafe way

This step puts the pull request title directly into the run script, with the double braces. GitHub replaces the expression before the shell runs. So the title becomes part of the script itself.

.github/workflows/pr.ymlyaml
name: PR check
on: pull_request
jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - name: Show the title
        run: echo "Checking ${{ github.event.pull_request.title }}"

The attack

Cat shows the attacker's pull request title. It closes the quote, and adds commands. Our helper sets this as the title, and the workflow runs the commands. Who am I prints the user, so the attacker's code really ran. In a real workflow, it could read secrets, or push code.

Terminal
cat title.txt
Fix typo"; whoami; echo "I ran a command
run-steps .github/workflows/pr.yml --event pull_request --set "github.event.pull_request.title=$(cat title.txt)" | grep -vE '^Workflow|Job'
  ✓ Show the title
      Checking Fix typo
      root
      I ran a command

The fix

The safe way passes the title through an environment variable. The shell reads it as plain text, and never runs it. Same evil title, and now it's just printed. Use this pattern for any value a stranger can control.

Terminal
cat .github/workflows/pr.yml | tail -3
        env:
          TITLE: ${{ github.event.pull_request.title }}
        run: echo "Checking $TITLE"
run-steps .github/workflows/pr.yml --event pull_request --set "github.event.pull_request.title=$(cat title.txt)" | grep -vE '^Workflow|Job'
  ✓ Show the title
      Checking Fix typo"; whoami; echo "I ran a command

Summary

Never put untrusted values straight into run scripts. Pass them in with env, and use them as shell variables. Next, a dangerous trigger.

Key takeaways
  • Titles are untrusted
  • Use env:
  • Then $VAR
Next up: pull_request_target Dangers — secrets plus strangers