index.md 31,1 КБ
Newer Older
Evan Read's avatar
Evan Read включено в состав коммита
1
2
3
4
---
type: reference, howto
---

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
5
# Static Application Security Testing (SAST) **(ULTIMATE)**
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
6

GitLab Bot's avatar
GitLab Bot включено в состав коммита
7
> [Introduced](https://gitlab.com/gitlab-org/gitlab/issues/3775)
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
8
9
10
11
12
13
14
15
16
17
18
19
in [GitLab Ultimate](https://about.gitlab.com/pricing/) 10.3.

NOTE: **4 of the top 6 attacks were application based.**
Download our whitepaper,
["A Seismic Shift in Application Security"](https://about.gitlab.com/resources/whitepaper-seismic-shift-application-security/)
to learn how to protect your organization.

## Overview

If you are using [GitLab CI/CD](../../../ci/README.md), you can analyze your source code for known
vulnerabilities using Static Application Security Testing (SAST).

GitLab Bot's avatar
GitLab Bot включено в состав коммита
20
21
22
23
24
You can take advantage of SAST by doing one of the following:

- [Including the CI job](#configuration) in your existing `.gitlab-ci.yml` file.
- Implicitly using [Auto SAST](../../../topics/autodevops/index.md#auto-sast-ultimate) provided by
  [Auto DevOps](../../../topics/autodevops/index.md).
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
25
26
27
28

GitLab checks the SAST report, compares the found vulnerabilities between the
source and target branches, and shows the information right on the merge request.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
29
![SAST Widget](img/sast_v12_9.png)
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

The results are sorted by the priority of the vulnerability:

1. Critical
1. High
1. Medium
1. Low
1. Unknown
1. Everything else

## Use cases

- Your code has a potentially dangerous attribute in a class, or unsafe code
  that can lead to unintended code execution.
- Your application is vulnerable to cross-site scripting (XSS) attacks that can
  be leveraged to unauthorized access to session data.

## Requirements

GitLab Bot's avatar
GitLab Bot включено в состав коммита
49
To run a SAST job, by default, you need GitLab Runner with the
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
50
51
52
53
54
[`docker`](https://docs.gitlab.com/runner/executors/docker.html#use-docker-in-docker-with-privileged-mode) or
[`kubernetes`](https://docs.gitlab.com/runner/install/kubernetes.html#running-privileged-containers-for-the-runners)
executor running in privileged mode. If you're using the shared Runners on GitLab.com,
this is enabled by default.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
55
56
57
Privileged mode is not necessary if you've [disabled Docker in Docker
for SAST](#disabling-docker-in-docker-for-sast)

GitLab Bot's avatar
GitLab Bot включено в состав коммита
58
59
CAUTION: **Caution:** Our SAST jobs currently expect a Linux container type. Windows containers are not yet supported.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
60
61
62
63
CAUTION: **Caution:**
If you use your own Runners, make sure that the Docker version you have installed
is **not** `19.03.00`. See [troubleshooting information](#error-response-from-daemon-error-processing-tar-file-docker-tar-relocation-error) for details.

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
64
65
66
67
68
69
70
## Supported languages and frameworks

The following table shows which languages, package managers and frameworks are supported and which tools are used.

| Language (package managers) / framework                                     | Scan tool                                                                              | Introduced in GitLab Version |
|-----------------------------------------------------------------------------|----------------------------------------------------------------------------------------|------------------------------|
| .NET                                                                        | [Security Code Scan](https://security-code-scan.github.io)                             | 11.0                         |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
71
| Any                                                                         | [Gitleaks](https://github.com/zricethezav/gitleaks) and [TruffleHog](https://github.com/dxa4481/truffleHog) | 11.9    |
Lucas Charles's avatar
Lucas Charles включено в состав коммита
72
| Apex (Salesforce)                                                           | [pmd](https://pmd.github.io/pmd/index.html)                                            | 12.1                         |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
73
| C/C++                                                                       | [Flawfinder](https://dwheeler.com/flawfinder/)                                         | 10.7                         |
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
74
75
76
77
| Elixir (Phoenix)                                                            | [Sobelow](https://github.com/nccgroup/sobelow)                                         | 11.10                        |
| Go                                                                          | [Gosec](https://github.com/securego/gosec)                                             | 10.7                         |
| Groovy ([Ant](https://ant.apache.org/), [Gradle](https://gradle.org/), [Maven](https://maven.apache.org/) and [SBT](https://www.scala-sbt.org/)) | [SpotBugs](https://spotbugs.github.io/) with the [find-sec-bugs](https://find-sec-bugs.github.io/) plugin | 11.3 (Gradle) & 11.9 (Ant, Maven, SBT) |
| Java ([Ant](https://ant.apache.org/), [Gradle](https://gradle.org/), [Maven](https://maven.apache.org/) and [SBT](https://www.scala-sbt.org/)) | [SpotBugs](https://spotbugs.github.io/) with the [find-sec-bugs](https://find-sec-bugs.github.io/) plugin | 10.6 (Maven), 10.8 (Gradle) & 11.9 (Ant, SBT) |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
78
| JavaScript                                                                  | [ESLint security plugin](https://github.com/nodesecurity/eslint-plugin-security)       | 11.8                         |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
79
| Kubernetes manifests                                                        | [Kubesec](https://github.com/controlplaneio/kubesec)                                   | 12.6                         |
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
80
81
| Node.js                                                                     | [NodeJsScan](https://github.com/ajinabraham/NodeJsScan)                                | 11.1                         |
| PHP                                                                         | [phpcs-security-audit](https://github.com/FloeDesignTechnologies/phpcs-security-audit) | 10.8                         |
Jed Brown's avatar
Jed Brown включено в состав коммита
82
| Python ([pip](https://pip.pypa.io/en/stable/))                              | [bandit](https://github.com/PyCQA/bandit)                                              | 10.3                         |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
83
| React                                                                       | [ESLint react plugin](https://github.com/yannickcr/eslint-plugin-react)                | 12.5                         |
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
84
85
| Ruby on Rails                                                               | [brakeman](https://brakemanscanner.org)                                                | 10.3                         |
| Scala ([Ant](https://ant.apache.org/), [Gradle](https://gradle.org/), [Maven](https://maven.apache.org/) and [SBT](https://www.scala-sbt.org/)) | [SpotBugs](https://spotbugs.github.io/) with the [find-sec-bugs](https://find-sec-bugs.github.io/) plugin | 11.0 (SBT) & 11.9 (Ant, Gradle, Maven) |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
86
| TypeScript                                                                  | [TSLint config security](https://github.com/webschik/tslint-config-security/)          | 11.9                         |
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
87
88
89
90
91
92

NOTE: **Note:**
The Java analyzers can also be used for variants like the
[Gradle wrapper](https://docs.gradle.org/current/userguide/gradle_wrapper.html),
[Grails](https://grails.org/) and the [Maven wrapper](https://github.com/takari/maven-wrapper).

GitLab Bot's avatar
GitLab Bot включено в состав коммита
93
94
95
96
## Contribute your scanner

The [Security Scanner Integration](../../../development/integrations/secure.md) documentation explains how to integrate other security scanners into GitLab.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
97
## Configuration
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
98

GitLab Bot's avatar
GitLab Bot включено в состав коммита
99
100
101
102
103
104
105
106
NOTE: **Note:**
You don't have to configure SAST manually as shown in this section if you're using [Auto SAST](../../../topics/autodevops/index.md#auto-sast-ultimate)
provided by [Auto DevOps](../../../topics/autodevops/index.md).

For GitLab 11.9 and later, to enable SAST you must [include](../../../ci/yaml/README.md#includetemplate)
the [`SAST.gitlab-ci.yml` template](https://gitlab.com/gitlab-org/gitlab/blob/master/lib/gitlab/ci/templates/Security/SAST.gitlab-ci.yml)
that is provided as a part of your GitLab installation. For GitLab versions earlier than 11.9, you
can copy and use the job as defined that template.
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
107

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
108
Add the following to your `.gitlab-ci.yml` file:
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
109
110
111

```yaml
include:
GitLab Bot's avatar
GitLab Bot включено в состав коммита
112
  - template: SAST.gitlab-ci.yml
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
113
114
115
116
117
```

The included template will create a `sast` job in your CI/CD pipeline and scan
your project's source code for possible vulnerabilities.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
118
The results will be saved as a
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
119
120
121
122
123
124
[SAST report artifact](../../../ci/yaml/README.md#artifactsreportssast-ultimate)
that you can later download and analyze. Due to implementation limitations, we
always take the latest SAST artifact available. Behind the scenes, the
[GitLab SAST Docker image](https://gitlab.com/gitlab-org/security-products/sast)
is used to detect the languages/frameworks and in turn runs the matching scan tools.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
125
### Customizing the SAST settings
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
126

GitLab Bot's avatar
GitLab Bot включено в состав коммита
127
128
The SAST settings can be changed through [environment variables](#available-variables)
by using the
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
129
130
131
132
133
134
135
[`variables`](../../../ci/yaml/README.md#variables) parameter in `.gitlab-ci.yml`.

In the following example, we include the SAST template and at the same time we
set the `SAST_GOSEC_LEVEL` variable to `2`:

```yaml
include:
GitLab Bot's avatar
GitLab Bot включено в состав коммита
136
  - template: SAST.gitlab-ci.yml
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
137
138
139
140
141
142
143
144

variables:
  SAST_GOSEC_LEVEL: 2
```

Because the template is [evaluated before](../../../ci/yaml/README.md#include)
the pipeline configuration, the last mention of the variable will take precedence.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
145
146
147
148
149
150
151
152
### Overriding the SAST template

If you want to override the job definition (for example, change properties like
`variables` or `dependencies`), you need to declare a `sast` job after the
template inclusion and specify any additional keys under it. For example:

```yaml
include:
GitLab Bot's avatar
GitLab Bot включено в состав коммита
153
  - template: SAST.gitlab-ci.yml
GitLab Bot's avatar
GitLab Bot включено в состав коммита
154
155
156
157
158
159

sast:
  variables:
    CI_DEBUG_TRACE: "true"
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
160
161
162
163
164
165
166
167
168
### Using environment variables to pass credentials for private repositories

Some analyzers require downloading the project's dependencies in order to
perform the analysis. In turn, such dependencies may live in private Git
repositories and thus require credentials like username and password to download them.
Depending on the analyzer, such credentials can be provided to
it via [custom environment variables](#custom-environment-variables).

#### Using a variable to pass username and password to a private Maven repository
Matt Penna's avatar
Matt Penna включено в состав коммита
169
170
171
172
173
174
175

If you have a private Apache Maven repository that requires login credentials,
you can use the `MAVEN_CLI_OPTS` [environment variable](#available-variables)
to pass a username and password. You can set it under your project's settings
so that your credentials aren't exposed in `.gitlab-ci.yml`.

If the username is `myuser` and the password is `verysecret` then you would
GitLab Bot's avatar
GitLab Bot включено в состав коммита
176
[set the following variable](../../../ci/variables/README.md#via-the-ui)
Matt Penna's avatar
Matt Penna включено в состав коммита
177
178
179
180
181
182
under your project's settings:

| Type | Key | Value |
| ---- | --- | ----- |
| Variable | `MAVEN_CLI_OPTS` | `-Drepository.password=verysecret -Drepository.user=myuser` |

GitLab Bot's avatar
GitLab Bot включено в состав коммита
183
184
185
186
187
188
189
### Disabling Docker in Docker for SAST

You can avoid the need for Docker in Docker by running the individual analyzers.
This does not require running the executor in privileged mode. For example:

```yaml
include:
GitLab Bot's avatar
GitLab Bot включено в состав коммита
190
  - template: SAST.gitlab-ci.yml
GitLab Bot's avatar
GitLab Bot включено в состав коммита
191
192
193
194
195
196
197

variables:
  SAST_DISABLE_DIND: "true"
```

This will create individual `<analyzer-name>-sast` jobs for each analyzer that runs in your CI/CD pipeline.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
198
199
200
201
202
203
204
205
206
207
#### Enabling kubesec analyzer

> [Introduced](https://gitlab.com/gitlab-org/gitlab/issues/12752) in GitLab Ultimate 12.6.

When [Docker in Docker is disabled](#disabling-docker-in-docker-for-sast),
you will need to set `SCAN_KUBERNETES_MANIFESTS` to `"true"` to enable the
kubesec analyzer. In `.gitlab-ci.yml`, define:

```yaml
include:
GitLab Bot's avatar
GitLab Bot включено в состав коммита
208
  - template: SAST.gitlab-ci.yml
GitLab Bot's avatar
GitLab Bot включено в состав коммита
209
210

variables:
GitLab Bot's avatar
GitLab Bot включено в состав коммита
211
  SAST_DISABLE_DIND: "true"
GitLab Bot's avatar
GitLab Bot включено в состав коммита
212
213
214
  SCAN_KUBERNETES_MANIFESTS: "true"
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
215
216
217
218
#### Pre-compilation

If your project requires custom build configurations, it can be preferable to avoid
compilation during your SAST execution and instead pass all job artifacts from an
GitLab Bot's avatar
GitLab Bot включено в состав коммита
219
220
earlier stage within the pipeline. This is the current strategy when requiring
a `before_script` execution to prepare your scan job.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
221
222
223
224
225
226
227
228
229
230
231
232
233
234

To pass your project's dependencies as artifacts, the dependencies must be included
in the project's working directory and specified using the `artifacts:path` configuration.
If all dependencies are present, the `-compile=false` flag can be provided to the
analyzer and compilation will be skipped:

```yaml
image: maven:3.6-jdk-8-alpine

stages:
 - build
 - test

include:
GitLab Bot's avatar
GitLab Bot включено в состав коммита
235
  - template: SAST.gitlab-ci.yml
GitLab Bot's avatar
GitLab Bot включено в состав коммита
236
237
238
239
240
241
242
243
244
245
246
247
248
249

variables:
  SAST_DISABLE_DIND: "true"

build:
  stage: build
  script:
    - mvn package -Dmaven.repo.local=./.m2/repository
  artifacts:
    paths:
      - .m2/
      - target/

spotbugs-sast:
GitLab Bot's avatar
GitLab Bot включено в состав коммита
250
251
  dependencies:
    - build
GitLab Bot's avatar
GitLab Bot включено в состав коммита
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
  script:
    - /analyzer run -compile=false
  variables:
    MAVEN_REPO_PATH: ./.m2/repository
  artifacts:
    reports:
      sast: gl-sast-report.json
```

NOTE: **Note:**
The path to the vendored directory must be specified explicitly to allow
the analyzer to recognize the compiled artifacts. This configuration can vary per
analyzer but in the case of Java above, `MAVEN_REPO_PATH` can be used.
See [Analyzer settings](#analyzer-settings) for the complete list of available options.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
267
268
269
270
### Available variables

SAST can be [configured](#customizing-the-sast-settings) using environment variables.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
271
272
273
274
275
#### Custom Certificate Authority

To trust a custom Certificate Authority, set the `ADDITIONAL_CA_CERT_BUNDLE` variable to the bundle
of CA certs that you want to trust within the SAST environment.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
276
277
278
279
#### Docker images

The following are Docker image-related variables.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
280
281
282
283
| Environment variable         | Description                                                                                                                                                                                                              |
|------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `SAST_ANALYZER_IMAGES`       | Comma separated list of custom images. Default images are still enabled. Read more about [customizing analyzers](analyzers.md). Not available when [Docker in Docker is disabled](#disabling-docker-in-docker-for-sast). |
| `SAST_ANALYZER_IMAGE_PREFIX` | Override the name of the Docker registry providing the default images (proxy). Read more about [customizing analyzers](analyzers.md).                                                                                    |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
284
| `SAST_ANALYZER_IMAGE_TAG`    | Override the Docker tag of the default images. Read more about [customizing analyzers](analyzers.md).                                                                                                                    |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
285
286
287
| `SAST_DEFAULT_ANALYZERS`     | Override the names of default images. Read more about [customizing analyzers](analyzers.md).                                                                                                                             |
| `SAST_DISABLE_DIND`          | Disable Docker in Docker and run analyzers [individually](#disabling-docker-in-docker-for-sast).                                                                                                                         |
| `SAST_PULL_ANALYZER_IMAGES`  | Pull the images from the Docker registry (set to 0 to disable). Read more about [customizing analyzers](analyzers.md). Not available when [Docker in Docker is disabled](#disabling-docker-in-docker-for-sast).          |
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
288

GitLab Bot's avatar
GitLab Bot включено в состав коммита
289
#### Vulnerability filters
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
290
291
292

Some analyzers make it possible to filter out vulnerabilities under a given threshold.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
293
294
295
| Environment variable    | Default value | Description |
|-------------------------|---------------|-------------|
| `SAST_EXCLUDED_PATHS`         | -   | Exclude vulnerabilities from output based on the paths. This is a comma-separated list of patterns. Patterns can be globs, or file or folder paths (for example, `doc,spec` ). Parent directories will also match patterns. |
GitLab Bot's avatar
GitLab Bot включено в состав коммита
296
297
298
299
300
| `SAST_BANDIT_EXCLUDED_PATHS`  | -   | comma-separated list of paths to exclude from scan. Uses Python's [`fnmatch` syntax](https://docs.python.org/2/library/fnmatch.html) |
| `SAST_BRAKEMAN_LEVEL`   |         1 | Ignore Brakeman vulnerabilities under given confidence level. Integer, 1=Low 3=High. |
| `SAST_FLAWFINDER_LEVEL` |         1 | Ignore Flawfinder vulnerabilities under given risk level. Integer, 0=No risk, 5=High risk. |
| `SAST_GITLEAKS_ENTROPY_LEVEL` | 8.0 | Minimum entropy for secret detection. Float, 0.0 = low, 8.0 = high. |
| `SAST_GOSEC_LEVEL`      |         0 | Ignore gosec vulnerabilities under given confidence level. Integer, 0=Undefined, 1=Low, 2=Medium, 3=High. |
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
301

GitLab Bot's avatar
GitLab Bot включено в состав коммита
302
#### Timeouts
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
303
304
305

The following variables configure timeouts.

Lucas Charles's avatar
Lucas Charles включено в состав коммита
306
307
| Environment variable | Default value | Description |
|----------------------|---------------|-------------|
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
308
309
310
311
| `SAST_DOCKER_CLIENT_NEGOTIATION_TIMEOUT` |      2m | Time limit for Docker client negotiation. Timeouts are parsed using Go's [`ParseDuration`](https://golang.org/pkg/time/#ParseDuration). Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". For example, "300ms", "1.5h" or "2h45m". |
| `SAST_PULL_ANALYZER_IMAGE_TIMEOUT`       |      5m | Time limit when pulling the image of an analyzer. Timeouts are parsed using Go's [`ParseDuration`](https://golang.org/pkg/time/#ParseDuration). Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". For example, "300ms", "1.5h" or "2h45m". |
| `SAST_RUN_ANALYZER_TIMEOUT`              |     20m | Time limit when running an analyzer. Timeouts are parsed using Go's [`ParseDuration`](https://golang.org/pkg/time/#ParseDuration). Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". For example, "300ms", "1.5h" or "2h45m".|

GitLab Bot's avatar
GitLab Bot включено в состав коммита
312
313
314
NOTE: **Note:**
Timeout variables are not applicable for setups with [disabled Docker In Docker](index.md#disabling-docker-in-docker-for-sast).

GitLab Bot's avatar
GitLab Bot включено в состав коммита
315
#### Analyzer settings
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
316
317
318

Some analyzers can be customized with environment variables.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
319
320
321
322
323
324
325
326
327
328
329
330
331
332
| Environment variable        | Analyzer | Description |
|-----------------------------|----------|-------------|
| `SCAN_KUBERNETES_MANIFESTS` | kubesec  | Set to `"true"` to scan Kubernetes manifests when [Docker in Docker](#disabling-docker-in-docker-for-sast) is disabled. |
| `ANT_HOME`                  | spotbugs | The `ANT_HOME` environment variable. |
| `ANT_PATH`                  | spotbugs | Path to the `ant` executable. |
| `GRADLE_PATH`               | spotbugs | Path to the `gradle` executable. |
| `JAVA_OPTS`                 | spotbugs | Additional arguments for the `java` executable. |
| `JAVA_PATH`                 | spotbugs | Path to the `java` executable. |
| `SAST_JAVA_VERSION`         | spotbugs | Which Java version to use. Supported versions are `8` and `11`. Defaults to `8`. |
| `MAVEN_CLI_OPTS`            | spotbugs | Additional arguments for the `mvn` or `mvnw` executable. |
| `MAVEN_PATH`                | spotbugs | Path to the `mvn` executable. |
| `MAVEN_REPO_PATH`           | spotbugs | Path to the Maven local repository (shortcut for the `maven.repo.local` property). |
| `SBT_PATH`                  | spotbugs | Path to the `sbt` executable. |
| `FAIL_NEVER`                | spotbugs | Set to `1` to ignore compilation failure. |
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
333

GitLab Bot's avatar
GitLab Bot включено в состав коммита
334
335
#### Custom environment variables

GitLab Bot's avatar
GitLab Bot включено в состав коммита
336
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/18193) in GitLab Ultimate 12.5.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
337
338
339
340
341
342
343
344
345
346

In addition to the aforementioned SAST configuration variables,
all [custom environment variables](../../../ci/variables/README.md#creating-a-custom-environment-variable) are propagated
to the underlying SAST analyzer images if
[the SAST vendored template](#configuration) is used.

CAUTION: **Caution:**
Variables having names starting with these prefixes will **not** be propagated to the SAST Docker container and/or
analyzer containers: `DOCKER_`, `CI`, `GITLAB_`, `FF_`, `HOME`, `PWD`, `OLDPWD`, `PATH`, `SHLVL`, `HOSTNAME`.

Victor Zagorodny's avatar
Victor Zagorodny включено в состав коммита
347
348
349
350
351
## Reports JSON format

CAUTION: **Caution:**
The JSON report artifacts are not a public API of SAST and their format may change in the future.

Victor Zagorodny's avatar
Victor Zagorodny включено в состав коммита
352
The SAST tool emits a JSON report report file. Here is an example of the report structure with all important parts of
Victor Zagorodny's avatar
Victor Zagorodny включено в состав коммита
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
it highlighted:

```json-doc
{
  "version": "2.0",
  "vulnerabilities": [
    {
      "category": "sast",
      "name": "Predictable pseudorandom number generator",
      "message": "Predictable pseudorandom number generator",
      "description": "The use of java.util.Random is predictable",
      "cve": "818bf5dacb291e15d9e6dc3c5ac32178:PREDICTABLE_RANDOM",
      "severity": "Medium",
      "confidence": "Medium",
      "scanner": {
        "id": "find_sec_bugs",
        "name": "Find Security Bugs"
      },
      "location": {
        "file": "groovy/src/main/groovy/com/gitlab/security_products/tests/App.groovy",
        "start_line": 47,
        "end_line": 47,
        "class": "com.gitlab.security_products.tests.App",
        "method": "generateSecretToken2",
        "dependency": {
          "package": {}
        }
      },
      "identifiers": [
        {
          "type": "find_sec_bugs_type",
          "name": "Find Security Bugs-PREDICTABLE_RANDOM",
          "value": "PREDICTABLE_RANDOM",
          "url": "https://find-sec-bugs.github.io/bugs.htm#PREDICTABLE_RANDOM"
        },
        {
          "type": "cwe",
          "name": "CWE-330",
          "value": "330",
          "url": "https://cwe.mitre.org/data/definitions/330.html"
        }
      ]
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
395
    },
Victor Zagorodny's avatar
Victor Zagorodny включено в состав коммита
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
    {
      "category": "sast",
      "message": "Probable insecure usage of temp file/directory.",
      "cve": "python/hardcoded/hardcoded-tmp.py:4ad6d4c40a8c263fc265f3384724014e0a4f8dd6200af83e51ff120420038031:B108",
      "severity": "Medium",
      "confidence": "Medium",
      "scanner": {
        "id": "bandit",
        "name": "Bandit"
      },
      "location": {
        "file": "python/hardcoded/hardcoded-tmp.py",
        "start_line": 10,
        "end_line": 10,
        "dependency": {
          "package": {}
        }
      },
      "identifiers": [
        {
          "type": "bandit_test_id",
          "name": "Bandit Test ID B108",
          "value": "B108",
          "url": "https://docs.openstack.org/bandit/latest/plugins/b108_hardcoded_tmp_directory.html"
        }
      ]
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
422
    },
Victor Zagorodny's avatar
Victor Zagorodny включено в состав коммита
423
424
425
426
427
  ],
  "remediations": []
}
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
428
429
430
CAUTION: **Deprecation:**
Beginning with GitLab 12.9, SAST no longer reports `undefined` severity and confidence levels.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
431
Here is the description of the report file structure nodes and their meaning. All fields are mandatory in
Victor Zagorodny's avatar
Victor Zagorodny включено в состав коммита
432
433
434
435
436
437
438
439
440
441
442
443
444
445
the report JSON unless stated otherwise. Presence of optional fields depends on the underlying analyzers being used.

| Report JSON node                        | Function |
|-----------------------------------------|----------|
| `version`                               | Report syntax version used to generate this JSON. |
| `vulnerabilities`                       | Array of vulnerability objects. |
| `vulnerabilities[].category`            | Where this vulnerability belongs (SAST, Dependency Scanning etc.). For SAST, it will always be `sast`. |
| `vulnerabilities[].name`                | Name of the vulnerability, this must not include the occurrence's specific information. Optional. |
| `vulnerabilities[].message`             | A short text that describes the vulnerability, it may include the occurrence's specific information. Optional. |
| `vulnerabilities[].description`         | A long text that describes the vulnerability. Optional. |
| `vulnerabilities[].cve`                 | A fingerprint string value that represents a concrete occurrence of the vulnerability. Is used to determine whether two vulnerability occurrences are same or different. May not be 100% accurate. **This is NOT a [CVE](https://cve.mitre.org/)**. |
| `vulnerabilities[].severity`            | How much the vulnerability impacts the software. Possible values: `Undefined` (an analyzer has not provided this info), `Info`, `Unknown`, `Low`, `Medium`, `High`, `Critical`. |
| `vulnerabilities[].confidence`          | How reliable the vulnerability's assessment is. Possible values: `Undefined` (an analyzer has not provided this info), `Ignore`, `Unknown`, `Experimental`, `Low`, `Medium`, `High`, `Confirmed`. |
| `vulnerabilities[].solution`            | Explanation of how to fix the vulnerability. Optional. |
Victor Zagorodny's avatar
Victor Zagorodny включено в состав коммита
446
| `vulnerabilities[].scanner`             | A node that describes the analyzer used to find this vulnerability. |
Victor Zagorodny's avatar
Victor Zagorodny включено в состав коммита
447
448
| `vulnerabilities[].scanner.id`          | Id of the scanner as a snake_case string. |
| `vulnerabilities[].scanner.name`        | Name of the scanner, for display purposes. |
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
449
| `vulnerabilities[].location`            | A node that tells where the vulnerability is located. |
Victor Zagorodny's avatar
Victor Zagorodny включено в состав коммита
450
451
452
453
454
455
456
457
458
| `vulnerabilities[].location.file`       | Path to the file where the vulnerability is located. Optional. |
| `vulnerabilities[].location.start_line` | The first line of the code affected by the vulnerability. Optional. |
| `vulnerabilities[].location.end_line`   | The last line of the code affected by the vulnerability. Optional. |
| `vulnerabilities[].location.class`      | If specified, provides the name of the class where the vulnerability is located. Optional. |
| `vulnerabilities[].location.method`     | If specified, provides the name of the method where the vulnerability is located. Optional. |
| `vulnerabilities[].identifiers`         | An ordered array of references that identify a vulnerability on internal or external DBs. |
| `vulnerabilities[].identifiers[].type`  | Type of the identifier. Possible values: common identifier types (among `cve`, `cwe`, `osvdb`, and `usn`) or analyzer-dependent ones (e.g., `bandit_test_id` for [Bandit analyzer](https://wiki.openstack.org/wiki/Security/Projects/Bandit)). |
| `vulnerabilities[].identifiers[].name`  | Name of the identifier for display purposes. |
| `vulnerabilities[].identifiers[].value` | Value of the identifier for matching purposes. |
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
459
| `vulnerabilities[].identifiers[].url`   | URL to identifier's documentation. Optional. |
Victor Zagorodny's avatar
Victor Zagorodny включено в состав коммита
460

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
461
462
## Secret detection

GitLab Bot's avatar
GitLab Bot включено в состав коммита
463
464
465
GitLab is also able to detect secrets and credentials that have been unintentionally pushed to the
repository (for example, an API key that allows write access to third-party deployment
environments).
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
466
467
468
469
470
471
472

This check is performed by a specific analyzer during the `sast` job. It runs regardless of the programming
language of your app, and you don't need to change anything to your
CI/CD configuration file to turn it on. Results are available in the SAST report.

GitLab currently includes [Gitleaks](https://github.com/zricethezav/gitleaks) and [TruffleHog](https://github.com/dxa4481/truffleHog) checks.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
473
474
475
476
477
478
NOTE: **Note:**
The secrets analyzer will ignore "Password in URL" vulnerabilities if the password begins
with a dollar sign (`$`) as this likely indicates the password being used is an environment
variable. For example, `https://username:$password@example.com/path/to/repo` will not be
detected, whereas `https://username:password@example.com/path/to/repo` would be detected.

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
479
480
481
## Security Dashboard

The Security Dashboard is a good place to get an overview of all the security
Mark Florian's avatar
Mark Florian включено в состав коммита
482
vulnerabilities in your groups, projects and pipelines. Read more about the
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
483
484
485
486
487
488
[Security Dashboard](../security_dashboard/index.md).

## Interacting with the vulnerabilities

Once a vulnerability is found, you can interact with it. Read more on how to
[interact with the vulnerabilities](../index.md#interacting-with-the-vulnerabilities).
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
489
490
491
492
493

## Vulnerabilities database update

For more information about the vulnerabilities database update, check the
[maintenance table](../index.md#maintenance-and-update-of-the-vulnerabilities-database).
Evan Read's avatar
Evan Read включено в состав коммита
494

GitLab Bot's avatar
GitLab Bot включено в состав коммита
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
## GitLab SAST in an offline air-gapped installation

For self-managed GitLab instances in an environment with limited, restricted, or intermittent access
to external resources via the internet, some adjustments are required for the SAST job to
successfully run.

### Requirements for offline SAST

To use SAST in an offline environment, you need:

- GitLab Runner with the [`docker` or `kubernetes` executor](#requirements).
- Docker Container Registry with locally available copies of SAST [analyzer](https://gitlab.com/gitlab-org/security-products/analyzers) images.

NOTE: **Note:**
GitLab Runner has a [default `pull policy` of `always`](https://docs.gitlab.com/runner/executors/docker.html#using-the-always-pull-policy),
meaning the runner may try to pull remote images even if a local copy is available. Set GitLab
Runner's [`pull_policy` to `if-not-present`](https://docs.gitlab.com/runner/executors/docker.html#using-the-if-not-present-pull-policy)
in an offline environment if you prefer using only locally available Docker images.

### Make GitLab SAST analyzer images available inside your Docker registry

For SAST with all [supported languages and frameworks](#supported-languages-and-frameworks),
import the following default SAST analyzer images from `registry.gitlab.com` to your local "offline"
registry:

```
registry.gitlab.com/gitlab-org/security-products/analyzers/bandit:2
registry.gitlab.com/gitlab-org/security-products/analyzers/brakeman:2
registry.gitlab.com/gitlab-org/security-products/analyzers/eslint:2
registry.gitlab.com/gitlab-org/security-products/analyzers/flawfinder:2
registry.gitlab.com/gitlab-org/security-products/analyzers/go-ast-scanner:2
registry.gitlab.com/gitlab-org/security-products/analyzers/gosec:2
registry.gitlab.com/gitlab-org/security-products/analyzers/kubesec:2
registry.gitlab.com/gitlab-org/security-products/analyzers/nodejs-scan:2
registry.gitlab.com/gitlab-org/security-products/analyzers/phpcs-security-audit:2
registry.gitlab.com/gitlab-org/security-products/analyzers/pmd-apex:2
registry.gitlab.com/gitlab-org/security-products/analyzers/secrets:2
registry.gitlab.com/gitlab-org/security-products/analyzers/security-code-scan:2
registry.gitlab.com/gitlab-org/security-products/analyzers/sobelow:2
registry.gitlab.com/gitlab-org/security-products/analyzers/spotbugs:2
registry.gitlab.com/gitlab-org/security-products/analyzers/tslint:2
```

The process for importing Docker images into a local offline Docker registry depends on
**your network security policy**. Please consult your IT staff to find an accepted and approved
process by which external resources can be imported or temporarily accessed. Note that these scanners are [updated periodically](../index.md#maintenance-and-update-of-the-vulnerabilities-database)
with new definitions, so consider if you are able to make periodic updates yourself.

For details on saving and transporting Docker images as a file, see Docker's documentation on
[`docker save`](https://docs.docker.com/engine/reference/commandline/save/), [`docker load`](https://docs.docker.com/engine/reference/commandline/load/),
[`docker export`](https://docs.docker.com/engine/reference/commandline/export/), and [`docker import`](https://docs.docker.com/engine/reference/commandline/import/).

### Set SAST CI job variables to use local SAST analyzers

[Override SAST environment variables](#customizing-the-sast-settings) to use to your [local container registry](./analyzers.md#using-a-custom-docker-mirror)
as the source for SAST analyzer images.

For example, assuming a local Docker registry repository of `localhost:5000/analyzers`:

  ```yaml
include:
  - template: SAST.gitlab-ci.yml

variables:
  SAST_ANALYZER_IMAGE_PREFIX: "localhost:5000/analyzers"
  SAST_DISABLE_DIND: "true"
  ```

The SAST job should now use local copies of the SAST analyzers to scan your code and generate
security reports without requiring internet access.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
566
## Troubleshooting
Evan Read's avatar
Evan Read включено в состав коммита
567

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
568
### Error response from daemon: error processing tar file: docker-tar: relocation error
Evan Read's avatar
Evan Read включено в состав коммита
569

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
570
571
572
This error occurs when the Docker version used to run the SAST job is `19.03.00`.
You are advised to update to Docker `19.03.01` or greater. Older versions are not
affected. Read more in
GitLab Bot's avatar
GitLab Bot включено в состав коммита
573
[this issue](https://gitlab.com/gitlab-org/gitlab/issues/13830#note_211354992 "Current SAST container fails").