mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
fix: removed redundant filtering
This commit is contained in:
1 parent
bf0fb14631
commit
e780c5fa4a
1 file changed
+51
-105
@@ -3,129 +3,75 @@
|
||||
namespace Appwrite\Domain\Validator;
|
||||
|
||||
use Utopia\System\System;
|
||||
use Utopia\Validator\Domain as ValidatorDomain;
|
||||
use Utopia\Validator\Domain;
|
||||
|
||||
class AppwriteDomain extends ValidatorDomain
|
||||
class AppwriteDomain extends Domain
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
$sitesDomain = System::getEnv('_APP_DOMAIN_SITES');
|
||||
$functionsDomain = System::getEnv('_APP_DOMAIN_FUNCTIONS');
|
||||
return "Value must be a valid domain name. For Appwrite-managed domains, only one-level subdomain is allowed.";
|
||||
return 'Value must be a valid one-level subdomain';
|
||||
}
|
||||
|
||||
public function isValid($value): bool
|
||||
{
|
||||
if (!is_string($value) || $value === '') {
|
||||
// Reject domains with leading/trailing whitespace
|
||||
if (!is_string($value) || $value !== trim($value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($value !== trim($value)) {
|
||||
if (str_starts_with($value, '.') || str_ends_with($value, '.') || str_contains($value, '..')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$domain = strtolower($value);
|
||||
|
||||
if (preg_match('/\\s/', $domain)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (strpos($domain, '_') !== false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (preg_match('/^[a-z]+:\/\//', $domain)) {
|
||||
$parsed = parse_url($domain);
|
||||
if ($parsed === false || !isset($parsed['host'])) {
|
||||
return false;
|
||||
}
|
||||
$domain = $parsed['host'];
|
||||
} elseif (strpos($domain, '/') !== false) {
|
||||
$parsed = parse_url('http://' . $domain);
|
||||
if ($parsed === false || !isset($parsed['host'])) {
|
||||
return false;
|
||||
}
|
||||
$domain = $parsed['host'];
|
||||
}
|
||||
|
||||
if (!preg_match('/^[a-z0-9.-]+$/', $domain)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (str_starts_with($domain, '.') || str_ends_with($domain, '.') || strpos($domain, '..') !== false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$parts = explode('.', $domain);
|
||||
if (count($parts) < 2 || empty($parts[0])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($parts as $label) {
|
||||
if ($label === '' || strlen($label) > 63) {
|
||||
return false;
|
||||
}
|
||||
if (!preg_match('/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/', $label)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$tld = end($parts);
|
||||
if (strlen($tld) < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$firstLabel = $parts[0];
|
||||
$firstLabelLower = strtolower($firstLabel);
|
||||
if (str_starts_with($firstLabelLower, 'commit-') || str_starts_with($firstLabelLower, 'branch-')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$functionsDomain = System::getEnv('_APP_DOMAIN_FUNCTIONS');
|
||||
$sitesDomain = System::getEnv('_APP_DOMAIN_SITES');
|
||||
|
||||
if ($domain === 'localhost' || $domain === 'api.localhost') {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ([$functionsDomain, $sitesDomain] as $managedDomain) {
|
||||
if (!empty($managedDomain)) {
|
||||
if ($domain === $managedDomain) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (str_ends_with($domain, '.' . $managedDomain)) {
|
||||
$subdomain = substr($domain, 0, -strlen('.' . $managedDomain));
|
||||
|
||||
if (
|
||||
$subdomain === '' ||
|
||||
strpos($subdomain, '.') !== false ||
|
||||
strlen($subdomain) > 63 ||
|
||||
!preg_match('/^[a-z0-9-]+$/', $subdomain) ||
|
||||
str_starts_with($subdomain, '-') ||
|
||||
str_ends_with($subdomain, '-')
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!parent::isValid($value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$domain = strtolower($value);
|
||||
|
||||
if ($domain === 'localhost' || $domain === 'api.localhost') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$parts = explode('.', $domain);
|
||||
$firstLabel = $parts[0];
|
||||
if (str_starts_with($firstLabel, 'commit-') || str_starts_with($firstLabel, 'branch-')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$managedDomains = [
|
||||
System::getEnv('_APP_DOMAIN_FUNCTIONS'),
|
||||
System::getEnv('_APP_DOMAIN_SITES')
|
||||
];
|
||||
|
||||
foreach ($managedDomains as $managedDomain) {
|
||||
if (empty($managedDomain)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Block exact match
|
||||
if ($domain === $managedDomain) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Validate subdomains - reject sub-subdomains
|
||||
if (str_ends_with($domain, '.' . $managedDomain)) {
|
||||
$subdomain = substr($domain, 0, -strlen('.' . $managedDomain));
|
||||
|
||||
// Reject sub-subdomains (contains dots) or invalid subdomain format
|
||||
if (
|
||||
$subdomain === '' ||
|
||||
strpos($subdomain, '.') !== false ||
|
||||
strlen($subdomain) > 63 ||
|
||||
!preg_match('/^[a-z0-9-]+$/i', $subdomain) ||
|
||||
str_starts_with($subdomain, '-') ||
|
||||
str_ends_with($subdomain, '-')
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getType(): string
|
||||
{
|
||||
return 'string';
|
||||
}
|
||||
|
||||
public function isArray(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user