Skip to content

Commit 487d150

Browse files
authored
Merge pull request #314 from jernst/master
Allow wildcard * to be used in trusted domains
2 parents 828106a + 2b96e90 commit 487d150

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

config/config.sample.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
* Your list of trusted domains that users can log into. Specifying trusted
5959
* domains prevents host header poisoning. Do not remove this, as it performs
6060
* necessary security checks.
61+
* You can specify:
62+
* - the exact hostname of your host or virtual host, e.g. demo.example.org.
63+
* - the exact hostname with permitted port, e.g. demo.example.org:443.
64+
* This disallows all other ports on this host
65+
* - use * as a wildcard, e.g. ubos-raspberry-pi*.local will allow
66+
* ubos-raspberry-pi.local and ubos-raspberry-pi-2.local
6167
*/
6268
'trusted_domains' =>
6369
array (

lib/private/Security/TrustedDomainHelper.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,28 @@ public function isTrustedDomain($domainWithPort) {
7070

7171
// Read trusted domains from config
7272
$trustedList = $this->config->getSystemValue('trusted_domains', []);
73-
if(!is_array($trustedList)) {
73+
if (!is_array($trustedList)) {
7474
return false;
7575
}
7676

7777
// Always allow access from localhost
7878
if (preg_match(Request::REGEX_LOCALHOST, $domain) === 1) {
7979
return true;
8080
}
81-
82-
// Compare with port appended
83-
if(in_array($domainWithPort, $trustedList, true)) {
84-
return true;
81+
// Reject misformed domains in any case
82+
if (strpos($domain,'-') === 0 || strpos($domain,'..') !== false) {
83+
return false;
8584
}
86-
87-
return in_array($domain, $trustedList, true);
85+
// Match, allowing for * wildcards
86+
foreach ($trustedList as $trusted) {
87+
if (gettype($trusted) !== 'string') {
88+
break;
89+
}
90+
$regex = '/^' . join('[-\.a-zA-Z0-9]*', array_map(function($v) { return preg_quote($v, '/'); }, explode('*', $trusted))) . '$/';
91+
if (preg_match($regex, $domain) || preg_match($regex, $domainWithPort)) {
92+
return true;
93+
}
94+
}
95+
return false;
8896
}
89-
9097
}

tests/lib/Security/TrustedDomainHelperTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function trustedDomainDataProvider() {
4949
'host.two.test',
5050
'[1fff:0:a88:85a3::ac1f]',
5151
'host.three.test:443',
52+
'*.leading.host',
53+
'trailing.host*',
54+
'cen*ter',
55+
'*.leadingwith.port:123',
56+
'trailingwith.port*:456',
5257
];
5358
return [
5459
// empty defaults to false with 8.1
@@ -76,7 +81,31 @@ public function trustedDomainDataProvider() {
7681
[$trustedHostTestList, 'localhost: evil.host', false],
7782
// do not trust casting
7883
[[1], '1', false],
84+
// leading *
85+
[$trustedHostTestList, 'abc.leading.host', true],
86+
[$trustedHostTestList, 'abc.def.leading.host', true],
87+
[$trustedHostTestList, 'abc.def.leading.host.another', false],
88+
[$trustedHostTestList, 'abc.def.leading.host:123', true],
89+
[$trustedHostTestList, 'leading.host', false],
90+
// trailing *
91+
[$trustedHostTestList, 'trailing.host', true],
92+
[$trustedHostTestList, 'trailing.host.abc', true],
93+
[$trustedHostTestList, 'trailing.host.abc.def', true],
94+
[$trustedHostTestList, 'trailing.host.abc:123', true],
95+
[$trustedHostTestList, 'another.trailing.host', false],
96+
// center *
97+
[$trustedHostTestList, 'center', true],
98+
[$trustedHostTestList, 'cenxxxter', true],
99+
[$trustedHostTestList, 'cen.x.y.ter', true],
100+
// with port
101+
[$trustedHostTestList, 'abc.leadingwith.port:123', true],
102+
[$trustedHostTestList, 'abc.leadingwith.port:1234', false],
103+
[$trustedHostTestList, 'trailingwith.port.abc:456', true],
104+
[$trustedHostTestList, 'trailingwith.port.abc:123', false],
105+
// bad hostname
106+
[$trustedHostTestList, '-bad', false],
107+
[$trustedHostTestList, '-bad.leading.host', false],
108+
[$trustedHostTestList, 'bad..der.leading.host', false],
79109
];
80110
}
81-
82111
}

0 commit comments

Comments
 (0)