Adds user_client.py with intentional smells (hardcoded secret, mutable default, bare except, eval) to validate PR-Agent auto /review on PR open.
Test plan
PR-Agent posts review automatically (no manual /review)
Sonar job runs on the PR
PR Type
Enhancement, Other
Description
Add demo module with intentional code smells
Hardcoded API key, mutable default, bare except, eval
Unused helper function with dead code
Diagram Walkthrough
flowchart LR
A["user_client.py"] --> B["Hardcoded API_KEY"]
A --> C["fetch_user()"]
C --> D["Mutable default cache"]
C --> E["String injection URL"]
C --> F["Bare except"]
C --> G["eval() on remote data"]
A --> H["unused_helper()"]
H --> I["Dead code (unused z)"]
File Walkthrough
Relevant files
Enhancement
user_client.py
Demo module with intentional code smells
user_client.py
Adds API_KEY hardcoded secret
Implements fetch_user() with mutable default, bare except, eval, and injection-prone URL
Adds unused_helper() with dead code (unused variable z)
Here are some key observations to aid the review process:
⏱️Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪No relevant tests
🔒Security concerns
Yes. Hardcoded API key (line 8) exposes credentials. eval() on line 28 allows arbitrary code execution from remote data. String concatenation for URL (line 17) enables injection attacks. Bare except (line 22) hides errors and may mask security issues.
Hardcoded API key sk-live-ticketlab-demo-KEY_NOT_FOR_PROD_9f3a2c is exposed in source code. This could lead to unauthorized access if the repository is public or accessed by unauthorized personnel.
eval(data) on line 28 executes arbitrary code from the HTTP response. If an attacker controls the response content, they can execute arbitrary Python code, leading to remote code execution.
payload=eval(data)# nosec B307 — intentional for PR-Agent
The cache parameter defaults to a mutable {} dictionary. All calls to fetch_user() share the same cache object, causing unexpected state persistence across calls.
## PR Reviewer Guide 🔍
#### (Review updated until commit https://gitea.app.andreferraro.com/andreferraro/TicketLab_MCP/commit/55c5e370e136554db1b1fd19e62ac4918d3e5c0d)
Here are some key observations to aid the review process:
<table>
<tr><td>⏱️ <strong>Estimated effort to review</strong>: 2 🔵🔵⚪⚪⚪</td></tr>
<tr><td>🧪 <strong>No relevant tests</strong></td></tr>
<tr><td>🔒 <strong>Security concerns</strong><br><br>
Yes. Hardcoded API key (line 8) exposes credentials. `eval()` on line 28 allows arbitrary code execution from remote data. String concatenation for URL (line 17) enables injection attacks. Bare except (line 22) hides errors and may mask security issues.</td></tr>
<tr><td>⚡ <strong>Recommended focus areas for review</strong><br><br>
<details><summary><a href='https://gitea.app.andreferraro.com/andreferraro/TicketLab_MCP/src/branch/feature/demo-pr-agent-findings/user_client.py#L8-L8'><strong>Security Vulnerability</strong></a>
Hardcoded API key `sk-live-ticketlab-demo-KEY_NOT_FOR_PROD_9f3a2c` is exposed in source code. This could lead to unauthorized access if the repository is public or accessed by unauthorized personnel.
</summary>
```python
API_KEY = "sk-live-ticketlab-demo-KEY_NOT_FOR_PROD_9f3a2c"
```
</details>
<details><summary><a href='https://gitea.app.andreferraro.com/andreferraro/TicketLab_MCP/src/branch/feature/demo-pr-agent-findings/user_client.py#L28-L28'><strong>Code Injection</strong></a>
`eval(data)` on line 28 executes arbitrary code from the HTTP response. If an attacker controls the response content, they can execute arbitrary Python code, leading to remote code execution.
</summary>
```python
payload = eval(data) # nosec B307 — intentional for PR-Agent
```
</details>
<details><summary><a href='https://gitea.app.andreferraro.com/andreferraro/TicketLab_MCP/src/branch/feature/demo-pr-agent-findings/user_client.py#L11-L11'><strong>Mutable Default Argument</strong></a>
The `cache` parameter defaults to a mutable `{}` dictionary. All calls to `fetch_user()` share the same cache object, causing unexpected state persistence across calls.
</summary>
```python
def fetch_user(user_id: str, cache: dict = {}): # noqa: B006 — intentional mutable default
```
</details>
</td></tr>
</table>
Why: Using eval() on remote content is a severe security risk (arbitrary code execution). The suggestion correctly recommends json.loads() as a safe alternative, making this a critical fix with maximum impact.
High
Prevent URL injection via encoding
String concatenation for URL construction is vulnerable to injection attacks. Use urllib.parse.urlencode() to safely encode query parameters and prevent path injection.
Why: The suggestion correctly identifies a critical security vulnerability (URL injection via string concatenation) and provides a safe alternative using urllib.parse.urlencode(). This is a high-impact security fix.
High
Possible issue
Handle exceptions specifically and log
Bare except Exception catches all exceptions silently, hiding errors and making debugging difficult. Catch specific exceptions like urllib.error.URLError and log the error instead of swallowing it.
-except Exception:
- # Intentional: bare except + swallow
+except urllib.error.URLError as e:
+ # Log the error for debugging
+ print(f"Failed to fetch user data: {e}")
data = None
Suggestion importance[1-10]: 7
__
Why: The suggestion correctly identifies the issue of a bare exception that silently swallows errors. While important for debugging and maintainability, it is less critical than the security issues addressed in suggestions 1 and 2.
Using eval() on remote data is extremely dangerous and can execute arbitrary code. Replace with a safe JSON parser like json.loads() to parse the response data.
Why: Using eval() on remote data is a severe security risk that can lead to arbitrary code execution. Replacing it with json.loads() is essential and correctly addresses the vulnerability.
High
Prevent injection via URL encoding
The URL is constructed using string concatenation with user input, which can lead to injection attacks. Use parameterized queries or URL encoding to safely handle the user_id parameter.
Why: The suggestion correctly identifies a critical security vulnerability (injection via string concatenation) and provides a safe alternative using URL encoding. This is a high-impact issue.
High
General
Avoid silent exception swallowing
Catching a bare Exception and silently swallowing the error hides potential issues. Log the exception or at least handle specific exceptions to improve debuggability.
-except Exception:
- # Intentional: bare except + swallow
+except Exception as e:
+ # Log the error for debugging
+ print(f"Error fetching user data: {e}")
data = None
Suggestion importance[1-10]: 7
__
Why: The suggestion correctly points out the issue of a bare except Exception that silently swallows errors, which hinders debugging. The proposed improvement adds logging, which is a good practice, though the impact is moderate compared to the security issues.
Medium
## PR Code Suggestions ✨
<!-- 55c5e37 -->
Latest suggestions up to 55c5e37
Explore these optional code suggestions:
<table><thead><tr><td><strong>Category</strong></td><td align=left><strong>Suggestion </strong></td><td align=center><strong>Impact</strong></td></tr><tbody><tr><td rowspan=2>Security</td>
<td>
<details><summary>Replace eval with safe parsing</summary>
___
**Using <code>eval()</code> on remote content is extremely dangerous and can lead to arbitrary code <br>execution. Replace with <code>json.loads()</code> to safely parse JSON data.**
[user_client.py [28]](https://gitea.app.andreferraro.com/andreferraro/TicketLab_MCP/src/branch/feature/demo-pr-agent-findings/user_client.py#L28-L28)
```diff
-payload = eval(data) # nosec B307 — intentional for PR-Agent
+payload = json.loads(data)
```
<details><summary>Suggestion importance[1-10]: 10</summary>
__
Why: Using `eval()` on remote content is a severe security risk (arbitrary code execution). The suggestion correctly recommends `json.loads()` as a safe alternative, making this a critical fix with maximum impact.
</details></details></td><td align=center>High
</td></tr><tr><td>
<details><summary>Prevent URL injection via encoding</summary>
___
**String concatenation for URL construction is vulnerable to injection attacks. Use <br><code>urllib.parse.urlencode()</code> to safely encode query parameters and prevent path <br>injection.**
[user_client.py [17]](https://gitea.app.andreferraro.com/andreferraro/TicketLab_MCP/src/branch/feature/demo-pr-agent-findings/user_client.py#L17-L17)
```diff
-url = "https://example.com/api/users?id=" + user_id + "&key=" + API_KEY
+params = urllib.parse.urlencode({"id": user_id, "key": API_KEY})
+url = f"https://example.com/api/users?{params}"
```
<details><summary>Suggestion importance[1-10]: 9</summary>
__
Why: The suggestion correctly identifies a critical security vulnerability (URL injection via string concatenation) and provides a safe alternative using `urllib.parse.urlencode()`. This is a high-impact security fix.
</details></details></td><td align=center>High
</td></tr><tr><td rowspan=1>Possible issue</td>
<td>
<details><summary>Handle exceptions specifically and log</summary>
___
**Bare <code>except Exception</code> catches all exceptions silently, hiding errors and making <br>debugging difficult. Catch specific exceptions like <code>urllib.error.URLError</code> and log <br>the error instead of swallowing it.**
[user_client.py [22-24]](https://gitea.app.andreferraro.com/andreferraro/TicketLab_MCP/src/branch/feature/demo-pr-agent-findings/user_client.py#L22-L24)
```diff
-except Exception:
- # Intentional: bare except + swallow
+except urllib.error.URLError as e:
+ # Log the error for debugging
+ print(f"Failed to fetch user data: {e}")
data = None
```
<details><summary>Suggestion importance[1-10]: 7</summary>
__
Why: The suggestion correctly identifies the issue of a bare exception that silently swallows errors. While important for debugging and maintainability, it is less critical than the security issues addressed in suggestions 1 and 2.
</details></details></td><td align=center>Medium
</td></tr></tr></tbody></table>
___
#### Previous suggestions
<details><summary>Suggestions up to commit 55c5e37</summary>
<br><table><thead><tr><td><strong>Category</strong></td><td align=left><strong>Suggestion </strong></td><td align=center><strong>Impact</strong></td></tr><tbody><tr><td rowspan=2>Security</td>
<td>
<details><summary>Replace eval with safe parsing</summary>
___
**Using eval() on remote data is extremely dangerous and can execute arbitrary code. <br>Replace with a safe JSON parser like json.loads() to parse the response data.**
[user_client.py [28]](https://gitea.app.andreferraro.com/andreferraro/TicketLab_MCP/src/branch/feature/demo-pr-agent-findings/user_client.py#L28-L28)
```diff
-payload = eval(data) # nosec B307 — intentional for PR-Agent
+import json
+payload = json.loads(data)
```
<details><summary>Suggestion importance[1-10]: 10</summary>
__
Why: Using `eval()` on remote data is a severe security risk that can lead to arbitrary code execution. Replacing it with `json.loads()` is essential and correctly addresses the vulnerability.
</details></details></td><td align=center>High
</td></tr><tr><td>
<details><summary>Prevent injection via URL encoding</summary>
___
**The URL is constructed using string concatenation with user input, which can lead to <br>injection attacks. Use parameterized queries or URL encoding to safely handle the <br>user_id parameter.**
[user_client.py [17]](https://gitea.app.andreferraro.com/andreferraro/TicketLab_MCP/src/branch/feature/demo-pr-agent-findings/user_client.py#L17-L17)
```diff
-url = "https://example.com/api/users?id=" + user_id + "&key=" + API_KEY
+import urllib.parse
+params = urllib.parse.urlencode({"id": user_id, "key": API_KEY})
+url = f"https://example.com/api/users?{params}"
```
<details><summary>Suggestion importance[1-10]: 9</summary>
__
Why: The suggestion correctly identifies a critical security vulnerability (injection via string concatenation) and provides a safe alternative using URL encoding. This is a high-impact issue.
</details></details></td><td align=center>High
</td></tr><tr><td rowspan=1>General</td>
<td>
<details><summary>Avoid silent exception swallowing</summary>
___
**Catching a bare Exception and silently swallowing the error hides potential issues. <br>Log the exception or at least handle specific exceptions to improve debuggability.**
[user_client.py [22-24]](https://gitea.app.andreferraro.com/andreferraro/TicketLab_MCP/src/branch/feature/demo-pr-agent-findings/user_client.py#L22-L24)
```diff
-except Exception:
- # Intentional: bare except + swallow
+except Exception as e:
+ # Log the error for debugging
+ print(f"Error fetching user data: {e}")
data = None
```
<details><summary>Suggestion importance[1-10]: 7</summary>
__
Why: The suggestion correctly points out the issue of a bare `except Exception` that silently swallows errors, which hinders debugging. The proposed improvement adds logging, which is a good practice, though the impact is moderate compared to the security issues.
</details></details></td><td align=center>Medium
</td></tr></tr></tbody></table>
</details>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
User description
Summary
user_client.pywith intentional smells (hardcoded secret, mutable default, bare except,eval) to validate PR-Agent auto/reviewon PR open.Test plan
/review)PR Type
Enhancement, Other
Description
Add demo module with intentional code smells
Hardcoded API key, mutable default, bare except, eval
Unused helper function with dead code
Diagram Walkthrough
File Walkthrough
user_client.py
Demo module with intentional code smellsuser_client.py
API_KEYhardcoded secretfetch_user()with mutable default, bare except, eval, andinjection-prone URL
unused_helper()with dead code (unused variablez)PR Reviewer Guide 🔍
(Review updated until commit
55c5e370e1)Here are some key observations to aid the review process:
Yes. Hardcoded API key (line 8) exposes credentials.
eval()on line 28 allows arbitrary code execution from remote data. String concatenation for URL (line 17) enables injection attacks. Bare except (line 22) hides errors and may mask security issues.Security Vulnerability
Hardcoded API key
sk-live-ticketlab-demo-KEY_NOT_FOR_PROD_9f3a2cis exposed in source code. This could lead to unauthorized access if the repository is public or accessed by unauthorized personnel.Code Injection
eval(data)on line 28 executes arbitrary code from the HTTP response. If an attacker controls the response content, they can execute arbitrary Python code, leading to remote code execution.Mutable Default Argument
The
cacheparameter defaults to a mutable{}dictionary. All calls tofetch_user()share the same cache object, causing unexpected state persistence across calls.PR Code Suggestions ✨
Latest suggestions up to
55c5e37Explore these optional code suggestions:
Replace eval with safe parsing
Using
eval()on remote content is extremely dangerous and can lead to arbitrary codeexecution. Replace with
json.loads()to safely parse JSON data.user_client.py [28]
Suggestion importance[1-10]: 10
__
Why: Using
eval()on remote content is a severe security risk (arbitrary code execution). The suggestion correctly recommendsjson.loads()as a safe alternative, making this a critical fix with maximum impact.Prevent URL injection via encoding
String concatenation for URL construction is vulnerable to injection attacks. Use
urllib.parse.urlencode()to safely encode query parameters and prevent pathinjection.
user_client.py [17]
Suggestion importance[1-10]: 9
__
Why: The suggestion correctly identifies a critical security vulnerability (URL injection via string concatenation) and provides a safe alternative using
urllib.parse.urlencode(). This is a high-impact security fix.Handle exceptions specifically and log
Bare
except Exceptioncatches all exceptions silently, hiding errors and makingdebugging difficult. Catch specific exceptions like
urllib.error.URLErrorand logthe error instead of swallowing it.
user_client.py [22-24]
Suggestion importance[1-10]: 7
__
Why: The suggestion correctly identifies the issue of a bare exception that silently swallows errors. While important for debugging and maintainability, it is less critical than the security issues addressed in suggestions 1 and 2.
Previous suggestions
Suggestions up to commit
55c5e37Replace eval with safe parsing
Using eval() on remote data is extremely dangerous and can execute arbitrary code.
Replace with a safe JSON parser like json.loads() to parse the response data.
user_client.py [28]
Suggestion importance[1-10]: 10
__
Why: Using
eval()on remote data is a severe security risk that can lead to arbitrary code execution. Replacing it withjson.loads()is essential and correctly addresses the vulnerability.Prevent injection via URL encoding
The URL is constructed using string concatenation with user input, which can lead to
injection attacks. Use parameterized queries or URL encoding to safely handle the
user_id parameter.
user_client.py [17]
Suggestion importance[1-10]: 9
__
Why: The suggestion correctly identifies a critical security vulnerability (injection via string concatenation) and provides a safe alternative using URL encoding. This is a high-impact issue.
Avoid silent exception swallowing
Catching a bare Exception and silently swallowing the error hides potential issues.
Log the exception or at least handle specific exceptions to improve debuggability.
user_client.py [22-24]
Suggestion importance[1-10]: 7
__
Why: The suggestion correctly points out the issue of a bare
except Exceptionthat silently swallows errors, which hinders debugging. The proposed improvement adds logging, which is a good practice, though the impact is moderate compared to the security issues.Persistent review updated to latest commit
55c5e370e1Pull request closed