Skip to content

fix(embedding): add optional dataset allowlist to guest tokens#39302

Open
sha174n wants to merge 6 commits intoapache:masterfrom
sha174n:feat/guest-token-dataset-allowlist
Open

fix(embedding): add optional dataset allowlist to guest tokens#39302
sha174n wants to merge 6 commits intoapache:masterfrom
sha174n:feat/guest-token-dataset-allowlist

Conversation

@sha174n
Copy link
Copy Markdown
Contributor

@sha174n sha174n commented Apr 12, 2026

SUMMARY

Adds an optional datasets field to the guest token API and JWT claims, giving embedding operators fine-grained control over which datasets a guest user can access.

How it works:

  • When datasets is omitted from the token request, all datasets linked to the embedded dashboard remain accessible — exactly the existing behaviour (full backwards compatibility).
  • When datasets is provided (e.g. [7, 8]), raise_for_access() enforces it as a strict allowlist: any datasource not in the list raises SupersetSecurityException. An explicit empty list ([]) blocks all datasource access.

Implementation details:

  • GuestToken TypedDict split into required-fields base (_GuestTokenRequired) + optional-fields extension (GuestToken, total=False) so the new field is typed correctly as present-or-absent rather than Optional
  • create_guest_access_token() accepts datasets: Optional[list[int]] = None; the claim is included in the JWT only when the value is not None
  • GuestTokenCreateSchema exposes a nullable datasets: List[Integer] field (load_default=None)
  • Allowlist check is appended to the existing guest datasource gate in raise_for_access()

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

N/A — API-only change

TESTING INSTRUCTIONS

# Run the new unit tests
pytest tests/unit_tests/security/test_guest_token_dataset_allowlist.py -v

Manual API test:

  1. Create a guest token without datasets → embedded dashboard loads all charts normally
  2. Create a guest token with datasets: [<id>] → only charts using that dataset load; others return 403
  3. Create a guest token with datasets: [] → all charts return 403

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration
  • Introduces new feature or API

🤖 Generated with Claude Code

@bito-code-review
Copy link
Copy Markdown
Contributor

bito-code-review bot commented Apr 12, 2026

Code Review Agent Run #6df525

Actionable Suggestions - 0
Review Details
  • Files reviewed - 4 · Commit Range: f32ea0b..f32ea0b
    • superset/security/api.py
    • superset/security/guest_token.py
    • superset/security/manager.py
    • tests/unit_tests/security/test_guest_token_dataset_allowlist.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 evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

@github-actions github-actions bot added the api Related to the REST API label Apr 12, 2026
@dosubot dosubot bot added the authentication Related to authentication label Apr 12, 2026
Comment thread superset/security/guest_token.py
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 12, 2026

Codecov Report

❌ Patch coverage is 65.00000% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.41%. Comparing base (3e25f02) to head (2f50df6).

Files with missing lines Patch % Lines
superset/security/manager.py 56.25% 5 Missing and 2 partials ⚠️

❌ Your project check has failed because the head coverage (99.81%) is below the target coverage (100.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #39302      +/-   ##
==========================================
- Coverage   64.45%   64.41%   -0.04%     
==========================================
  Files        2555     2553       -2     
  Lines      132721   132573     -148     
  Branches    30802    30752      -50     
==========================================
- Hits        85539    85400     -139     
+ Misses      45696    45684      -12     
- Partials     1486     1489       +3     
Flag Coverage Δ
hive 39.95% <20.00%> (-0.01%) ⬇️
mysql 60.59% <65.00%> (-0.01%) ⬇️
postgres 60.68% <65.00%> (-0.01%) ⬇️
presto 41.74% <30.00%> (-0.01%) ⬇️
python 62.26% <65.00%> (-0.01%) ⬇️
sqlite 60.31% <65.00%> (-0.01%) ⬇️
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.

Introduces a `datasets` field to the guest token JWT and the
`/api/v1/security/guest_token/` API schema.

When `datasets` is present in the token, `raise_for_access()` enforces it
as a strict allowlist — only datasource IDs listed in the claim are
accessible to the embedded guest.  When the field is absent the behaviour
is unchanged: all datasets linked to the dashboard remain accessible,
preserving full backwards compatibility.

Changes:
- `GuestToken` TypedDict split into required base + optional extension so
  the new field is correctly typed as `list[int] | absent`
- `create_guest_access_token()` accepts an optional `datasets` kwarg and
  includes it in the JWT claims only when provided
- `GuestTokenCreateSchema` exposes a nullable `datasets` list field
- `raise_for_access()` checks the claim when present and raises
  `SupersetSecurityException` for any datasource not in the allowlist
- 8 new unit tests covering JWT claim presence/absence, schema loading,
  and allowlist enforcement (allow, block, empty-list blocks all)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sha174n sha174n force-pushed the feat/guest-token-dataset-allowlist branch from f32ea0b to 75cf184 Compare April 12, 2026 16:38
@sha174n sha174n changed the title feat(security): add optional dataset allowlist to guest tokens fix(embedding): add optional dataset allowlist to guest tokens Apr 12, 2026
Comment thread superset/security/api.py Outdated
Comment thread superset/security/api.py Outdated
Comment thread superset/security/manager.py Outdated
… g.user

`g.user` is set by Flask-Login on every real request but is absent in unit
test contexts that only push an app context.  Switch to `getattr(g, "user",
None)` so the method returns None gracefully instead of raising AttributeError,
matching the expected semantics of "return the guest user if one exists".

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@bito-code-review
Copy link
Copy Markdown
Contributor

bito-code-review bot commented Apr 12, 2026

Code Review Agent Run #d5d8ca

Actionable Suggestions - 0
Review Details
  • Files reviewed - 4 · Commit Range: 75cf184..660ab62
    • superset/security/api.py
    • superset/security/guest_token.py
    • superset/security/manager.py
    • tests/unit_tests/security/test_guest_token_dataset_allowlist.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 evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

- get_current_guest_user_if_guest: use isinstance(GuestUser) instead of
  trusting is_guest_user(), fixing AttributeError when tests mock
  is_guest_user but g.user is a regular FAB User object
- api.py: use body.get("user", {}) to avoid KeyError when user field omitted
- api.py: pass datasets kwarg conditionally to preserve backwards compat
  with custom security manager overrides that lack the parameter
- manager.py: use isinstance(allowed_datasets, list) guard to avoid
  TypeError on malformed JWT claims
- guest_token.py: add docstring to _GuestTokenRequired

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@bito-code-review
Copy link
Copy Markdown
Contributor

bito-code-review bot commented Apr 12, 2026

Code Review Agent Run #fcd79f

Actionable Suggestions - 0
Review Details
  • Files reviewed - 3 · Commit Range: 660ab62..1b3c4f5
    • superset/security/api.py
    • superset/security/guest_token.py
    • superset/security/manager.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 evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

…non-guest path

- Test get_current_guest_user_if_guest() directly for both the GuestUser
  and regular-user branches (previously only exercised through mocks)
- Test that the dataset allowlist block is skipped entirely for
  non-guest users, covering the is_guest_user()=False branch

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@bito-code-review
Copy link
Copy Markdown
Contributor

bito-code-review bot commented Apr 12, 2026

Code Review Agent Run #3efc24

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: 1b3c4f5..0f707a3
    • tests/unit_tests/security/test_guest_token_dataset_allowlist.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 evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

…type guard for allowlist

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@bito-code-review
Copy link
Copy Markdown
Contributor

bito-code-review bot commented Apr 14, 2026

Code Review Agent Run #053ca6

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: 0f707a3..ed04988
    • superset/security/manager.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 evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

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 size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant