fix: redirect validator v2

This commit is contained in:
loks0n committed 2025-01-23 15:00:54 +00:00
1 parent 85539dfb29
commit 170f3656e1
8 files changed
+129 -13

No files matched your search

+1
View File
@@ -266,6 +266,7 @@ class Mapper
case 'Appwrite\Utopia\Database\Validator\CustomId':
case 'Utopia\Validator\Domain':
case 'Appwrite\Network\Validator\Email':
case 'Appwrite\Network\Validator\Redirect':
case 'Appwrite\Event\Validator\Event':
case 'Appwrite\Event\Validator\FunctionEvent':
case 'Utopia\Validator\HexColor':
@@ -0,0 +1,70 @@
<?php
namespace Appwrite\Network\Validator;
use Utopia\Validator\Host;
/**
* Redirect
*
* Validate that URL has an allowed host for redirect
*
* @package Utopia\Validator
*/
class Redirect extends Host
{
/**
* @param array $whitelist
*/
public function __construct(array $whitelist)
{
parent::__construct($whitelist);
}
/**
* Get Description
*
* Returns validator description
*
* @return string
*/
public function getDescription(): string
{
return "HTTP or HTTPS URL host must be one of: " .
\implode(", ", $this->whitelist);
}
/**
* Is valid
*
* Validation will pass if scheme is not http or https or host is in whitelist
*
* @param mixed $value
* @return bool
*/
public function isValid($value): bool
{
// `parse_url()` returns false for URLs with only a scheme.
// We need to check for this case separately.
if (preg_match('/^([a-z][a-z0-9+\.-]*):\/\/?$/i', $value, $matches)) {
$scheme = strtolower($matches[1]);
return $scheme !== 'javascript';
}
$url = \parse_url($value);
if ($url === false || !isset($url["scheme"])) {
return false;
}
$scheme = strtolower($url["scheme"]);
if ($scheme === "javascript") {
return false;
}
if (!\in_array($scheme, ["http", "https"])) {
return true;
}
return parent::isValid($value);
}
}
@@ -375,6 +375,7 @@ class OpenAPI3 extends Format
$node['schema']['format'] = 'email';
$node['schema']['x-example'] = 'email@example.com';
break;
case 'Appwrite\Network\Validator\Redirect':
case 'Utopia\Validator\Host':
case 'Utopia\Validator\URL':
$node['schema']['type'] = $validator->getType();
@@ -393,6 +393,7 @@ class Swagger2 extends Format
$node['format'] = 'email';
$node['x-example'] = 'email@example.com';
break;
case 'Appwrite\Network\Validator\Redirect':
case 'Utopia\Validator\Host':
case 'Utopia\Validator\URL':
$node['type'] = $validator->getType();