Skip to content

Commit 892dd80

Browse files
kmichel-aivensentrivana
authored andcommitted
fix(integrations): don't send full env to subprocess
During the arguments modification to `subprocess.Popen.__init__`, an explicitly empty environment of `{}` is incorrectly confused with a `None` environment. This causes sentry to pass the entire environment of the parent process instead of sending just the injected environment variables. Fix it by only replacing the environment with `os.environ` if the variable is None, and not just falsy.
1 parent 51a906c commit 892dd80

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

sentry_sdk/integrations/stdlib.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@ def sentry_patched_popen_init(self, *a, **kw):
207207
for k, v in hub.iter_trace_propagation_headers(span):
208208
if env is None:
209209
env = _init_argument(
210-
a, kw, "env", 10, lambda x: dict(x or os.environ)
210+
a,
211+
kw,
212+
"env",
213+
10,
214+
lambda x: dict(x if x is not None else os.environ),
211215
)
212216
env["SUBPROCESS_" + k.upper().replace("-", "_")] = v
213217

tests/integrations/stdlib/test_subprocess.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ def test_subprocess_basic(
180180
assert sys.executable + " -c" in subprocess_init_span["description"]
181181

182182

183+
def test_subprocess_empty_env(sentry_init, monkeypatch):
184+
monkeypatch.setenv("TEST_MARKER", "should_not_be_seen")
185+
sentry_init(integrations=[StdlibIntegration()], traces_sample_rate=1.0)
186+
with start_transaction(name="foo"):
187+
args = [
188+
sys.executable,
189+
"-c",
190+
"import os; print(os.environ.get('TEST_MARKER', None))",
191+
]
192+
output = subprocess.check_output(args, env={}, text=True)
193+
assert "should_not_be_seen" not in output
194+
195+
183196
def test_subprocess_invalid_args(sentry_init):
184197
sentry_init(integrations=[StdlibIntegration()])
185198

0 commit comments

Comments
 (0)