Urgent.News

What's breaking now, across thousands of outlets.

Editions

Tech

Taking the network away from a Cloud Run job

If you execute other people's code, at some point you have to decide what it is allowed to reach. Mine should reach nothing: no package registry, no pastebin, no callback to a server that tells it the answer. It should see its own filesystem, its own loopback interface, and the end of the world in every other direction. The usual answers are all heavy. Run your own Kubernetes with a deny-all…

When you run someone else's code, you must determine what it is permitted to access. Mine should be limited to its own filesystem and loopback interface, with no external connections. The usual solutions involve managing your own Kubernetes cluster or using Firecracker directly, but these require additional operational overhead.

Instead, I sought a managed solution where the platform handles container execution and notifies when it completes, without a network connection. Here's how to achieve this on Cloud Run, along with two details not mentioned in the documentation.

A Cloud Run job runs the command you provide. To strip away the network, use the command 'unshare -rn -- ./run-the-untrusted-thing'. The '-r' flag is crucial as it creates a new network namespace, which includes only a loopback device and no routes to the outside world. Consequently, nothing within this namespace can interact with the external network.

This method works specifically with Cloud Run's second-generation execution environment, which utilizes a microVM with a real Linux kernel rather than gVisor's syscall-filtering layer. However, on the first-generation environment, such operations are unsupported and will fail. To resolve this, include a line of code that raises the loopback interface: import fcntl, socket, struct, os, sys; define a function to modify network flags; and execute it before running the user's code.

This ten-line shim raises the loopback interface without introducing additional processes or altering the exit code of the actual command.

Lastly, remember that managed job runners retry failed executions by default. While this is appropriate for data pipelines, it can be problematic for applications where failures should be recorded as failures rather than successes. To prevent automatic retries, set maxRetries to 0. Isolating the code in a separate network namespace provides a strong boundary for security, assuming no kernel exploits are present.

Remember, however, that this does not guarantee complete protection against sophisticated attacks and that the platform's resources are shared.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Laravel env() Outside config/: Catch Deployment Bugs Before config:cache

The bug only appears after configuration caching A Laravel application can behave perfectly during development and then fail after deployment because a service class reads an environment variable…

  • Direct env() calls outside config/ may fail during deployment
  • Laravel Env Guard tool scans for unsafe env() usage
  • Review recommends comparing environment file inventories

A container per job, without a daemon

Some workloads want an image each. A build that needs a specific toolchain, a test that needs a specific database client, a task whose dependencies are described by something other than a human.

  • Individual images for each task without Docker or build daemon
  • Package list provided to tools like apko for reproducible image
  • Eliminates build step, malicious input risk, and build-time entropy

More from Thursday 20 August →