Skip to content

Commit

Permalink
Small improvements and sql injection prevention.
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Sep 6, 2012
1 parent 9a5dc54 commit 6320f95
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
12 changes: 9 additions & 3 deletions ckanext/datastore/db.py
Expand Up @@ -27,13 +27,19 @@
_methods = [INSERT, UPSERT, UPDATE]


def _strip(input):
if isinstance(input, basestring):
return input.strip('"')
return input


def _get_list(input):
"""Transforms a string or list to a list"""
if input == None:
return
if input == '':
return []
return aslist(input, ',', True)
return [_strip(x) for x in aslist(input, ',', True)]


def _get_bool(input, default=False):
Expand Down Expand Up @@ -457,7 +463,7 @@ def upsert_data(context, data_dict):


def _get_unique_key(context, data_dict):
sql_get_uique_key = '''
sql_get_unique_key = '''
select
a.attname as column_names
from
Expand All @@ -473,7 +479,7 @@ def _get_unique_key(context, data_dict):
and idx.indisprimary = false
and t.relname = '%s'
'''
key_parts = context['connection'].execute(sql_get_uique_key, data_dict['resource_id'])
key_parts = context['connection'].execute(sql_get_unique_key, data_dict['resource_id'])
return [x[0] for x in key_parts]


Expand Down
6 changes: 3 additions & 3 deletions ckanext/datastore/logic/action.py
Expand Up @@ -3,6 +3,7 @@
import ckan.logic as logic
import ckan.plugins as p
import ckanext.datastore.db as db
from sqlalchemy import text

log = logging.getLogger(__name__)
_get_or_bust = logic.get_or_bust
Expand Down Expand Up @@ -159,9 +160,8 @@ def datastore_search(context, data_dict):
alias_exists = False
if not res_exists:
# assume id is an alias
alias_sql = ('select alias_of from "_table_metadata" '
"where name = '{}'").format(id)
result = db._get_engine(None, data_dict).execute(alias_sql).fetchone()
alias_sql = text('select alias_of from "_table_metadata" where name = :id')
result = db._get_engine(None, data_dict).execute(alias_sql, id=id).fetchone()
if result:
alias_exists = model.Resource.get(result[0].strip('"'))

Expand Down
2 changes: 1 addition & 1 deletion ckanext/datastore/tests/test_datastore.py
Expand Up @@ -20,6 +20,7 @@ def test_list(self):
assert db._get_list('') == []
assert db._get_list('foo') == ['foo']
assert db._get_list('foo, bar') == ['foo', 'bar']
assert db._get_list('"foo", "bar"') == ['foo', 'bar']
assert db._get_list(u'foo, bar') == ['foo', 'bar']
assert db._get_list(['foo', 'bar']) == ['foo', 'bar']
assert db._get_list([u'foo', u'bar']) == ['foo', 'bar']
Expand Down Expand Up @@ -443,7 +444,6 @@ def setup_class(cls):
resource = model.Package.get('annakarenina').resources[0]
cls.data = {
'resource_id': resource.id,
'alias': 'books3',
'fields': [{'id': u'b\xfck', 'type': 'text'},
{'id': 'author', 'type': 'text'},
{'id': 'published'}],
Expand Down

0 comments on commit 6320f95

Please sign in to comment.