Compare commits

...

2 Commits

Author SHA1 Message Date
Guy Sartorelli
e6a0f2dc34
ENH Use symfony/validation logic 2024-09-25 10:18:52 +12:00
Guy Sartorelli
8dfd9ce148
API Use new names for renamed classes (#98) 2024-09-23 14:40:19 +12:00
2 changed files with 14 additions and 13 deletions

View File

@ -3,12 +3,13 @@
namespace SilverStripe\IFrame; namespace SilverStripe\IFrame;
use Page; use Page;
use SilverStripe\Core\Validation\ConstraintValidator;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField; use SilverStripe\Forms\TextField;
use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\ValidationException; use SilverStripe\Core\Validation\ValidationResult;
use SilverStripe\ORM\ValidationResult; use Symfony\Component\Validator\Constraints\Url;
/** /**
* Iframe page type embeds an iframe of URL of choice into the page. * Iframe page type embeds an iframe of URL of choice into the page.
@ -119,21 +120,21 @@ class IFramePage extends Page
/** /**
* Ensure that the IFrameURL is a valid url and prevents XSS * Ensure that the IFrameURL is a valid url and prevents XSS
* *
* @throws ValidationException
* @return ValidationResult * @return ValidationResult
*/ */
public function validate() public function validate()
{ {
$result = parent::validate(); $fullResult = parent::validate();
//whitelist allowed URL schemes $allowedSchemes = ['http', 'https'];
$allowed_schemes = array('http', 'https'); $message = _t(__CLASS__ . '.VALIDATION_URL', 'Please enter a valid URL');
if ($matches = parse_url($this->IFrameURL ?? '')) { $result = ConstraintValidator::validate(
if (isset($matches['scheme']) && !in_array($matches['scheme'], $allowed_schemes ?? [])) { $this->value,
$result->addError(_t(__CLASS__ . '.VALIDATION_BANNEDURLSCHEME', "This URL scheme is not allowed.")); new Url(message: $message, protocols: $allowedSchemes),
} $this->getName()
} );
$fullResult->combineAnd($result);
return $result; return $fullResult;
} }
} }

View File

@ -4,7 +4,7 @@ namespace SilverStripe\IFrame\Tests;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\ORM\ValidationException; use SilverStripe\Core\Validation\ValidationException;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\IFrame\IFramePage; use SilverStripe\IFrame\IFramePage;
use SilverStripe\IFrame\IFramePageController; use SilverStripe\IFrame\IFramePageController;