bito-code-review[bot] commented on code in PR #40746:
URL: https://github.com/apache/superset/pull/40746#discussion_r3417549219
##########
superset/mcp_service/user/schemas.py:
##########
@@ -277,10 +305,16 @@ def serialize_user_object(
if include_sensitive and include_roles:
user_roles = getattr(user, "roles", None)
if user_roles is not None:
- try:
- roles = [r.name for r in user_roles if hasattr(r, "name")]
- except (AttributeError, DetachedInstanceError):
- roles = None
+ roles = []
+ for r in user_roles:
+ try:
+ if hasattr(r, "name") and isinstance(r.name, str):
+ roles.append(escape_llm_context_delimiters(r.name))
+ except (AttributeError, DetachedInstanceError):
+ logger.debug(
+ "Skipping role that raised exception in
serialize_user_object"
+ )
+ continue
Review Comment:
<div>
<div id="suggestion">
<div id="issue"><b>Move try-except outside loop</b></div>
<div id="fix">
Move the `try-except` block outside the loop to avoid performance overhead.
Restructure to iterate only over roles (PERF203 issue).
</div>
<details>
<summary>
<b>Code suggestion</b>
</summary>
<blockquote>Check the AI-generated fix before applying</blockquote>
<div id="code">
````suggestion
try:
roles = [
escape_llm_context_delimiters(r.name)
for r in user_roles
if hasattr(r, "name") and isinstance(r.name, str)
]
except (AttributeError, DetachedInstanceError):
logger.debug(
"Skipping role that raised exception in
serialize_user_object"
)
roles = []
````
</div>
</details>
</div>
<small><i>Code Review Run #b88a15</i></small>
</div><div>
<div id="suggestion">
<div id="issue"><b>CWE-78: Missing sanitization for roles</b></div>
<div id="fix">
Role names use `escape_llm_context_delimiters` but skip the wrapping
delimiters that `sanitize_for_llm_context` adds. Compare lines 312 vs 322-327:
`first_name` gets wrapped in `<UNTRUSTED-CONTENT>` tags but `roles` do not.
Since role names are user-controlled database strings passed to LLMs, they
require identical wrapping to prevent prompt injection. (See also:
[CWE-78](https://cwe.mitre.org/data/definitions/78.html))
</div>
<details>
<summary>
<b>Code suggestion</b>
</summary>
<blockquote>Check the AI-generated fix before applying</blockquote>
<div id="code">
```
--- superset/mcp_service/user/schemas.py
+++ superset/mcp_service/user/schemas.py
@@ -309,7 +309,7 @@
for r in user_roles:
try:
if hasattr(r, "name") and isinstance(r.name, str):
- roles.append(escape_llm_context_delimiters(r.name))
+ roles.append(sanitize_for_llm_context(r.name,
field_path=("roles",)))
except (AttributeError, DetachedInstanceError):
logger.debug(
"Skipping role that raised exception in
serialize_user_object"
```
</div>
</details>
</div>
<details>
<summary><b>Citations</b></summary>
<ul>
<li>
Rule Violated: <a
href="https://github.com/apache/superset/blob/f245722/AGENTS.md#L79">AGENTS.md:79</a>
</li>
</ul>
</details>
<small><i>Code Review Run #d86eca</i></small>
</div>
---
Should Bito avoid suggestions like this for future reviews? (<a
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
- [ ] Yes, avoid them
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]