Customizing analyzer settings

Authentication

Authentication is handled by providing the authentication token as a header or cookie. You can provide a script that performs an authentication flow or calculates the token.

HTTP Basic Authentication

HTTP basic authentication is an authentication method built into the HTTP protocol and used in conjunction with transport layer security (TLS).

We recommended that you create a CI/CD variable for the password (for example, TEST_API_PASSWORD), and set it to be masked. You can create CI/CD variables from the GitLab project’s page at Settings > CI/CD, in the Variables section. Because of the limitations on masked variables, you should Base64-encode the password before adding it as a variable.

Finally, add two CI/CD variables to your .gitlab-ci.yml file:

  • APISEC_HTTP_USERNAME: The username for authentication.
  • APISEC_HTTP_PASSWORD_BASE64: The Base64-encoded password for authentication.
stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_PROFILE: Quick
  APISEC_HAR: test-api-recording.har
  APISEC_TARGET_URL: http://test-deployment/
  APISEC_HTTP_USERNAME: testuser
  APISEC_HTTP_PASSWORD_BASE64: $TEST_API_PASSWORD

Raw password

If you do not want to Base64-encode the password (or if you are using GitLab 15.3 or earlier) you can provide the raw password APISEC_HTTP_PASSWORD, instead of using APISEC_HTTP_PASSWORD_BASE64.

Bearer tokens

Bearer tokens are used by several different authentication mechanisms, including OAuth2 and JSON Web Tokens (JWT). Bearer tokens are transmitted using the Authorization HTTP header. To use Bearer tokens with API security testing, you need one of the following:

  • A token that doesn’t expire.
  • A way to generate a token that lasts the length of testing.
  • A Python script that API security testing can call to generate the token.

Token doesn’t expire

If the Bearer token doesn’t expire, use the APISEC_OVERRIDES_ENV variable to provide it. This variable’s content is a JSON snippet that provides headers and cookies to add to outgoing HTTP requests for API security testing.

Follow these steps to provide the Bearer token with APISEC_OVERRIDES_ENV:

  1. Create a CI/CD variable, for example TEST_API_BEARERAUTH, with the value {"headers":{"Authorization":"Bearer dXNlcm5hbWU6cGFzc3dvcmQ="}} (substitute your token). You can create CI/CD variables from the GitLab projects page at Settings > CI/CD, in the Variables section. Due to the format of TEST_API_BEARERAUTH it’s not possible to mask the variable. To mask the token’s value, you can create a second variable with the token values, and define TEST_API_BEARERAUTH with the value {"headers":{"Authorization":"Bearer $MASKED_VARIABLE"}}.

  2. In your .gitlab-ci.yml file, set APISEC_OVERRIDES_ENV to the variable you just created:

    stages:
      - dast
    
    include:
      - template: API-Security.gitlab-ci.yml
    
    variables:
      APISEC_PROFILE: Quick
      APISEC_OPENAPI: test-api-specification.json
      APISEC_TARGET_URL: http://test-deployment/
      APISEC_OVERRIDES_ENV: $TEST_API_BEARERAUTH
    
  3. To validate that authentication is working, run API security testing and review the job logs and the test API’s application logs.

Token generated at test runtime

If the Bearer token must be generated and doesn’t expire during testing, you can provide API security testing with a file that has the token. A prior stage and job, or part of the API security testing job, can generate this file.

API security testing expects to receive a JSON file with the following structure:

{
  "headers" : {
    "Authorization" : "Bearer dXNlcm5hbWU6cGFzc3dvcmQ="
  }
}

This file can be generated by a prior stage and provided to API security testing through the APISEC_OVERRIDES_FILE CI/CD variable.

Set APISEC_OVERRIDES_FILE in your .gitlab-ci.yml file:

stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_PROFILE: Quick
  APISEC_OPENAPI: test-api-specification.json
  APISEC_TARGET_URL: http://test-deployment/
  APISEC_OVERRIDES_FILE: dast-api-overrides.json

To validate that authentication is working, run API security testing and review the job logs and the test API’s application logs.

Token has short expiration

If the Bearer token must be generated and expires prior to the scan’s completion, you can provide a program or script for the API security testing scanner to execute on a provided interval. The provided script runs in an Alpine Linux container that has Python 3 and Bash installed. If the Python script requires additional packages, it must detect this and install the packages at runtime.

The script must create a JSON file containing the Bearer token in a specific format:

{
  "headers" : {
    "Authorization" : "Bearer dXNlcm5hbWU6cGFzc3dvcmQ="
  }
}

You must provide three CI/CD variables, each set for correct operation:

  • APISEC_OVERRIDES_FILE: JSON file the provided command generates.
  • APISEC_OVERRIDES_CMD: Command that generates the JSON file.
  • APISEC_OVERRIDES_INTERVAL: Interval (in seconds) to run command.

For example:

stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_PROFILE: Quick
  APISEC_OPENAPI: test-api-specification.json
  APISEC_TARGET_URL: http://test-deployment/
  APISEC_OVERRIDES_FILE: dast-api-overrides.json
  APISEC_OVERRIDES_CMD: renew_token.py
  APISEC_OVERRIDES_INTERVAL: 300

To validate that authentication is working, run API security testing and review the job logs and the test API’s application logs. See the overrides section for more information about override commands.

Overrides

API security testing provides a method to add or override specific items in your request, for example:

  • Headers
  • Cookies
  • Query string
  • Form data
  • JSON nodes
  • XML nodes

You can use this to inject semantic version headers, authentication, and so on. The authentication section includes examples of using overrides for that purpose.

Overrides use a JSON document, where each type of override is represented by a JSON object:

{
  "headers": {
    "header1": "value",
    "header2": "value"
  },
  "cookies": {
    "cookie1": "value",
    "cookie2": "value"
  },
  "query":      {
    "query-string1": "value",
    "query-string2": "value"
  },
  "body-form":  {
    "form-param1": "value",
    "form-param2": "value"
  },
  "body-json":  {
    "json-path1": "value",
    "json-path2": "value"
  },
  "body-xml" :  {
    "xpath1":    "value",
    "xpath2":    "value"
  }
}

Example of setting a single header:

{
  "headers": {
    "Authorization": "Bearer dXNlcm5hbWU6cGFzc3dvcmQ="
  }
}

Example of setting both a header and cookie:

{
  "headers": {
    "Authorization": "Bearer dXNlcm5hbWU6cGFzc3dvcmQ="
  },
  "cookies": {
    "flags": "677"
  }
}

Example usage for setting a body-form override:

{
  "body-form":  {
    "username": "john.doe"
  }
}

The override engine uses body-form when the request body has only form-data content.

Example usage for setting a body-json override:

{
  "body-json":  {
    "$.credentials.access-token": "iddqd!42.$"
  }
}

Each JSON property name in the object body-json is set to a JSON Path expression. The JSON Path expression $.credentials.access-token identifies the node to be overridden with the value iddqd!42.$. The override engine uses body-json when the request body has only JSON content.

For example, if the body is set to the following JSON:

{
    "credentials" : {
        "username" :"john.doe",
        "access-token" : "non-valid-password"
    }
}

It is changed to:

{
    "credentials" : {
        "username" :"john.doe",
        "access-token" : "iddqd!42.$"
    }
}

Here’s an example for setting a body-xml override. The first entry overrides an XML attribute and the second entry overrides an XML element:

{
  "body-xml" :  {
    "/credentials/@isEnabled": "true",
    "/credentials/access-token/text()" : "iddqd!42.$"
  }
}

Each JSON property name in the object body-xml is set to an XPath v2 expression. The XPath expression /credentials/@isEnabled identifies the attribute node to override with the value true. The XPath expression /credentials/access-token/text() identifies the element node to override with the value iddqd!42.$. The override engine uses body-xml when the request body has only XML content.

For example, if the body is set to the following XML:

<credentials isEnabled="false">
  <username>john.doe</username>
  <access-token>non-valid-password</access-token>
</credentials>

It is changed to:

<credentials isEnabled="true">
  <username>john.doe</username>
  <access-token>iddqd!42.$</access-token>
</credentials>

You can provide this JSON document as a file or environment variable. You may also provide a command to generate the JSON document. The command can run at intervals to support values that expire.

Using a file

To provide the overrides JSON as a file, the APISEC_OVERRIDES_FILE CI/CD variable is set. The path is relative to the job current working directory.

Here’s an example .gitlab-ci.yml:

stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_PROFILE: Quick
  APISEC_OPENAPI: test-api-specification.json
  APISEC_TARGET_URL: http://test-deployment/
  APISEC_OVERRIDES_FILE: dast-api-overrides.json

Using a CI/CD variable

To provide the overrides JSON as a CI/CD variable, use the APISEC_OVERRIDES_ENV variable. This allows you to place the JSON as variables that can be masked and protected.

In this example .gitlab-ci.yml, the APISEC_OVERRIDES_ENV variable is set directly to the JSON:

stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_PROFILE: Quick
  APISEC_OPENAPI: test-api-specification.json
  APISEC_TARGET_URL: http://test-deployment/
  APISEC_OVERRIDES_ENV: '{"headers":{"X-API-Version":"2"}}'

In this example .gitlab-ci.yml, the SECRET_OVERRIDES variable provides the JSON. This is a group or instance CI/CD variable defined in the UI:

stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_PROFILE: Quick
  APISEC_OPENAPI: test-api-specification.json
  APISEC_TARGET_URL: http://test-deployment/
  APISEC_OVERRIDES_ENV: $SECRET_OVERRIDES

Using a command

If the value must be generated or regenerated on expiration, you can provide a program or script for the API security testing scanner to execute on a specified interval. The provided command runs in an Alpine Linux container that has Python 3 and Bash installed.

You have to set the environment variable APISEC_OVERRIDES_CMD to the program or script you would like to execute. The provided command creates the overrides JSON file as defined previously.

You might want to install other scripting runtimes like NodeJS or Ruby, or maybe you need to install a dependency for your overrides command. In this case, you should set the APISEC_PRE_SCRIPT to the file path of a script which provides those prerequisites. The script provided by APISEC_PRE_SCRIPT is executed once before the analyzer starts.

note
When performing actions that require elevated permissions, make use of the sudo command. For example, sudo apk add nodejs.

See the Alpine Linux package management page for information about installing Alpine Linux packages.

You must provide three CI/CD variables, each set for correct operation:

  • APISEC_OVERRIDES_FILE: File generated by the provided command.
  • APISEC_OVERRIDES_CMD: Overrides command in charge of generating the overrides JSON file periodically.
  • APISEC_OVERRIDES_INTERVAL: Interval in seconds to run command.

Optionally:

  • APISEC_PRE_SCRIPT: Script to install runtimes or dependencies before the scan starts.
caution
To execute scripts in Alpine Linux you must first use the command chmod to set the execution permission. For example, to set the execution permission of script.py for everyone, use the command: sudo chmod a+x script.py. If needed, you can version your script.py with the execution permission already set.
stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_PROFILE: Quick
  APISEC_OPENAPI: test-api-specification.json
  APISEC_TARGET_URL: http://test-deployment/
  APISEC_OVERRIDES_FILE: dast-api-overrides.json
  APISEC_OVERRIDES_CMD: renew_token.py
  APISEC_OVERRIDES_INTERVAL: 300

Debugging overrides

By default the output of the overrides command is hidden. If the overrides command returns a non zero exit code, the command is displayed as part of your job output. Optionally, you can set the variable APISEC_OVERRIDES_CMD_VERBOSE to any value to display overrides command output as it is generated. This is useful when testing your overrides script, but should be disabled afterwards as it slows down testing.

It is also possible to write messages from your script to a log file that is collected when the job completes or fails. The log file must be created in a specific location and following a naming convention.

Adding some basic logging to your overrides script is useful in case the script fails unexpectedly during standard running of the job. The log file is automatically included as an artifact of the job, allowing you to download it after the job has finished.

Following our example, we provided renew_token.py in the environment variable APISEC_OVERRIDES_CMD. Notice two things in the script:

  • Log file is saved in the location indicated by the environmental variable CI_PROJECT_DIR.
  • Log filename should match gl-*.log.
#!/usr/bin/env python

# Example of an overrides command

# Override commands can update the overrides json file
# with new values to be used.  This is a great way to
# update an authentication token that will expire
# during testing.

import logging
import json
import os
import requests
import backoff

# [1] Store log file in directory indicated by env var CI_PROJECT_DIR
working_directory = os.environ.get( 'CI_PROJECT_DIR')
overrides_file_name = os.environ.get('APISEC_OVERRIDES_FILE', 'dast-api-overrides.json')
overrides_file_path = os.path.join(working_directory, overrides_file_name)

# [2] File name should match the pattern: gl-*.log
log_file_path = os.path.join(working_directory, 'gl-user-overrides.log')

# Set up logger
logging.basicConfig(filename=log_file_path, level=logging.DEBUG)

# Use `backoff` decorator to retry in case of transient errors.
@backoff.on_exception(backoff.expo,
                      (requests.exceptions.Timeout,
                       requests.exceptions.ConnectionError),
                       max_time=30)
def get_auth_response():
    authorization_url = 'https://authorization.service/api/get_api_token'
    return requests.get(
        f'{authorization_url}',
        auth=(os.environ.get('AUTH_USER'), os.environ.get('AUTH_PWD'))
    )

# In our example, access token is retrieved from a given endpoint
try:

    # Performs a http request, response sample:
    # { "Token" : "abcdefghijklmn" }
    response = get_auth_response()

    # Check that the request is successful. may raise `requests.exceptions.HTTPError`
    response.raise_for_status()

    # Gets JSON data
    response_body = response.json()

# If needed specific exceptions can be caught
# requests.ConnectionError                  : A network connection error problem occurred
# requests.HTTPError                        : HTTP request returned an unsuccessful status code. [Response.raise_for_status()]
# requests.ConnectTimeout                   : The request timed out while trying to connect to the remote server
# requests.ReadTimeout                      : The server did not send any data in the allotted amount of time.
# requests.TooManyRedirects                 : The request exceeds the configured number of maximum redirections
# requests.exceptions.RequestException      : All exceptions that related to Requests
except json.JSONDecodeError as json_decode_error:
    # logs errors related decoding JSON response
    logging.error(f'Error, failed while decoding JSON response. Error message: {json_decode_error}')
    raise
except requests.exceptions.RequestException as requests_error:
    # logs  exceptions  related to `Requests`
    logging.error(f'Error, failed while performing HTTP request. Error message: {requests_error}')
    raise
except Exception as e:
    # logs any other error
    logging.error(f'Error, unknown error while retrieving access token. Error message: {e}')
    raise

# computes object that holds overrides file content.
# It uses data fetched from request
overrides_data = {
    "headers": {
        "Authorization": f"Token {response_body['Token']}"
    }
}

# log entry informing about the file override computation
logging.info("Creating overrides file: %s" % overrides_file_path)

# attempts to overwrite the file
try:
    if os.path.exists(overrides_file_path):
        os.unlink(overrides_file_path)

    # overwrites the file with our updated dictionary
    with open(overrides_file_path, "wb+") as fd:
        fd.write(json.dumps(overrides_data).encode('utf-8'))
except Exception as e:
    # logs any other error
    logging.error(f'Error, unknown error when overwriting file {overrides_file_path}. Error message: {e}')
    raise

# logs informing override has finished successfully
logging.info("Override file has been updated")

# end

In the overrides command example, the Python script depends on the backoff library. To make sure the library is installed before executing the Python script, the APISEC_PRE_SCRIPT is set to a script that installs the dependencies of your overrides command. As for example, the following script user-pre-scan-set-up.sh

#!/bin/bash

# user-pre-scan-set-up.sh
# Ensures python dependencies are installed

echo "**** install python dependencies ****"

sudo pip3 install --no-cache --upgrade --break-system-packages \
    backoff

echo "**** python dependencies installed ****"

# end

You have to update your configuration to set the APISEC_PRE_SCRIPT to our new user-pre-scan-set-up.sh script. For example:

stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_PROFILE: Quick
  APISEC_OPENAPI: test-api-specification.json
  APISEC_TARGET_URL: http://test-deployment/
  APISEC_PRE_SCRIPT: ./user-pre-scan-set-up.sh
  APISEC_OVERRIDES_FILE: dast-api-overrides.json
  APISEC_OVERRIDES_CMD: renew_token.py
  APISEC_OVERRIDES_INTERVAL: 300

In the previous sample, you could use the script user-pre-scan-set-up.sh to also install new runtimes or applications that later on you could use in our overrides command.

Request Headers

The request headers feature lets you specify fixed values for the headers during the scan session. For example, you can use the configuration variable APISEC_REQUEST_HEADERS to set a fixed value in the Cache-Control header. If the headers you need to set include sensitive values like the Authorization header, use the masked variable feature along with the variable APISEC_REQUEST_HEADERS_BASE64.

If the Authorization header or any other header needs to get updated while the scan is in progress, consider using the overrides feature.

The variable APISEC_REQUEST_HEADERS lets you specify a comma-separated (,) list of headers. These headers are included on each request that the scanner performs. Each header entry in the list consists of a name followed by a colon (:) and then by its value. Whitespace before the key or value is ignored. For example, to declare a header name Cache-Control with the value max-age=604800, the header entry is Cache-Control: max-age=604800. To use two headers, Cache-Control: max-age=604800 and Age: 100, set APISEC_REQUEST_HEADERS variable to Cache-Control: max-age=604800, Age: 100.

The order in which the different headers are provided into the variable APISEC_REQUEST_HEADERS does not affect the result. Setting APISEC_REQUEST_HEADERS to Cache-Control: max-age=604800, Age: 100 produces the same result as setting it to Age: 100, Cache-Control: max-age=604800.

Base64

The APISEC_REQUEST_HEADERS_BASE64 variable accepts the same list of headers as APISEC_REQUEST_HEADERS, with the only difference that the entire value of the variable must be Base64-encoded. For example, to set APISEC_REQUEST_HEADERS_BASE64 variable to Authorization: QmVhcmVyIFRPS0VO, Cache-control: bm8tY2FjaGU=, ensure you convert the list to its Base64 equivalent: QXV0aG9yaXphdGlvbjogUW1WaGNtVnlJRlJQUzBWTywgQ2FjaGUtY29udHJvbDogYm04dFkyRmphR1U9, and the Base64-encoded value must be used. This is useful when storing secret header values in a masked variable, which has character set restrictions.

caution
Base64 is used to support the masked variable feature. Base64 encoding is not by itself a security measure, because sensitive values can be easily decoded.

Example: Adding a list of headers on each request using plain text

In the following example of a .gitlab-ci.yml, APISEC_REQUEST_HEADERS configuration variable is set to provide two header values as explained in request headers.

stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_PROFILE: Quick
  APISEC_OPENAPI: test-api-specification.json
  APISEC_TARGET_URL: http://test-deployment/
  APISEC_REQUEST_HEADERS: 'Cache-control: no-cache, Save-Data: on'

Example: Using a masked CI/CD variable

The following .gitlab-ci.yml sample assumes the masked variable SECRET_REQUEST_HEADERS_BASE64 is defined as a group or instance CI/CD variable defined in the UI. The value of SECRET_REQUEST_HEADERS_BASE64 is set to WC1BQ01FLVNlY3JldDogc31jcnt0ISwgWC1BQ01FLVRva2VuOiA3MDVkMTZmNWUzZmI=, which is the Base64-encoded text version of X-ACME-Secret: s3cr3t!, X-ACME-Token: 705d16f5e3fb. Then, it can be used as follows:

stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_PROFILE: Quick
  APISEC_OPENAPI: test-api-specification.json
  APISEC_TARGET_URL: http://test-deployment/
  APISEC_REQUEST_HEADERS_BASE64: $SECRET_REQUEST_HEADERS_BASE64

Consider using APISEC_REQUEST_HEADERS_BASE64 when storing secret header values in a masked variable, which has character set restrictions.

Exclude Paths

When testing an API it can be useful to exclude certain paths. For example, you might exclude testing of an authentication service or an older version of the API. To exclude paths, use the APISEC_EXCLUDE_PATHS CI/CD variable . This variable is specified in your .gitlab-ci.yml file. To exclude multiple paths, separate entries using the ; character. In the provided paths you can use a single character wildcard ? and * for a multiple character wildcard.

To verify the paths are excluded, review the Tested Operations and Excluded Operations portion of the job output. You should not see any excluded paths listed under Tested Operations.

2021-05-27 21:51:08 [INF] API SECURITY: --[ Tested Operations ]-------------------------
2021-05-27 21:51:08 [INF] API SECURITY: 201 POST http://target:7777/api/users CREATED
2021-05-27 21:51:08 [INF] API SECURITY: ------------------------------------------------
2021-05-27 21:51:08 [INF] API SECURITY: --[ Excluded Operations ]-----------------------
2021-05-27 21:51:08 [INF] API SECURITY: GET http://target:7777/api/messages
2021-05-27 21:51:08 [INF] API SECURITY: POST http://target:7777/api/messages
2021-05-27 21:51:08 [INF] API SECURITY: ------------------------------------------------

Examples

This example excludes the /auth resource. This does not exclude child resources (/auth/child).

variables:
  APISEC_EXCLUDE_PATHS: /auth

To exclude /auth, and child resources (/auth/child), we use a wildcard.

variables:
  APISEC_EXCLUDE_PATHS: /auth*

To exclude multiple paths we use the ; character. In this example we exclude /auth* and /v1/*.

variables:
  APISEC_EXCLUDE_PATHS: /auth*;/v1/*

To exclude one or more nested levels within a path we use **. In this example we are testing API endpoints. We are testing /api/v1/ and /api/v2/ of a data query requesting mass, brightness and coordinates data for planet, moon, star, and satellite objects. Example paths that could be scanned include, but are not limited to:

  • /api/v2/planet/coordinates
  • /api/v1/star/mass
  • /api/v2/satellite/brightness

In this example we test the brightness endpoint only:

variables:
  APISEC_EXCLUDE_PATHS: /api/**/mass;/api/**/coordinates

Exclude parameters

While testing an API you may might want to exclude a parameter (query string, header, or body element) from testing. This may be needed because a parameter always causes a failure, slows down testing, or for other reasons. To exclude parameters, you can set one of the following variables: APISEC_EXCLUDE_PARAMETER_ENV or APISEC_EXCLUDE_PARAMETER_FILE.

The APISEC_EXCLUDE_PARAMETER_ENV allows providing a JSON string containing excluded parameters. This is a good option if the JSON is short and does not change often. Another option is the variable APISEC_EXCLUDE_PARAMETER_FILE. This variable is set to a file path that can be checked into the repository, created by another job as an artifact, or generated at runtime with a pre-script using APISEC_PRE_SCRIPT.

Exclude parameters using a JSON document

The JSON document contains a JSON object, this object uses specific properties to identify which parameter should be excluded. You can provide the following properties to exclude specific parameters during the scanning process:

  • headers: Use this property to exclude specific headers. The property’s value is an array of header names to be excluded. Names are case-insensitive.
  • cookies: Use this property’s value to exclude specific cookies. The property’s value is an array of cookie names to be excluded. Names are case-sensitive.
  • query: Use this property to exclude specific fields from the query string. The property’s value is an array of field names from the query string to be excluded. Names are case-sensitive.
  • body-form: Use this property to exclude specific fields from a request that uses the media type application/x-www-form-urlencoded. The property’s value is an array of the field names from the body to be excluded. Names are case-sensitive.
  • body-json: Use this property to exclude specific JSON nodes from a request that uses the media type application/json. The property’s value is an array, each entry of the array is a JSON Path expression.
  • body-xml: Use this property to exclude specific XML nodes from a request that uses media type application/xml. The property’s value is an array, each entry of the array is a XPath v2 expression.

Thus, the following JSON document is an example of the expected structure to exclude parameters.

{
  "headers": [
    "header1",
    "header2"
  ],
  "cookies": [
    "cookie1",
    "cookie2"
  ],
  "query": [
    "query-string1",
    "query-string2"
  ],
  "body-form": [
    "form-param1",
    "form-param2"
  ],
  "body-json": [
    "json-path-expression-1",
    "json-path-expression-2"
  ],
  "body-xml" : [
    "xpath-expression-1",
    "xpath-expression-2"
  ]
}

Examples

Excluding a single header

To exclude the header Upgrade-Insecure-Requests, set the header property’s value to an array with the header name: [ "Upgrade-Insecure-Requests" ]. For instance, the JSON document looks like this:

{
  "headers": [ "Upgrade-Insecure-Requests" ]
}

Header names are case-insensitive, so the header name UPGRADE-INSECURE-REQUESTS is equivalent to Upgrade-Insecure-Requests.

Excluding both a header and two cookies

To exclude the header Authorization, and the cookies PHPSESSID and csrftoken, set the headers property’s value to an array with header name [ "Authorization" ] and the cookies property’s value to an array with the cookies’ names [ "PHPSESSID", "csrftoken" ]. For instance, the JSON document looks like this:

{
  "headers": [ "Authorization" ],
  "cookies": [ "PHPSESSID", "csrftoken" ]
}
Excluding a body-form parameter

To exclude the password field in a request that uses application/x-www-form-urlencoded, set the body-form property’s value to an array with the field name [ "password" ]. For instance, the JSON document looks like this:

{
  "body-form":  [ "password" ]
}

The exclude parameters uses body-form when the request uses a content type application/x-www-form-urlencoded.

Excluding a specific JSON nodes using JSON Path

To exclude the schema property in the root object, set the body-json property’s value to an array with the JSON Path expression [ "$.schema" ].

The JSON Path expression uses special syntax to identify JSON nodes: $ refers to the root of the JSON document, . refers to the current object (in our case the root object), and the text schema refers to a property name. Thus, the JSON path expression $.schema refers to a property schema in the root object. For instance, the JSON document looks like this:

{
  "body-json": [ "$.schema" ]
}

The exclude parameters uses body-json when the request uses a content type application/json. Each entry in body-json is expected to be a JSON Path expression. In JSON Path characters like $, *, . among others have special meaning.

Excluding multiple JSON nodes using JSON Path

To exclude the property password on each entry of an array of users at the root level, set the body-json property’s value to an array with the JSON Path expression [ "$.users[*].password" ].

The JSON Path expression starts with $ to refer to the root node and uses . to refer to the current node. Then, it uses users to refer to a property and the characters [ and ] to enclose the index in the array you want to use, instead of providing a number as an index you use * to specify any index. After the index reference, we find . which now refers to any given selected index in the array, preceded by a property name password.

For instance, the JSON document looks like this:

{
  "body-json": [ "$.users[*].password" ]
}

The exclude parameters uses body-json when the request uses a content type application/json. Each entry in body-json is expected to be a JSON Path expression. In JSON Path characters like $, *, . among others have special meaning.

Excluding a XML attribute

To exclude an attribute named isEnabled located in the root element credentials, set the body-xml property’s value to an array with the XPath expression [ "/credentials/@isEnabled" ].

The XPath expression /credentials/@isEnabled, starts with / to indicate the root of the XML document, then it is followed by the word credentials which indicates the name of the element to match. It uses a / to refer to a node of the previous XML element, and the character @ to indicate that the name isEnable is an attribute.

For instance, the JSON document looks like this:

{
  "body-xml": [
    "/credentials/@isEnabled"
  ]
}

The exclude parameters uses body-xml when the request uses a content type application/xml. Each entry in body-xml is expected to be a XPath v2 expression. In XPath expressions characters like @, /, :, [, ] among others have special meanings.

Excluding a XML text’s element

To exclude the text of the username element contained in root node credentials, set the body-xml property’s value to an array with the XPath expression [/credentials/username/text()" ].

In the XPath expression /credentials/username/text(), the first character / refers to the root XML node, and then after it indicates an XML element’s name credentials. Similarly, the character / refers to the current element, followed by a new XML element’s name username. Last part has a / that refers to the current element, and uses a XPath function called text() which identifies the text of the current element.

For instance, the JSON document looks like this:

{
  "body-xml": [
    "/credentials/username/text()"
  ]
}

The exclude parameters uses body-xml when the request uses a content type application/xml. Each entry in body-xml is expected to be a XPath v2 expression. In XPath expressions characters like @, /, :, [, ] among others have special meanings.

Excluding an XML element

To exclude the element username contained in root node credentials, set the body-xml property’s value to an array with the XPath expression [/credentials/username" ].

In the XPath expression /credentials/username, the first character / refers to the root XML node, and then after it indicates an XML element’s name credentials. Similarly, the character / refers to the current element, followed by a new XML element’s name username.

For instance, the JSON document looks like this:

{
  "body-xml": [
    "/credentials/username"
  ]
}

The exclude parameters uses body-xml when the request uses a content type application/xml. Each entry in body-xml is expected to be a XPath v2 expression. In XPath expressions characters like @, /, :, [, ] among others have special meanings.

Excluding an XML node with namespaces

To exclude anXML element login which is defined in namespace s, and contained in credentials root node, set the body-xml property’s value to an array with the XPath expression [ "/credentials/s:login" ].

In the XPath expression /credentials/s:login, the first character / refers to the root XML node, and then after it indicates an XML element’s name credentials. Similarly, the character / refers to the current element, followed by a new XML element’s name s:login. Notice that name contains the character :, this character separates the namespace from the node name.

The namespace name should have been defined in the XML document which is part of the body request. You may check the namespace in the specification document HAR, OpenAPI, or Postman Collection file.

{
  "body-xml": [
    "/credentials/s:login"
  ]
}

The exclude parameters uses body-xml when the request uses a content type application/xml. Each entry in body-xml is expected to be an XPath v2 expression. In XPath, expressions characters like @, /, :, [, ] among others have special meanings.

Using a JSON string

To provide the exclusion JSON document set the variable APISEC_EXCLUDE_PARAMETER_ENV with the JSON string. In the following example, the .gitlab-ci.yml, the APISEC_EXCLUDE_PARAMETER_ENV variable is set to a JSON string:

stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_PROFILE: Quick
  APISEC_OPENAPI: test-api-specification.json
  APISEC_TARGET_URL: http://test-deployment/
  APISEC_EXCLUDE_PARAMETER_ENV: '{ "headers": [ "Upgrade-Insecure-Requests" ] }'

Using a file

To provide the exclusion JSON document set the variable APISEC_EXCLUDE_PARAMETER_FILE with the JSON file path. The file path is relative to the job current working directory. In the following example .gitlab-ci.yml content, the APISEC_EXCLUDE_PARAMETER_FILE variable is set to a JSON file path:

stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_PROFILE: Quick
  APISEC_OPENAPI: test-api-specification.json
  APISEC_TARGET_URL: http://test-deployment/
  APISEC_EXCLUDE_PARAMETER_FILE: dast-api-exclude-parameters.json

The dast-api-exclude-parameters.json is a JSON document that follows the structure of exclude parameters document.

Exclude URLs

As an alternative to excluding by paths, you can filter by any other component in the URL by using the APISEC_EXCLUDE_URLS CI/CD variable. This variable can be set in your .gitlab-ci.yml file. The variable can store multiple values, separated by commas (,). Each value is a regular expression. Because each entry is a regular expression, an entry like .* excludes all URLs because it is a regular expression that matches everything.

In your job output you can check if any URLs matched any provided regular expression from APISEC_EXCLUDE_URLS. Matching operations are listed in the Excluded Operations section. Operations listed in the Excluded Operations should not be listed in the Tested Operations section. For example the following portion of a job output:

2021-05-27 21:51:08 [INF] API SECURITY: --[ Tested Operations ]-------------------------
2021-05-27 21:51:08 [INF] API SECURITY: 201 POST http://target:7777/api/users CREATED
2021-05-27 21:51:08 [INF] API SECURITY: ------------------------------------------------
2021-05-27 21:51:08 [INF] API SECURITY: --[ Excluded Operations ]-----------------------
2021-05-27 21:51:08 [INF] API SECURITY: GET http://target:7777/api/messages
2021-05-27 21:51:08 [INF] API SECURITY: POST http://target:7777/api/messages
2021-05-27 21:51:08 [INF] API SECURITY: ------------------------------------------------
note
Each value in APISEC_EXCLUDE_URLS is a regular expression. Characters such as . , * and $ among many others have special meanings in regular expressions.

Examples

Excluding a URL and child resources

The following example excludes the URL http://target/api/auth and its child resources.

stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_TARGET_URL: http://target/
  APISEC_OPENAPI: test-api-specification.json
  APISEC_EXCLUDE_URLS: http://target/api/auth
Excluding two URLs and allow their child resources

To exclude the URLs http://target/api/buy and http://target/api/sell but allowing to scan their child resources, for instance: http://target/api/buy/toy or http://target/api/sell/chair. You could use the value http://target/api/buy/$,http://target/api/sell/$. This value is using two regular expressions, each of them separated by a , character. Hence, it contains http://target/api/buy$ and http://target/api/sell$. In each regular expression, the trailing $ character points out where the matching URL should end.

stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_TARGET_URL: http://target/
  APISEC_OPENAPI: test-api-specification.json
  APISEC_EXCLUDE_URLS: http://target/api/buy/$,http://target/api/sell/$
Excluding two URLs and their child resources

To exclude the URLs: http://target/api/buy and http://target/api/sell, and their child resources. To provide multiple URLs we use the , character as follows:

stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_TARGET_URL: http://target/
  APISEC_OPENAPI: test-api-specification.json
  APISEC_EXCLUDE_URLS: http://target/api/buy,http://target/api/sell
Excluding URL using regular expressions

To exclude exactly https://target/api/v1/user/create and https://target/api/v2/user/create or any other version (v3,v4, and more). We could use https://target/api/v.*/user/create$, in the previous regular expression . indicates any character and * indicates zero or more times, additionally $ indicates that the URL should end there.

stages:
  - dast

include:
  - template: API-Security.gitlab-ci.yml

variables:
  APISEC_TARGET_URL: http://target/
  APISEC_OPENAPI: test-api-specification.json
  APISEC_EXCLUDE_URLS: https://target/api/v.*/user/create$