Skip to content
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(chart-data-api): assert referenced columns are present in datasource #10451

Merged
merged 5 commits into from
Aug 14, 2020

Conversation

villebro
Copy link
Member

@villebro villebro commented Jul 28, 2020

SUMMARY

Currently it might be possible to perform limited SQL injection via both the legacy and new chart data API by manipulating the columns, groupby, filters or metrics properties of QueryObject. This plugs that hole both on the new and legacy chart data API.

Further work is required to ensure other fields aren't vulnerable to SQL injection. However, to keep the PR size manageable I suggest limiting the scope of this PR to fields with direct column references and adding more assertions later to the where and having fields + SQL type ad-hoc metrics.

TEST PLAN

  • CI
  • new tests
  • manual testing - clicked through all examples dashboards and didn't notice any chart breakage

ADDITIONAL INFORMATION

Copy link
Member

@willbarrett willbarrett left a comment

Choose a reason for hiding this comment

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

LGTM once CI passes

@villebro
Copy link
Member Author

villebro commented Aug 3, 2020

@willbarrett I'll add a few more assertions and improve the tests, should be done in a few days.

@villebro villebro force-pushed the villebro/assert-column-names branch from 23b59b7 to 24fd909 Compare August 7, 2020 13:44
@pull-request-size pull-request-size bot added size/L and removed size/M labels Aug 7, 2020
@villebro villebro force-pushed the villebro/assert-column-names branch from 909549c to 57aaed8 Compare August 10, 2020 06:14
Comment on lines +93 to +105
column_names = [
"created_on",
"changed_on",
"id",
"start_dttm",
"end_dttm",
"layer_id",
"short_descr",
"long_descr",
"json_metadata",
"created_by_fk",
"changed_by_fk",
]
Copy link
Member Author

Choose a reason for hiding this comment

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

These had to be added, as AnnotationDatasources don't have any defined columns.

@villebro villebro changed the title fix(chart-data-api): assert requested columns are present in datasource fix(chart-data-api): assert referenced columns are present in datasource Aug 10, 2020
payload["queries"][0]["groupby"] = ["currentDatabase()"]
query_context = ChartDataQueryContextSchema().load(payload)
query_payload = query_context.get_payload()
assert query_payload[0].get("error") is not None
Copy link
Member

Choose a reason for hiding this comment

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

is assert preferable to self.assertEqual or self.assertIsNotNone ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this is the pytest way

Copy link
Member

@dpgaspar dpgaspar left a comment

Choose a reason for hiding this comment

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

Some doubts regarding how metrics are handled

table = self.get_table_by_name(table_name)
payload = get_query_context(table.name, table.id, table.type)
payload["queries"][0]["groupby"] = ["name"]
payload["queries"][0]["metrics"] = []
Copy link
Member

Choose a reason for hiding this comment

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

Are we able to deny an injected metric?

Copy link
Member Author

Choose a reason for hiding this comment

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

I haven't been able to create one yet, would be interested to see if someone is able to do one.

Copy link
Member

Choose a reason for hiding this comment

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

Not easy that's for sure

table_name = "birth_names"
table = self.get_table_by_name(table_name)
payload = get_query_context(table.name, table.id, table.type)
payload["queries"][0]["groupby"] = ["currentDatabase()"]
Copy link
Member

Choose a reason for hiding this comment

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

If currentDatabase() is a defined metric will it run ok?

Copy link
Member Author

Choose a reason for hiding this comment

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

It shouldn't, as it isn't an aggregate expression, hence will be missing from the groupby causing an invalid query.

@codecov-commenter
Copy link

Codecov Report

Merging #10451 into master will increase coverage by 0.38%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #10451      +/-   ##
==========================================
+ Coverage   59.49%   59.88%   +0.38%     
==========================================
  Files         767      413     -354     
  Lines       36282    13433   -22849     
  Branches     3430     3430              
==========================================
- Hits        21587     8044   -13543     
+ Misses      14502     5196    -9306     
  Partials      193      193              
Flag Coverage Δ
#javascript 59.88% <ø> (ø)
#python ?

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

Impacted Files Coverage Δ
...sions/db4b49eb0782_add_tables_for_sql_lab_state.py
superset/utils/import_datasource.py
superset/databases/schemas.py
superset/views/database/forms.py
superset/datasets/commands/update.py
superset/migrations/versions/1e2841a4128_.py
superset/views/base_api.py
...set/migrations/versions/d94d33dbe938_form_strip.py
superset/dashboards/commands/exceptions.py
...318dfe5fb6c_adding_verbose_name_to_druid_column.py
... and 339 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7f84927...560211a. Read the comment docs.

@villebro villebro merged commit acb00f5 into apache:master Aug 14, 2020
@villebro villebro deleted the villebro/assert-column-names branch August 19, 2020 19:53
villebro added a commit that referenced this pull request Sep 21, 2020
…rce (#10451)

* fix(chart-data-api): assert requested columns are present in datasource

* add filter tests

* add column_names to AnnotationDatasource

* add assertion for simple metrics

* lint
Ofeknielsen pushed a commit to ofekisr/incubator-superset that referenced this pull request Oct 5, 2020
…rce (apache#10451)

* fix(chart-data-api): assert requested columns are present in datasource

* add filter tests

* add column_names to AnnotationDatasource

* add assertion for simple metrics

* lint
auxten pushed a commit to auxten/incubator-superset that referenced this pull request Nov 20, 2020
…rce (apache#10451)

* fix(chart-data-api): assert requested columns are present in datasource

* add filter tests

* add column_names to AnnotationDatasource

* add assertion for simple metrics

* lint
cccs-rc pushed a commit to CybercentreCanada/superset that referenced this pull request Mar 6, 2024
…rce (apache#10451)

* fix(chart-data-api): assert requested columns are present in datasource

* add filter tests

* add column_names to AnnotationDatasource

* add assertion for simple metrics

* lint
@mistercrunch mistercrunch added 🍒 0.37.2 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 0.38.0 labels Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/L v0.37 v0.37.2 🍒 0.37.2 🚢 0.38.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

form_data on request could be editable, and run a custom query
5 participants