Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 36 ยท Security and Compliance

Security Scanning: SAST, Secrets and Dependencies

Add SAST, secret detection, dependency, container and DAST scans to a GitLab pipeline with templates, read the reports, and set a severity gate for merge requests.

Most security problems are found late: after release, by a customer or an attacker. GitLab lets you find them early by running scanners inside the pipeline of every merge request. A scanner is just a CI job that reads your code, your dependencies or your running application and writes a report in a standard format. GitLab then shows the findings next to the code change.

This lesson lists the scan types, shows how to switch them on with templates, and explains how to turn findings into a decision. The scanners themselves cannot run on this site, so the YAML is read-only, and the example code models a report and a severity gate.

The scan types

Scan Looks at Finds
SAST (static application security testing) Your source code Risky patterns such as SQL built from strings or weak crypto
Secret detection The repository and its history API keys, tokens and passwords committed by mistake
Dependency scanning Lock files and manifests Known vulnerabilities (CVEs) in libraries you use
Container scanning The built image Vulnerable operating system packages and libraries in the image
DAST (dynamic testing) A running application, from outside Missing headers, injection, broken authentication
IaC scanning Terraform, Kubernetes, CloudFormation files Misconfigurations such as public buckets
Fuzzing Programs with random input Crashes and memory errors

Different scans catch different problems, so they complement one another. Static scans are quick and run on every commit; DAST needs a deployed target, so it usually runs against a review app or staging.

Switching scans on with templates

GitLab ships CI templates. Including one adds the jobs to your pipeline:

YAML
stages:
  - build
  - test
  - deploy

include:
  - template: Security/SAST.gitlab-ci.yml
  - template: Security/Secret-Detection.gitlab-ci.yml
  - template: Security/Dependency-Scanning.gitlab-ci.yml
  - template: Security/Container-Scanning.gitlab-ci.yml
  - template: DAST.gitlab-ci.yml

Each template adds jobs to the test stage (names like semgrep-sast, secret_detection, container_scanning; the exact analyzer and job names change between versions), and you can adjust them through variables:

YAML
variables:
  SAST_EXCLUDED_PATHS: "spec, test, tests, tmp"
  SECURE_LOG_LEVEL: "info"

container_scanning:
  variables:
    CS_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA

Container and DAST scans need a target: the image you pushed to the registry (see Container Registry) or the URL of a review app (see Review Apps and Dynamic Environments). Dependency scanning relies on a lock file, so commit package-lock.json, poetry.lock or the equivalent.

Which features appear in the interface depends on your tier. In the free tier, the scanners run and write a JSON report you can download as an artifact. Ultimate adds the security widget in the merge request, the vulnerability report for the project, severity filtering, status tracking and security policies. Check the tier table in the documentation before planning a rollout.

Reports and artifacts

Scanner jobs publish their output through the artifacts:reports keyword, for example:

YAML
semgrep-sast:
  artifacts:
    reports:
      sast: gl-sast-report.json

The report is JSON with a list of vulnerabilities, each having a name, severity (Critical, High, Medium, Low, Info, Unknown), a location (file and line, or dependency name and version), identifiers (CWE or CVE numbers) and sometimes a solution. You can read it yourself, and the next step shows how to write a gate that uses it.

Findings: triage, not panic

Scanners produce false positives and findings that are real but unreachable. A healthy workflow is:

  1. Triage: for each finding decide confirmed, dismissed (with a reason such as "false positive" or "used in tests only") or needs more information.
  2. Fix confirmed findings, starting with Critical and High severity that are reachable.
  3. Track the rest as issues with a due date, so they do not vanish.

Dismissing a finding should leave a note, because the next person asks why. Treat secret detection differently: a leaked secret must be revoked and rotated, and removing it from the file is not enough, since it stays in Git history.

Gates: stop risky merges

A report is only useful if it changes decisions. With Ultimate you can use merge request approval policies (a kind of security policy) such as "if a new Critical or High vulnerability is introduced, require approval from the security team". The same idea can be built in any tier with a job that reads the report and fails when a threshold is exceeded.

A good gate looks at new findings only, not the whole backlog, otherwise every merge request fails on old problems nobody is allowed to fix in this change. Policies can also force scanners to run (a scan execution policy), so a team cannot "forget" to include the template.

Stand-in: a severity gate

This example takes a small report in the shape described above (simplified) and fails when a finding is at or above a chosen severity and is not already known.

JS A severity gate

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The second example is a tiny secret detector. Real detectors use many patterns plus entropy checks and live in GitLab's analyzer; this one shows the idea with two invented formats, so only run it on sample text.

JS A toy secret detector

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which scan looks at a running application from the outside?

    DAST sends requests to a deployed application and watches the responses, so it needs a target such as a review app or staging.
  2. 2. A scanner finds an API key in a commit. What must you do besides deleting the line?

    Anyone who cloned the repository, or can read its history, can still read the old commit. The key must be considered compromised.
  3. 3. How do you enable SAST with the least effort?

    The template adds the scanner jobs to your pipeline, and variables tune them.
  4. 4. Why should a merge request gate focus on new findings?

    A gate that blocks on old problems is ignored or bypassed. Comparing against the target branch keeps it fair and effective.

Summary

  • Scanners are CI jobs: SAST, secret detection, dependency, container, DAST and IaC scanning each cover different risks.
  • Include GitLab's templates, commit lock files, and give container and DAST scans a target.
  • Reports are JSON artifacts; the interface features (merge request widget, vulnerability report, policies) depend on your tier.
  • Triage findings, rotate any leaked secret, and gate merges on new high-severity findings.

Next: Compliance, Audit Events and Approvals