GitLab DevSecOps: How to Add Security Scanning to Your CI/CD Pipeline
DevOps / SRE Engineer & CEO, Cloud DevOps Solutions
Most teams bolt security on at the end. A penetration test before launch finds 40 vulnerabilities, half of them in code that shipped months ago. The fixes take weeks. The release slips. Nobody is happy.
DevSecOps moves security earlier. Instead of a gate at the end, every merge request runs the same security scans that a manual audit would catch: static analysis, dependency vulnerabilities, secret detection, container image scanning, and dynamic application testing. The developer sees the finding while the code is still in their head, not six weeks later in a PDF report.
GitLab builds this into the platform. Every security scanner is a CI/CD job that runs in the same pipeline as your tests. Results appear in the merge request, not in a separate tool. This guide covers how to enable each scanner, what it catches, and how to manage findings in production.
What GitLab DevSecOps covers
GitLab provides seven security scanning categories, all configurable as CI/CD pipeline jobs:
| Scanner | What it finds | When it runs |
|---|---|---|
| SAST (Static Application Security Testing) | Code-level vulnerabilities: SQL injection, XSS, insecure crypto, hardcoded secrets | On every commit |
| DAST (Dynamic Application Security Testing) | Runtime vulnerabilities: OWASP Top 10, misconfigurations, exposed endpoints | Against a running application |
| Dependency Scanning | Known CVEs in third-party libraries (npm, pip, Maven, Go modules, etc.) | On every commit |
| Container Scanning | CVEs in base images and installed packages in Docker images | After image build |
| Secret Detection | API keys, tokens, passwords, private keys committed to the repository | On every commit |
| Licence Compliance | Licences in dependencies that conflict with your policy (GPL in a proprietary product) | On every commit |
| Infrastructure as Code (IaC) Scanning | Misconfigurations in Terraform, CloudFormation, Kubernetes manifests | On every commit |
Each scanner produces a JSON report in a standardised format. GitLab parses these reports and displays findings in the merge request security widget, the vulnerability report, and the security dashboard.
Enabling security scanning in your pipeline
The minimal setup
GitLab provides CI/CD templates that add security scanners with a single include statement. Add this to your .gitlab-ci.yml:
include:
- template: Security/SAST.gitlab-ci.yml
- template: Security/Dependency-Scanning.gitlab-ci.yml
- template: Security/Secret-Detection.gitlab-ci.yml
- template: Security/Container-Scanning.gitlab-ci.yml
- template: Security/DAST.gitlab-ci.yml
- template: Security/License-Scanning.gitlab-ci.yml
- template: Security/IaC-Scanning.gitlab-ci.yml
Each template creates one or more jobs in the test stage (or a dedicated stage). The scanners auto-detect your project’s languages and frameworks.
A complete pipeline example
Here is a realistic pipeline that builds, tests, scans, and deploys a containerised application:
stages:
- build
- test
- scan
- deploy
variables:
CS_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
DAST_WEBSITE: https://staging.example.com
include:
- template: Security/SAST.gitlab-ci.yml
- template: Security/Dependency-Scanning.gitlab-ci.yml
- template: Security/Secret-Detection.gitlab-ci.yml
- template: Security/Container-Scanning.gitlab-ci.yml
- template: Security/DAST.gitlab-ci.yml
- template: Security/License-Scanning.gitlab-ci.yml
build-image:
stage: build
image: docker:24
services:
- docker:24-dind
script:
- docker build -t $CS_IMAGE .
- docker push $CS_IMAGE
unit-tests:
stage: test
image: node:20
script:
- npm ci
- npm test
container_scanning:
stage: scan
variables:
CS_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
dast:
stage: scan
needs: ["deploy-staging"]
deploy-staging:
stage: deploy
script:
- kubectl set image deployment/app app=$CS_IMAGE
environment:
name: staging
url: https://staging.example.com
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
The security jobs from the included templates run automatically. SAST, dependency scanning, and secret detection run in the test stage alongside unit tests. Container scanning runs after the image is built. DAST runs after the application is deployed to staging.
SAST: Static Application Security Testing
SAST analyses your source code without executing it. GitLab auto-detects the language and selects the appropriate analyser.
Supported languages and analysers
GitLab SAST supports over 15 languages including:
- JavaScript/TypeScript: Semgrep, NodeJsScan
- Python: Semgrep, Bandit
- Java/Kotlin: Semgrep, SpotBugs
- Go: Semgrep, Gosec
- Ruby: Semgrep, Brakeman
- C/C++: Semgrep, Flawfinder
- PHP: Semgrep, PHPStan
- .NET/C#: Semgrep, Security Code Scan
Semgrep is the primary analyser for most languages. It uses pattern-matching rules that are regularly updated by GitLab’s security team.
What SAST catches
- SQL injection
- Cross-site scripting (XSS)
- Path traversal
- Insecure deserialisation
- Hardcoded credentials
- Weak cryptographic algorithms
- Insecure random number generation
- Command injection
- Server-side request forgery (SSRF)
Customising SAST
Override variables to customise behaviour:
sast:
variables:
SAST_EXCLUDED_PATHS: "spec,test,tests,tmp,vendor"
SEARCH_MAX_DEPTH: 10
SAST_BANDIT_EXCLUDED_PATHS: "*/test/*"
For custom Semgrep rules, add a .semgrep.yml file to your repository root:
rules:
- id: no-eval-in-production
pattern: eval($X)
message: "eval() is dangerous and should not be used in production code"
severity: ERROR
languages: [python]
Dependency Scanning
Dependency scanning checks your project’s third-party libraries against known vulnerability databases (NVD, GitLab Advisory Database, and others).
Supported package managers
- npm / yarn / pnpm (JavaScript)
- pip / pipenv / poetry (Python)
- Maven / Gradle (Java)
- Go modules
- Bundler (Ruby)
- NuGet (.NET)
- Composer (PHP)
- Conan (C/C++)
What it finds
Every dependency is checked against CVE databases. The scanner reports:
- The vulnerable package and version
- The CVE identifier and severity (Critical, High, Medium, Low)
- The fixed version (if one exists)
- A description of the vulnerability and its impact
Handling findings
When dependency scanning finds a vulnerability, you have several options:
- Upgrade the package to the fixed version (the preferred approach)
- Dismiss the finding if it is a false positive or does not apply to your usage
- Create an issue to track the remediation
- Add the finding to the vulnerability report for ongoing tracking
For automated dependency updates, pair GitLab dependency scanning with Dependabot or Renovate Bot, which creates merge requests to upgrade vulnerable packages automatically.
Container Scanning
Container scanning analyses your Docker images for known vulnerabilities in OS packages and language-specific dependencies.
How it works
GitLab uses Trivy (the default scanner) or Grype to scan container images. The scanner:
- Pulls the built image from your container registry
- Analyses every installed package (dpkg, rpm, apk) and language dependency
- Checks each against CVE databases
- Reports findings with severity, CVE ID, and fixed version
Configuration
container_scanning:
variables:
CS_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
CS_SEVERITY_THRESHOLD: HIGH
CS_SEVERITY_THRESHOLD controls which severities appear in the merge request widget. Setting it to HIGH shows only high and critical findings, reducing noise from low-severity issues that rarely matter in practice.
Base image selection matters
Most container vulnerabilities come from the base image, not your application code. Choosing the right base image eliminates hundreds of findings:
- Alpine-based images (e.g.
node:20-alpine) have the smallest attack surface - Distroless images (e.g.
gcr.io/distroless/nodejs20-debian12) contain only the runtime, no shell or package manager - Chainguard images provide minimal, FIPS-compliant base images with near-zero CVEs
A switch from node:20 (Debian-based, 200+ packages) to node:20-alpine (30 packages) typically eliminates 60-80% of container scanning findings. For more on container security practices, see our DevSecOps and container security service.
Secret Detection
Secret detection scans your repository for accidentally committed credentials: API keys, tokens, passwords, private keys, and other sensitive strings.
What it catches
GitLab’s secret detection uses pattern-matching rules to find:
- AWS access keys and secret keys
- GCP service account keys
- Azure subscription keys
- GitHub / GitLab personal access tokens
- Stripe, Twilio, SendGrid, and other SaaS API keys
- SSH and PGP private keys
- Database connection strings with embedded passwords
- Generic high-entropy strings that look like secrets
Prevention vs detection
Secret detection in CI/CD catches secrets after they are committed. For prevention (blocking the commit before it reaches the repository), use:
Git pre-commit hooks with GitLeaks or detect-secrets:
# Install gitleaks
brew install gitleaks
# Add as a pre-commit hook
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/sh
gitleaks protect --staged --verbose
EOF
chmod +x .git/hooks/pre-commit
GitLab push rules (available in Premium/Ultimate):
Enable “Prevent pushing secret files” in Settings > Repository > Push rules. This rejects pushes that contain files matching secret patterns.
When secrets are found
If a secret is detected in your repository:
- Rotate the secret immediately. The secret must be considered compromised even if the repository is private. Generate a new key/token.
- Remove the secret from the code. Use environment variables, a secrets manager like HashiCorp Vault, or GitLab CI/CD variables (Settings > CI/CD > Variables).
- Clean git history if the secret was in a public repository. Use
git filter-repoor BFG Repo-Cleaner to remove it from all commits. - Dismiss the finding in GitLab after remediation.
DAST: Dynamic Application Security Testing
DAST tests a running application by sending real HTTP requests and analysing the responses. It finds vulnerabilities that static analysis cannot: misconfigurations, authentication flaws, and runtime injection points.
DAST configuration
dast:
variables:
DAST_WEBSITE: https://staging.example.com
DAST_FULL_SCAN_ENABLED: "true"
DAST_BROWSER_SCAN: "true"
- Passive scan (default): analyses responses to normal browsing. Fast, non-invasive. Catches missing security headers, information disclosure, insecure cookies.
- Full scan: actively probes for vulnerabilities (SQL injection, XSS, command injection). Slower but thorough. Only run against staging or test environments, never production.
- Browser-based scan: uses a real browser to render JavaScript-heavy applications, catching issues in SPAs that a traditional crawler would miss.
DAST with authentication
For applications behind a login, configure DAST to authenticate:
dast:
variables:
DAST_WEBSITE: https://staging.example.com
DAST_AUTH_URL: https://staging.example.com/login
DAST_USERNAME: test-user
DAST_PASSWORD: $DAST_TEST_PASSWORD
DAST_USERNAME_FIELD: "css:input[name='email']"
DAST_PASSWORD_FIELD: "css:input[name='password']"
DAST_SUBMIT_FIELD: "css:button[type='submit']"
Store DAST_TEST_PASSWORD as a masked CI/CD variable. Use a dedicated test account with limited permissions, not a real user account.
What DAST catches
- Missing security headers (CSP, HSTS, X-Frame-Options)
- Insecure cookies (missing Secure, HttpOnly, SameSite flags)
- Cross-site scripting (reflected and stored)
- SQL injection
- Directory traversal
- Information disclosure (stack traces, version numbers, debug endpoints)
- Open redirects
- Server misconfigurations
Licence Compliance
Licence compliance scanning identifies the licences of your project’s dependencies and flags those that conflict with your organisation’s policy.
Why it matters
Including a GPL-licensed library in a proprietary product can create legal obligations to open-source your code. Licence compliance scanning catches this before it ships.
Configuration
license_scanning:
variables:
LICENSE_FINDER_CLI_OPTS: "--decisions-file=./licence-decisions.yml"
Create a licence policy in GitLab (Security and Compliance > Policies) or use a licence-decisions.yml file to approve or deny specific licences:
- Permissive (typically safe): MIT, Apache 2.0, BSD, ISC
- Copyleft (requires review): GPL, LGPL, AGPL, MPL
- Denied (block in pipeline): whatever your legal team says
IaC Scanning
Infrastructure as Code scanning analyses your Terraform, CloudFormation, Kubernetes manifests, and Helm charts for security misconfigurations.
What it catches
- S3 buckets without encryption
- Security groups with 0.0.0.0/0 ingress on sensitive ports
- Kubernetes pods running as root
- Missing network policies
- Unencrypted database instances
- IAM policies with overly broad permissions
- Missing logging and monitoring configuration
Configuration
IaC scanning uses KICS (Keeping Infrastructure as Code Secure) by default:
iac_scanning:
variables:
SAST_IAS_EXCLUDED_PATHS: "examples,test"
For teams using Terraform, pair GitLab IaC scanning with Infracost for cost estimation in the same merge request: security and cost impact, visible before the code merges.
Managing vulnerabilities
The merge request security widget
When security scans complete, GitLab adds a security widget to the merge request. It shows:
- New vulnerabilities introduced by this merge request
- Fixed vulnerabilities (present in the target branch but resolved in this branch)
- Existing vulnerabilities (already present in the target branch)
This distinction is critical. Developers should focus on new vulnerabilities they introduced, not existing technical debt. Existing vulnerabilities are tracked separately in the vulnerability report.
Vulnerability report
The project-level vulnerability report (Security and Compliance > Vulnerability report) aggregates findings across all scanners. For each vulnerability:
- Detected: newly found, needs triage
- Confirmed: triaged and confirmed as a real issue
- Dismissed: false positive or accepted risk
- Resolved: the fix has been merged
Security policies
GitLab security policies (available in Ultimate) automate enforcement:
Scan execution policies ensure security scans run on every pipeline, even if a developer removes the include statement from .gitlab-ci.yml. The policy is enforced at the group or project level.
Merge request approval policies require security team approval when a merge request introduces a critical or high severity vulnerability. This prevents vulnerable code from merging without review.
Example: require approval from @security-team when any scanner finds a critical vulnerability:
type: approval_policy
name: Block Critical Vulnerabilities
actions:
- type: require_approval
approvals_required: 1
user_approvers:
- security-team-lead
rules:
- type: scan_finding
scanners:
- sast
- dependency_scanning
- container_scanning
severity_levels:
- critical
vulnerability_states:
- new_needs_triage
Pipeline performance
Security scanning adds time to your pipeline. Here is how to keep it fast:
Run scans in parallel
GitLab templates already configure scans as parallel jobs. Ensure your runners have enough capacity to run them concurrently. Five security scans running in parallel add only as much time as the slowest scan, not the sum of all five.
Use caching
Dependency scanning and SAST download analyser images on every run. Configure a Docker registry mirror or pull-through cache on your runners to eliminate the download overhead.
Limit DAST scope
Full DAST scans can take 30+ minutes. For merge request pipelines, run a passive scan (fast). Run the full scan on a schedule (nightly or weekly) against the staging environment.
dast:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
variables:
DAST_FULL_SCAN_ENABLED: "false"
- if: $CI_PIPELINE_SOURCE == "schedule"
variables:
DAST_FULL_SCAN_ENABLED: "true"
Skip unchanged paths
SAST and secret detection run on the full codebase by default. For large monorepos, use SAST_EXCLUDED_PATHS to skip directories that do not contain application code (documentation, generated files, vendored dependencies).
GitLab DevSecOps tiers
Security scanning features vary by GitLab tier:
| Feature | Free | Premium | Ultimate |
|---|---|---|---|
| SAST | Yes | Yes | Yes |
| Secret Detection | Yes | Yes | Yes |
| Dependency Scanning | Limited | Yes | Yes |
| Container Scanning | Limited | Yes | Yes |
| DAST | No | No | Yes |
| Licence Compliance | No | No | Yes |
| IaC Scanning | Yes | Yes | Yes |
| Security Dashboard | No | No | Yes |
| Vulnerability Management | No | No | Yes |
| Security Policies | No | No | Yes |
Teams on Free or Premium can still use SAST, secret detection, and IaC scanning. For the full DevSecOps experience (DAST, vulnerability management, security policies), Ultimate is required.
For teams that use GitHub Actions instead of GitLab CI, our CI/CD pipeline guide covers how to implement similar security scanning with GitHub-native and third-party tools. And for a broader view of pipeline security across platforms, see our CI/CD best practices guide.
Getting started checklist
- Add the SAST and secret detection templates to your
.gitlab-ci.yml. These are free and catch the most common issues. - Add dependency scanning. Most teams have more third-party code than first-party; the vulnerabilities are there.
- Add container scanning if you build Docker images. Switch to a minimal base image first to reduce noise.
- Set up merge request approval policies to require review for critical findings.
- Run DAST on a schedule against your staging environment. Start with passive scans.
- Review the vulnerability report weekly. Triage findings, dismiss false positives, and track confirmed issues to resolution.
- Add pre-commit hooks for secret detection to catch secrets before they reach the repository.
For teams that want help implementing DevSecOps across their pipeline, our DevSecOps and container security service covers the full spectrum from scanner configuration to policy enforcement and developer training. We also help integrate these practices with cloud security posture management for end-to-end protection.
Key takeaways
- GitLab integrates seven security scanners directly into the CI/CD pipeline: SAST, DAST, dependency scanning, container scanning, secret detection, licence compliance, and IaC scanning
- Adding security scanning is as simple as including GitLab’s CI/CD templates; auto-detection handles language and framework configuration
- The merge request security widget separates new vulnerabilities (your responsibility) from existing ones (tracked separately), keeping developer focus clear
- Secret detection catches committed credentials, but pre-commit hooks prevent the commit in the first place
- Container scanning findings drop dramatically when you switch from full OS images to Alpine or distroless base images
- Security policies enforce scanning even when developers modify the pipeline, and merge request approval policies gate critical vulnerabilities
- SAST and secret detection are available on GitLab Free; the full DevSecOps suite (DAST, vulnerability management, policies) requires Ultimate
Want to embed security into your CI/CD pipeline without slowing down releases? Book a free consultation and we will design a DevSecOps workflow for your team.
Related service
Need hands-on help? Our certified engineers deliver this as a managed service.
DevSecOps & Container Security