##// END OF EJS Templates
validators: added json validator.
marcink -
r2400:539f2965 default
parent child Browse files
Show More
@@ -1,140 +1,149 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2011-2017 RhodeCode GmbH
3 # Copyright (C) 2011-2017 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import os
21 import os
22 import re
22 import re
23 import logging
23 import logging
24
24
25
25
26 import ipaddress
26 import ipaddress
27 import colander
27 import colander
28
28
29 from rhodecode.translation import _
29 from rhodecode.translation import _
30 from rhodecode.lib.utils2 import glob2re, safe_unicode
30 from rhodecode.lib.utils2 import glob2re, safe_unicode
31 from rhodecode.lib.ext_json import json
31
32
32 log = logging.getLogger(__name__)
33 log = logging.getLogger(__name__)
33
34
34
35
35 def ip_addr_validator(node, value):
36 def ip_addr_validator(node, value):
36 try:
37 try:
37 # this raises an ValueError if address is not IpV4 or IpV6
38 # this raises an ValueError if address is not IpV4 or IpV6
38 ipaddress.ip_network(safe_unicode(value), strict=False)
39 ipaddress.ip_network(safe_unicode(value), strict=False)
39 except ValueError:
40 except ValueError:
40 msg = _(u'Please enter a valid IPv4 or IpV6 address')
41 msg = _(u'Please enter a valid IPv4 or IpV6 address')
41 raise colander.Invalid(node, msg)
42 raise colander.Invalid(node, msg)
42
43
43
44
44 class IpAddrValidator(object):
45 class IpAddrValidator(object):
45 def __init__(self, strict=True):
46 def __init__(self, strict=True):
46 self.strict = strict
47 self.strict = strict
47
48
48 def __call__(self, node, value):
49 def __call__(self, node, value):
49 try:
50 try:
50 # this raises an ValueError if address is not IpV4 or IpV6
51 # this raises an ValueError if address is not IpV4 or IpV6
51 ipaddress.ip_network(safe_unicode(value), strict=self.strict)
52 ipaddress.ip_network(safe_unicode(value), strict=self.strict)
52 except ValueError:
53 except ValueError:
53 msg = _(u'Please enter a valid IPv4 or IpV6 address')
54 msg = _(u'Please enter a valid IPv4 or IpV6 address')
54 raise colander.Invalid(node, msg)
55 raise colander.Invalid(node, msg)
55
56
56
57
57 def glob_validator(node, value):
58 def glob_validator(node, value):
58 try:
59 try:
59 re.compile('^' + glob2re(value) + '$')
60 re.compile('^' + glob2re(value) + '$')
60 except Exception:
61 except Exception:
61 msg = _(u'Invalid glob pattern')
62 msg = _(u'Invalid glob pattern')
62 raise colander.Invalid(node, msg)
63 raise colander.Invalid(node, msg)
63
64
64
65
65 def valid_name_validator(node, value):
66 def valid_name_validator(node, value):
66 from rhodecode.model.validation_schema import types
67 from rhodecode.model.validation_schema import types
67 if value is types.RootLocation:
68 if value is types.RootLocation:
68 return
69 return
69
70
70 msg = _('Name must start with a letter or number. Got `{}`').format(value)
71 msg = _('Name must start with a letter or number. Got `{}`').format(value)
71 if not re.match(r'^[a-zA-z0-9]{1,}', value):
72 if not re.match(r'^[a-zA-z0-9]{1,}', value):
72 raise colander.Invalid(node, msg)
73 raise colander.Invalid(node, msg)
73
74
74
75
75 class InvalidCloneUrl(Exception):
76 class InvalidCloneUrl(Exception):
76 allowed_prefixes = ()
77 allowed_prefixes = ()
77
78
78
79
79 def url_validator(url, repo_type, config):
80 def url_validator(url, repo_type, config):
80 from rhodecode.lib.vcs.backends.hg import MercurialRepository
81 from rhodecode.lib.vcs.backends.hg import MercurialRepository
81 from rhodecode.lib.vcs.backends.git import GitRepository
82 from rhodecode.lib.vcs.backends.git import GitRepository
82 from rhodecode.lib.vcs.backends.svn import SubversionRepository
83 from rhodecode.lib.vcs.backends.svn import SubversionRepository
83
84
84 if repo_type == 'hg':
85 if repo_type == 'hg':
85 allowed_prefixes = ('http', 'svn+http', 'git+http')
86 allowed_prefixes = ('http', 'svn+http', 'git+http')
86
87
87 if 'http' in url[:4]:
88 if 'http' in url[:4]:
88 # initially check if it's at least the proper URL
89 # initially check if it's at least the proper URL
89 # or does it pass basic auth
90 # or does it pass basic auth
90
91
91 MercurialRepository.check_url(url, config)
92 MercurialRepository.check_url(url, config)
92 elif 'svn+http' in url[:8]: # svn->hg import
93 elif 'svn+http' in url[:8]: # svn->hg import
93 SubversionRepository.check_url(url, config)
94 SubversionRepository.check_url(url, config)
94 elif 'git+http' in url[:8]: # git->hg import
95 elif 'git+http' in url[:8]: # git->hg import
95 raise NotImplementedError()
96 raise NotImplementedError()
96 else:
97 else:
97 exc = InvalidCloneUrl('Clone from URI %s not allowed. '
98 exc = InvalidCloneUrl('Clone from URI %s not allowed. '
98 'Allowed url must start with one of %s'
99 'Allowed url must start with one of %s'
99 % (url, ','.join(allowed_prefixes)))
100 % (url, ','.join(allowed_prefixes)))
100 exc.allowed_prefixes = allowed_prefixes
101 exc.allowed_prefixes = allowed_prefixes
101 raise exc
102 raise exc
102
103
103 elif repo_type == 'git':
104 elif repo_type == 'git':
104 allowed_prefixes = ('http', 'svn+http', 'hg+http')
105 allowed_prefixes = ('http', 'svn+http', 'hg+http')
105 if 'http' in url[:4]:
106 if 'http' in url[:4]:
106 # initially check if it's at least the proper URL
107 # initially check if it's at least the proper URL
107 # or does it pass basic auth
108 # or does it pass basic auth
108 GitRepository.check_url(url, config)
109 GitRepository.check_url(url, config)
109 elif 'svn+http' in url[:8]: # svn->git import
110 elif 'svn+http' in url[:8]: # svn->git import
110 raise NotImplementedError()
111 raise NotImplementedError()
111 elif 'hg+http' in url[:8]: # hg->git import
112 elif 'hg+http' in url[:8]: # hg->git import
112 raise NotImplementedError()
113 raise NotImplementedError()
113 else:
114 else:
114 exc = InvalidCloneUrl('Clone from URI %s not allowed. '
115 exc = InvalidCloneUrl('Clone from URI %s not allowed. '
115 'Allowed url must start with one of %s'
116 'Allowed url must start with one of %s'
116 % (url, ','.join(allowed_prefixes)))
117 % (url, ','.join(allowed_prefixes)))
117 exc.allowed_prefixes = allowed_prefixes
118 exc.allowed_prefixes = allowed_prefixes
118 raise exc
119 raise exc
119
120
120
121
121 class CloneUriValidator(object):
122 class CloneUriValidator(object):
122 def __init__(self, repo_type):
123 def __init__(self, repo_type):
123 self.repo_type = repo_type
124 self.repo_type = repo_type
124
125
125 def __call__(self, node, value):
126 def __call__(self, node, value):
126 from rhodecode.lib.utils import make_db_config
127 from rhodecode.lib.utils import make_db_config
127 try:
128 try:
128 config = make_db_config(clear_session=False)
129 config = make_db_config(clear_session=False)
129 url_validator(value, self.repo_type, config)
130 url_validator(value, self.repo_type, config)
130 except InvalidCloneUrl as e:
131 except InvalidCloneUrl as e:
131 log.warning(e)
132 log.warning(e)
132 msg = _(u'Invalid clone url, provide a valid clone '
133 msg = _(u'Invalid clone url, provide a valid clone '
133 u'url starting with one of {allowed_prefixes}').format(
134 u'url starting with one of {allowed_prefixes}').format(
134 allowed_prefixes=e.allowed_prefixes)
135 allowed_prefixes=e.allowed_prefixes)
135 raise colander.Invalid(node, msg)
136 raise colander.Invalid(node, msg)
136 except Exception:
137 except Exception:
137 log.exception('Url validation failed')
138 log.exception('Url validation failed')
138 msg = _(u'invalid clone url for {repo_type} repository').format(
139 msg = _(u'invalid clone url for {repo_type} repository').format(
139 repo_type=self.repo_type)
140 repo_type=self.repo_type)
140 raise colander.Invalid(node, msg)
141 raise colander.Invalid(node, msg)
142
143
144 def json_validator(node, value):
145 try:
146 json.loads(value)
147 except (Exception,):
148 msg = _(u'Please enter a valid json object')
149 raise colander.Invalid(node, msg)
General Comments 0
You need to be logged in to leave comments. Login now