> ## 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.

# Block Malicious Package Downloads

> Block npm, PyPI and other package downloads that Socket flags, on the Formal Endpoint or Connector

## Overview

Developers and coding agents install packages from public registries all day. A single malicious package can steal credentials from the machine that installs it.

This guide blocks package downloads that [Socket](https://socket.dev) flags. The Formal Endpoint intercepts package registry traffic. A [policy](https://docs.formal.ai/docs/glossary/index.md#policy) reads Socket's verdict and blocks the download. It covers npm, PyPI, Maven, crates.io, RubyGems, Open VSX, NuGet, Go and Conda.

## Prerequisites

* A Socket organization with an organization API token that has the `packages:list` scope
* The Formal Endpoint with [Transparent Mode](https://docs.formal.ai/docs/guides/client-apps/desktop-app.md#transparent-mode) enabled

## Connect Socket

Go to [Socket](https://app.formal.ai/socket) under **Controls**. Paste the token and click **Connect Socket**. See [Socket](https://docs.formal.ai/docs/guides/integrations/socket.md#connect-socket) for details.

**Verify:** the page shows your Socket organization name.

## Create the network rule

This rule intercepts HTTPS traffic to supported package registries only. The Endpoint leaves all other traffic alone.

> [tab] Control Plane
>
> 1. Navigate to [Network Rules](https://app.formal.ai/network-rules)
> 2. Create a rule named `package-registries`
> 3. Paste the CEL below
> 4. Save the rule and set it to **Active**
>
> ```cel theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> {
>   "condition": {
>     "pre_tls": is_package_registry,
>     "post_tls": technology == "http"
>   }
> }
> ```

> [tab] Terraform
>
> ```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> resource "formal_network_rule" "package_registries" {
>   name        = "package-registries"
>   description = "Intercept package registry downloads for Socket checks"
>   status      = "active"
>
>   cel_expression = <<-EOT
>     {
>       "condition": {
>         "pre_tls": is_package_registry,
>         "post_tls": technology == "http"
>       }
>     }
>   EOT
> }
> ```

To check only npm, match `package_registry == "npm"` instead. Change the policy guard to `input.http.package_registry == "npm"` to match. See [Network rule inputs](https://docs.formal.ai/docs/guides/network-rules.md#network-rule-inputs).

To check packages on a [Connector](https://docs.formal.ai/docs/glossary/index.md#connector) instead of the Endpoint, add `"outputs": {"forward_to_connector": true}`. The Connector needs an HTTP [resource](https://docs.formal.ai/docs/glossary/index.md#resource) for each registry host. Connector policies read the same `input.integrations.socket`.

## Create the policy

This policy blocks a package download when your Socket policy marks the package as `error`.

> [tab] Control Plane
>
> 1. Navigate to [Policies](https://app.formal.ai/policies)
> 2. Click **Create Policy**
> 3. Set name to `block-socket-flagged-packages`
> 4. Paste the Rego below into the editor
> 5. Click **Create Policy** to save
>
> ```rego theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> package formal.v2
>
> import future.keywords.if
>
> request := {
>   "action": "block",
>   "type": "block_with_custom_message",
>   "message": sprintf("Socket blocked %s", [input.integrations.socket.purl])
> } if {
>   input.http.package_registry != ""
>   input.integrations.socket.action == "error"
> }
> ```

> [tab] Terraform
>
> ```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> resource "formal_policy" "block_socket_flagged_packages" {
>   name        = "block-socket-flagged-packages"
>   description = "Block package downloads that Socket flags"
>   status      = "active"
>
>   module = <<-EOT
>     package formal.v2
>
>     import future.keywords.if
>
>     request := {
>       "action": "block",
>       "type": "block_with_custom_message",
>       "message": sprintf("Socket blocked %s", [input.integrations.socket.purl])
>     } if {
>       input.http.package_registry != ""
>       input.integrations.socket.action == "error"
>     }
>   EOT
> }
> ```

> [!WARNING]
>
> Always check `input.http.package_registry` 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.

To block on specific alerts instead, match `alerts`:

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

import future.keywords.if
import future.keywords.in

request := {
  "action": "block",
  "type": "block_with_formal_message"
} if {
  input.http.package_registry != ""
  some alert in input.integrations.socket.alerts
  alert.type == "malware"
}
```

The policy fails open: if Socket is unreachable or out of quota, downloads continue. To fail closed, see [Handle failed checks](https://docs.formal.ai/docs/guides/integrations/socket.md#handle-failed-checks).

## Verify downloads are blocked

1. Confirm the network rule and the policy are **Active**.
2. Clear npm's cache with `npm cache clean --force`. A cached tarball never reaches the registry, so Socket never checks it.
3. Install a package Socket flags as malware, such as `npm i lodahs`. The install fails:

   ```text theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
   npm error code E403
   npm error 403 403 Forbidden - GET https://registry.npmjs.org/lodahs/-/lodahs-0.0.1-security.tgz
   ```

   npm does not print the policy's block message.
4. Install a safe package, such as `npm install lodash`. It succeeds.
5. Open [Logs](https://docs.formal.ai/docs/guides/observability/logs.md) and find the `lodahs` tarball request. It shows `block-socket-flagged-packages` as the matching policy.

## Troubleshooting

> [accordion] Downloads are not checked
>
> **Cause:** The network rule does not match, or Transparent Mode is off.
>
> **Fix:** Run `formal transparent-proxy status`. Confirm the install uses a public registry, such as `registry.npmjs.org`.

> [accordion] Policy input has error.type formal\_error
>
> **Cause:** Socket is not connected, or the Endpoint cannot reach Formal's Control Plane.
>
> **Fix:** Check the [Socket](https://app.formal.ai/socket) page shows a connected organization.

> [accordion] Policy input has status\_code 429
>
> **Cause:** Your Socket organization ran out of quota.
>
> **Fix:** Raise your Socket quota, or guard the rule more narrowly so fewer requests call Socket.

## Next steps

> [card] [Socket integration](https://docs.formal.ai/docs/guides/integrations/socket.md)
>
> Every Socket policy input and how checks are made

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


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