Theme
Tier 4 · lesson 10 of 10 · 30 min

Audit the gate before someone else does

You will: Read a deploy workflow the way an auditor would, and separate what it guarantees from what it only appears to.

Builds on A pipeline for a machine with motors.

The previous lesson designed a gate. This one attacks it. The skill is the one an auditor or a red team brings to any pipeline: read every line that runs near a credential, and for each one ask who can make this line do something else?

Start with what holds, because it is a good deal. The workflow asks for read-only tokens, and the repository's default token is read-only too. The tag becomes a digest before anyone approves, so what was approved is what lands. A typed DEPLOY stops a misclick. With no robot to reach, it fails honestly at the SSH step.

The first version of this workflow had five gaps. An audit in November 2026 found them. Four are now closed in the file, and one cannot be closed. Each is shown with what was wrong and what replaced it, because the before is where the lesson is. The version the audit read is still in the history, at commit 99cb2a4.

1. Whoever starts a deploy can approve it. Still open. The robot environment has one required reviewer, the repository's owner, and GitHub's "Prevent self-review" is off. So the gate is a deliberate pause, not separation of duties: one person can dispatch and approve. With a single maintainer it cannot be otherwise, and saying so is better than letting "required reviewer" suggest a second pair of eyes.

2. The audit line named the wrong person. Closed. The summary wrote approved by @${{ github.actor }}, but github.actor is whoever started the run, not whoever approved it. Here they were the same person, so the line was right by accident, which is the worst way for an audit record to be right. It now asks GitHub for the run's approvals (gh api repos/{owner}/{repo}/actions/runs/{run_id}/approvals) and records who started the run on a separate line.

3. Inputs became code. Closed. ${{ inputs.image_tag }} and ${{ inputs.robot_id }} were pasted into the scripts by GitHub before the shell read them. A robot id containing a quote and a command would have closed the quoting and run the command: in the preflight, which runs before anyone approves, and again in the script sent over SSH, where it would run on the robot. Now every input enters through env: and is used as a shell variable, so it arrives as data. The robot's script is a quoted heredoc (<<'SSH'), so the runner splices nothing into it, and the values cross as arguments quoted with printf %q. shellcheck had flagged the unquoted heredoc as SC2087 all along. A new preflight step also refuses any tag or id that is not a plain name, because nothing needs one that is not.

4. The key would have sat outside the gate. Closed in the file; the setting is a separate check. The workflow said to store ROBOT_HOST, ROBOT_USER and ROBOT_SSH_KEY as repository secrets. Every workflow in the repository, on every branch, can read a repository secret, so a branch carrying a new workflow file could have used the robot's key without ever meeting the approval. It now says environment secrets on robot, which are released only to a job that has passed the gate. A deployment-branch rule would also limit that job to main. That is a setting, not a line in the file, so no file can prove it is there: look at Settings → Environments → robot.

5. The robot was never authenticated. Closed. StrictHostKeyChecking=accept-new trusted the first host key it saw and remembered it. A fresh CI runner remembers nothing, so every run would have trusted whatever answered at ROBOT_HOST, and anything able to sit between the runner and the robot would have received the deploy. The robot's host key is now a fourth secret, ROBOT_HOST_KEY, written to a known_hosts file, and SSH runs with StrictHostKeyChecking=yes. With no host key set, the deploy refuses to start.

None of these was ever exploitable, because no robot exists and none of the ROBOT_* secrets has been set. That is exactly why they were fixed then: before the first key exists, each fix costs a few lines. Afterwards, each one comes with a key rotation.

In the simulation

Audit this page's gate the way the lesson audits the workflow: try every route a command can take, and separate what holds from what only looks like it does. The joint limits hold on every route. The bench does not: it is reported, never refused.

Kinematic: joints move at their real speed limit, but there is no gravity, no load and no physics. That is what the steps below are for.

The SO-101 loads as you reach it.

In this lesson

  1. Route one, the slider. Drag shoulder_lift to the bottom of its range, -100°.
  2. Route two, the keys. Jog shoulder_pan with , until it reaches its lower limit, -110°.
  3. Press h to home the arm.
  4. Now drive the arm into the bench. It goes, turns red, and is reported, but nothing stopped it: a gap like a clamped goal that still reports SUCCEEDED.

Do it

  1. Find every place the workflow uses an input.

    grep -n 'inputs\.' .github/workflows/hardware-deploy.yml
    

    Expect: A comment, then inputs.confirm once, in an if: that GitHub evaluates. Every inputs.image_tag and inputs.robot_id is on an env: line or in the job's display name, never inside a run: script.

  2. Ask the same question of the version the audit read.

    git show 99cb2a4:.github/workflows/hardware-deploy.yml | grep -n 'inputs\.'
    

    Expect: inputs.image_tag and inputs.robot_id inside scripts: in the preflight, which runs before approval, in the docker run sent to the robot, and in the deployment record.

  3. Find the token's permissions, the gate, the secrets, the host-key check and the audit line.

    grep -n -E 'permissions|environment:|secrets\.|StrictHostKeyChecking|approvals' .github/workflows/hardware-deploy.yml
    

    Expect: The permissions: block (every line under it is read, including actions: read for the approvals), the registry login's GITHUB_TOKEN, four ROBOT_* secrets used only in the gated deploy job, StrictHostKeyChecking=yes, and the approvals API in the record step.

  4. On your own copy, open Settings → Environments → robot (create it if it does not exist yet) and read four things: the required reviewers, whether "Prevent self-review" is ticked, the deployment-branch rule, and whether any ROBOT_* secret is stored there or, wrongly, under the repository's secrets.

  5. On your copy, run the workflow with DEPLOY and an image tag that exists in your registry, and approve it yourself. When it fails at the SSH step, read the summary's "approved by" and "started by" lines, and compare them with the reviewer the run page shows.

    Expect: Your name in all three places, because you did both. The difference from the old version is where "approved by" comes from: the run's approval record, so it stays right when the reviewer is someone else.

You will know it worked when: You can name the five gaps, point at the line or setting behind each and at what replaced it, and say which one a project with a single maintainer cannot close.

When it goes wrong

Secrets were "behind the approval", and a new workflow on a branch read them anyway.

Why: They were repository secrets. An environment's rules guard its own secrets and the jobs that use it, nothing else.

Fix: Move them to environment secrets on robot, and add a deployment-branch rule for main.

Quoting the input as '${{ inputs.robot_id }}' did not stop the injection.

Why: GitHub substitutes the expression into the script text before the shell parses anything, so a quote inside the input closes yours.

Fix: Put the input in env: and use "$ROBOT_ID" in the script. The shell then receives it as a value, never as code.

The deploy stops with "ROBOT_HOST_KEY is not set".

Why: It refuses to trust whatever answers at ROBOT_HOST, and no host key was given to check against.

Fix: Read /etc/ssh/ssh_host_ed25519_key.pub on the robot itself, not over the network, and store ROBOT_HOST, a space and that line as the ROBOT_HOST_KEY environment secret on robot.

Terms

Separation of duties
No single person can both request and approve a sensitive change. A required reviewer provides it only when the reviewer cannot be the requester.
Expression injection
Untrusted text substituted into a script before the script is parsed, so the text can become commands. In GitHub Actions, any ${{ }} inside a run: block.
Environment secret
A secret released only to jobs that use a given environment, and therefore only after that environment's reviewers and branch rules are satisfied.
Trust on first use (TOFU)
Accepting a server's identity the first time you see it and checking it from then on. On a machine that starts fresh every run, every use is the first.

Every term in the course: glossary.

Check yourself

Answer in your head first, then open it.

The preflight job had only a read token and runs before approval. Why did an injection there still matter?

It runs before anyone has looked at anything, so the approval cannot prevent it, and a read token can still pull private images or leak whatever the job can see. The gate protects the deploy job, not the steps before it.

Why does moving the SSH key to an environment secret change who can use it, with the workflow file unchanged?

A repository secret is available to every workflow on every branch. An environment secret is released only to a job that names the environment and has passed its rules, so the key exists only after approval.

Why do the values reach the robot as printf %q arguments rather than being written into the heredoc?

An unquoted heredoc is expanded by the runner's shell, so a value becomes part of the remote script's text and the robot's shell parses it as code. A quoted heredoc sends the script verbatim. The values travel as arguments, quoted for the one shell that parses them on the robot, and arrive as $1 and $2.

Which of the five gaps can a one-person project not close, and what is the honest response?

Separation of duties: with one maintainer, the requester and the approver are the same person. The honest response is to say so, that the gate is a deliberate pause and not a second opinion, rather than let "required reviewer" suggest otherwise.

Stretch

Make gap 3 stay closed. Write a check that fails CI if any workflow in .github/workflows/ has a ${{ inputs.* }} or ${{ github.event.* }} expression inside a run: script, and show it failing on the version at 99cb2a4 and passing on today's.

Hint

A linter is not enough on its own: actionlint, run on the old file, reported the unquoted heredoc and nothing about the inputs. Parse the YAML rather than grepping it, so an expression on an env: line is allowed and the same expression in run: is not.

One way to do it

Load each workflow with a YAML parser, walk jobs.*.steps[*].run, and fail on any match of \$\{\{\s*(inputs|github\.event)\. with the file, job and step named. Run it as a fast test beside the others, so it fails in seconds. On 99cb2a4 it names the preflight's resolve step and the deploy step; on today's file it finds nothing.

Going deeper