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