-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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
fix: prevent guest user from modifying metrics #26749
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
import pytest | ||
from pytest_mock import MockFixture | ||
|
||
from superset.common.query_object import QueryObject | ||
from superset.exceptions import SupersetSecurityException | ||
from superset.extensions import appbuilder | ||
from superset.security.manager import SupersetSecurityManager | ||
|
@@ -31,6 +32,81 @@ def test_security_manager(app_context: None) -> None: | |
assert sm | ||
|
||
|
||
def test_raise_for_access_guest_user( | ||
mocker: MockFixture, | ||
app_context: None, | ||
) -> None: | ||
""" | ||
Test that guest user can't modify chart payload. | ||
""" | ||
sm = SupersetSecurityManager(appbuilder) | ||
mocker.patch.object(sm, "is_guest_user", return_value=True) | ||
mocker.patch.object(sm, "can_access", return_value=True) | ||
|
||
query_context = mocker.MagicMock() | ||
query_context.slice_.id = 42 | ||
stored_metrics = [ | ||
{ | ||
"aggregate": None, | ||
"column": None, | ||
"datasourceWarning": False, | ||
"expressionType": "SQL", | ||
"hasCustomLabel": False, | ||
"label": "COUNT(*) + 1", | ||
"optionName": "metric_ssa1gwimio_cxpyjc7vj3s", | ||
"sqlExpression": "COUNT(*) + 1", | ||
} | ||
] | ||
query_context.slice_.params_dict = { | ||
"metrics": stored_metrics, | ||
} | ||
|
||
# normal request | ||
query_context.form_data = { | ||
"slice_id": 42, | ||
"metrics": stored_metrics, | ||
} | ||
query_context.queries = [QueryObject(metrics=stored_metrics)] # type: ignore | ||
sm.raise_for_access(query_context=query_context) | ||
|
||
# tampered requests | ||
query_context.form_data = { | ||
"slice_id": 43, | ||
"metrics": stored_metrics, | ||
} | ||
query_context.queries = [QueryObject(metrics=stored_metrics)] # type: ignore | ||
with pytest.raises(SupersetSecurityException): | ||
sm.raise_for_access(query_context=query_context) | ||
|
||
tampered_metrics = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @betodealmeida Not a blocker, but I think it's a good practice to separate different test cases in different tests. That way, it's easier to identify which part of a feature is failing. In this case, we could have a test for a legit request and another for a tampered request. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, good point. |
||
{ | ||
"aggregate": None, | ||
"column": None, | ||
"datasourceWarning": False, | ||
"expressionType": "SQL", | ||
"hasCustomLabel": False, | ||
"label": "COUNT(*) + 2", | ||
"optionName": "metric_ssa1gwimio_cxpyjc7vj3s", | ||
"sqlExpression": "COUNT(*) + 2", | ||
} | ||
] | ||
|
||
query_context.form_data = { | ||
"slice_id": 42, | ||
"metrics": tampered_metrics, | ||
} | ||
with pytest.raises(SupersetSecurityException): | ||
sm.raise_for_access(query_context=query_context) | ||
|
||
query_context.form_data = { | ||
"slice_id": 42, | ||
"metrics": stored_metrics, | ||
} | ||
query_context.queries = [QueryObject(metrics=tampered_metrics)] # type: ignore | ||
with pytest.raises(SupersetSecurityException): | ||
sm.raise_for_access(query_context=query_context) | ||
|
||
|
||
def test_raise_for_access_query_default_schema( | ||
mocker: MockFixture, | ||
app_context: None, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar question as here: https://github.com/apache/superset/pull/26748/files#diff-30f4c6ffdcb1f78a9e1ebbb60e1f297b379c181534d5a185a4cd37b1b16ac6f8R301
Does this raise a 5xx error? I think these are generally user-generated, so we would want to know about them, but be able to distinguish them from a system error. Is that correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this logic impact queries to fetch filter values? Also, are we concerned with the dimensions as well (or un-aggregated columns for table charts)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good point. Let me verify!