A tool adapter launches a local document converter. Its command contains a filename and an output option. The adapter uses an argument list, disables the shell and closes extra file descriptors. The converter can still receive the supervisor’s API credential through an inherited environment variable.

I would make the child environment part of the tool’s reviewed contract. A converter that needs no service credential should receive none through that channel, even when the coordinator needs several to do its own work.

This is ordinary Python process behaviour, not a reported Claude Code vulnerability. The fixture below starts real subprocesses under a synthetic supervisor environment. It compares four launch policies without passing your real environment into the deliberately weak cases. It makes no network calls.

Open the diagram at full size.

An argument list does not restrict environment inheritance

The Python subprocess.run documentation states that supplying env replaces the default inheritance of the current process environment. When env is None, the child inherits it.

Consider a coordinator with a model-service key in its environment:

subprocess.run(
    [converter_path, input_path],
    shell=False,
    close_fds=True,
    check=True,
)

There is no env argument. The converter receives the inherited environment even though the key appears nowhere in the command. The program need not be malicious to expose it later: a diagnostic dump or a child program launched by the converter could copy it again. Those are possible downstream paths, not behaviours demonstrated by this fixture.

shell=False avoids invoking a command shell for this launch. close_fds=True controls file-descriptor inheritance, subject to the documented standard-stream and platform rules. Neither option filters environment variables. The Python Popen reference describes these as separate arguments because they control separate inputs to the process.

This complements the earlier option-injection example. Keeping shell syntax out of a command does not settle how the executable interprets arguments, and it does not settle which credentials the executable receives.

Reproduce the inheritance without using a real secret

Download and inspect agent-subprocess-environment.py. It uses the standard library and targets POSIX systems such as macOS and Linux. Run it with Python 3.11 or later:

python3 agent-subprocess-environment.py
python3 agent-subprocess-environment.py --test
python3 -O agent-subprocess-environment.py --test

The outer launcher constructs a synthetic environment for a supervisor process. It includes DEMO_CONTROL_TOKEN=FAKE_SUPERVISOR_CREDENTIAL_9c72 and a second fake credential under DEMO_FUTURE_CREDENTIAL. No mapping is copied from the caller’s environment into this supervisor.

The supervisor launches a Python probe that reports only those two named fields and DEMO_TOOL_MODE. It never dumps the whole environment. The executable is selected through sys.executable, rather than searched for using an inherited PATH. The child runs with -I -S to reduce interference from Python startup configuration. Those flags do not prevent the running program from reading variables through os.environ.

The executed cases returned these results:

  • Default inheritance exposed both synthetic credentials to the probe.
  • Copying the environment and adding DEMO_TOOL_MODE=offline still exposed both credentials.
  • Removing the known DEMO_CONTROL_TOKEN hid that marker but left DEMO_FUTURE_CREDENTIAL visible.
  • Passing the explicit mapping hid both markers and retained the intended offline setting.

The explicit case produced:

{
  "observed": {
    "DEMO_CONTROL_TOKEN": null,
    "DEMO_FUTURE_CREDENTIAL": null,
    "DEMO_TOOL_MODE": "offline"
  },
  "parent_unchanged": true
}

Here null means that os.environ.get() did not find the named variable in the child. It does not mean that the child ran inside a sandbox or could not obtain credentials through another route.

Seven unique regression tests passed, including a second run with the test runner under Python optimisation. They cover the four policies, the approved tool setting, preservation of the supervisor’s marker and creation of a fresh policy dictionary for each call. The tests use unittest checks rather than removable assert statements. The probe subprocesses use the same fixed interpreter flags in both test runs.

Build the mapping rather than subtracting known secrets

This common pattern preserves everything the supervisor already holds:

env = {**os.environ, "DEMO_TOOL_MODE": "offline"}

It is an override, not a restricted environment. A denylist improves one known case but requires every future credential name to be recognised. The fixture’s second fake variable demonstrates that maintenance gap without relying on any particular cloud provider’s naming scheme.

For the supplied probe, the complete application policy is small:

def tool_environment():
    return {"LANG": "C", "DEMO_TOOL_MODE": "offline"}

The launcher passes that mapping as env=tool_environment(). This policy belongs to the example application. Python does not infer it from a tool description or an agent’s stated intent.

A real converter may need a locale, a writable temporary directory or a configuration path. Add each required value deliberately. Prefer a dedicated task directory and a reviewed configuration file to inheriting the operator’s home directory. If the tool launches helpers, specify how it resolves those helpers; an absolute path for the first executable does not constrain every later executable it starts.

Avoid temporarily deleting credentials from the coordinator’s global os.environ around a launch. That introduces shared mutable state into a concurrent service. Another thread could start a process during the temporary change, or observe the wrong configuration. Passing a separate mapping keeps the choice local to this subprocess call. The fixture verifies that its supervisor’s marker remains unchanged.

An allowlisted variable name also needs an approved value. Copying PATH, a plugin directory or a configuration location from untrusted input can give the child a different execution path while passing a name-only check. Review provenance as well as spelling.

A clean environment is one boundary, not process isolation

The child still runs with the operating-system identity used for the launch. Removing a credential variable does not revoke access to readable credential files, a mounted socket or a network identity endpoint. The fixture does not test those routes and does not restrict filesystem or network access.

For a production deployment, map those routes separately. A converter that can read the operator’s home directory may discover configuration without needing an explicit HOME variable. A tool that receives a credential file through a volume mount still has that credential after its environment is cleaned. Use the deployment’s identity, filesystem and network controls to enforce the intended boundary, then test those controls in that deployment.

The same caution applies to portability. Python documents additional environment requirements on Windows, including a valid SystemRoot for side-by-side assemblies. This POSIX fixture exits on other platforms rather than claiming its two-entry mapping is a universal launch policy. Test the actual runtime and executable before changing an installed service.

Some tools genuinely need authentication. Give those tools an explicit credential-delivery design with the intended audience, permissions and lifetime. Do not silently restore the coordinator’s whole environment because one integration fails to start. The MCP token-audience article covers a related service-boundary check; it does not make local environment inheritance safe by itself.

Make the launch contract testable

For each process-launching adapter, record the approved executable, arguments, working directory and environment policy. Keep credential values out of that record. Test required behaviour as well as absence: a converter that loses its required configuration and produces no output has not become a working tool merely because the canary disappeared.

Add a synthetic variable that the tool must never receive, then launch the real adapter under test. Inspect a purpose-built probe’s result, not a debug dump containing live secrets. Repeat with a newly named marker to catch code that strips one remembered key while copying everything else. Keep these tests separate from tests of filesystem access and service authorisation.

For the wider operating and review workflow, see my Claude Code book, available as a Kindle edition on Amazon UK.

Start with one subprocess that should need no credentials. Define its environment explicitly, run its normal functional test, and verify that a synthetic supervisor credential is absent inside the child.