Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2020 Adobe
* All Rights Reserved.
*/

declare(strict_types=1);
Expand All @@ -16,10 +16,15 @@
*/
class IframeSrcAttributeValidator implements AttributeValidatorInterface
{
/**
* @var array
*/
private array $allowedHostsMap;

/**
* @var string[]
*/
private $allowedHosts;
private array $allowedHosts;

/**
* IframeSrcAttributeValidator constructor.
Expand All @@ -28,33 +33,33 @@ class IframeSrcAttributeValidator implements AttributeValidatorInterface
*/
public function __construct(array $allowedHosts)
{
$this->allowedHosts = $allowedHosts;
$normalized = array_map('strtolower', $allowedHosts);
$this->allowedHosts = $normalized;
$this->allowedHostsMap = array_fill_keys($normalized, true);
}

/**
* @inheritDoc
*/
public function validate(string $tag, string $attributeName, string $value): void
{
if ($tag !== 'iframe' || $attributeName !== 'src') {
if ($tag !== 'iframe' || $attributeName !== 'src' || !$this->allowedHosts) {
return;
}

if (mb_strpos($value, 'http') !== 0) {
//Relative link
return;
}
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$srcHost = parse_url($value, PHP_URL_HOST);
if (!$srcHost || !$this->allowedHosts) {
//Either the link is invalid or we do not have the allowed list.
if (!$srcHost) {
throw new ValidationException(__('Invalid IFRAME source provided'));
}

$srcHost = strtolower($srcHost);
if (isset($this->allowedHostsMap[$srcHost])) {
return;
}
$srcHostLength = mb_strlen($srcHost);

foreach ($this->allowedHosts as $host) {
$hostLength = mb_strlen($host);
$foundIndex = mb_strpos($srcHost, $host);
if ($foundIndex !== false && ($foundIndex + $hostLength) === $srcHostLength) {
if (str_ends_with($srcHost, '.' . $host)) {
return;
}
}
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/PageBuilder/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,4 @@ OK,OK
"Save Content as Template","Save Content as Template"
"Template Name","Template Name"
"Could not delete the Template: %1","Could not delete the Template: %1"
"Invalid IFRAME source provided","Invalid IFRAME source provided"