GitLab CI review: YAML structure and runner shell boundaries

Review GitLab CI script blocks, YAML quoting, runner shells, variable expansion and destructive job commands using exact findings and practical examples.

A .gitlab-ci.yml file describes jobs, but the job commands are interpreted by the runner’s shell. YAML quoting and shell quoting operate at different stages. Review the parsed command, the selected execution environment and the source of each CI variable before deciding whether a finding is a syntax issue, an unsafe argument boundary or an environment assumption.

Keep YAML strings unambiguous

A colon followed by whitespace can make YAML interpret an unquoted command as a mapping instead of a string. Quoting the entire command can protect the YAML structure, while quotes inside the resulting command protect shell arguments. Block scalars are useful for multiline scripts, but literal and folded styles join lines differently. Inspect the intended command boundaries and explicit && or failure handling; valid YAML does not establish that every command in a combined shell string stops the job on failure.

Match variables to the runner shell

Bash and sh read a CI environment variable with $NAME, while PowerShell can use $env:NAME. A Linux image declaration does not describe every possible runner executor or inherited shell configuration. Check the executor and job environment when making portability claims. For shell-family commands, quote data expansions unless deliberate splitting is required. Do not put untrusted values through eval or generate a command string merely because the value originated in a CI variable.

Review the complete job lifecycle

before_script can establish dependencies and working state for script, whereas after_script has separate execution-context behavior and should not be assumed to inherit every shell change. Includes, extends and project variables may contribute configuration absent from a pasted file. Look for setup failures, explicit directory guards and paths used by cleanup or artifacts. A predictable temporary name can collide on shared execution environments; a disposable runner does not prove that every mounted directory is disposable.

Distinguish secret policy from string handling

Protected and masked variables have project-level policies that cannot be read from the script alone. Masking reduces accidental log disclosure but is not a sandbox against malicious job code. Avoid printing credentials, passing them through debugging traces or baking them into artifacts. Review branch and merge request conditions around privileged jobs, then inspect remote downloads and their verification. If an external script or included configuration is not supplied, its behavior and dependencies remain unknown.

Review example

This sample preserves original YAML line numbers for an unchecked directory change, unquoted arguments and an unsafe cleanup destination. It does not imply those variables exist on a real runner.

stages:
  - build
build:
  image: debian:12-slim
  script:
    - cd /workspace/build
    - cp $SOURCE $DESTINATION
    - rm -rf $OUTPUT_DIR/*
Open the checker

What cannot be verified

Bash Checker extracts before_script, script and after_script content for instant shell-family checks, but does not resolve every include or validate the full expanded pipeline. Runner settings, protected variables, mounted paths and external dependencies require separate evidence. A suggested safeguard should state that dependency instead of promising the job is secure. Use the exact rule section linked from a finding to review the actual shell issue.

Finding references

rm -rf with a variable path

Recursive/forced rm using a variable path. If the variable is empty or unset, this can delete far more than intended.

Suggested safeguard: Guard the path: use "${dir:?dir is required}" and quote the expansion, e.g. rm -rf "${dir:?}/subdir".

SC2115

Piping a download into a shell

Remote content is piped straight into a shell. A compromised or partially downloaded response executes with your privileges.

Suggested safeguard: Download to a file, verify it (checksum or signature), inspect it, then execute it as a separate step.

eval with an expansion

eval is used with an expansion. Any attacker-controlled part of that string becomes executable code.

Suggested safeguard: Replace eval with an array, a case statement, or indirect expansion (${!name}) — none of which re-parse data as code.

SC2294

World-writable chmod 777

chmod 777 makes the target world-writable, so any local account can modify or replace it.

Suggested safeguard: Grant the narrowest mode that works — 755 for executables, 644 for data, 700 for private state.

cd without a failure guard

cd without a failure guard. If the directory does not exist the script keeps running in the wrong working directory.

Suggested safeguard: Write cd "$dir" || exit 1 (or set -e plus a quoted path) so the script stops instead of operating on the wrong tree.

SC2164

Unquoted variable expansion

Unquoted variable expansion. Values containing spaces or glob characters get word-split and globbed before the command sees them.

Suggested safeguard: Wrap the expansion in double quotes: "$var" / "${arr[@]}".

SC2086

Legacy backtick substitution

Legacy backtick command substitution. It nests badly and handles backslashes differently from the modern form.

Suggested safeguard: Use $(command) instead of `command`.

SC2006

Non-portable echo -e

echo -e is not portable — it is undefined in POSIX sh and behaves differently across shells and /bin/echo builds.

Suggested safeguard: Use printf '%b\n' "$value" for escape handling that behaves the same everywhere.

SC3037

sudo inside a script

The script invokes sudo. It will either prompt interactively in the middle of a run or silently require passwordless sudo.

Suggested safeguard: Check privileges up front (if [[ $EUID -ne 0 ]]) and fail with a clear message, rather than escalating mid-script.

Predictable /tmp path

Fixed path under /tmp. Predictable temp paths are a symlink-attack vector on shared machines and collide between concurrent runs.

Suggested safeguard: Create the file with mktemp (or mktemp -d) and remove it in a trap.

SC2129

Parsing ls output

Parsing the output of ls. Filenames containing spaces or newlines break the pipeline.

Suggested safeguard: Use a glob or find -print0 with a while IFS= read -r -d '' loop.

SC2012

read without -r

read without -r mangles backslashes in the input.

Suggested safeguard: Use read -r (and IFS= read -r line when preserving leading whitespace).

SC2162

Official references