Skip to content

Commit eb0e82e

Browse files
authored
Merge pull request #175 from utopia-php/fix-domains-validator
Fix: Domain validator
2 parents c2203e8 + 08186e6 commit eb0e82e

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/Validator/Domain.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ public function isValid($value): bool
4545
return false;
4646
}
4747

48-
if (\filter_var($value, FILTER_VALIDATE_DOMAIN) === false) {
48+
if (\filter_var($value, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) === false) {
49+
return false;
50+
}
51+
52+
if (\str_ends_with($value, '.') || \str_ends_with($value, '-')) {
4953
return false;
5054
}
5155

tests/Validator/DomainTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,25 @@ public function testIsValid()
3131
$this->assertEquals(true, $this->domain->isValid('example.com'));
3232
$this->assertEquals(true, $this->domain->isValid('subdomain.example.com'));
3333
$this->assertEquals(true, $this->domain->isValid('subdomain.example-app.com'));
34-
$this->assertEquals(true, $this->domain->isValid('subdomain.example_app.com'));
34+
$this->assertEquals(false, $this->domain->isValid('subdomain.example_app.com'));
3535
$this->assertEquals(true, $this->domain->isValid('subdomain-new.example.com'));
36-
$this->assertEquals(true, $this->domain->isValid('subdomain_new.example.com'));
36+
$this->assertEquals(false, $this->domain->isValid('subdomain_new.example.com'));
3737
$this->assertEquals(true, $this->domain->isValid('localhost'));
3838
$this->assertEquals(true, $this->domain->isValid('example.io'));
3939
$this->assertEquals(true, $this->domain->isValid('example.org'));
4040
$this->assertEquals(true, $this->domain->isValid('example.org'));
4141
$this->assertEquals(false, $this->domain->isValid(false));
42+
$this->assertEquals(false, $this->domain->isValid('api.appwrite.io.'));
43+
$this->assertEquals(false, $this->domain->isValid('.api.appwrite.io'));
44+
$this->assertEquals(false, $this->domain->isValid('.api.appwrite.io'));
45+
$this->assertEquals(false, $this->domain->isValid('api..appwrite.io'));
46+
$this->assertEquals(false, $this->domain->isValid('api-.appwrite.io'));
47+
$this->assertEquals(false, $this->domain->isValid('api.-appwrite.io'));
48+
$this->assertEquals(false, $this->domain->isValid('app write.io'));
49+
$this->assertEquals(false, $this->domain->isValid(' appwrite.io'));
50+
$this->assertEquals(false, $this->domain->isValid('appwrite.io '));
51+
$this->assertEquals(false, $this->domain->isValid('-appwrite.io'));
52+
$this->assertEquals(false, $this->domain->isValid('appwrite.io-'));
4253
$this->assertEquals(false, $this->domain->isValid('.'));
4354
$this->assertEquals(false, $this->domain->isValid('..'));
4455
$this->assertEquals(false, $this->domain->isValid(''));

tests/e2e/ResponseTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,27 @@ public function testAliasWithParameter(): void
9999
$this->assertEquals(200, $response['headers']['status-code']);
100100
$this->assertEquals('db2;col2', $response['body']);
101101
}
102+
103+
public function testDoubleSlash()
104+
{
105+
$response = $this->client->call(Client::METHOD_GET, '//');
106+
$this->assertEquals(200, $response['headers']['status-code']);
107+
$this->assertEquals('Hello World!', $response['body']);
108+
109+
$response = $this->client->call(Client::METHOD_GET, '//path-404');
110+
$this->assertEquals(200, $response['headers']['status-code']);
111+
$this->assertEquals('Hello World!', $response['body']);
112+
113+
$response = $this->client->call(Client::METHOD_GET, '//value/123');
114+
$this->assertEquals(200, $response['headers']['status-code']);
115+
$this->assertEmpty($response['body']);
116+
117+
$response = $this->client->call(Client::METHOD_GET, '/value//123');
118+
$this->assertEquals(200, $response['headers']['status-code']);
119+
$this->assertEmpty($response['body']);
120+
121+
$response = $this->client->call(Client::METHOD_GET, '//value//123');
122+
$this->assertEquals(200, $response['headers']['status-code']);
123+
$this->assertEmpty($response['body']);
124+
}
102125
}

0 commit comments

Comments
 (0)