Skip to content

Commit a9def2f

Browse files
authored
fix: support nested function calls in cache_key_wrapper (apache#38569)
1 parent 27197fa commit a9def2f

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

superset/jinja_context.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ class ExtraCache:
122122
# be added to the cache key.
123123
regex = re.compile(
124124
r"(\{\{|\{%)[^{}]*?("
125-
r"current_user_id\([^()]*\)|"
126-
r"current_username\([^()]*\)|"
127-
r"current_user_email\([^()]*\)|"
128-
r"current_user_rls_rules\([^()]*\)|"
129-
r"current_user_roles\([^()]*\)|"
130-
r"cache_key_wrapper\([^()]*\)|"
131-
r"url_param\([^()]*\)"
125+
r"current_user_id\([^)]*\)|"
126+
r"current_username\([^)]*\)|"
127+
r"current_user_email\([^)]*\)|"
128+
r"current_user_rls_rules\([^)]*\)|"
129+
r"current_user_roles\([^)]*\)|"
130+
r"cache_key_wrapper\([^)]*\)|"
131+
r"url_param\([^)]*\)"
132132
r")"
133133
r"[^{}]*?(\}\}|\%\})"
134134
)

tests/unit_tests/jinja_context_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,3 +1693,24 @@ def test_undefined_template_variable_not_function(mocker: MockerFixture) -> None
16931693
template = "SELECT {{ undefined_variable.some_method() }}"
16941694
with pytest.raises(UndefinedError):
16951695
processor.process_template(template)
1696+
1697+
1698+
@pytest.mark.parametrize(
1699+
("sql", "expected"),
1700+
[
1701+
("SELECT {{ cache_key_wrapper(abc) }}", True),
1702+
("SELECT {{ cache_key_wrapper(myfunc()) }}", True),
1703+
("SELECT {{ url_param('foo') }}", True),
1704+
("SELECT {{ url_param(get_param('foo')) }}", True),
1705+
("SELECT {{ current_user_id() }}", True),
1706+
("SELECT {{ current_username() }}", True),
1707+
("SELECT {{ current_user_email() }}", True),
1708+
("SELECT {{ current_user_roles() }}", True),
1709+
("SELECT {{ current_user_rls_rules() }}", True),
1710+
("SELECT 'cache_key_wrapper(abc)' AS false_positive", False),
1711+
("SELECT 1", False),
1712+
("SELECT '{{ 1 + 1 }}'", False),
1713+
],
1714+
)
1715+
def test_extra_cache_regex(sql: str, expected: bool) -> None:
1716+
assert bool(ExtraCache.regex.search(sql)) is expected

0 commit comments

Comments
 (0)