##// END OF EJS Templates
tweens: removed utf8 marker
super-admin -
r5049:e30cd90e default
parent child Browse files
Show More
@@ -1,127 +1,126 b''
1 # -*- coding: utf-8 -*-
2
1
3 # Copyright (C) 2010-2020 RhodeCode GmbH
2 # Copyright (C) 2010-2020 RhodeCode GmbH
4 #
3 #
5 # This program is free software: you can redistribute it and/or modify
4 # 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
5 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
6 # (only), as published by the Free Software Foundation.
8 #
7 #
9 # This program is distributed in the hope that it will be useful,
8 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
11 # GNU General Public License for more details.
13 #
12 #
14 # You should have received a copy of the GNU Affero General Public License
13 # 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/>.
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
15 #
17 # This program is dual-licensed. If you wish to learn more about the
16 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
17 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
18 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
19
21
20
22 import logging
21 import logging
23
22
24 import pyramid.tweens
23 import pyramid.tweens
25 from pyramid.httpexceptions import HTTPException, HTTPBadRequest
24 from pyramid.httpexceptions import HTTPException, HTTPBadRequest
26
25
27 from rhodecode.lib.middleware.vcs import (
26 from rhodecode.lib.middleware.vcs import (
28 detect_vcs_request, VCS_TYPE_KEY, VCS_TYPE_SKIP)
27 detect_vcs_request, VCS_TYPE_KEY, VCS_TYPE_SKIP)
29
28
30
29
31 log = logging.getLogger(__name__)
30 log = logging.getLogger(__name__)
32
31
33
32
34 def vcs_detection_tween_factory(handler, registry):
33 def vcs_detection_tween_factory(handler, registry):
35
34
36 def vcs_detection_tween(request):
35 def vcs_detection_tween(request):
37 """
36 """
38 Do detection of vcs type, and save results for other layers to re-use
37 Do detection of vcs type, and save results for other layers to re-use
39 this information
38 this information
40 """
39 """
41 vcs_server_enabled = request.registry.settings.get('vcs.server.enable')
40 vcs_server_enabled = request.registry.settings.get('vcs.server.enable')
42
41
43 vcs_handler = vcs_server_enabled and detect_vcs_request(
42 vcs_handler = vcs_server_enabled and detect_vcs_request(
44 request.environ, request.registry.settings.get('vcs.backends'))
43 request.environ, request.registry.settings.get('vcs.backends'))
45
44
46 if vcs_handler:
45 if vcs_handler:
47 # save detected VCS type for later re-use
46 # save detected VCS type for later re-use
48 request.environ[VCS_TYPE_KEY] = vcs_handler.SCM
47 request.environ[VCS_TYPE_KEY] = vcs_handler.SCM
49 request.vcs_call = vcs_handler.SCM
48 request.vcs_call = vcs_handler.SCM
50
49
51 log.debug('Processing request with `%s` handler', handler.__name__)
50 log.debug('Processing request with `%s` handler', handler.__name__)
52 return handler(request)
51 return handler(request)
53
52
54 # mark that we didn't detect an VCS, and we can skip detection later on
53 # mark that we didn't detect an VCS, and we can skip detection later on
55 request.environ[VCS_TYPE_KEY] = VCS_TYPE_SKIP
54 request.environ[VCS_TYPE_KEY] = VCS_TYPE_SKIP
56
55
57 log.debug('Processing request with `%s` handler', handler.__name__)
56 log.debug('Processing request with `%s` handler', handler.__name__)
58 return handler(request)
57 return handler(request)
59
58
60 return vcs_detection_tween
59 return vcs_detection_tween
61
60
62
61
63 def junk_encoding_detector(request):
62 def junk_encoding_detector(request):
64 """
63 """
65 Detect bad encoded GET params, and fail immediately with BadRequest
64 Detect bad encoded GET params, and fail immediately with BadRequest
66 """
65 """
67
66
68 try:
67 try:
69 request.GET.get("", None)
68 request.GET.get("", None)
70 except UnicodeDecodeError:
69 except UnicodeDecodeError:
71 raise HTTPBadRequest("Invalid bytes in query string.")
70 raise HTTPBadRequest("Invalid bytes in query string.")
72
71
73
72
74 def bad_url_data_detector(request):
73 def bad_url_data_detector(request):
75 """
74 """
76 Detect invalid bytes in a path.
75 Detect invalid bytes in a path.
77 """
76 """
78 try:
77 try:
79 request.path_info
78 request.path_info
80 except UnicodeDecodeError:
79 except UnicodeDecodeError:
81 raise HTTPBadRequest("Invalid bytes in URL.")
80 raise HTTPBadRequest("Invalid bytes in URL.")
82
81
83
82
84 def junk_form_data_detector(request):
83 def junk_form_data_detector(request):
85 """
84 """
86 Detect bad encoded POST params, and fail immediately with BadRequest
85 Detect bad encoded POST params, and fail immediately with BadRequest
87 """
86 """
88
87
89 if request.method == "POST":
88 if request.method == "POST":
90 try:
89 try:
91 request.POST.get("", None)
90 request.POST.get("", None)
92 except ValueError:
91 except ValueError:
93 raise HTTPBadRequest("Invalid bytes in form data.")
92 raise HTTPBadRequest("Invalid bytes in form data.")
94
93
95
94
96 def sanity_check_factory(handler, registry):
95 def sanity_check_factory(handler, registry):
97 def sanity_check(request):
96 def sanity_check(request):
98 log.debug('Checking current URL sanity for bad data')
97 log.debug('Checking current URL sanity for bad data')
99 try:
98 try:
100 junk_encoding_detector(request)
99 junk_encoding_detector(request)
101 bad_url_data_detector(request)
100 bad_url_data_detector(request)
102 junk_form_data_detector(request)
101 junk_form_data_detector(request)
103 except HTTPException as exc:
102 except HTTPException as exc:
104 return exc
103 return exc
105
104
106 return handler(request)
105 return handler(request)
107
106
108 return sanity_check
107 return sanity_check
109
108
110
109
111 def includeme(config):
110 def includeme(config):
112 config.add_subscriber('rhodecode.subscribers.add_renderer_globals',
111 config.add_subscriber('rhodecode.subscribers.add_renderer_globals',
113 'pyramid.events.BeforeRender')
112 'pyramid.events.BeforeRender')
114 config.add_subscriber('rhodecode.subscribers.update_celery_conf',
113 config.add_subscriber('rhodecode.subscribers.update_celery_conf',
115 'pyramid.events.NewRequest')
114 'pyramid.events.NewRequest')
116 config.add_subscriber('rhodecode.subscribers.set_user_lang',
115 config.add_subscriber('rhodecode.subscribers.set_user_lang',
117 'pyramid.events.NewRequest')
116 'pyramid.events.NewRequest')
118 config.add_subscriber('rhodecode.subscribers.reset_log_bucket',
117 config.add_subscriber('rhodecode.subscribers.reset_log_bucket',
119 'pyramid.events.NewRequest')
118 'pyramid.events.NewRequest')
120 config.add_subscriber('rhodecode.subscribers.add_request_user_context',
119 config.add_subscriber('rhodecode.subscribers.add_request_user_context',
121 'pyramid.events.ContextFound')
120 'pyramid.events.ContextFound')
122 config.add_tween('rhodecode.tweens.vcs_detection_tween_factory')
121 config.add_tween('rhodecode.tweens.vcs_detection_tween_factory')
123 config.add_tween('rhodecode.tweens.sanity_check_factory')
122 config.add_tween('rhodecode.tweens.sanity_check_factory')
124
123
125 # This needs to be the LAST item
124 # This needs to be the LAST item
126 config.add_tween('rhodecode.lib.middleware.request_wrapper.RequestWrapperTween', under=pyramid.tweens.INGRESS)
125 config.add_tween('rhodecode.lib.middleware.request_wrapper.RequestWrapperTween', under=pyramid.tweens.INGRESS)
127 log.debug('configured all tweens')
126 log.debug('configured all tweens')
General Comments 0
You need to be logged in to leave comments. Login now