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

# Internetzugriff

export const SandboxConfigHint = () => {
  if (typeof document === "undefined") {
    return null;
  } else {
    return <Note>Before running the example code in this document, please ensure you have properly configured environment variables. For details, please refer to <a href="/docs/de/guides/sandbox-your-first-agent-sandbox#configure-environment-variables">Configure Environment Variables</a>.</Note>;
  }
};

Internetkonnektivität ist in jeder Sandbox verfügbar, und externer Zugriff wird über eine öffentliche URL bereitgestellt.

<SandboxConfigHint />

## Internetzugriff umschalten

Beim Erstellen einer Sandbox kannst du den Parameter `allowInternetAccess` / `allow_internet_access` verwenden, um die Internetkonnektivität zu konfigurieren. Der Internetzugriff ist standardmäßig aktiviert, kann aber für Workloads mit strengeren Sicherheitsanforderungen deaktiviert werden.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  // Create sandbox with internet access enabled (default)
  const sandbox = await Sandbox.create({ allowInternetAccess: true })

  // Create sandbox without internet access
  const isolatedSandbox = await Sandbox.create({ allowInternetAccess: false })
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  # Create sandbox with internet access enabled (default)
  sandbox = Sandbox.create(allow_internet_access=True)

  # Create sandbox without internet access
  isolated_sandbox = Sandbox.create(allow_internet_access=False)
  ```
</CodeGroup>

Wenn der Internetzugriff deaktiviert ist, wird die Sandbox daran gehindert, ausgehende Netzwerkanfragen zu initiieren. Dies hilft, beim Ausführen von sensiblem Code eine zusätzliche Schutzschicht hinzuzufügen.

<Note>
  Das Übergeben eines falsy-Werts an `allowInternetAccess` / `allow_internet_access` hat denselben Effekt wie das Hinzufügen von `['0.0.0.0/0']` zu `network.denyOut` / `network.deny_out`, wodurch jedes Ziel blockiert wird.
</Note>

## Feingranulare Netzwerksteuerung

Die Netzwerkkonfiguration bietet feingranularere Kontrolle über ausgehenden Datenverkehr, indem du Zulassungslisten und Sperrlisten definieren kannst.

### Zulassungs- und Sperrlisten

IP-Adressen, CIDR-Blöcke oder Domainnamen, auf die die Sandbox zugreifen darf, können angegeben werden.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  // Deny all traffic except specific IPs
  const sandbox = await Sandbox.create({
    network: {
      denyOut: ['0.0.0.0/0'],
      allowOut: ['1.1.1.1', '8.8.8.0/24']
    }
  })

  // Deny specific IPs only
  const restrictedSandbox = await Sandbox.create({
    network: {
      denyOut: ['8.8.8.8']
    }
  })
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  # Deny all traffic except specific IPs
  sandbox = Sandbox.create(
      network={
          "deny_out": ["0.0.0.0/0"],
          "allow_out": ["1.1.1.1", "8.8.8.0/24"]
      }
  )

  # Deny specific IPs only
  restricted_sandbox = Sandbox.create(
      network={
          "deny_out": ["8.8.8.8"]
      }
  )
  ```
</CodeGroup>

<Note>
  Das CIDR `'0.0.0.0/0'` / `"0.0.0.0/0"` ist eine Kurzschreibweise für „jedes Ziel“. Eine exportierte Konstante `ALL_TRAFFIC` wird auf denselben Wert `0.0.0.0/0` aufgelöst, falls du eine benannte Alternative zum Literal bevorzugst.
</Note>

### Domainbasiertes Filtern

Du kannst Hostnamen in `allowOut` / `allow_out` angeben, um ausgehenden Datenverkehr zu ausgewählten Domains zu erlauben. Wenn domainbasiertes Filtern aktiviert ist, muss der gesamte verbleibende Datenverkehr über `denyOut` / `deny_out` blockiert werden. Domain-Einträge werden nur in Zulassungslisten unterstützt und können nicht in Sperrlisten verwendet werden.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  // Allow only traffic to google.com
  const sandbox = await Sandbox.create({
    network: {
      allowOut: ['google.com'],
      denyOut: ['0.0.0.0/0']
    }
  })
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  # Allow only traffic to google.com
  sandbox = Sandbox.create(
      network={
          "allow_out": ["google.com"],
          "deny_out": ["0.0.0.0/0"]
      }
  )
  ```
</CodeGroup>

<Note>
  Immer wenn eine Domain in der Konfiguration erscheint, wird der Standard-Nameserver `8.8.8.8` automatisch zugelassen, damit die DNS-Auflösung weiterhin funktioniert.
</Note>

Du kannst außerdem jede Subdomain einer Domain mit einem Wildcard abgleichen:

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  // Allow traffic to any subdomain of mydomain.com
  const sandbox = await Sandbox.create({
    network: {
      allowOut: ['*.mydomain.com'],
      denyOut: ['0.0.0.0/0']
    }
  })
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  # Allow traffic to any subdomain of mydomain.com
  sandbox = Sandbox.create(
      network={
          "allow_out": ["*.mydomain.com"],
          "deny_out": ["0.0.0.0/0"]
      }
  )
  ```
</CodeGroup>

Domains, IP-Adressen und CIDR-Bereiche können alle in derselben Liste kombiniert werden:

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  // Allow traffic to specific domains and IPs
  const sandbox = await Sandbox.create({
    network: {
      allowOut: ['api.example.com', '*.github.com', '8.8.8.8'],
      denyOut: ['0.0.0.0/0']
    }
  })
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  # Allow traffic to specific domains and IPs
  sandbox = Sandbox.create(
      network={
          "allow_out": ["api.example.com", "*.github.com", "8.8.8.8"],
          "deny_out": ["0.0.0.0/0"]
      }
  )
  ```
</CodeGroup>

<Note>
  Das Filtern nach Domain gilt nur für HTTP über Port 80 (geprüft über den Host-Header) und TLS über Port 443 (geprüft über SNI). Jeder andere Port fällt auf CIDR-basiertes Matching zurück, und UDP-Protokolle wie QUIC/HTTP3 können nicht nach Domain gefiltert werden.
</Note>

### Verhalten blockierter TCP-Verbindungen

Aufgrund der Firewall-Architektur kann eine blockierte ausgehende Verbindung innerhalb der Sandbox dennoch erfolgreich erscheinen.

Die Firewall muss die TCP-Verbindung zunächst akzeptieren, bevor sie auswerten kann, ob das Ziel erlaubt ist. Daher kann Code, der innerhalb der Sandbox ausgeführt wird, sehen, dass die Verbindung erfolgreich ist und der Socket geöffnet wird, selbst wenn das Ziel blockiert ist. In diesem Fall wird tatsächlich kein Datenverkehr an den entfernten Endpunkt zugestellt.

Um zu bestätigen, dass das Ziel erreichbar ist, solltest du eine Antwort auf Anwendungsebene validieren, anstatt dich nur auf den Erfolg der TCP-Verbindung zu verlassen. Prüfe beispielsweise auf einen HTTP-Statuscode, einen abgeschlossenen TLS-Handshake oder die erwarteten Antwortbytes des Protokolls.

Dieses Verhalten ist eine aktuelle Einschränkung der Art und Weise, wie ausgehender Sandbox-Datenverkehr durch unsere Firewall geleitet wird, und kann in Zukunft aktualisiert werden.

### Prioritätsregeln

Wenn sowohl Zulassungs- als auch Sperrregeln konfiguriert sind, **haben die Zulassungsregeln Vorrang**. Daher wird jede IP-Adresse, die in beiden Listen erscheint, weiterhin erlaubt.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  // Even though all traffic is denied, 1.1.1.1 and 8.8.8.8 are explicitly allowed
  const sandbox = await Sandbox.create({
    network: {
      denyOut: ['0.0.0.0/0'],
      allowOut: ['1.1.1.1', '8.8.8.8']
    }
  })
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  # Even though all traffic is denied, 1.1.1.1 and 8.8.8.8 are explicitly allowed
  sandbox = Sandbox.create(
      network={
          "deny_out": ["0.0.0.0/0"],
          "allow_out": ["1.1.1.1", "8.8.8.8"]
      }
  )
  ```
</CodeGroup>

Die Einstellungen `network` werden nur wirksam, wenn die Sandbox erstellt wird — übergib sie an `Sandbox.create`. Sobald die Sandbox existiert, sind sie festgelegt und können nicht geändert werden.

## Öffentliche Sandbox-URL

Auf Dienste in einer Sandbox kann über die öffentliche URL der Sandbox zugegriffen werden.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  const sandbox = await Sandbox.create()

  // You need to always pass a port number to get the host
  const host = sandbox.getHost(3000)
  console.log(`https://${host}`)
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  sandbox = Sandbox.create()

  # You need to always pass a port number to get the host
  host = sandbox.get_host(3000)
  print(f'https://{host}')
  ```
</CodeGroup>

Die Ausgabe sieht wie folgt aus:

<CodeGroup>
  ```bash JavaScript & TypeScript icon="terminal" theme={"system"}
  https://3000-i62mff4ahtrdfdkyn2esc.sandbox.novita.ai
  ```

  ```bash Python icon="terminal" theme={"system"}
  https://3000-i62mff4ahtrdfdkyn2esc.sandbox.novita.ai
  ```
</CodeGroup>

Das ganz linke Segment des Hostnamens ist genau die Portnummer, die du der Methode übergeben hast.

## Verbindung zu einem Server, der innerhalb der Sandbox läuft

Du kannst dich mit einem Server verbinden, der innerhalb der Sandbox läuft, indem du die zuvor beschriebene Methode verwendest. Starte beispielsweise einen schlanken HTTP-Server auf Port 3000, um Dateien aus seinem Startverzeichnis bereitzustellen.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  const sandbox = await Sandbox.create()

  // Start a simple HTTP server inside the sandbox.
  const process = await sandbox.commands.run('python -m http.server 3000', { background: true })
  const host = sandbox.getHost(3000)
  const url = `https://${host}`
  console.log('Server started at:', url)

  // Fetch data from the server inside the sandbox.
  const response = await fetch(url);
  const data = await response.text();
  console.log('Response from server inside sandbox:', data);

  // Kill the server process inside the sandbox.
  await process.kill()
  ```

  ```python Python icon="python" theme={"system"}
  import requests
  from novita_sandbox.code_interpreter import Sandbox

  sandbox = Sandbox.create()

  # Start a simple HTTP server inside the sandbox.
  process = sandbox.commands.run("python -m http.server 3000", background=True)
  host = sandbox.get_host(3000)
  url = f"https://{host}"
  print('Server started at:', url)

  # Fetch data from the server inside the sandbox.
  response = requests.get(url)
  data = response.text
  print('Response from server inside sandbox:', data)

  # Kill the server process inside the sandbox.
  process.kill()
  ```
</CodeGroup>

Diese Ausgabe sieht wie folgt aus:

<CodeGroup>
  ```bash JavaScript & TypeScript icon="terminal" theme={"system"}
  Server started at: https://3000-ip3nfrvajtqu5ktoxugc7.sandbox.novita.ai
  Response from server inside sandbox: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Directory listing for /</title>
  </head>
  <body>
  <h1>Directory listing for /</h1>
  <hr>
  <ul>
  <li><a href=".bash_logout">.bash_logout</a></li>
  <li><a href=".bashrc">.bashrc</a></li>
  <li><a href=".profile">.profile</a></li>
  </ul>
  <hr>
  </body>
  </html>
  ```

  ```bash Python icon="terminal" theme={"system"}
  Server started at: https://3000-ip3nfrvajtqu5ktoxugc7.sandbox.novita.ai
  Response from server inside sandbox: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Directory listing for /</title>
  </head>
  <body>
  <h1>Directory listing for /</h1>
  <hr>
  <ul>
  <li><a href=".bash_logout">.bash_logout</a></li>
  <li><a href=".bashrc">.bashrc</a></li>
  <li><a href=".profile">.profile</a></li>
  </ul>
  <hr>
  </body>
  </html>
  ```
</CodeGroup>

## Maskieren von Host-Headern in Anfragen

Du kannst die Option `maskRequestHost` / `mask_request_host` verwenden, um den Host-Header anzupassen, der an Dienste gesendet wird, die innerhalb der Sandbox laufen. Das ist nützlich, wenn deine Anwendung erwartet, dass Anfragen einem bestimmten Host-Format folgen.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  // Create sandbox with custom host masking
  const sandbox = await Sandbox.create({
    network: {
      maskRequestHost: 'localhost:${PORT}'
    }
  })

  // The ${PORT} variable will be replaced with the actual port number
  // Requests to the sandbox will have Host header set to for example: localhost:8080
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  # Create sandbox with custom host masking
  sandbox = Sandbox.create(
      network={
          "mask_request_host": "localhost:${PORT}"
      }
  )

  # The ${PORT} variable will be replaced with the actual port number
  # Requests to the sandbox will have Host header set to for example: localhost:8080
  ```
</CodeGroup>

Zum Anfragezeitpunkt wird `${PORT}` in der Maske durch die tatsächliche Portnummer des angesprochenen Dienstes ersetzt.
