CVE-2024-40647
Executive Summary
CVE-2024-40647 is a medium-severity information disclosure vulnerability in the official Sentry Python SDK (sentry-sdk) affecting versions prior to 2.8.0. The flaw resides in the Stdlib integration, which is enabled by default. When developers explicitly set env={} in Python's subprocess calls to prevent environment variable leakage to child processes, the Sentry SDK overrides this security measure, causing all environment variables to be passed to subprocesses. This can lead to accidental exposure of sensitive data such as API keys, database credentials, and cloud provider tokens.
This record — and 170,000+ others — are available via the ValtersIT API. One endpoint replaces five: NVD + GitHub CVE + CISA KEV + EPSS + Nuclei + Exploit-DB, unified.
No signup required
Supply Chain & Package Security
This vulnerability affects the PyPI package sentry-sdk. Organizations using this SDK in production environments should immediately upgrade to version 2.8.0 or later. The package is widely used for error tracking and performance monitoring, making this a supply chain risk for any Python application integrating Sentry.
CVSS Breakdown & Threat Level
- CVSS Score: 5.3 (Medium)
- Vector: CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:N/A:N
- Attack Vector: Local (requires ability to execute code on the system)
- Attack Complexity: Low
- Privileges Required: Low
- User Interaction: None
- Scope: Unchanged
- Confidentiality Impact: High (environment variables exposed)
- Integrity Impact: None
- Availability Impact: None
Deep Dive & Mechanics
The vulnerability is rooted in how the Sentry SDK's Stdlib integration wraps Python's subprocess.Popen and related functions. When a developer calls subprocess.Popen with env={}, the intention is to create a child process with an empty environment. However, the Sentry SDK's instrumentation intercepts this call and, due to a bug, does not respect the empty env dictionary. Instead, it passes the parent process's full environment to the child process.
The bug occurs because the SDK's wrapper code incorrectly merges the provided env with the parent's environment, rather than using the provided env as-is. This is a classic case of improper handling of default arguments in Python.
Vulnerable vs. Patched Code
Vulnerable Code (sentry-sdk < 2.8.0):
# Simplified representation of the bug
def instrument_subprocess():
original_popen = subprocess.Popen
def sentry_popen(*args, **kwargs):
env = kwargs.get('env', os.environ)
# Bug: env is merged with os.environ even if explicitly set to {}
if env is not None:
env = {**os.environ, **env}
kwargs['env'] = env
return original_popen(*args, **kwargs)
subprocess.Popen = sentry_popen
Patched Code (sentry-sdk >= 2.8.0):
# Corrected implementation
def instrument_subprocess():
original_popen = subprocess.Popen
def sentry_popen(*args, **kwargs):
# Respect the user's explicit env setting
if 'env' in kwargs and kwargs['env'] is not None:
env = kwargs['env']
else:
env = os.environ
kwargs['env'] = env
return original_popen(*args, **kwargs)
subprocess.Popen = sentry_popen
Exploitation & Lab Setup
Red Team
Prerequisites:
- Python environment with sentry-sdk < 2.8.0 installed
- Ability to execute Python code on the target system
Exploitation Steps:
- Create a Python script that uses
subprocesswithenv={}:
import subprocess
import sentry_sdk
sentry_sdk.init("https://examplePublicKey@o0.ingest.sentry.io/0")
# Intended to run with empty environment
proc = subprocess.Popen(['env'], env={}, stdout=subprocess.PIPE)
output = proc.communicate()[0].decode()
print(output) # Will show all parent environment variables
- The output will contain all environment variables from the parent process, including sensitive data like
AWS_SECRET_ACCESS_KEY,DATABASE_URL, etc.
Blue Team
Detection:
- Monitor for unexpected environment variable leakage in logs or monitoring systems
- Check for child processes that should not have inherited environment variables
- Audit all Python applications using sentry-sdk for version < 2.8.0
Mitigation:
- Upgrade to sentry-sdk >= 2.8.0 immediately
- If upgrade is not possible, disable all default integrations:
import sentry_sdk
sentry_sdk.init("...", default_integrations=False)
Indicators of Compromise (IOCs)
- File: Python scripts using
subprocess.Popenwithenv={}while sentry-sdk < 2.8.0 is loaded - Process: Child processes unexpectedly inheriting parent environment variables
- Log: Sentry SDK logs showing environment variable capture from subprocesses
- Network: Unusual outbound connections from child processes that should have no network access
Detection Engineering
YARA Rule
rule sentry_sdk_env_leak_cve_2024_40647 {
meta:
description = "Detects vulnerable sentry-sdk versions (< 2.8.0) in Python packages"
author = "Valters IT"
date = "2024-01-01"
cve = "CVE-2024-40647"
strings:
$sentry_init = "sentry_sdk.init" ascii wide nocase
$version_check = "__version__" ascii wide
$version_2_7 = "2.7" ascii wide
$version_2_6 = "2.6" ascii wide
$version_2_5 = "2.5" ascii wide
condition:
$sentry_init and ($version_check and ($version_2_7 or $version_2_6 or $version_2_5))
}
Sigma Rule
title: CVE-2024-40647 - Sentry SDK Environment Variable Leak
id: 8a9b7c6d-5e4f-3a2b-1c0d-9e8f7a6b5c4d
status: experimental
description: Detects child processes that unexpectedly inherit parent environment variables due to vulnerable sentry-sdk
logsource:
category: process_creation
product: windows
detection:
selection:
ParentImage|endswith: '\python.exe'
CommandLine|contains: 'env'
condition: selection
falsepositives:
- Legitimate use of subprocess with env={} on patched SDK
level: medium
Suricata Rule
alert tcp $HOME_NET any -> $EXTERNAL_NET any (msg:"CVE-2024-40647 - Sentry SDK Environment Variable Leak"; flow:to_server,established; content:"sentry_sdk"; nocase; content:"env"; nocase; within:100; reference:cve,2024-40647; classtype:attempted-recon; sid:1000001; rev:1;)
Remediation & Mitigation
Immediate Actions:
- Upgrade sentry-sdk to version 2.8.0 or later:
pip install --upgrade sentry-sdk
- If upgrade is not possible, disable all default integrations:
import sentry_sdk
sentry_sdk.init("...", default_integrations=False)
Long-term Measures:
- Implement runtime environment variable monitoring
- Use secrets management solutions (e.g., HashiCorp Vault) instead of environment variables
- Conduct regular dependency audits with tools like
pip-auditorsafety - Apply principle of least privilege to all subprocess calls
Detection:
- Monitor for unexpected environment variable access in child processes
- Review Sentry SDK logs for unusual subprocess activity
- Implement network segmentation to limit blast radius of leaked credentials
Valters IT
Senior DevSecOps & Cyber Threat Intelligence Expert
This report is generated for educational and defensive purposes only.