> For the complete documentation index, see [llms.txt](https://docs.safetycli.com/safety-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.safetycli.com/safety-docs/firewall/installation-and-configuration/using-safety-firewall-upstream-of-nexus.md). Agents can also search and read this documentation through its [MCP server](https://docs.safetycli.com/safety-docs/~gitbook/mcp).

# Using Safety Firewall Upstream of Nexus

This guide explains how to set Safety Firewall as the upstream of your Sonatype Nexus Repository proxy repositories, so that Nexus caches only the packages that your policies allow.

### Overview

Nexus proxy repositories can get packages from Safety Firewall instead of from PyPI or the npm registry. Safety Firewall applies the policies of your organization to each request. It removes blocked versions from the package index, so they never get into the Nexus cache. Developers and CI systems continue to use Nexus with no change.

{% hint style="warning" %}
Nexus does not check a cached package again. Create new, empty proxy repositories. Do not change the remote URL of your current proxy repositories: their cache can already hold versions that Safety Firewall blocks.
{% endhint %}

### Prerequisites

* Administrator access to Nexus Repository 3 (tested on version 3.98.1)
* A Safety API key (available from [Safety Platform](https://platform.safetycli.com))
* Outbound HTTPS from the Nexus server to `pkgs.safetycli.com`

### 1. Create the Firewall Credential

Nexus sends your API key to Safety Firewall in an encoded credential. Run this command to make it:

```bash
export SAFETY_API_KEY="your-api-key-here"

python3 -c 'import base64, json, os; print(base64.urlsafe_b64encode(json.dumps({"version": "1.0", "access_token": None, "api_key": os.environ["SAFETY_API_KEY"], "machine_id": None, "project_id": None, "session_id": None}).encode()).decode())'
```

The steps below call the output `<firewall-credential>`. Keep it as secret as your API key.

### 2. Create the Proxy Repositories

In Nexus, go to **Settings > Repository > Repositories** (in the Classic UI: the gear icon, then **Repository > Repositories**). Click **Create repository**, and create one proxy repository for each ecosystem:

{% tabs %}
{% tab title="PyPI" %}
Select **pypi (proxy)** and set these fields:

| Field                          | Value                                               |
| ------------------------------ | --------------------------------------------------- |
| Remote Storage                 | `https://pkgs.safetycli.com/repository/nexus/pypi/` |
| Authentication Type            | Username                                            |
| Username                       | `user`                                              |
| Password                       | `<firewall-credential>`                             |
| Use pre-emptive authentication | Selected                                            |

<figure><img src="https://1428014516-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0ABDwXSDJWM5juLdc6ie%2Fuploads%2Fgit-blob-0cc53d8f9e068741a7f876f5fa646d96bc3045ba%2Fnexus-pypi-proxy-authentication.png?alt=media" alt="Authentication settings of a Nexus PyPI proxy repository"><figcaption></figcaption></figure>

To use the REST API instead:

```bash
curl -u admin -X POST -H 'Content-Type: application/json' \
  https://<nexus-host>/service/rest/v1/repositories/pypi/proxy -d '{
  "name": "pypi-safety-firewall",
  "online": true,
  "storage": {"blobStoreName": "default", "strictContentTypeValidation": true},
  "proxy": {"remoteUrl": "https://pkgs.safetycli.com/repository/nexus/pypi/", "contentMaxAge": 1440, "metadataMaxAge": 1440},
  "negativeCache": {"enabled": true, "timeToLive": 1440},
  "httpClient": {"blocked": false, "autoBlock": true,
    "authentication": {"type": "username", "username": "user", "password": "<firewall-credential>", "preemptive": true}}
}'
```

{% endtab %}

{% tab title="npm" %}
{% hint style="warning" %}
Use the Classic UI or the REST API for the npm proxy repository. In Nexus 3.98.1, the new UI cannot save a bearer token: Nexus rejects it with `Unknown AuthenticationConfiguration type: bearer`. To open the Classic UI, click **Switch to Classic UI**.
{% endhint %}

Select **npm (proxy)** and set these fields:

| Field               | Value                                                |
| ------------------- | ---------------------------------------------------- |
| Remote storage      | `https://pkgs.safetycli.com/repository/nexus/npmjs/` |
| Authentication type | Preemptive Bearer Token                              |
| Token               | `<firewall-credential>`                              |

<figure><img src="https://1428014516-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0ABDwXSDJWM5juLdc6ie%2Fuploads%2Fgit-blob-beda3c3d9fb18a039df27e80c30d00c4c05467db%2Fnexus-npm-proxy-authentication.png?alt=media" alt="Authentication settings of a Nexus npm proxy repository"><figcaption></figcaption></figure>

To use the REST API instead:

```bash
curl -u admin -X POST -H 'Content-Type: application/json' \
  https://<nexus-host>/service/rest/v1/repositories/npm/proxy -d '{
  "name": "npm-safety-firewall",
  "online": true,
  "storage": {"blobStoreName": "default", "strictContentTypeValidation": true},
  "proxy": {"remoteUrl": "https://pkgs.safetycli.com/repository/nexus/npmjs/", "contentMaxAge": 1440, "metadataMaxAge": 1440},
  "negativeCache": {"enabled": true, "timeToLive": 1440},
  "httpClient": {"blocked": false, "autoBlock": true,
    "authentication": {"type": "bearerToken", "bearerToken": "<firewall-credential>"}}
}'
```

{% endtab %}
{% endtabs %}

{% hint style="danger" %}
Nexus must send the credential on every request. Without **Use pre-emptive authentication** (PyPI) or **Preemptive Bearer Token** (npm), Nexus sends no credential, and Safety Firewall does not apply the policies of your organization. Nexus shows no error in this case.
{% endhint %}

If your clients use a Nexus group repository, replace the PyPI or npm proxy in the group with the new proxy repository.

### 3. Verify the Setup

Do this check for each proxy repository: Nexus sends the PyPI and npm credentials through different settings. The check installs nothing.

{% tabs %}
{% tab title="PyPI" %}
Add a version of a harmless package to the deny list of your organization, for example `six==1.16.0`. Then read the package index through Nexus:

```bash
curl -sSf https://<nexus-host>/repository/pypi-safety-firewall/simple/six/ -o six.html && grep -c 'six-1.16.0' six.html
```

If your Nexus does not allow anonymous access, add `-u <nexus-username>` to the `curl` command.

The command prints `0` when Safety Firewall applies your policy. If it prints a larger number, Nexus does not send the credential. If `curl` shows an error, Nexus did not return the package index: fix that error first.
{% endtab %}

{% tab title="npm" %}
Add an earlier version of a harmless package to the deny list of your organization, for example `is-number@6.0.0`. Do not use the latest version: `npm view` reads the latest version first. Then read the package metadata through Nexus:

```bash
npm view is-number@6.0.0 version --registry https://<nexus-host>/repository/npm-safety-firewall/
```

If your Nexus does not allow anonymous access, configure npm with your Nexus credentials first.

The command fails with `No match found for version 6.0.0` when Safety Firewall applies your policy. If it prints `6.0.0`, Nexus does not send the credential. If it shows a different error, Nexus did not return the package metadata: fix that error first.
{% endtab %}
{% endtabs %}

If a client used the proxy repository before you added the deny rule, click **Invalidate Cache** first. Remove the deny rule when the check is complete.

### Caching

Safety Firewall cannot remove a package from the Nexus cache. When Safety Firewall starts to block a version that Nexus already holds:

* Nexus removes the version from the PyPI package index after **Maximum Metadata Age** (default: 1440 minutes). To remove newly blocked versions sooner, set a lower value, for example 60 minutes. A lower value sends more index requests to Safety Firewall.
* Nexus continues to serve the cached file until you delete the component. Clients that request the file by URL, for example from a lock file, still get it. For npm, Nexus also adds the cached version back into the package index.

When Safety reports a newly blocked version, search for it in Nexus and delete the component. To apply a policy change at once, click **Invalidate Cache** on the repository.

### Troubleshooting

| Issue                                                                     | Solution                                                                                                                                                                                                                                                                                                                                      |
| ------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| A package that your policy denies installs through Nexus                  | Set **Use pre-emptive authentication** (PyPI) or **Preemptive Bearer Token** (npm). Then click **Invalidate Cache**, and delete the components that Nexus cached before the fix.                                                                                                                                                              |
| Clients get "not found" for every package that Nexus did not cache        | Safety Firewall rejected the credential, and Nexus auto-blocked the repository with the reason `Unauthorized`. Test the credential from the Nexus server: `curl -s -o /dev/null -w '%{http_code}\n' -u 'user:<firewall-credential>' https://pkgs.safetycli.com/repository/nexus/pypi/simple/requests/`. `401` means the API key is not valid. |
| Nexus answers `403` with "You must accept the End User License Agreement" | Accept the Nexus Community Edition EULA in the onboarding wizard or through the Nexus REST API.                                                                                                                                                                                                                                               |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.safetycli.com/safety-docs/firewall/installation-and-configuration/using-safety-firewall-upstream-of-nexus.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
