MCPcopy Create free account
hub / github.com/Rello/analytics / ExternalUrlValidator

Class ExternalUrlValidator

lib/Security/ExternalUrlValidator.php:11–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9use OCP\Security\IRemoteHostValidator;
10
11class ExternalUrlValidator {
12 public function __construct(private IRemoteHostValidator $remoteHostValidator) {
13 }
14
15 public function validate(string $url): ?string {
16 $url = trim($url);
17 if ($url === '') {
18 return 'External URL is empty';
19 }
20
21 $parts = parse_url($url);
22 if (!is_array($parts) || !isset($parts['scheme'], $parts['host'])) {
23 return 'External URL is invalid';
24 }
25 if (isset($parts['user']) || isset($parts['pass'])) {
26 return 'Credentials in external URLs are not allowed';
27 }
28
29 $scheme = strtolower((string)$parts['scheme']);
30 if (!in_array($scheme, ['http', 'https'], true)) {
31 return 'External URL scheme is not allowed';
32 }
33
34 $host = strtolower(rtrim((string)$parts['host'], '.'));
35 if (str_starts_with($host, '[') && str_ends_with($host, ']')) {
36 $host = substr($host, 1, -1);
37 }
38 if ($host === '' || $host === 'localhost' || str_ends_with($host, '.localhost')) {
39 return 'External URL host is not allowed';
40 }
41
42 if (!$this->remoteHostValidator->isValid($host)) {
43 return 'External URL host is not allowed by server configuration';
44 }
45
46 return null;
47 }
48
49 public function isAllowed(string $url): bool {
50 return $this->validate($url) === null;
51 }
52}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected