fix(mcp): coerce Role ORM objects to role names in UserInfo schema
Fixes #40733
Problem
Multiple MCP tools (get_dataset_info, get_chart_info, generate_chart) fail with a Pydantic validation error when the calling user has Superset roles assigned:
2 validation errors for UserInfo
roles.0
Input should be a valid string [type=string_type, input_value=Admin, input_type=Role]
The UserInfo model uses from_attributes=True, so Pydantic maps user.roles directly. With the SQLAlchemy ORM, roles returns Role objects—not strings—causing serialization to fail.
Fix
Add a field_validator("roles", mode=before) to UserInfo that coerces each item:
- If already a
str, keep it - If it has a
.nameattribute (Role ORM object), extractstr(item.name)
This is idempotent and backward-compatible.
Impact
- Fixes false-negative responses from MCP tools
- Prevents retry storms from LLM clients that interpret the error as a failure
generate_chartwithsave_chart=trueno longer silently saves while reporting failure
Codecov Report
:x: Patch coverage is 14.28571% with 24 lines in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 64.69%. Comparing base (fbc2495) to head (19ccf20).
:warning: Report is 2 commits behind head on master.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| superset/mcp_service/user/schemas.py | 14.28% | 24 Missing :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## master #40746 +/- ##
==========================================
- Coverage 64.70% 64.69% -0.02%
==========================================
Files 2701 2701
Lines 149839 149863 +24
Branches 34497 34504 +7
==========================================
- Hits 96958 96955 -3
- Misses 51106 51130 +24
- Partials 1775 1778 +3
| Flag | Coverage Δ | |
|---|---|---|
| hive | 39.15% <14.28%> (-0.01%) |
:arrow_down: |
| mysql | 57.67% <14.28%> (-0.02%) |
:arrow_down: |
| postgres | 57.73% <14.28%> (-0.02%) |
:arrow_down: |
| presto | 40.67% <14.28%> (-0.01%) |
:arrow_down: |
| python | 59.13% <14.28%> (-0.02%) |
:arrow_down: |
| sqlite | 57.32% <14.28%> (-0.02%) |
:arrow_down: |
| unit | 100.00% <ø> (ø) |
Flags with carried forward coverage won't be shown. Click here to find out more.
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
Pushed an update to the _extract_role_names validator:
- Bare strings are now rejected instead of being split into characters.
- Empty role lists are preserved as
[]rather than collapsed toNone. DetachedInstanceErroronRole.nameis caught per-item and skipped.- Added unit tests covering string rejection, empty-list preservation, role-object coercion, detached-instance handling, and the
serialize_user_object->UserInforound trip.
Let me know if anything else looks off.
Deploy Preview for superset-docs-preview ready!
| Name | Link |
|---|---|
| Latest commit | f4c29901c7f22b8b0d69cb0b2218dd0c77011896 |
| Latest deploy log | https://app.netlify.com/projects/superset-docs-preview/deploys/6a4d2f89b92be1000700ac39 |
| Deploy Preview | https://deploy-preview-40746--superset-docs-preview.netlify.app |
| Preview on mobile | Toggle QR Code...Use your smartphone camera to open QR code link. |
| Make changes | Run an agent on this branch |
To edit notification comments on pull requests, go to your Netlify project configuration.
Good catch on the spacing. Added the missing blank line between the validator and the next field.
Fixed the ruff E402 issue aminghadersohi flagged — moved logger = logging.getLogger(__name__) to after all imports.
Code Review Agent Run #aa437d
Actionable Suggestions - 1
-
tests/unit_tests/mcp_service/user/test_schemas.py - 1
- Semantic duplication in test setup · Line 65-87
Review Details
-
Files reviewed - 2 · Commit Range:
14a3da4..7259d57- superset/mcp_service/user/schemas.py
- tests/unit_tests/mcp_service/user/test_schemas.py
-
Files skipped - 0
-
Tools
- Whispers (Secret Scanner) - ✔︎ Successful
- Detect-secrets (Secret Scanner) - ✔︎ Successful
- MyPy (Static Code Analysis) - ✔︎ Successful
- Astral Ruff (Static Code Analysis) - ✔︎ Successful
Bito Usage Guide
Commands
Type the following command in the pull request comment and save the comment.
-
/review- Manually triggers a full AI review. -
/pause- Pauses automatic reviews on this pull request. -
/resume- Resumes automatic reviews. -
/resolve- Marks all Bito-posted review comments as resolved. -
/abort- Cancels all in-progress reviews.
Refer to the documentation for additional commands.
Configuration
This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].
Documentation & Help
Fixed — roles now use sanitize_for_llm_context with the same field_path=("roles",) wrapping as first_name/last_name, so all user fields are consistent in LLM context output.
Added tests for the remaining parameter combinations: include_roles=False, include_sensitive=False, and falsy user inputs (None, 0, empty string). These cover the three paths Bito flagged as missing.
Hi @aminghadersohi, thanks for the approval! It looks like the CI workflows haven't been triggered yet for the latest commit. Could you take a look when you have a chance? Thanks!
Bito Automatic Review Failed - Technical Failure
Bito encountered technical difficulties while generating code feedback . To retry, type /review in a comment and save. If the issue persists, contact [email protected] and provide the following details:
Agent Run ID: 21861dca-42a6-4162-a374-f05cddb3229e
Code Review Agent Run #d86eca
Actionable Suggestions - 1
-
superset/mcp_service/user/schemas.py - 1
- CWE-78: Missing sanitization for roles · Line 308-317
Additional Suggestions - 1
-
tests/unit_tests/mcp_service/user/test_schemas.py - 1
-
Inconsistent escaping between code paths · Line 41-50The `test_user_info_coerces_role_objects_to_names` test uses plain ASCII role names. If `UserInfo._extract_role_names` (schemas.py line 126) does not escape role names but `serialize_user_object` does (schemas.py line 312), the two paths produce different `UserInfo.roles` outputs for the same input.
-
Review Details
-
Files reviewed - 2 · Commit Range:
9e5c824..f245722- superset/mcp_service/user/schemas.py
- tests/unit_tests/mcp_service/user/test_schemas.py
-
Files skipped - 0
-
Tools
- Whispers (Secret Scanner) - ✔︎ Successful
- Detect-secrets (Secret Scanner) - ✔︎ Successful
- MyPy (Static Code Analysis) - ✔︎ Successful
- Astral Ruff (Static Code Analysis) - ✔︎ Successful
Bito Usage Guide
Commands
Type the following command in the pull request comment and save the comment.
-
/review- Manually triggers a full AI review. -
/pause- Pauses automatic reviews on this pull request. -
/resume- Resumes automatic reviews. -
/resolve- Marks all Bito-posted review comments as resolved. -
/abort- Cancels all in-progress reviews.
Refer to the documentation for additional commands.
Configuration
This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].
Documentation & Help
Fixed both issues raised by Bito:
-
Added
escape_llm_context_delimiters()to_extract_role_namesvalidator so both the Pydantic coercion path and theserialize_user_objectpath produce consistently escaped role names. -
Added
test_user_info_escapes_malicious_role_namesto verify that role names containing LLM context delimiters are properly escaped.
Approving CI, but note that each update of the branch requires us to re-approve CI and might slow things down 😓
⚠️ Translation Regression Detected
A source change in this PR renamed or reworded strings, invalidating existing translations (they are now #, fuzzy) in ar, it, ko, pt. Please resolve the affected .po files before merging.
Note: intentionally deleting a translatable string is not a regression and is not flagged here — only translations invalidated by a renamed/reworded source string are.
| Language | Fuzzy before | Fuzzy after | New |
|---|---|---|---|
ar |
1130 | 1133 | +3 |
it |
1660 | 1662 | +2 |
ko |
1514 | 1516 | +2 |
pt |
1835 | 1837 | +2 |
How to fix
1. Install dependencies (if not already set up):
pip install -r superset/translations/requirements.txt
sudo apt-get install gettext # or: brew install gettext
2. Re-extract strings and sync .po files:
./scripts/translations/babel_update.sh
This rewrites superset/translations/messages.pot from the current source files and merges the changes into every .po file. Strings whose msgid changed will be marked #, fuzzy.
3. Resolve the fuzzy entries in the affected language files (ar, it, ko, pt):
grep -n '#, fuzzy' superset/translations/<lang>/LC_MESSAGES/messages.po
For each fuzzy entry, either rewrite the msgstr to match the new string and remove the #, fuzzy line, or clear the msgstr to "" if you cannot provide a translation.
4. Commit your changes to the .po files.
Hi @aminghadersohi, thanks for the approval! It looks like the CI workflows haven't been triggered yet for the latest commit. Could you take a look when you have a chance? Thanks!
We have to approve CI each time the PR is edited. You're rebasing and force-pushing a LOT here, which is causing Bito to re-review it a ton, and if CI-auto ran, this PR would cost ASF infra a fortune.
Feel free to push fixes as often as needed, but the rebases might not be needed so often :P
Deleted a ton of noise due to rebasing from the thread, and will run CI now. Please hold if you want to see how it completes :D
Fixed the current CI failures on the latest push: ruff formatting/import ordering in the user schema tests, and the ECharts query event handler support needed by Echart.test.tsx and the TypeScript check.
Verified locally:
ruff format --check/ruff checkon the changed Python filesnpm run test -- --shard=5/8 --coverageReporters=jsonnpm run plugins:build && npm run type
The pull_request workflows are currently waiting for maintainer approval to run after this push.
Looks like some pretty worrisome bot review comments above... can we avoid those test and functionality regressions?
Running CI again, but would still appreciate comments on the open review comments above. Seems like you might have addressed them, but I'll give it a more thorough review when CI is (hopefully) green.
Running CI again. @aminghadersohi you might want to give this another review pass, including any open/unaddressed bot comments.