Modal logo

OutboundPolicy

Ordered rules for a Sandbox's outbound traffic.

Each connection is handled by the first OutboundRoute that matches it, and goes to its original destination. The last route must be OutboundRoute.any, which matches everything earlier routes didn't and either lets it through or, with block(), blocks it.

const secret = await modal.secrets.fromName("api-token");

const policy = new modal.OutboundPolicy({
  allowTlsDecryption: true,
  routes: [
    modal.OutboundRoute.https({
      domains: ["api.example.com"],
      headerReplacements: [
        { secret, headers: { Authorization: "Bearer $API_TOKEN" } },
      ],
    }),
    modal.OutboundRoute.tls({
      domains: ["github.com", "*.github.com", "pypi.org"],
    }),
    modal.OutboundRoute.ip({ cidrs: ["203.0.113.0/24"] }),
    modal.OutboundRoute.any().block(),
  ],
});

const sb = await modal.sandboxes.create(app, image, { outboundPolicy: policy });

constructor

new OutboundPolicy(params: {
  allowTlsDecryption?: boolean;
  routes: OutboundRoute[];
})
  • params: .allowTlsDecryption Allow Modal to decrypt the Sandbox's TLS traffic on routes that need it, by generating a certificate authority for the Sandbox and attempting to make the Sandbox trust it. Decrypts nothing on its own. Required by HTTPS routes. Fixed when the Sandbox is created. Sandboxes that allow TLS decryption don't support memory snapshots. Defaults to false.
  • params: .routes Ordered routes; the first route matching a connection handles it. Must end with exactly one OutboundRoute.any route.