Skip to content

Commit fc127d7

Browse files
Add APP_REMOTE_HOST_WHITE_LIST .env parameter
1 parent 6981620 commit fc127d7

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

app/Misc/Helper.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ public static function getRemoteFileContents($url, $follow_redirects = true)
18021802
}
18031803
}
18041804

1805-
public static function sanitizeRemoteUrl($url)
1805+
public static function sanitizeRemoteUrl($url, $throw_exception = false)
18061806
{
18071807
$parts = parse_url($url ?? '');
18081808

@@ -1815,6 +1815,11 @@ public static function sanitizeRemoteUrl($url)
18151815
if (empty($parts['host'])) {
18161816
return '';
18171817
}
1818+
1819+
$host_white_list_str = str_replace(' ', '', mb_strtolower(config('app.remote_host_white_list')));
1820+
$host_white_list = explode(',', $host_white_list_str);
1821+
1822+
// Sanitize host name.
18181823
$parts['host'] = mb_strtolower($parts['host']);
18191824
$hostname = gethostname();
18201825
$host_ip = gethostbyname($hostname);
@@ -1830,13 +1835,24 @@ public static function sanitizeRemoteUrl($url)
18301835
$_SERVER['LOCAL_ADDR'] ?? ''
18311836
];
18321837

1833-
if (in_array($parts['host'], $restricted_hosts)) {
1834-
return '';
1838+
if (in_array($parts['host'], $restricted_hosts) && !in_array($parts['host'], $host_white_list)) {
1839+
if ($throw_exception) {
1840+
throw new \Exception(__('Domain or IP address is not allowed: :%host%. Whitelist it via APP_REMOTE_HOST_WHITE_LIST .env parameter.', ['%host%' => $parts['host']]), 1);
1841+
} else {
1842+
return '';
1843+
}
18351844
}
18361845

1846+
// Sanitize host IP address.
18371847
$remote_host_ip = gethostbyname($parts['host']);
1838-
if (in_array($remote_host_ip, ['0.0.0.0', '127.0.0.1', $host_ip, $_SERVER['SERVER_ADDR'] ?? '', $_SERVER['LOCAL_ADDR'] ?? ''])) {
1839-
return '';
1848+
if (in_array($remote_host_ip, ['0.0.0.0', '127.0.0.1', $host_ip, $_SERVER['SERVER_ADDR'] ?? '', $_SERVER['LOCAL_ADDR'] ?? ''])
1849+
&& !in_array($remote_host_ip, $host_white_list)
1850+
) {
1851+
if ($throw_exception) {
1852+
throw new \Exception(__('Domain or IP address is not allowed: :%host%. Whitelist it via APP_REMOTE_HOST_WHITE_LIST .env parameter.', ['%host%' => $remote_host_ip]), 1);
1853+
} else {
1854+
return '';
1855+
}
18401856
}
18411857

18421858
return $url;

config/app.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,15 @@
521521
*/
522522
'alternative_reply_separation' => env('APP_ALTERNATIVE_REPLY_SEPARATION', false),
523523

524+
/*
525+
|--------------------------------------------------------------------------
526+
| Comma separated list of white listed hosts.
527+
| If some input containing URL becomes blank after saving it - add its host or IP here.
528+
| Example: example.org,test.example.org,192.168.1.97
529+
|-------------------------------------------------------------------------
530+
*/
531+
'remote_host_white_list' => env('APP_REMOTE_HOST_WHITE_LIST', ''),
532+
524533
/*
525534
|--------------------------------------------------------------------------
526535
| Autoloaded Service Providers

0 commit comments

Comments
 (0)