Merge branch 'MDL-73883-master' of https://github.com/andrewnicols/moodle
[moodle.git] / lib / tests / curl_security_helper_test.php
blobed205b5b54495f663fcd6a382d14771eb649da19
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Unit tests for /lib/classes/curl/curl_security_helper.php.
20 * @package core
21 * @copyright 2016 Jake Dallimore <jrhdallimore@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * cURL security test suite.
30 * Note: The curl_security_helper class performs forward and reverse DNS look-ups in some cases. This class will not attempt to test
31 * this functionality as look-ups can vary from machine to machine. Instead, human testing with known inputs/outputs is recommended.
33 * @package core
34 * @copyright 2016 Jake Dallimore <jrhdallimore@gmail.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_curl_security_helper_testcase extends advanced_testcase {
38 /**
39 * Test for \core\files\curl_security_helper::url_is_blocked().
41 * @param array $dns a mapping between hosts and IPs to be used instead of a real DNS lookup. The values must be arrays.
42 * @param string $url the url to validate.
43 * @param string $blockedhosts the list of blocked hosts.
44 * @param string $allowedports the list of allowed ports.
45 * @param bool $expected the expected result.
46 * @dataProvider curl_security_url_data_provider
48 public function test_curl_security_helper_url_is_blocked($dns, $url, $blockedhosts, $allowedports, $expected) {
49 $this->resetAfterTest(true);
50 $helper = $this->getMockBuilder('\core\files\curl_security_helper')
51 ->onlyMethods(['get_host_list_by_name'])
52 ->getMock();
54 // Override the get host list method to return hard coded values based on a mapping provided by $dns.
55 $helper->method('get_host_list_by_name')->will(
56 $this->returnCallback(
57 function($host) use ($dns) {
58 return isset($dns[$host]) ? $dns[$host] : [];
63 set_config('curlsecurityblockedhosts', $blockedhosts);
64 set_config('curlsecurityallowedport', $allowedports);
65 $this->assertEquals($expected, $helper->url_is_blocked($url));
68 /**
69 * Data provider for test_curl_security_helper_url_is_blocked().
71 * @return array
73 public function curl_security_url_data_provider() {
74 $simpledns = ['localhost' => ['127.0.0.1']];
75 $multiplerecorddns = [
76 'sub.example.com' => ['1.2.3.4', '5.6.7.8']
78 // Format: url, blocked hosts, allowed ports, expected result.
79 return [
80 // Base set without the blocklist enabled - no checking takes place.
81 [$simpledns, "http://localhost/x.png", "", "", false], // IP=127.0.0.1, Port=80 (port inferred from http).
82 [$simpledns, "http://localhost:80/x.png", "", "", false], // IP=127.0.0.1, Port=80 (specific port overrides http scheme).
83 [$simpledns, "https://localhost/x.png", "", "", false], // IP=127.0.0.1, Port=443 (port inferred from https).
84 [$simpledns, "http://localhost:443/x.png", "", "", false], // IP=127.0.0.1, Port=443 (specific port overrides http scheme).
85 [$simpledns, "localhost/x.png", "", "", false], // IP=127.0.0.1, Port=80 (port inferred from http fallback).
86 [$simpledns, "localhost:443/x.png", "", "", false], // IP=127.0.0.1, Port=443 (port hard specified, despite http fallback).
87 [$simpledns, "http://127.0.0.1/x.png", "", "", false], // IP=127.0.0.1, Port=80 (port inferred from http).
88 [$simpledns, "127.0.0.1/x.png", "", "", false], // IP=127.0.0.1, Port=80 (port inferred from http fallback).
89 [$simpledns, "http://localhost:8080/x.png", "", "", false], // IP=127.0.0.1, Port=8080 (port hard specified).
90 [$simpledns, "http://192.168.1.10/x.png", "", "", false], // IP=192.168.1.10, Port=80 (port inferred from http).
91 [$simpledns, "https://192.168.1.10/x.png", "", "", false], // IP=192.168.1.10, Port=443 (port inferred from https).
92 [$simpledns, "http://sub.example.com/x.png", "", "", false], // IP=::1, Port = 80 (port inferred from http).
93 [$simpledns, "http://s-1.d-1.com/x.png", "", "", false], // IP=::1, Port = 80 (port inferred from http).
95 // Test set using domain name filters but with all ports allowed (empty).
96 [$simpledns, "http://localhost/x.png", "localhost", "", true],
97 [$simpledns, "localhost/x.png", "localhost", "", true],
98 [$simpledns, "localhost:0/x.png", "localhost", "", true],
99 [$simpledns, "ftp://localhost/x.png", "localhost", "", true],
100 [$simpledns, "http://sub.example.com/x.png", "localhost", "", false],
101 [$simpledns, "http://example.com/x.png", "example.com", "", true],
102 [$simpledns, "http://sub.example.com/x.png", "example.com", "", false],
104 // Test set using wildcard domain name filters but with all ports allowed (empty).
105 [$simpledns, "http://sub.example.com/x.png", "*.com", "", true],
106 [$simpledns, "http://example.com/x.png", "*.example.com", "", false],
107 [$simpledns, "http://sub.example.com/x.png", "*.example.com", "", true],
108 [$simpledns, "http://sub.example.com/x.png", "*.sub.example.com", "", false],
109 [$simpledns, "http://sub.example.com/x.png", "*.example", "", false],
111 // Test set using IP address filters but with all ports allowed (empty).
112 [$simpledns, "http://localhost/x.png", "127.0.0.1", "", true],
113 [$simpledns, "http://127.0.0.1/x.png", "127.0.0.1", "", true],
115 // Test set using CIDR IP range filters but with all ports allowed (empty).
116 [$simpledns, "http://localhost/x.png", "127.0.0.0/24", "", true],
117 [$simpledns, "http://127.0.0.1/x.png", "127.0.0.0/24", "", true],
119 // Test set using last-group range filters but with all ports allowed (empty).
120 [$simpledns, "http://localhost/x.png", "127.0.0.0-30", "", true],
121 [$simpledns, "http://127.0.0.1/x.png", "127.0.0.0-30", "", true],
123 // Test set using port filters but with all hosts allowed (empty).
124 [$simpledns, "http://localhost/x.png", "", "80\n443", false],
125 [$simpledns, "http://localhost:80/x.png", "", "80\n443", false],
126 [$simpledns, "https://localhost/x.png", "", "80\n443", false],
127 [$simpledns, "http://localhost:443/x.png", "", "80\n443", false],
128 [$simpledns, "http://sub.example.com:8080/x.png", "", "80\n443", true],
129 [$simpledns, "http://sub.example.com:-80/x.png", "", "80\n443", true],
130 [$simpledns, "http://sub.example.com:aaa/x.png", "", "80\n443", true],
132 // Test set using port filters and hosts filters.
133 [$simpledns, "http://localhost/x.png", "127.0.0.1", "80\n443", true],
134 [$simpledns, "http://127.0.0.1/x.png", "127.0.0.1", "80\n443", true],
136 // Test using multiple A records.
137 // Multiple record DNS gives two IPs for the same host, we want to make
138 // sure that if we block one of those (doesn't matter which one)
139 // the request is blocked.
140 [$multiplerecorddns, "http://sub.example.com", '1.2.3.4', "", true],
141 [$multiplerecorddns, "http://sub.example.com", '5.6.7.8', "", true],
143 // Test when DNS resolution fails.
144 [[], "http://example.com", "127.0.0.1", "", true],
146 // Test some freaky deaky Unicode domains. Should be blocked always.
147 [$simpledns, "http://169。254。169。254/", "127.0.0.1", "", true],
148 [$simpledns, "http://169。254。169。254/", "1.2.3.4", "", true],
149 [$simpledns, "http://169。254。169。254/", "127.0.0.1", "80\n443", true]
151 // Note on testing URLs using IPv6 notation:
152 // At present, the curl_security_helper class doesn't support IPv6 url notation.
153 // E.g. http://[ad34::dddd]:port/resource
154 // This is because it uses clean_param(x, PARAM_URL) as part of parsing, which won't validate urls having IPv6 notation.
155 // The underlying IPv6 address and range support is in place, however, so if clean_param is changed in future,
156 // please add the following test sets.
157 // 1. ["http://[::1]/x.png", "", "", false]
158 // 2. ["http://[::1]/x.png", "::1", "", true]
159 // 3. ["http://[::1]/x.png", "::1/64", "", true]
160 // 4. ["http://[fe80::dddd]/x.png", "fe80::cccc-eeee", "", true]
161 // 5. ["http://[fe80::dddd]/x.png", "fe80::dddd/128", "", true].
166 * Test for \core\files\curl_security_helper->is_enabled().
168 * @param string $blockedhosts the list of blocked hosts.
169 * @param string $allowedports the list of allowed ports.
170 * @param bool $expected the expected result.
171 * @dataProvider curl_security_settings_data_provider
173 public function test_curl_security_helper_is_enabled($blockedhosts, $allowedports, $expected) {
174 $this->resetAfterTest(true);
175 $helper = new \core\files\curl_security_helper();
176 set_config('curlsecurityblockedhosts', $blockedhosts);
177 set_config('curlsecurityallowedport', $allowedports);
178 $this->assertEquals($expected, $helper->is_enabled());
182 * Data provider for test_curl_security_helper_is_enabled().
184 * @return array
186 public function curl_security_settings_data_provider() {
187 // Format: blocked hosts, allowed ports, expected result.
188 return [
189 ["", "", false],
190 ["127.0.0.1", "", true],
191 ["localhost", "", true],
192 ["127.0.0.0/24\n192.0.0.0/24", "", true],
193 ["", "80\n443", true],
198 * Test for \core\files\curl_security_helper::host_is_blocked().
200 * @param string $host the host to validate.
201 * @param string $blockedhosts the list of blocked hosts.
202 * @param bool $expected the expected result.
203 * @dataProvider curl_security_host_data_provider
205 public function test_curl_security_helper_host_is_blocked($host, $blockedhosts, $expected) {
206 $this->resetAfterTest(true);
207 $helper = new \core\files\curl_security_helper();
208 set_config('curlsecurityblockedhosts', $blockedhosts);
209 $this->assertEquals($expected, phpunit_util::call_internal_method($helper, 'host_is_blocked', [$host],
210 '\core\files\curl_security_helper'));
214 * Data provider for test_curl_security_helper_host_is_blocked().
216 * @return array
218 public function curl_security_host_data_provider() {
219 return [
220 // IPv4 hosts.
221 ["127.0.0.1", "127.0.0.1", true],
222 ["127.0.0.1", "127.0.0.0/24", true],
223 ["127.0.0.1", "127.0.0.0-40", true],
224 ["", "127.0.0.0/24", false],
226 // IPv6 hosts.
227 // Note: ["::", "::", true], - should match but 'address_in_subnet()' has trouble with fully collapsed IPv6 addresses.
228 ["::1", "::1", true],
229 ["::1", "::0-cccc", true],
230 ["::1", "::0/64", true],
231 ["FE80:0000:0000:0000:0000:0000:0000:0000", "fe80::/128", true],
232 ["fe80::eeee", "fe80::ddde/64", true],
233 ["fe80::dddd", "fe80::cccc-eeee", true],
234 ["fe80::dddd", "fe80::ddde-eeee", false],
236 // Domain name hosts.
237 ["example.com", "example.com", true],
238 ["sub.example.com", "example.com", false],
239 ["example.com", "*.com", true],
240 ["example.com", "*.example.com", false],
241 ["sub.example.com", "*.example.com", true],
242 ["sub.sub.example.com", "*.example.com", true],
243 ["sub.example.com", "*example.com", false],
244 ["sub.example.com", "*.example", false],
246 // International domain name hosts.
247 ["xn--nw2a.xn--j6w193g", "xn--nw2a.xn--j6w193g", true], // The domain 見.香港 is ace-encoded to xn--nw2a.xn--j6w193g.
252 * Test for \core\files\curl_security_helper->port_is_blocked().
254 * @param int|string $port the port to validate.
255 * @param string $allowedports the list of allowed ports.
256 * @param bool $expected the expected result.
257 * @dataProvider curl_security_port_data_provider
259 public function test_curl_security_helper_port_is_blocked($port, $allowedports, $expected) {
260 $this->resetAfterTest(true);
261 $helper = new \core\files\curl_security_helper();
262 set_config('curlsecurityallowedport', $allowedports);
263 $this->assertEquals($expected, phpunit_util::call_internal_method($helper, 'port_is_blocked', [$port],
264 '\core\files\curl_security_helper'));
268 * Data provider for test_curl_security_helper_port_is_blocked().
270 * @return array
272 public function curl_security_port_data_provider() {
273 return [
274 ["", "80\n443", true],
275 [" ", "80\n443", true],
276 ["-1", "80\n443", true],
277 [-1, "80\n443", true],
278 ["n", "80\n443", true],
279 [0, "80\n443", true],
280 ["0", "80\n443", true],
281 [8080, "80\n443", true],
282 ["8080", "80\n443", true],
283 ["80", "80\n443", false],
284 [80, "80\n443", false],
285 [443, "80\n443", false],
286 [0, "", true], // Port 0 and below are always invalid, even when the admin hasn't set allowed entries.
287 [-1, "", true], // Port 0 and below are always invalid, even when the admin hasn't set allowed entries.
288 [null, "", true], // Non-string, non-int values are invalid.
293 * Test for \core\files\curl_security_helper::get_blocked_url_string().
295 public function test_curl_security_helper_get_blocked_url_string() {
296 $helper = new \core\files\curl_security_helper();
297 $this->assertEquals(get_string('curlsecurityurlblocked', 'admin'), $helper->get_blocked_url_string());