Skip to content

Test parameter escaping #46

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

Merged
merged 10 commits into from
Oct 14, 2022
Merged

Test parameter escaping #46

merged 10 commits into from
Oct 14, 2022

Conversation

susodapop
Copy link
Contributor

@susodapop susodapop commented Aug 26, 2022

Background

databricks-sql-connector adheres to PEP-249 which defines a parameter binding API. For supported databases, parameter binding avoids SQL-injection vulnerabilities because parameter values are always escaped at runtime. The hive thrift server doesn't support safe parameter binding (yet!). This means that all queries are passed as strings and are therefore vulnerable to SQL-injection. It's the client's responsibility to safely handle parameterised input before calling cursor.execute(<some query text>).

databricks-sql-connector automatically sanitises inputs that follow the PEP-249 calling convention. This pull request adds unit tests to demonstrate the sanitisation behaviour. It also sets a baseline we can use for testing server-side parameter binding once it's supported by the Databricks runtime.

In a follow-up PR, I will add examples that use the PEP-249 parameter binding interface. Once Databricks runtime supports proper server-side binding, users of databricks-sql-connector will benefit from its enhanced safety "for free", without needing to rewrite their client code.

Copy link
Collaborator

@moderakh moderakh left a comment

Choose a reason for hiding this comment

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

Thanks @susodapop LGTM.

I added a few non-blocking comments for adding more tests.

assert pe.escape_string("golly bob howdy") == "'golly bob howdy'"

def test_escape_string_that_includes_quotes(self):
# Databricks queries support just one special character: a single quote mark

Choose a reason for hiding this comment

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

i don't understand this comment, per the spark docs backslash is used for escaping (so it too is a special character)

you might also try this query in Databricks SQL:

select 'cat\'s meow' 
union all
select 'cat\\\'s meow'
union all
select '\U0001F44D \u3042'

see also #51

Copy link
Contributor Author

@susodapop susodapop Oct 10, 2022

Choose a reason for hiding this comment

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

Good callout. Removing this comment in dd25478

Jesse Whitehouse added 2 commits October 3, 2022 11:24
Jesse Whitehouse added 8 commits October 10, 2022 14:55
escaping a single quote with a second single quote.

Signed-off-by: Jesse Whitehouse <[email protected]>
Signed-off-by: Jesse Whitehouse <[email protected]>
Signed-off-by: Jesse Whitehouse <[email protected]>
Signed-off-by: Jesse Whitehouse <[email protected]>
Signed-off-by: Jesse Whitehouse <[email protected]>
This was originally written in such a way that it always passed.

Signed-off-by: Jesse Whitehouse <[email protected]>
@susodapop susodapop merged commit d2d9015 into main Oct 14, 2022
@susodapop susodapop deleted the test-param-escaping branch October 14, 2022 17:03
ueshin added a commit to databricks/dbt-databricks that referenced this pull request Jan 12, 2023
### Description

Upgrades `databricks-sql-connector` to `2.2.2`.

It includes:

- Add tests for parameter sanitisation / escaping (databricks/databricks-sql-python#46) for the fix for #249
- Support Python 3.11 (databricks/databricks-sql-python#60)
ueshin added a commit to databricks/dbt-databricks that referenced this pull request Jan 12, 2023
### Description

Upgrades `databricks-sql-connector` to `2.2.2`.

It includes:

- Add tests for parameter sanitisation / escaping (databricks/databricks-sql-python#46) for the fix for #249
- Support Python 3.11 (databricks/databricks-sql-python#60)
ueshin added a commit to databricks/dbt-databricks that referenced this pull request Jan 12, 2023
### Description

Upgrades `databricks-sql-connector` to `2.2.2`.

It includes:

- Add tests for parameter sanitisation / escaping (databricks/databricks-sql-python#46) for the fix for #249
- Support Python 3.11 (databricks/databricks-sql-python#60)
ueshin added a commit to databricks/dbt-databricks that referenced this pull request Jan 12, 2023
### Description

Upgrades `databricks-sql-connector` to `2.2.2`.

It includes:

- Add tests for parameter sanitisation / escaping (databricks/databricks-sql-python#46) for the fix for #249
- Support Python 3.11 (databricks/databricks-sql-python#60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants