CD7: CI pipelines with GitHub Actions

Lesson 3 of 5 · reading 15–25 minutes; practice and project time is on the module page

Keeping workflows secure

🎯You'll be able to

Give workflows least privilege, pin the actions they use, handle untrusted input safely, and know the risks of forks and self-hosted runners.

🏠The everyday picture

A building's maintenance contractor gets a key to the plant room, not to every flat. The key is cut from a lock you've checked, not a copy someone hands you in the car park. And notes slipped under the door are read, not obeyed.

📘What it really is

📝A worked example

Input: the starter workflow has permissions: write-all, uses: actions/checkout@main and a step run: echo "Deploying with token ${{ secrets.DEPLOY_TOKEN }}".

Expected output: Project 2 test 2 fails with set top-level permissions: contents: read, and test 3 fails with actions/checkout@main isn't pinned to a version tag or commit SHA, then with a step prints a secret. Changing to contents: read, @v7 tags (or SHAs) and deleting the echo step makes both pass.

🌍Where it's used

📝Case scenario

A project's workflow labelled pull requests using pull_request_target, so it could write labels. A later change checked out the pull request's code in the same workflow to run a linter. An attacker opened a pull request that changed the linter's configuration to send the repository's secrets to their server. The project split the work into two workflows: an unprivileged one for the fork's code and a privileged one that only read the results.

🖱️Try it

Find every uses: line in your workflow and check each is pinned. Then search the workflow for ${{ github.event. inside a run: line. Success: every action has @v<number> or a SHA, and no event field appears directly inside a script.

⚠️Common mistakes

🛠️For your project

Project 2 tests 2 and 3 check contents: read, no write-all, pinned actions, and no step that prints a secret.

✅Quick check

Why pin an action to a commit SHA?

Show the answer

A SHA can't be moved to different code, unlike a tag or branch.

Why are self-hosted runners risky on public repositories?

Show the answer

Anyone can open a pull request whose workflow runs on your machine.

← PreviousWriting RouteRani's workflow
ProLearners Assist