Skip to content

feat(security): add guest user attributes and get_guest_user_attribute() macro #33924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Yash2412
Copy link

SUMMARY

Add support for user attributes in guest tokens and a corresponding Jinja macro get_guest_user_attribute() to enable dynamic user-specific data filtering in embedded dashboards.

Changes include:

  • Add attributes field to GuestTokenUser TypedDict and GuestUser class
  • Implement get_guest_user_attribute(attribute_name, default=None) Jinja macro
  • Update guest token API schema to accept and validate user attributes
  • Integrate attributes with cache key mechanism for proper cache invalidation
  • Comprehensive test coverage for new functionality

This enables embedded dashboard use cases like multi-tenant data filtering, role-based access control, and regional data restrictions by allowing applications to pass user-specific attributes when creating guest tokens.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

  1. Test Guest Token Creation with Attributes:

    # Create guest token with user attributes
    user = {
        "username": "test_user",
        "attributes": {
            "tenant": "company_123",
            "region": "us-west"
        }
    }
    resources = [{"type": "dashboard", "id": "test-dashboard-uuid"}]
    token = security_manager.create_guest_access_token(user, resources, [])
  2. Test Jinja Macro in SQL Lab:

    • Login as admin and go to SQL Lab
    • Create a test dataset with query:
    SELECT 
        '{{ get_guest_user_attribute("tenant", "default") }}' as tenant,
        '{{ get_guest_user_attribute("region", "global") }}' as region
    • Verify it returns "default" and "global" for non-guest users
  3. Test with Guest Token:

    • Use created guest token to access embedded dashboard
    • Verify SQL queries using the macro resolve to correct attribute values
    • Test cache key generation includes attribute values
  4. Test Backward Compatibility:

    • Create guest token without attributes field
    • Verify existing functionality works unchanged
    • Verify macro returns default values when attributes not present
  5. Run Test Suite:

    python -m pytest tests/integration_tests/security_tests.py::TestGuestTokens -v
    python -m pytest tests/unit_tests/jinja_context_test.py -v

ADDITIONAL INFORMATION

  • Has associated issue: #33922
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

Files Modified:

  • superset/security/guest_token.py - Add attributes field to GuestTokenUser and GuestUser
  • superset/security/api.py - Update UserSchema validation
  • superset/jinja_context.py - Add get_guest_user_attribute() macro
  • tests/integration_tests/security_tests.py - Guest token tests
  • tests/unit_tests/jinja_context_test.py - Jinja macro tests

Backward Compatibility: Fully backward compatible - attributes field is optional with graceful fallbacks.

@github-actions github-actions bot added api Related to the REST API doc Namespace | Anything related to documentation labels Jun 26, 2025
@dosubot dosubot bot added authentication Related to authentication global:jinja Related to Jinja templating labels Jun 26, 2025
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congrats on making your first PR and thank you for contributing to Superset! 🎉 ❤️

We hope to see you in our Slack community too! Not signed up? Use our Slack App to self-register.

Copy link

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Status
Security SQL Injection Vulnerability in Guest User Attributes ▹ view 🧠 Incorrect
Files scanned
File Path Reviewed
superset/security/guest_token.py
superset/security/api.py
docs/docs/configuration/sql-templating.mdx
superset/jinja_context.py

Explore our documentation to understand the languages and file types we support and the files we ignore.

Check out our docs on how you can make Korbit work best for you and your team.

Loving Korbit!? Share us on LinkedIn Reddit and X

Copy link

codecov bot commented Jun 27, 2025

Codecov Report

Attention: Patch coverage is 20.83333% with 19 lines in your changes missing coverage. Please review.

Project coverage is 72.87%. Comparing base (e25be0f) to head (eff9b17).
Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
superset/jinja_context.py 9.52% 19 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #33924       +/-   ##
===========================================
+ Coverage        0   72.87%   +72.87%     
===========================================
  Files           0      559      +559     
  Lines           0    40471    +40471     
  Branches        0     4256     +4256     
===========================================
+ Hits            0    29492    +29492     
- Misses          0     9875     +9875     
- Partials        0     1104     +1104     
Flag Coverage Δ
hive 47.17% <20.83%> (?)
mysql 71.86% <20.83%> (?)
postgres 71.91% <20.83%> (?)
presto 50.94% <20.83%> (?)
python 72.83% <20.83%> (?)
sqlite 71.45% <20.83%> (?)
unit 100.00% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sadpandajoe
Copy link
Member

@Yash2412 thanks for opening a SIP for this. Here are the instruction for the SIP process before this PR can be merged: #5602. If @mistercrunch or @michael-s-molina does not think this requires a SIP, we can go through our regular PR process to get this merged.

@Yash2412 Yash2412 force-pushed the feat/user-attribute-in-guest-token branch from 8516495 to 110aaa0 Compare June 30, 2025 10:55
@Yash2412 Yash2412 force-pushed the feat/user-attribute-in-guest-token branch from 110aaa0 to eff9b17 Compare July 8, 2025 09:40
@Yash2412 Yash2412 requested a review from mistercrunch July 8, 2025 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to the REST API authentication Related to authentication doc Namespace | Anything related to documentation global:jinja Related to Jinja templating hold:sip! size/XXL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants