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