##// END OF EJS Templates
tweens: use explicit position of measuring tween
super-admin -
r5014:5e247eb3 default
parent child Browse files
Show More
@@ -1,124 +1,127 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2020 RhodeCode GmbH
3 # Copyright (C) 2010-2020 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
21
22 import logging
22 import logging
23
24 import pyramid.tweens
23 from pyramid.httpexceptions import HTTPException, HTTPBadRequest
25 from pyramid.httpexceptions import HTTPException, HTTPBadRequest
24
26
25 from rhodecode.lib.middleware.vcs import (
27 from rhodecode.lib.middleware.vcs import (
26 detect_vcs_request, VCS_TYPE_KEY, VCS_TYPE_SKIP)
28 detect_vcs_request, VCS_TYPE_KEY, VCS_TYPE_SKIP)
27
29
28
30
29 log = logging.getLogger(__name__)
31 log = logging.getLogger(__name__)
30
32
31
33
32 def vcs_detection_tween_factory(handler, registry):
34 def vcs_detection_tween_factory(handler, registry):
33
35
34 def vcs_detection_tween(request):
36 def vcs_detection_tween(request):
35 """
37 """
36 Do detection of vcs type, and save results for other layers to re-use
38 Do detection of vcs type, and save results for other layers to re-use
37 this information
39 this information
38 """
40 """
39 vcs_server_enabled = request.registry.settings.get('vcs.server.enable')
41 vcs_server_enabled = request.registry.settings.get('vcs.server.enable')
42
40 vcs_handler = vcs_server_enabled and detect_vcs_request(
43 vcs_handler = vcs_server_enabled and detect_vcs_request(
41 request.environ, request.registry.settings.get('vcs.backends'))
44 request.environ, request.registry.settings.get('vcs.backends'))
42
45
43 if vcs_handler:
46 if vcs_handler:
44 # save detected VCS type for later re-use
47 # save detected VCS type for later re-use
45 request.environ[VCS_TYPE_KEY] = vcs_handler.SCM
48 request.environ[VCS_TYPE_KEY] = vcs_handler.SCM
46 request.vcs_call = vcs_handler.SCM
49 request.vcs_call = vcs_handler.SCM
47
50
48 log.debug('Processing request with `%s` handler', handler.__name__)
51 log.debug('Processing request with `%s` handler', handler.__name__)
49 return handler(request)
52 return handler(request)
50
53
51 # mark that we didn't detect an VCS, and we can skip detection later on
54 # mark that we didn't detect an VCS, and we can skip detection later on
52 request.environ[VCS_TYPE_KEY] = VCS_TYPE_SKIP
55 request.environ[VCS_TYPE_KEY] = VCS_TYPE_SKIP
53
56
54 log.debug('Processing request with `%s` handler', handler.__name__)
57 log.debug('Processing request with `%s` handler', handler.__name__)
55 return handler(request)
58 return handler(request)
56
59
57 return vcs_detection_tween
60 return vcs_detection_tween
58
61
59
62
60 def junk_encoding_detector(request):
63 def junk_encoding_detector(request):
61 """
64 """
62 Detect bad encoded GET params, and fail immediately with BadRequest
65 Detect bad encoded GET params, and fail immediately with BadRequest
63 """
66 """
64
67
65 try:
68 try:
66 request.GET.get("", None)
69 request.GET.get("", None)
67 except UnicodeDecodeError:
70 except UnicodeDecodeError:
68 raise HTTPBadRequest("Invalid bytes in query string.")
71 raise HTTPBadRequest("Invalid bytes in query string.")
69
72
70
73
71 def bad_url_data_detector(request):
74 def bad_url_data_detector(request):
72 """
75 """
73 Detect invalid bytes in a path.
76 Detect invalid bytes in a path.
74 """
77 """
75 try:
78 try:
76 request.path_info
79 request.path_info
77 except UnicodeDecodeError:
80 except UnicodeDecodeError:
78 raise HTTPBadRequest("Invalid bytes in URL.")
81 raise HTTPBadRequest("Invalid bytes in URL.")
79
82
80
83
81 def junk_form_data_detector(request):
84 def junk_form_data_detector(request):
82 """
85 """
83 Detect bad encoded POST params, and fail immediately with BadRequest
86 Detect bad encoded POST params, and fail immediately with BadRequest
84 """
87 """
85
88
86 if request.method == "POST":
89 if request.method == "POST":
87 try:
90 try:
88 request.POST.get("", None)
91 request.POST.get("", None)
89 except ValueError:
92 except ValueError:
90 raise HTTPBadRequest("Invalid bytes in form data.")
93 raise HTTPBadRequest("Invalid bytes in form data.")
91
94
92
95
93 def sanity_check_factory(handler, registry):
96 def sanity_check_factory(handler, registry):
94 def sanity_check(request):
97 def sanity_check(request):
95 log.debug('Checking current URL sanity for bad data')
98 log.debug('Checking current URL sanity for bad data')
96 try:
99 try:
97 junk_encoding_detector(request)
100 junk_encoding_detector(request)
98 bad_url_data_detector(request)
101 bad_url_data_detector(request)
99 junk_form_data_detector(request)
102 junk_form_data_detector(request)
100 except HTTPException as exc:
103 except HTTPException as exc:
101 return exc
104 return exc
102
105
103 return handler(request)
106 return handler(request)
104
107
105 return sanity_check
108 return sanity_check
106
109
107
110
108 def includeme(config):
111 def includeme(config):
109 config.add_subscriber('rhodecode.subscribers.add_renderer_globals',
112 config.add_subscriber('rhodecode.subscribers.add_renderer_globals',
110 'pyramid.events.BeforeRender')
113 'pyramid.events.BeforeRender')
111 config.add_subscriber('rhodecode.subscribers.update_celery_conf',
114 config.add_subscriber('rhodecode.subscribers.update_celery_conf',
112 'pyramid.events.NewRequest')
115 'pyramid.events.NewRequest')
113 config.add_subscriber('rhodecode.subscribers.set_user_lang',
116 config.add_subscriber('rhodecode.subscribers.set_user_lang',
114 'pyramid.events.NewRequest')
117 'pyramid.events.NewRequest')
115 config.add_subscriber('rhodecode.subscribers.reset_log_bucket',
118 config.add_subscriber('rhodecode.subscribers.reset_log_bucket',
116 'pyramid.events.NewRequest')
119 'pyramid.events.NewRequest')
117 config.add_subscriber('rhodecode.subscribers.add_request_user_context',
120 config.add_subscriber('rhodecode.subscribers.add_request_user_context',
118 'pyramid.events.ContextFound')
121 'pyramid.events.ContextFound')
119 config.add_tween('rhodecode.tweens.vcs_detection_tween_factory')
122 config.add_tween('rhodecode.tweens.vcs_detection_tween_factory')
120 config.add_tween('rhodecode.tweens.sanity_check_factory')
123 config.add_tween('rhodecode.tweens.sanity_check_factory')
121
124
122 # This needs to be the LAST item
125 # This needs to be the LAST item
123 config.add_tween('rhodecode.lib.middleware.request_wrapper.RequestWrapperTween')
126 config.add_tween('rhodecode.lib.middleware.request_wrapper.RequestWrapperTween', under=pyramid.tweens.INGRESS)
124 log.debug('configured all tweens')
127 log.debug('configured all tweens')
General Comments 0
You need to be logged in to leave comments. Login now