Zum Inhalt

SAST & Secret Detection Pipeline

This repository includes a Static Application Security Testing (SAST) pipeline designed to automatically scan the codebase for: - Security vulnerabilities using Semgrep (runs in the background) - Secrets (like API keys, passwords, or tokens) using: - GitLab's built-in secret detection - A custom secret detection job (with detection rules defined in .gitlab/.gitleaks.toml)

The purpose of this pipeline is to detect security issues before they are deployed, ensuring that sensitive information remains secure.

Pipeline Jobs

When the pipeline runs, three main jobs run in the Security stage:

Job Name Tool Used Purpose
Secret Detection (GitLab) GitLab's built-in secret detection Scans the codebase for known secret patterns (e.g., API keys, passwords, tokens).
Custom Secret Detection Gitleaks with .gitlab/.gitleaks.toml rules Detects additional secrets defined by us, tailored to our project needs.

Running the Pipeline

The pipeline runs automatically on:

  • Merge Requests (MRs) into the main branch

If the pipeline does not automatically run after starting the MR, please push a commit to the branch (for which the MR was created), and this will trigger the pipeline.

Steps to add changes and check scan results

  • Create a branch
  • Open a Merge Request for that branch with target as "main" to trigger the pipeline
  • Make required changes (code)
  • Commit & push (avoid “allow secrets” in the commit message unless you really want to skip scans)
  • Open the pipeline and check the results (link to the pipeline will appear on the Merge Request Overview page)

Where to see the results of the scans

  • In the MR page, click on the Pipelines tab to see the overview of whether the pipeline was successfull.
  • To see detailed results, in the MR page, click on the Pipelines tab, and click on the Download Artifacts button on the right side of each Pipeline and the scan results will be downloaded as artifacts. (Please note that results will not be generated for skipped scans (scan that contain “allow secrets” in the commit message))

Note: The exact UI panels available for security reports depend on your GitLab edition and project settings, but the artifact will always be available from the job.

Overriding Allowed Secrets

This section gives more information on how to temporarily allow known/acceptable “secrets” (e.g., ULB email) when they get flagged as secrets. Right now, the repository provides a commit-message override mechanism:

  • Put the phrase “allow secrets” anywhere in the commit message (case-insensitive) and push it to the branch for which the MR has been created [The global before_script will detect it and exit 0 early, allowing the job(s) to pass, but as a result, no artifacts/scan results will be generated]
  • As a recommended practice, avoid using the override for the first run of the pipeline.

When to use the override

  • Quick-fix: you know the finding is a false positive and need the MR pipeline to pass to complete the MR. Uses the above-defined approach to override allowed secrets.

Advanced For a more permanent approach, .gitlab/.gitleaks.toml can be edited to refine detection patterns. (That keeps scanning active while reducing false positives.)

  • Identify the false positive in the pipeline report and identify its regex pattern [Please see regex for more information on regex]
  • Add the regex pattern that captures this to the allowlist section in .gitlab/.gitleaks.toml. For eg, let the the existing allowlist be:
[allowlist]
description = "Allow ULB email address"
regexes = [
  '''^wdm@ulb\.tu-darmstadt\.de$'''
]

In order to allow one more secret (eg, "abc123"), the allowlist can be changed to:

[allowlist]
description = "Allow ULB email address"
regexes = [
  '''^wdm@ulb\.tu-darmstadt\.de$''',
  '''^abc123$'''
]
  • Commit and push the updated .gitlab/.gitleaks.toml
  • Rerun the pipeline

Adding New Secret Detection Rules

The current detection defines rules like these (summarized):

  • possible-secret – looks for keys like key|secret|password|apiKey|token followed by a 16+ char value.
  • env_variable_secret – catches export SOMEVAR_USER|PASSWORD|KEY|TOKEN="value"
  • url_token – catches access_token":"<value>"
  • api_header – catches the header X-ELS-APIKey: "<value>"

If you find a secret that is not caught by the pipeline,

  • open .gitlab/.gitleaks.toml
  • add a new [[rules]] entry that matches the secret pattern. Each rule includes:
    • id: Unique identifier for the rule
    • description: What the rule detects
    • regex: Pattern to match the secret
    • tags: Metadata tags
    • eg:
      [[rules]]  
      id = "aws-access-key"       
      description = "AWS Access Key"  
      regex = '''AKIA[0-9A-Z]{16}'''  
      tags = ["aws", "secret"]  
      
    • Commit and push the change
    • Re-run Pipeline on a branch with a sample match and check results

Tips - Start from a similar existing rule and adapt the regex - Keep patterns as specific as possible to avoid false positives, but avoid overfitting - Commit, open an MR, and check the Custom Secret Detection job’s report to ensure desired behaviour

Using this pipeline in a new repository

  • Copy .gitlab-ci.yml and the folder .gitlab/.gitleaks.toml to the new repo.
  • Ensure that there is a runner with tag ulb, or update the tag in .gitlab-ci.yml file.
  • Open a Merge Request to run both secret scans and check results
  • Merge to main

Working with the Protected main Branch

The main branch is protected to ensure that all changes are reviewed and validated by the security pipeline before they are merged. Direct pushes to main are intentionally disabled by default to protect the repository history and enforce the Merge Request workflow.

What happens if I accidentally commit to main?

Create a new branch from your current local main. Your commits will automatically be included in the new branch.

Example:

Before creating a new branch:

origin/main: A---B---C

local main:  A---B---C---D---E

After creating a new branch:

origin/main: A---B---C

local main:       A---B---C---D---E
my-feature:       A---B---C---D---E

Steps to move your work to a feature branch

  1. Create a new branch from your local main(which has the new commits):
git switch -c my-new-branch-name

or

git checkout -b my-new-branch-name
  1. Push the new branch:
git push -u origin my-new-branch-name
  1. Open a Merge Request with:
  2. Source branch: my-new-branch-name
  3. Target branch: main

  4. The Merge Request will trigger the security pipeline described above. (to skip the pipeline (not recommended), include the words "allow secrets" in the MR commit message)

  5. After the Merge Request has been merged, update the local main:

git switch main
git fetch origin
git reset --hard origin/main

Advanced

The pipeline currently detects secrets in the current directory of the branch. But, secrets may be present in the commit history as well, although they may not be present in the branch now (commit history can be viewed by anyone with access to repository). If the commit history of the branch also needs to be analyzed for secrets, the secret_detection_custom_scan job can be slightly modified to achieve this. In order to do this, open the .gitlab-ci.yml file and remove --no-git from the line:

- gitleaks detect --no-git --source . --config .gitlab/.gitleaks.toml --report-format json --report-path gl-custom-detection-report.json

to obtain:

- gitleaks detect --source . --config .gitlab/.gitleaks.toml --report-format json --report-path gl-custom-detection-report.json

Force Pushes

The protected main branch also prevents force pushes (git push --force or git push --force-with-lease) unless they have been explicitly enabled for users with push permissions. [ For settings, see: Harvesting Repo: protected branch settings]

As a recommended practice:

  • Do not unprotect the main branch to push local commits.
  • Do not enable force pushes unless there is a specific need to rewrite the branch history.
  • Always create a feature branch and open a Merge Request. This ensures that the security scans and code review process are executed before changes are merged into main.

Pre-commit Hook Usage

This repository includes a pre-commit hook that enables a local secret_detection scan on every commit made locally.

Please ensure the following pre-requisites to enable the commit based scan, or enable them using the commands mentioned:

One-time setup after cloning:

  • pre-commit installation: pip install pre-commit
  • Navigate to repository root and then: pre-commit install
  • install gitleaks:
    • Windows: winget install --id=Gitleaks.Gitleaks -e gitleaks version or choco install gitleaks
    • Mac: brew install gitleaks
    • Linux: install via your package manager or download the binary from Gitleaks releases
    • GitBash: cmd.exe /c "winget install --id=Gitleaks.Gitleaks -e --accept-source-agreements --accept-package-agreements" , and then confirm installation using: cmd.exe /c "gitleaks version"
  • For an initial full sweep or after updating rules(in the .toml file): pre-commit run gitleaks --all-files

After these steps are completed, the secret_detection will run on every local commit and will output the results of the scan in the terminal.