# Turn off active scan for your first ZAP run on a client’s API

> Scanning early lets you fix flaws before the security review, provided you pick the right environment and read the exit codes correctly.

Original: https://fdetimes.net/en/tools/first-zap-api-scan-without-active-scan/

The tool you plan to use to prepare for a security review can delete the client's data. ZAP's own FAQ warns that active scanning is dangerous and, depending on the application, may create, modify or delete data.

ZAP still belongs in an FDE's toolkit. It is free and open source under the Apache v2 license. Since ZAP partnered with Checkmarx in 2024, decisions about the project have remained with the ZAP Core Team.

The time to use it is when the integration API is running on staging and the review is far enough away to leave room for fixes. A warning you find yourself is just a ticket. The same warning, found by the client's security team, can delay go-live. The practical question is how to scan early without causing an incident of your own.

## Your spec may point at the wrong server

ZAP's Docker image ships with a script called `zap-api-scan.py`, which accepts an OpenAPI, SOAP or GraphQL definition. Its active scan is tuned for APIs, so it skips tests such as XSS. The OpenAPI add-on reads versions 1.2, 2.0, 3.0 and 3.1, so a client's old Swagger file is not necessarily an obstacle.

Before running anything, open the spec and read the server name. If a remote spec lists the production address and you want to scan staging, the `-O` option overrides the hostname. Check this line twice: with the wrong hostname, the entire scan runs against the wrong system.

The documentation describes `-O` for remote specs. If your spec is a file on your machine, do not rely on this option: edit the server address in a copy of the file before you scan.

## The first run: few privileges, little risk

Say you have just delivered an order API to a client, staging holds only disposable data and the spec is loaded remotely.

The script below is a skeleton and will not run as is. You need to fill in three values from the ZAP documentation for your version: the image name, the working directory inside the container, and the spec declaration with its format.

Save it as `scan.sh`.

```bash
#!/usr/bin/env bash
# Skeleton: fill in from the ZAP docs; the script stops if any value is missing
: "${ZAP_IMAGE:?image name}"
: "${ZAP_WORKDIR:?working directory in the container}"
: "${TEST_TOKEN:?test token}"
TARGET_ARGS=( )   # declare the remote spec and its format
[ "${#TARGET_ARGS[@]}" -gt 0 ] || exit 3

export ZAP_AUTH_HEADER_VALUE="Bearer $TEST_TOKEN"
export ZAP_AUTH_HEADER_SITE="staging-api.example.com"

# -O is for remote specs; -v is here to keep the rule file
docker run --rm \
  -e ZAP_AUTH_HEADER_VALUE \
  -e ZAP_AUTH_HEADER_SITE \
  -v "$PWD:$ZAP_WORKDIR:rw" \
  "$ZAP_IMAGE" zap-api-scan.py \
  "${TARGET_ARGS[@]}" \
  -O staging-api.example.com \
  "$@"
```

For the first run, call it like this:

```bash
bash scan.sh -S -g rules.conf
```

`-S` is safe mode: it skips the active scan and runs a baseline scan instead. `-g` generates the rule configuration file. Do not treat `-S` as a license to scan production; stay on staging and confirm the scope with the client in writing.

In the Docker command, `-e` passes the two authentication variables from your machine into the container. `-v` mounts the current directory in the container, so `rules.conf` is written to your machine and can be read back next time. `--rm` removes the container when the run ends.

`ZAP_AUTH_HEADER_VALUE` is added as a header to every request, while `ZAP_AUTH_HEADER_SITE` limits which site receives it, so do not leave out the second variable. If the client uses a different header, also set `ZAP_AUTH_HEADER` and pass it with `-e`. Use a test token with narrow permissions, then compare against the API logs: if every response is a 401, you have not scanned anything of substance.

Once you have permission and a way to restore the data, drop `-S` to run the active scan. State clearly in the report which runs used `-S` and which did not.

## Why your CI gate may block the wrong thing

By default, every `zap-api-scan.py` alert is a WARN. Deciding what is serious enough to block is up to you: open the rule file, change WARN to FAIL or IGNORE, then load it back with `-c` (or `-u`).

Back to the order API. Suppose six rules raise alerts. You set two to FAIL and one to IGNORE, and leave three as WARN. The decisions are shown below with placeholder rule names; in the real `rules.conf`, keep the format generated by `-g` and change only the word WARN.

```text
rule-1   FAIL
rule-2   FAIL
rule-3   IGNORE
rule-4   WARN
rule-5   WARN
rule-6   WARN
```

With this file, the script returns 1. Once the two FAIL rules are fixed, it returns 2 because three WARNs remain. An ordinary CI step treats any nonzero code as a failure, so the pipeline stays red even with no FAIL left.

| Exit code | Scan result | What to do in CI |
|---|---|---|
| 0 | Success | Pass, archive the report |
| 1 | At least one FAIL | Block, assign someone to fix it |
| 2 | WARNs present, no FAIL | Pass with a ticket, if the client agrees |
| 3 | Other failure | Block, repair the scan and rerun |

Here is that table as a CI step. Keep `-S` until you have permission to run an active scan.

```bash
code=0
bash scan.sh -S -c rules.conf || code=$?

case "$code" in
  0) echo "Passed" ;;
  1) echo "FAIL rules present: blocking"; exit 1 ;;
  2) echo "Only WARNs left: passing, open a ticket" ;;
  *) echo "Scan broken (code $code): blocking"; exit "$code" ;;
esac
```

While `-S` is in place, this gate runs only the baseline. A 0 here therefore says even less than a 0 from a full active scan, so record the scan mode next to the result.

The costliest mistake is adding `|| true` to turn the pipeline green again. It swallows both code 1 and code 3, so a broken scan looks identical to a clean one. In the step above, `|| code=$?` keeps the code so each case can be handled, and the `*` branch blocks on Docker errors too. Require every IGNORE line to carry a reason and the name of the person who accepted the risk.

## What does a green result leave unanswered?

ZAP's documentation says plainly that logical vulnerabilities such as broken access control will not be found by any active or automated vulnerability scanning. For the order API, that means customer A's token reading customer B's orders. The response may be perfectly valid; the fault lies in the business rule.

So write this test yourself for every endpoint that accepts an ID: use token A to request B's record and check that the API refuses. The test complements ZAP, and manual penetration testing is still necessary.

**Key point:** A clean scan does not prove that users can see only their own data.

Where you run the scan is another constraint. The packaged Docker scans expose the ZAP API without authentication to the host running them. Avoid the client's shared jump box; choose an isolated machine and clean up when the run is done.

## What to learn first, and how to present it to employers

Start with three skills, in this order: reading an OpenAPI spec, sending the right token, classifying rules. Leave the CI script for later, because an automated gate built on the wrong scope only repeats the error faster.

When reading FDE job descriptions, watch for phrases such as "security review" or "customer environment": they suggest this skill will be used. On your résumé, "knows ZAP" is too thin. Point to a repo containing a rule file with reasons, handling for the four exit codes and an authorization test.

Bring the report, the rule file and a list of what has not been tested to the review. The security team will have reason to trust what you say is fine, because you have also shown them the parts nobody has verified yet.

**Try this week:**

- Run zap-api-scan.py with -S and -g on a test API of your own, then classify each rule as FAIL, WARN or IGNORE, with a reason.
- Add a CI step that handles exit codes 1, 2 and 3 separately, then deliberately break the scan to see whether the pipeline reports it correctly.
- Write an authorization test: use customer A's token to read customer B's record.

## Sources

- [Zed Attack Proxy (ZAP)](https://www.zaproxy.org/)

- [ZAP Has Joined Forces With Checkmarx](https://www.zaproxy.org/blog/2024-09-24-zap-has-joined-forces-with-checkmarx/)

- [ZAP - API Scan](https://www.zaproxy.org/docs/docker/api-scan/)

- [zap-api-scan.py (zaproxy/zaproxy, main branch)](https://raw.githubusercontent.com/zaproxy/zaproxy/main/docker/zap-api-scan.py)

- [OpenAPI Support](https://www.zaproxy.org/docs/desktop/addons/openapi-support/)

- [Handling Authentication Yourself (in Automation)](https://www.zaproxy.org/docs/getting-further/authentication/handling-auth-yourself/)

- [Is there any danger when scanning with ZAP against a live website (e.g. create/delete/update/corrupt data)?](https://www.zaproxy.org/faq/is-there-any-danger-when-scanning-with-zap-against-a-live-website-e-g-create-delete-update-corrupt-data/)

- [Active Scan](https://www.zaproxy.org/docs/desktop/start/features/ascan/)

- [ZAP Docker User Guide](https://www.zaproxy.org/docs/docker/about/)
