Skip to content

Add sandboxed network-isolated test execution#987

Open
Soner (shyim) wants to merge 8 commits intomainfrom
claude/sandbox-go-tests-ci-DcR0Y
Open

Add sandboxed network-isolated test execution#987
Soner (shyim) wants to merge 8 commits intomainfrom
claude/sandbox-go-tests-ci-DcR0Y

Conversation

@shyim
Copy link
Copy Markdown
Member

Summary

This change adds a new test execution script that runs Go tests in a sandboxed environment with network access disabled, ensuring tests don't depend on external network resources.

Key Changes

  • New script scripts/run-tests.sh: Implements platform-specific sandboxing for test execution

    • On macOS: Uses sandbox-exec with a sandbox profile to restrict network access
    • On Linux: Uses bubblewrap (bwrap) to create a network-isolated container
    • Pre-warms the build cache before entering the sandbox to avoid network fetches
    • Sets GOPROXY=off and -mod=readonly flags to fail fast on any cache misses
    • Supports custom test arguments (defaults to -v ./...)
  • Updated GitHub Actions workflow: Modified the test step to use the new sandboxed test script

    • Added bubblewrap installation for Linux runners
    • Changed test command from go test -v ./... to ./scripts/run-tests.sh

Implementation Details

  • The script uses set -euo pipefail for strict error handling
  • Properly resolves script and repository directories for portability
  • Validates required sandboxing tools are available before execution
  • Provides clear error messages for missing dependencies or unsupported platforms

https://claude.ai/code/session_01W1JE67zRHGp8JndUxGRDaF

Adds scripts/run-tests.sh that runs the Go test suite inside a
network-disabled sandbox: sandbox-exec with the existing
sandbox-no-network.sb profile on macOS, and bubblewrap with
--unshare-net on Linux. The GitHub Actions workflow installs
bubblewrap and invokes the script so any test that performs a
network call fails, matching the constraints of the NixOS build.
The sandboxed go test run has no network, so any uncompiled test
package or unresolved module would hang or fail with a confusing
proxy error. Pre-build all test binaries with `go test -run='^$'`
while the network is still available, then enter the sandbox with
GOPROXY=off and -mod=readonly so any cache miss surfaces as a fast,
explicit error rather than a network attempt.
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5bf54e3b0c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

Comment thread .github/workflows/go_test.yml Outdated
Comment thread scripts/run-tests.sh
echo "error: sandbox-exec not found" >&2
exit 1
fi
exec sandbox-exec -f "$REPO_DIR/sandbox-no-network.sb" go test "$@"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Enforce full network deny on macOS test sandbox

The Darwin branch executes sandbox-exec with sandbox-no-network.sb, but that profile currently allows network* and only denies outbound TCP on ports 80/443. This means tests can still access external services over other ports/protocols, so the new script does not actually provide the claimed "no network" isolation on macOS.

Useful? React with 👍 / 👎.

util-linux is preinstalled on every linux distro and on the github
ubuntu-latest runner, so `unshare --user --map-root-user --net`
gives us a netns-isolated test run with zero apt install. drops the
extra workflow step and avoids needing harden-runner allowlist
entries for ubuntu mirrors.
Ubuntu 24.04 (the ubuntu-latest image) ships with
kernel.apparmor_restrict_unprivileged_userns=1, which lets unshare
create the user namespace but blocks the uid_map write, so
`unshare --user --map-root-user --net` exits non-zero even though
the wrapped command ran successfully. Toggle the sysctl off before
the sandboxed test step so the wrapper exits cleanly.
Without an active lo interface, tests that use httptest.NewServer
(bound to 127.0.0.1) and UDP loopback (e.g. internal/tracking) fail
even though they make no external network calls. Match the nix-build
sandbox semantics by bringing lo up inside the namespace so only
external traffic is blocked.
Three classes of test failures showed up under the new sandboxed
test runner; this commit addresses the ones that need code changes
(loopback-only failures are handled by the runner script itself).

- internal/phplint, internal/esbuild: tests that fetch PHP wasm
  binaries or dart-sass from github.com now also skip when
  SHOPWARE_CLI_NO_NETWORK is set, alongside the existing NIX_CC
  skip. The sandbox script exports that variable.
- internal/extension/TestValidateTheme_ReadError: the test relied
  on a 0000-permission file to provoke a read error, which root
  bypasses. Replaced with a directory at the same path so
  os.ReadFile fails regardless of UID.
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.

2 participants