> ## Documentation Index
> Fetch the complete documentation index at: https://docs.formal.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Socket

> Check npm, PyPI and other package downloads against Socket in Formal policies

## Overview

Formal's Socket integration lets [policies](https://docs.formal.ai/docs/glossary/index.md#policy) check package downloads against [Socket](https://socket.dev) (supply chain security for open source packages). You connect your own Socket organization. Checks run on your Socket plan and quota.

Policies read Socket's verdict from `input.integrations.socket`. The Formal Endpoint and the [Connector](https://docs.formal.ai/docs/glossary/index.md#connector) evaluate these policies locally.

## Connect Socket

**1. Create a Socket organization API token**

A Socket owner or admin creates an organization API token in [Socket settings](https://socket.dev/dashboard/settings/integrations/api-tokens) (organization API tokens). Give it the `packages:list` scope.

**2. Connect the token in Formal**

Go to [Socket](https://app.formal.ai/socket) under **Controls**. Paste the token and click **Connect Socket**.

Formal checks the token before it saves it. The token must belong to exactly one Socket organization. Formal then runs one Socket check on a public npm package to confirm the token can use Socket's firewall.

**Verify:** the page shows your Socket organization name, its ID, and when it was connected.

Formal never shows the token again. Each Formal organization connects one Socket organization. To change the token, click **Disconnect** and connect again.

## How checks work

Formal adds `input.integrations.socket` to the request stage of every HTTP request. That includes requests on the Formal Endpoint and requests a Connector receives.

* **Request fields:** `url` is always set. It is not determined via Socket's API and does not make a network request.
* **Verdict fields:** the rest are fetched only when a policy reads one of them. Formal then makes one Socket check for that request, however many fields the policy reads.

> [!WARNING]
>
> Always check [`input.http.package_registry`](https://docs.formal.ai/docs/guides/policies/evaluation#http-object) or `input.http.hostname` (such as `input.http.hostname == "registry.npmjs.org"`) before reading a Socket verdict. Otherwise Formal sends every HTTP request to Socket, and each check counts against your Socket organization's quota.

## Policy input

`input.integrations.socket` is available in the request stage of HTTP requests.

| Value | Type | Description |
| - | - | - |
| `url` | **STRING** | The `https` URL Formal sends to Socket for this request. |
| `status_code` | **NUMBER** | HTTP status Socket answered with. Unset when no response came back. |
| `error` | **Error Object** | Set when Formal got no usable answer from Socket. |
| `purl` | **STRING** | Package URL Socket identified, such as `pkg:npm/lodash@4.17.21`. |
| `status` | **STRING** | Socket's scan status for the package. Set only when Socket answered with a 2xx status. |
| `action` | **STRING** | Socket's verdict from your Socket policy, such as `error` for a package your Socket policy blocks. |
| `reason` | **STRING** | Socket's reason for the verdict, when it gives one. |
| `alerts` | **\[]Alert Object** | Socket alerts on the package. |

The verdict fields (`purl`, `status`, `action`, `reason` and `alerts`) use Socket's own names and values. They are set only when Socket answered with a 2xx status. A non-2xx answer, such as `429` when the Socket quota runs out, sets only `status_code`.

### Alert object

| Value | Type | Description |
| - | - | - |
| `type` | **STRING** | Socket alert type, such as `malware` |
| `severity` | **STRING** | Socket alert severity, such as `critical` |
| `category` | **STRING** | Socket alert category, such as `supplyChainRisk` |
| `action` | **STRING** | Action your Socket policy sets for this alert |

### Error object

| Value | Type | Description |
| - | - | - |
| `type` | **ENUM** | `formal_error` when the Endpoint or Connector could not reach Formal's Control Plane, or Socket is not connected. `socket_error` when Socket was unreachable or its response could not be read. |
| `message` | **STRING** | What failed |

### Handle failed checks

A Socket check can fail, for example when Socket is down, your quota runs out or the token is revoked. Verdict fields like `action` are unset when it does.

By default, a rule that matches on `action` doesn't match a failed check, so Formal allows the download (fail open). To block downloads Socket couldn't check instead (fail closed), add a rule that blocks when `status` is unset:

```rego theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
package formal.v2

import future.keywords.if

request := {
  "action": "block",
  "type": "block_with_formal_message"
} if {
  input.http.package_registry != ""
  not input.integrations.socket.status
}
```

To act on one kind of failure, match `status_code` (for example `== 429`) or `error.type`.

This rule only covers failed checks. Socket still answers `not_scanned` or `not_identified` for packages it has no verdict for, and `status` is set. To block those too, match `input.integrations.socket.status != "scanned"`.

See [Block malicious package downloads](https://docs.formal.ai/docs/guides/example-use-cases/block-malicious-package-downloads.md) for a complete setup.

## Troubleshooting

> [accordion] Connect fails with "invalid Socket API token"
>
> **Cause:** Socket rejected the token.
>
> **Fix:** Check that you copied the whole token and that it has not been revoked.

> [accordion] Connect fails with "cannot query Socket's firewall"
>
> **Cause:** The token is valid but lacks the `packages:list` scope. The error includes Socket's status and response.
>
> **Fix:** Create a token with `packages:list` and connect again.

> [accordion] Connect fails with "must belong to exactly one organization"
>
> **Cause:** The token is a personal token with access to several Socket organizations.
>
> **Fix:** Create an organization API token instead.

> [accordion] status\_code is 401 or 403 in policy input
>
> **Cause:** The token was revoked, or its scope or plan changed after you connected it.
>
> **Fix:** Disconnect Socket and connect a new token.

## Next Steps

> [card] [Block malicious packages](https://docs.formal.ai/docs/guides/example-use-cases/block-malicious-package-downloads.md)
>
> Intercept package registries and block flagged packages

> [card] [Network rules](https://docs.formal.ai/docs/guides/network-rules.md)
>
> Match package registry traffic on the Formal Endpoint


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.