Skip to content

Superset no longer accepts custom jinja template functions for caching #33775

Closed

Description

@neuromantik33

Bug description

There is a regression in the PR fix(Jinja): Extra cache keys to consider vars with set #30549 . I mentioned it as a comment within the PR but I guess since it's merged I'm filing a proper bug report.

This PR breaks are existing usage of RLS (thank heavens one for our campus users notified us 😅 ). This is our input string:

campus_id IN UNNEST( {{ cache_key_wrapper(current_user_campus_ids()) }} )

which doesn't match using the pattern : cache_key_wrapper\([^()]*\),
but does using the old one (v4.0.2): cache_key_wrapper\(.*\).

The function we use looks as follows:

def current_user_campus_ids() -> list[int]:
    """
    Get the list of campus ids associated with the current user
    """
    user: FortyTwoUser = g.user
    return user.campus_ids()

and is populated using the oauth2 jwt from keycloak.
Is the only workaround to monkey patch the jinja ExtraCache processor to add our macro within? How is the cache_key_wrapper() to be used if it can't invoke another function?

Screenshots/recordings

No response

Superset version

4.1.2

Python version

3.10

Node version

I don't know

Browser

Chrome

Additional context

No response

Checklist

  • I have searched Superset docs and Slack and didn't find a solution to my problem.
  • I have searched the GitHub issue tracker and didn't find a similar bug report.
  • I have checked Superset's logs for errors and if I found a relevant Python stacktrace, I included it here as text in the "additional context" section.

Activity

  1. added
    authentication:row-level-security
    global:jinja
    on Jun 15, 2025
  2. dosubot commented on Jun 15, 2025

    @dosubot

    This regression is due to a change in the regex used by Superset to detect cache key wrappers in Jinja templates—it now only allows simple, non-nested expressions like cache_key_wrapper(foo) and blocks nested calls like cache_key_wrapper(current_user_campus_ids()) that worked in previous versions. This breaks use cases like yours for RLS and dynamic user-based cache keys. There’s currently no official workaround or extensibility for this in the code or docs, and no open issues or discussions about it in the repo.

    Monkey patching the Jinja ExtraCache processor is the only available workaround if you need to restore the previous behavior, since the regex restriction is hardcoded and there’s no config or plugin hook for extending cache key computation. If you go this route, be aware it’s an unsupported hack and could break with future updates. Otherwise, you’d need to refactor your templates to avoid nested calls, but that may not be possible for your use case.

    For reference, here’s the relevant implementation and restriction in the code:
    cache_key_wrapper and regex in ExtraCache
    And the intended usage in the docs:
    SQL templating and cache_key_wrapper

    If you need to keep your current pattern, monkey patching is your only option until the project provides a more flexible or documented solution.

    To reply, just mention @dosu.


    Docs are dead. Just use Dosu.

    Leave Feedback Ask Dosu about superset Share Dosu with your team

  3. neuromantik33 commented on Jun 17, 2025

    @neuromantik33
    Author

    @dosu Ok that is great but according to the docs you mentioned :

    Explicitly Including Values in Cache Key
    The {{ cache_key_wrapper() }} function explicitly instructs Superset to add a value to the accumulated list of values used in the calculation of the cache key.
    This function is only needed when you want to wrap your own custom function return values in the cache key.

    If I can't include a function in the cache_key_wrapper what exactly is it wrapping? I don't know how to call a function without including parentheses unless it's a property accessor. Can you at least agree that the regexp for cache_key_wrapper should be a little more lenient?

  4. dosubot commented on Jun 17, 2025

    @dosubot

    You're right—there's a disconnect between the docs and the current implementation. The docs say cache_key_wrapper() is for wrapping custom function return values, but the regex in the code now only allows simple, non-nested expressions (like a single variable or property accessor) inside cache_key_wrapper, not function calls with parentheses or any nested expressions. So, in practice, you can't use it to wrap the result of a function call like current_user_campus_ids() anymore—only something like cache_key_wrapper(variable) will work, which is pretty limiting for dynamic use cases like yours.

    I agree that the regex should be more lenient to match the documented intent and real-world needs. Right now, the only way around this is to monkey patch the ExtraCache processor or revert the regex change, since there's no config or extension point for this part of the logic. No relevant discussions or plans to relax the regex have come up in the repo or docs so far.

    If you need this flexibility, advocating for a change to the regex or opening a discussion about restoring support for nested calls would be reasonable next steps. For now, the restriction is hardcoded and not aligned with the documentation or common Jinja usage patterns.
    Reference: implementation and regex restriction
    Reference: docs on intended usage

    To reply, just mention @dosu.


    Share context across your team and agents. Try Dosu.

    Leave Feedback Learn about superset with Dosu Add Dosu to your team

  5. rusackas commented on Jun 24, 2025

    @rusackas
    Member

    Pinging @geido (the author) and @villebro (the reviewer) of the PR in question, for good measure.

  6. added
    cares:preset
    on Jun 24, 2025
  7. neuromantik33 commented on Jul 16, 2025

    @neuromantik33
    Author

    Any news on this issue? This is a pretty big security regression IMHO, I guess I'll have to resort to monkeypatching the cache_key_wrapper..

  8. rusackas commented on Mar 11, 2026

    @rusackas
    Member

    Can anyone from the core team or Preset provide an update on whether accepting custom Jinja template functions for caching is planned to be restored? The reporter noted this is a security-relevant regression. @rusackas / @betodealmeida — any guidance appreciated.

  9. villebro commented on Mar 11, 2026

    @villebro
    Member

    Let me take a look

  10. villebro commented on Mar 11, 2026

    @villebro
    Member

    I agree agree this is a regression. A workaround would be to split this in two parts, which seems unnecessary:

    {% set abc = myfunc() %}
    {{ cache_key_wrapper(abc) }}

    is the same as

    {{ cache_key_wrapper(myfunc()) }}

    I can open a PR to fix this.

  11. sfirke commented on Nov 12, 2026

    @sfirke
    Member

    Since this issue just got mentioned in the Contributing to Superset webinar, I'm commenting. The most optimistic thing I can say is that this issue going many months without a response is indicative of how thing were on the repo 1+ years ago. Compared to then, we now have many fewer open issues (<200, mostly fresh) vs. since 2022 when I got involved in the project, we usually had 500+ open issues, mostly stale and neglected.

    To the actual question on the webinar, you're absolutely welcome to open a PR immediately open opening an issue. And I think that's a nice pattern, to document the bug separately from the fix, even if you have the fix ready. But I'm hopeful this kind of thing happens less often now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

authentication:row-level-securitycares:presetglobal:jinja

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions