Critical-1/S28: sanitizers that strip <x>, NUL or %XX could rebuild the $variable( marker after the raw check; every cleaned string (AttrWriter at every depth, VariableRef settings) is checked again. S29: custom_meta_ map names and *meta_key setting values obey META_KEY. Minor-2: malformed dynamic content maps fail with InvalidArgumentException. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
96 lines
3.3 KiB
PHP
96 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Compose;
|
|
|
|
use LIT\MCP\Integrations\Divi\Compose\AttrWriter;
|
|
use LIT\MCP\Integrations\Divi\Compose\MediaResolver;
|
|
use LIT\MCP\Integrations\Divi\Compose\Placeholders;
|
|
use LIT\MCP\Integrations\Divi\Compose\ValueSanitizer;
|
|
use LIT\MCP\Tests\Unit\Integrations\Divi\Design\DesignSystemTest;
|
|
use LIT\MCP\Tests\Unit\Integrations\Divi\Schema\ModuleCatalogTest;
|
|
|
|
/**
|
|
* Compose classes wired with the real Divi 5.2.1 sample catalog, the sample design system and
|
|
* predictable fakes for sanitizing and media (no WordPress). $unfiltered switches the fake
|
|
* sanitizer between a user with and without unfiltered_html (ruling S7).
|
|
*/
|
|
final class ComposeFixture
|
|
{
|
|
public static function sanitizer(bool $unfiltered = false): ValueSanitizer
|
|
{
|
|
return new class ($unfiltered) implements ValueSanitizer {
|
|
public function __construct(private bool $unfiltered)
|
|
{
|
|
}
|
|
|
|
/** Like sanitize_text_field: NUL bytes, tags and %XX octets go, then trim. */
|
|
public function text(string $value): string
|
|
{
|
|
$value = strip_tags(str_replace(chr(0), '', $value));
|
|
|
|
return trim((string) preg_replace('/%[a-f0-9]{2}/i', '', $value));
|
|
}
|
|
|
|
/**
|
|
* Like WpValueSanitizer (ruling S4): raw for users with unfiltered_html; otherwise like
|
|
* wp_kses_post: NUL bytes, <script> blocks and tags outside a small allow list go.
|
|
*/
|
|
public function html(string $value): string
|
|
{
|
|
if ($this->unfiltered) {
|
|
return $value;
|
|
}
|
|
|
|
$value = (string) preg_replace('#<script\b[^>]*>.*?</script>#is', '', str_replace(chr(0), '', $value));
|
|
|
|
return (string) preg_replace(
|
|
'#</?(?!(?:p|b|i|em|strong|a|br|span|h[1-6]|ul|ol|li)\b)[a-z][^>]*>#i',
|
|
'',
|
|
$value
|
|
);
|
|
}
|
|
|
|
public function code(string $value): string
|
|
{
|
|
return $value;
|
|
}
|
|
|
|
public function url(string $value): string
|
|
{
|
|
return str_starts_with(strtolower(trim($value)), 'javascript:') ? '' : trim($value);
|
|
}
|
|
|
|
public function allowsUnfilteredHtml(): bool
|
|
{
|
|
return $this->unfiltered;
|
|
}
|
|
};
|
|
}
|
|
|
|
public static function media(): MediaResolver
|
|
{
|
|
return new class implements MediaResolver {
|
|
public function image(int $id): ?array
|
|
{
|
|
return match ($id) {
|
|
33 => ['url' => 'https://kunde.test/wp-content/uploads/team.jpg', 'alt' => 'Unser Team'],
|
|
12 => ['url' => 'https://kunde.test/wp-content/uploads/halle.jpg', 'alt' => ''],
|
|
default => null,
|
|
};
|
|
}
|
|
};
|
|
}
|
|
|
|
public static function placeholders(): Placeholders
|
|
{
|
|
return new Placeholders(DesignSystemTest::sample());
|
|
}
|
|
|
|
public static function attrWriter(bool $unfiltered = false): AttrWriter
|
|
{
|
|
return new AttrWriter(ModuleCatalogTest::sampleCatalog(), self::placeholders(), self::sanitizer($unfiltered));
|
|
}
|
|
}
|