Skip to content

fix: sandbox function runtime network access - #1194

Open
sauraww wants to merge 1 commit into
mainfrom
fix/function-sandbox-ssrf
Open

sauraww wants to merge 1 commit into
mainfrom
fix/function-sandbox-ssrf

Conversation

@sauraww

@sauraww sauraww commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

User-defined functions run on our servers and can call fetch. That made it possible to reach internal-only addresses from a function, for example:

  • the cloud metadata service (169.254.169.254, metadata.google.internal), which returns instance credentials
  • private / loopback networks (10.0.0.0/8, 127.0.0.1, 192.168.0.0/16, …) and internal service names

This is a Server-Side Request Forgery (SSRF) risk. Apart from fetch, there were other ways to reach the network:

  • raw sockets and DNS through the Deno global (Deno.connect, Deno.resolveDns, Deno.createHttpClient)
  • module imports (import("http://169.254.169.254/")), which go through a separate loader, and file:// imports, which read local files

Solution

A new module, crates/context_aware_config/src/function_sandbox.rs, sets up the function runtime. Everything goes through rustyscript's public RuntimeOptions, so the rustyscript fork needs no changes.

Layer What it does
Egress policy (EgressPolicy trait, PublicInternetOnly impl) Decides which IPs and hostnames are allowed. It blocks private, loopback, link-local, CGNAT, multicast and reserved ranges (IPv4 and IPv6, including IPv4-mapped / NAT64 forms), Azure's 168.63.129.16, metadata hostnames, and internal suffixes (.internal, .local, .svc, …) and single-label names.
Request hook Checks every fetch request before it is sent, including each redirect hop. Only http/https are allowed.
DNS resolver Rejects a hostname if any of its resolved IPs is blocked. This catches public names that point to internal IPs (e.g. 169.254.169.254.nip.io).
Op middleware Disables raw network ops in Rust (op_net_*, op_dns_*, op_tls_start, op_tls_handshake, op_fetch_custom_client). They throw op is disabled even if user code finds a way to reach them.
DenyImports Blocks all imports from function code. Only our own function.js load is allowed.
JS prelude Removes the Deno global from the function scope (defense in depth). It throws if the removal fails.

Normal functions are not affected: fetch to public URLs, console, JSON, timers, etc. work as before. The execution timeout (1500ms) is unchanged; it is now a shared FUNCTION_TIMEOUT constant.

Blocked requests fail with:
Network access to '<host>' is not allowed from functions

Testing

  • Unit tests in function_sandbox.rs:
    • policy allow/deny cases
    • blocked fetch targets
    • redirect to metadata (local redirect server)
    • raw ops disabled even with Deno present
    • prelude removes Deno and keeps web APIs
    • dynamic / static / JSON imports denied
  • validation_functions::tests::functions_run_sandboxed checks that real function execution goes through the sandbox.
  • Ignored (needs internet): public fetch allowed, and DNS-rebinding-style names blocked. Run with cargo test -p context_aware_config -- --ignored.
  • Checked that the tests fail if the sandbox or the middleware is removed.

Environment variable changes

None.

Pre-deployment activity

  • Check whether any existing customer functions call internal / private endpoints or use import or Deno.*. These will start failing after this change.

Post-deployment activity

  • Watch function execution errors for is not allowed from functions / op is disabled / Importing modules messages, to catch any legitimate use that is now blocked.

API changes

None. The request and response formats are unchanged; only function runtime behaviour changes.

Endpoint Method Request body Response Body
— — — —

Possible Issues in the future

  • Breaking for some functions: functions that call internal services, private IPs or use imports will now fail.
  • rustyscript / deno upgrades: op names may change, and the middleware matches them by prefix, so a renamed op could slip through. The prelude is a backup layer, and the tests should catch a regression.
  • Pinned crates: deno_fetch and deno_error are pinned to the versions rustyscript uses. They must be bumped together with rustyscript.
  • DNS check timing: the resolver checks the IPs at lookup time. hyper connects to exactly those IPs, so DNS rebinding between check and connect isn't possible through fetch.

@sauraww
sauraww requested a review from a team as a code owner December 1, 2026 13:16
@semanticdiff-com

Copy link
Copy Markdown

Review changes with  SemanticDiff

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant