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