bito-code-review[bot] commented on code in PR #40746:
URL: https://github.com/apache/superset/pull/40746#discussion_r3495571999


##########
superset/mcp_service/user/schemas.py:
##########
@@ -277,10 +304,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 for performance</b></div>
   <div id="fix">
   
   The try-except block inside the loop in `serialize_user_object` at lines 
312-316 incurs performance overhead. Similar pattern exists at lines 125-132.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ````suggestion
                   if hasattr(r, "name"):
                       try:
                           name = r.name
                           if isinstance(name, str):
                               roles.append(escape_llm_context_delimiters(name))
                       except DetachedInstanceError:
                           logger.debug(
                               "Skipping role with detached instance in 
serialize_user_object",
                           )
   ````
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #b65788</i></small>
   </div><div>
   
   
   <div id="suggestion">
   <div id="issue"><b>CWE-117: Double Sanitization of Output</b></div>
   <div id="fix">
   
   `escape_llm_context_delimiters` is applied to `r.name` here (line 311) AND 
again by the `_extract_role_names` validator at lines 121-127 when `UserInfo` 
is constructed. Since the delimiter-escaping function is not idempotent over 
its own output, a role name containing `<|endofmessage|>` gets escaped to 
`[ESCAPED-...]` in serialize_user_object then escaped again to 
`[ESCAPED-[ESCAPED-...]]` by the validator — mangling the role name. Remove the 
call here and let the validator own this responsibility. 
([CWE-117](https://cwe.mitre.org/data/definitions/117.html))
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #10604e</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]

Reply via email to