Added "none" to language whitelist

This commit is contained in:
kansi 2017-11-03 11:38:38 +05:30
parent 8310b04b17
commit 99aa38b217
2 changed files with 131 additions and 14 deletions

View File

@ -17,13 +17,14 @@ import logging
import bigchaindb
from bigchaindb.backend.connection import connect
from bigchaindb.common.exceptions import ValidationError
from bigchaindb.common.utils import validate_all_value_for_key
logger = logging.getLogger(__name__)
TABLES = ('bigchain', 'backlog', 'votes', 'assets')
VALID_LANGUAGES = ('danish', 'dutch', 'english', 'finnish', 'french', 'german',
'hungarian', 'italian', 'norwegian', 'portuguese', 'romanian',
'russian', 'spanish', 'swedish', 'turkish',
'russian', 'spanish', 'swedish', 'turkish', 'none',
'da', 'nl', 'en', 'fi', 'fr', 'de', 'hu', 'it', 'nb', 'pt',
'ro', 'ru', 'es', 'sv', 'tr')
@ -107,19 +108,41 @@ def init_database(connection=None, dbname=None):
create_indexes(connection, dbname)
def validate_if_exists_asset_language(tx_body):
data = tx_body['asset'].get('data', {})
def validate_if_exists_language(obj, key):
"""Validate all nested "language" key in `obj`.
if data and ('language' in data):
Args:
obj (dict): dictonary whose "language" key is to be validated.
language = data.get('language')
backend = bigchaindb.config['database']['backend']
Returns:
None: validation successfull
if (backend == 'mongodb') and (language not in VALID_LANGUAGES):
error_str = ('MongoDB does not support text search for the '
'language "{}". If you do not understand this error '
'message then please rename key/field "language" to '
'something else like "lang".').format(language)
raise ValidationError(error_str) from ValueError()
Raises:
ValidationError: raises execption incase language is not valid.
"""
backend = bigchaindb.config['database']['backend']
return
if backend == 'mongodb':
data = obj.get(key, {}) or {}
validate_all_value_for_key(data, 'language', validate_language)
def validate_language(value):
"""Check if `value` is a valid language
https://docs.mongodb.com/manual/reference/text-search-languages/
Args:
value (str): language to validated
Returns:
None: validation successfull
Raises:
ValidationError: raises execption incase language is not valid.
"""
if value not in VALID_LANGUAGES:
error_str = ('MongoDB does not support text search for the '
'language "{}". If you do not understand this error '
'message then please rename key/field "language" to '
'something else like "lang".').format(value)
raise ValidationError(error_str) from ValueError()

View File

@ -1,7 +1,10 @@
import time
import re
import rapidjson
import bigchaindb
from bigchaindb.common.exceptions import ValidationError
def gen_timestamp():
"""The Unix time, rounded to the nearest second.
@ -46,3 +49,94 @@ def deserialize(data):
string.
"""
return rapidjson.loads(data)
def validate_txn_obj(obj_name, obj, key, validation_fun):
"""Validates value associated to `key` in `obj` by applying
`validation_fun`.
Args:
obj_name (str): name for `obj` being validated.
obj (dict): dictonary object.
key (str): key to be validated in `obj`.
validation_fun (function): function used to validate the value
of `key`.
Returns:
None: indicates validation successfull
Raises:
ValidationError: `validation_fun` will raise this error on failure
"""
backend = bigchaindb.config['database']['backend']
if backend == 'mongodb':
data = obj.get(key, {}) or {}
validate_all_keys(obj_name, data, validation_fun)
def validate_all_keys(obj_name, obj, validation_fun):
"""Validates all (nested) keys in `obj` by using `validation_fun`
Args:
obj_name (str): name for `obj` being validated.
obj (dict): dictonary object.
validation_fun (function): function used to validate the value
of `key`.
Returns:
None: indicates validation successfull
Raises:
ValidationError: `validation_fun` will raise this error on failure
"""
for key, value in obj.items():
validation_fun(obj_name, key)
if type(value) is dict:
validate_all_keys(obj_name, value, validation_fun)
return
def validate_all_value_for_key(obj, key, validation_fun):
"""Validates value for all (nested) occurences of `key` in `obj`
using `validation_fun`
Args:
obj (dict): dictonary object.
key (str): key whose value is to be validated.
validation_fun (function): function used to validate the value
of `key`.
Returns:
None: indicates validation successfull
Raises:
ValidationError: `validation_fun` will raise this error on failure
"""
for vkey, value in obj.items():
if vkey == key:
validation_fun(value)
elif type(value) is dict:
validate_all_value_for_key(value, key, validation_fun)
return
def validate_key(obj_name, key):
"""Check if `key` contains ".", "$" or null characters
https://docs.mongodb.com/manual/reference/limits/#Restrictions-on-Field-Names
Args:
obj_name (str): object name to use when raising exception
key (str): key to validated
Returns:
None: indicates validation successfull
Raises:
ValidationError: raise execption incase of regex match.
"""
if re.search(r'^[$]|\.|\x00', key):
error_str = ('Invalid key name "{}" in {} object. The '
'key name cannot contain characters '
'".", "$" or null characters').format(key, obj_name)
raise ValidationError(error_str) from ValueError()