bito-code-review[bot] commented on code in PR #40746:
URL: https://github.com/apache/superset/pull/40746#discussion_r3408736733
##########
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>CWE-117: Inconsistent Sanitization</b></div>
<div id="fix">
The `roles` list items use `escape_llm_context_delimiters()` while
`first_name`/`last_name` use `sanitize_for_llm_context()`. These produce
different output formats — one wraps strings in `<UNTRUSTED-CONTENT>`
delimiters, the other only escapes existing delimiters. Per [11852], all code
paths returning user data must apply identical formatting. Apply the same
sanitization strategy to all string fields consistently. (See also:
[CWE-117](https://cwe.mitre.org/data/definitions/117.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 (lines 305-320) ---
305: roles: list[str] | None = None
306: if include_sensitive and include_roles:
307: user_roles = getattr(user, "roles", None)
308: if user_roles is not None:
309: roles = []
310: for r in user_roles:
311: try:
312: if hasattr(r, "name") and isinstance(r.name, str):
313: + # Use same sanitization as
first_name/last_name for consistency
314: + roles.append(sanitize_for_llm_context(r.name,
field_path=("roles",)))
315: -
roles.append(escape_llm_context_delimiters(r.name))
316: except (AttributeError, DetachedInstanceError):
317: logger.debug(
318: "Skipping role that raised exception in
serialize_user_object"
319: )
320: continue
```
</div>
</details>
</div>
<small><i>Code Review Run #53b259</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]