##// END OF EJS Templates
deps: bumped venusian==3.1.0 for python 3.12 compat
deps: bumped venusian==3.1.0 for python 3.12 compat

File last commit:

r5608:6d33e504 default
r5622:5bcb0167 tip default
Show More
validators.py
160 lines | 5.5 KiB | text/x-python | PythonLexer
core: updated copyright to 2024
r5608 # Copyright (C) 2011-2024 RhodeCode GmbH
repo-schema: added a custom schema for repo settings....
r1719 #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
dan
reviewers: add repo review rule models and expose default...
r821 import re
repo-schema: added a custom schema for repo settings....
r1719 import logging
gists: use colander schema to validate input data....
r523 import ipaddress
import colander
from rhodecode.translation import _
validators/schemas: python3 fixes str vs unicode and few test breaking fixes
r5066 from rhodecode.lib.utils2 import glob2re
from rhodecode.lib.str_utils import safe_str
core: revamp of automation/scheduler/artifacts EE functionality
r5137 from rhodecode.lib.ext_json import json, sjson
gists: use colander schema to validate input data....
r523
repo-schema: added a custom schema for repo settings....
r1719 log = logging.getLogger(__name__)
gists: use colander schema to validate input data....
r523
def ip_addr_validator(node, value):
try:
# this raises an ValueError if address is not IpV4 or IpV6
validators/schemas: python3 fixes str vs unicode and few test breaking fixes
r5066 ipaddress.ip_network(safe_str(value), strict=False)
gists: use colander schema to validate input data....
r523 except ValueError:
validators/schemas: python3 fixes str vs unicode and few test breaking fixes
r5066 msg = _('Please enter a valid IPv4 or IpV6 address')
gists: use colander schema to validate input data....
r523 raise colander.Invalid(node, msg)
dan
reviewers: add repo review rule models and expose default...
r821
validation-schema: added StringBoolean type and IPAddr validator.
r1149 class IpAddrValidator(object):
def __init__(self, strict=True):
self.strict = strict
def __call__(self, node, value):
try:
# this raises an ValueError if address is not IpV4 or IpV6
validators/schemas: python3 fixes str vs unicode and few test breaking fixes
r5066 ipaddress.ip_network(safe_str(value), strict=self.strict)
validation-schema: added StringBoolean type and IPAddr validator.
r1149 except ValueError:
validators/schemas: python3 fixes str vs unicode and few test breaking fixes
r5066 msg = _('Please enter a valid IPv4 or IpV6 address')
validation-schema: added StringBoolean type and IPAddr validator.
r1149 raise colander.Invalid(node, msg)
dan
reviewers: add repo review rule models and expose default...
r821 def glob_validator(node, value):
try:
re.compile('^' + glob2re(value) + '$')
except Exception:
validators/schemas: python3 fixes str vs unicode and few test breaking fixes
r5066 msg = _('Invalid glob pattern')
dan
reviewers: add repo review rule models and expose default...
r821 raise colander.Invalid(node, msg)
repo-schemas: refactor repository schemas and use it in API update/create functions....
r1153
def valid_name_validator(node, value):
from rhodecode.model.validation_schema import types
if value is types.RootLocation:
return
msg = _('Name must start with a letter or number. Got `{}`').format(value)
if not re.match(r'^[a-zA-z0-9]{1,}', value):
raise colander.Invalid(node, msg)
repo-schema: added a custom schema for repo settings....
r1719
class InvalidCloneUrl(Exception):
allowed_prefixes = ()
def url_validator(url, repo_type, config):
from rhodecode.lib.vcs.backends.hg import MercurialRepository
from rhodecode.lib.vcs.backends.git import GitRepository
from rhodecode.lib.vcs.backends.svn import SubversionRepository
if repo_type == 'hg':
allowed_prefixes = ('http', 'svn+http', 'git+http')
if 'http' in url[:4]:
# initially check if it's at least the proper URL
# or does it pass basic auth
validators: fix problems with new mercurial url validation.
r2781 return MercurialRepository.check_url(url, config)
repo-schema: added a custom schema for repo settings....
r1719 elif 'svn+http' in url[:8]: # svn->hg import
SubversionRepository.check_url(url, config)
elif 'git+http' in url[:8]: # git->hg import
raise NotImplementedError()
else:
exc = InvalidCloneUrl('Clone from URI %s not allowed. '
'Allowed url must start with one of %s'
% (url, ','.join(allowed_prefixes)))
exc.allowed_prefixes = allowed_prefixes
raise exc
elif repo_type == 'git':
allowed_prefixes = ('http', 'svn+http', 'hg+http')
if 'http' in url[:4]:
# initially check if it's at least the proper URL
# or does it pass basic auth
validators: fix problems with new mercurial url validation.
r2781 return GitRepository.check_url(url, config)
repo-schema: added a custom schema for repo settings....
r1719 elif 'svn+http' in url[:8]: # svn->git import
raise NotImplementedError()
elif 'hg+http' in url[:8]: # hg->git import
raise NotImplementedError()
else:
exc = InvalidCloneUrl('Clone from URI %s not allowed. '
'Allowed url must start with one of %s'
% (url, ','.join(allowed_prefixes)))
exc.allowed_prefixes = allowed_prefixes
raise exc
api: security, fix problem when absolute paths are specified with API call, that would allow...
r2663 elif repo_type == 'svn':
# no validation for SVN yet
return
modernize: updates for python3
r5095 raise InvalidCloneUrl(f'Invalid repo type specified: `{repo_type}`')
repo-schema: added a custom schema for repo settings....
r1719
class CloneUriValidator(object):
def __init__(self, repo_type):
self.repo_type = repo_type
def __call__(self, node, value):
api: security, fix problem when absolute paths are specified with API call, that would allow...
r2663
repo-schema: added a custom schema for repo settings....
r1719 from rhodecode.lib.utils import make_db_config
try:
config = make_db_config(clear_session=False)
url_validator(value, self.repo_type, config)
except InvalidCloneUrl as e:
log.warning(e)
validators/schemas: python3 fixes str vs unicode and few test breaking fixes
r5066 raise colander.Invalid(node, str(e))
except Exception as e:
repo-schema: added a custom schema for repo settings....
r1719 log.exception('Url validation failed')
validators/schemas: python3 fixes str vs unicode and few test breaking fixes
r5066 reason = repr(e)
reason = reason.replace('<', '&lt;').replace('>', '&gt;')
msg = _('invalid clone url or credentials for {repo_type} repository. Reason: {reason}')\
.format(reason=reason, repo_type=self.repo_type)
repo-schema: added a custom schema for repo settings....
r1719 raise colander.Invalid(node, msg)
validators: added json validator.
r2400
def json_validator(node, value):
try:
json.loads(value)
validators/schemas: python3 fixes str vs unicode and few test breaking fixes
r5066 except (Exception,):
msg = _('Please enter a valid json object')
validators: added json validator.
r2400 raise colander.Invalid(node, msg)
events: added new fts build event
r4583
def json_validator_with_exc(node, value):
core: revamp of automation/scheduler/artifacts EE functionality
r5137
events: added new fts build event
r4583 try:
json.loads(value)
except (Exception,) as e:
core: revamp of automation/scheduler/artifacts EE functionality
r5137 msg = _(f'Please enter a valid json object type={type(value)}: `{e}`')
events: added new fts build event
r4583 raise colander.Invalid(node, msg)