superset icon indicating copy to clipboard operation
superset copied to clipboard

fix(mcp): coerce Role ORM objects to role names in UserInfo schema

Open goingforstudying-ctrl opened this issue 6 months ago • 62 comments

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 .name attribute (Role ORM object), extract str(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_chart with save_chart=true no longer silently saves while reporting failure

goingforstudying-ctrl avatar Jun 03 '26 23:06 goingforstudying-ctrl

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.

codecov[bot] avatar Jun 05 '26 17:06 codecov[bot]

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 to None.
  • DetachedInstanceError on Role.name is 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 -> UserInfo round trip.

Let me know if anything else looks off.

goingforstudying-ctrl avatar Jun 05 '26 22:06 goingforstudying-ctrl

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...

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.

netlify avatar Jun 05 '26 22:06 netlify

Good catch on the spacing. Added the missing blank line between the validator and the next field.

goingforstudying-ctrl avatar Jun 06 '26 03:06 goingforstudying-ctrl

Fixed the ruff E402 issue aminghadersohi flagged — moved logger = logging.getLogger(__name__) to after all imports.

goingforstudying-ctrl avatar Jun 10 '26 20:06 goingforstudying-ctrl

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

AI Code Review powered by Bito Logo

bito-code-review[bot] avatar Jun 10 '26 22:06 bito-code-review[bot]

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.

goingforstudying-ctrl avatar Jun 11 '26 21:06 goingforstudying-ctrl

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.

goingforstudying-ctrl avatar Jun 13 '26 07:06 goingforstudying-ctrl

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!

goingforstudying-ctrl avatar Jun 14 '26 10:06 goingforstudying-ctrl

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

bito-code-review[bot] avatar Jun 16 '26 16:06 bito-code-review[bot]

Code Review Agent Run #d86eca

Actionable Suggestions - 1
  • superset/mcp_service/user/schemas.py - 1
Additional Suggestions - 1
  • tests/unit_tests/mcp_service/user/test_schemas.py - 1
    • Inconsistent escaping between code paths · Line 41-50
      The `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

AI Code Review powered by Bito Logo

bito-code-review[bot] avatar Jun 16 '26 21:06 bito-code-review[bot]

Fixed both issues raised by Bito:

  1. Added escape_llm_context_delimiters() to _extract_role_names validator so both the Pydantic coercion path and the serialize_user_object path produce consistently escaped role names.

  2. Added test_user_info_escapes_malicious_role_names to verify that role names containing LLM context delimiters are properly escaped.

goingforstudying-ctrl avatar Jun 17 '26 04:06 goingforstudying-ctrl

Approving CI, but note that each update of the branch requires us to re-approve CI and might slow things down 😓

rusackas avatar Jun 23 '26 18:06 rusackas

⚠️ 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.

github-actions[bot] avatar Jun 24 '26 12:06 github-actions[bot]

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

rusackas avatar Jun 24 '26 17:06 rusackas

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

rusackas avatar Jun 24 '26 17:06 rusackas

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 check on the changed Python files
  • npm run test -- --shard=5/8 --coverageReporters=json
  • npm run plugins:build && npm run type

The pull_request workflows are currently waiting for maintainer approval to run after this push.

goingforstudying-ctrl avatar Jun 24 '26 19:06 goingforstudying-ctrl

Looks like some pretty worrisome bot review comments above... can we avoid those test and functionality regressions?

rusackas avatar Jun 25 '26 06:06 rusackas

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.

rusackas avatar Jun 27 '26 06:06 rusackas

Running CI again. @aminghadersohi you might want to give this another review pass, including any open/unaddressed bot comments.

rusackas avatar Jun 30 '26 17:06 rusackas