forked from utopia-php/http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostname.php
More file actions
114 lines (98 loc) · 2.8 KB
/
Hostname.php
File metadata and controls
114 lines (98 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace Utopia\Http\Validator;
use Utopia\Http\Validator;
class Hostname extends Validator
{
/**
* @var string[]
*/
protected array $allowList = [];
/**
* Constructor
*
* Sets allowed hostname patterns
*
* @param string[] $allowList
*/
public function __construct(array $allowList = [])
{
$this->allowList = $allowList;
}
/**
* @return string
*/
public function getDescription(): string
{
return 'Value must be a valid hostname without path, port and protocol.';
}
/**
* Is array
*
* Function will return true if object is array.
*
* @return bool
*/
public function isArray(): bool
{
return false;
}
/**
* Get Type
*
* Returns validator type.
*
* @return string
*/
public function getType(): string
{
return self::TYPE_STRING;
}
/**
* @param mixed $value
* @return bool
*/
public function isValid(mixed $value): bool
{
// Validate proper format
if (!\is_string($value) || empty($value)) {
return false;
}
// Max length 253 chars: https://en.wikipedia.org/wiki/Hostname#:~:text=The%20entire%20hostname%2C%20including%20the,maximum%20of%20253%20ASCII%20characters
if (\mb_strlen($value) > 253) {
return false;
}
// This tests: 'http://', 'https://', and 'myapp.com/route'
if (\str_contains($value, '/')) {
return false;
}
// This tests for: 'myapp.com:3000'
if (\str_contains($value, ':')) {
return false;
}
// Logic #1: Empty allowList means everything is allowed
if (empty($this->allowList)) {
return true;
}
// Logic #2: Allow List not empty, there are rules to check
// Loop through all allowed hostnames until match is found
foreach ($this->allowList as $allowedHostname) {
// If exact match; allow
// If *, allow everything
if ($value === $allowedHostname || $allowedHostname === '*') {
return true;
}
// If wildcard symbol used
if (\str_starts_with($allowedHostname, '*')) {
// Remove starting * symbol before comparing
$allowedHostname = substr($allowedHostname, 1);
// If rest of hostname match; allow
// Notice allowedHostname still includes starting dot. Root domain is NOT allowed by wildcard.
if (\str_ends_with($value, $allowedHostname)) {
return true;
}
}
}
// If finished loop above without result, match is not found
return false;
}
}