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