Skip to content

Commit 1227b89

Browse files
committed
update
1 parent 99c945a commit 1227b89

File tree

2 files changed

+135
-21
lines changed

2 files changed

+135
-21
lines changed

security/data_exfiltration_detection.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Detecting potential data exfiltration caused by Python programs is important.
55
:::{danger}
66
Detecting Data Exfiltration in Python Code that Uses Telemetry, Remote Analytics, and SaaS Integrations
77

8-
An essential step in **mitigating security risks**.
8+
This is an essential step in **mitigating security risks**.
99
:::
1010

1111

@@ -73,6 +73,6 @@ Every external API endpoint is a potential point of failure.
7373

7474
Data sent to a third party is only as secure as their defenses.
7575

76-
- Loss of Custody: Once data leaves your perimeter, you lose the ability to protect it.
76+
- **Loss of Custody**: Once data leaves your perimeter, you lose the ability to protect it.
7777
- **Transparency Gaps:** You are dependent on the provider to detect and report breaches—a process that often takes months.
7878

Lines changed: 133 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
11
# How to Check for Data Exfiltration
22

3-
**[Python Code Audit](https://nocomplexity.com/codeaudit/)** includes functionality to detect potential data exfiltration risks. This feature is available through:
3+
## The Challenge
44

5-
- the [CLI interface](userguide), and
5+
Modern Python applications often interact with external services such as logging platforms, cloud APIs, analytics systems, and AI services. While these integrations provide valuable functionality, they can also introduce data exfiltration risks.
66

7-
- the [API](apidocs/modules).
7+
Data exfiltration occurs when sensitive information leaves the application and is transmitted to external systems without proper controls.
88

9-
Using the CLI
9+
Examples include:
1010

11-
The egress detection function can be activated with the following command:
12-
```bash
13-
codeaudit filescan <pythonfile|package-name|directory> [OUTPUTFILE]
14-
```
11+
- Sending application data to external monitoring services
1512

16-
**Report Output**
13+
- Transmitting logs containing sensitive metadata
1714

18-
In the generated HTML report, each analysed file is evaluated for potential data exfiltration to external services.
19-
20-
If a potential risk is detected, the report will display:
21-
> *&#9888;&#65039; External Egress Risk: Detected outbound connection logic or API keys that may facilitate data egress.*
15+
- Uploading files to cloud storage services
2216

23-
The report also highlights the exact lines of code that triggered the detection.
17+
- Sending prompts or data to external AI APIs
2418

25-
If no external egress risks are identified, the report will display:
26-
> *&#x2705; No logic for connecting to remote services found. Risk of data exfiltration to external systems is low.*
19+
## The Threat
2720

28-
:::{important}
29-
No tool can provide 100% guarantees. This applies to Python Code Audit as well as to any other security analysis tool.
30-
:::
21+
External service integrations can expose applications to several security risks depending on the type of service used.
3122

3223
Data egress exposes Python applications to a wide range of security threats and risks:
3324

@@ -41,6 +32,55 @@ Data egress exposes Python applications to a wide range of security threats and
4132
| Communication Gateways | Financial Risk / Phishing | Twilio, SendGrid, Slack Webhooks| 🔴 High |
4233

4334

35+
## Vulnerable Code Example
36+
37+
A common source of data exfiltration risk is code that sends application data to external services without proper validation or filtering.
38+
39+
```python
40+
import requests
41+
import os
42+
43+
API_KEY = os.getenv("API_KEY")
44+
45+
def send_user_data(user_data):
46+
url = "https://external-service.example/api/upload"
47+
48+
payload = {
49+
"api_key": API_KEY,
50+
"data": user_data
51+
}
52+
53+
requests.post(url, json=payload)
54+
```
55+
56+
Why this is risky
57+
58+
This code transmits potentially sensitive information to an external API:
59+
60+
- User data is sent without validation
61+
62+
- External endpoint communication is hardcoded
63+
64+
- API credentials are used directly
65+
66+
- There is no monitoring or restriction of outbound traffic
67+
68+
[Python Code Audit](https://nocomplexity.com/codeaudit/) (and some other SAST tools) can detect these patterns and flag them as potential egress risks.
69+
70+
71+
## Secure Mitigation
72+
73+
To mitigate these risks, security reviewers must flag and investigate all outbound logic. Using the Python Code Audit CLI, you can audit your files or packages with a single command when using Python Code Audit. Python Code Audit includes an egress detection feature that scans source code for potential outbound communication and external service integrations.
74+
75+
Use the following command:
76+
77+
```bash
78+
codeaudit filescan <pythonfile|package-name|directory> [OUTPUTFILE]
79+
```
80+
81+
The tool analyzes the specified file, package, or directory and generates an HTML security report.
82+
83+
4484
:::{admonition} High-risk integrations are a key focus for [Python Code Audit](https://nocomplexity.com/codeaudit/) egress detection!
4585
:class: danger, dropdown
4686

@@ -56,3 +96,77 @@ The following categories represent common classes of external service integratio
5696

5797
:::
5898

99+
100+
Interpreting the Results
101+
The generated HTML report provides immediate feedback on your egress posture:
102+
103+
104+
If a potential risk is detected, the report will display:
105+
> *&#9888;&#65039; External Egress Risk: Detected outbound connection logic or API keys that may facilitate data egress.*
106+
107+
The report also highlights the exact lines of code that triggered the detection.
108+
109+
If no external egress risks are identified, the report will display:
110+
> *&#x2705; No logic for connecting to remote services found. Risk of data exfiltration to external systems is low.*
111+
112+
113+
To reduce the risk of data exfiltration, apply the following security practices.
114+
115+
**1 .Restrict outbound communication**
116+
117+
Limit which services your application can contact.
118+
119+
Examples:
120+
121+
- Adjust or improve the Python Code!
122+
- Network egress filtering
123+
124+
- Firewall rules
125+
126+
- API allowlists
127+
128+
**2. Avoid sending sensitive data externally**
129+
130+
Ensure that external APIs do not receive:
131+
132+
- Personal data
133+
134+
- Secrets / Credentials
135+
136+
- Internal system metadata
137+
138+
- Implement data filtering and redaction before transmission.
139+
140+
:::{important}
141+
No tool can provide 100% guarantees. This applies to Python Code Audit as well as to any other security analysis tool.
142+
:::
143+
144+
## Discussion
145+
146+
Detecting potential data exfiltration paths is an important part of secure software development.
147+
148+
While external integrations are often necessary, they must be carefully reviewed to ensure:
149+
150+
- sensitive data is not transmitted unintentionally
151+
152+
- credentials are handled securely
153+
154+
- outbound communication is controlled
155+
156+
- third-party services are trusted and properly configured
157+
158+
Automated analysis tools such as Python Code Audit help developers and security teams identify these risks early in the development lifecycle.
159+
160+
However, automated scanning should always be combined with:
161+
162+
- manual code reviews
163+
164+
- architecture analysis
165+
166+
- runtime monitoring
167+
168+
Together, these practices significantly reduce the risk of unintentional data leakage from Python applications.
169+
170+
171+
------
172+

0 commit comments

Comments
 (0)