Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix XSS when using setFormatValue($callable) #6255

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/Field/Configurator/CommonPostConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
use function Symfony\Component\String\u;
use Twig\Markup;
Expand Down Expand Up @@ -53,6 +54,14 @@

$formatted = $callable($field->getValue(), $entityDto->getInstance());

// we don't want to unintentionally allow people to add XSS vulnerabilities
// in the code just because some people need to have HTML/JS
// so that if you want know what you're doing you have to explicitly
// disable this.
if ($field->getCustomOption(FieldTrait::OPTION_STRIP_TAGS) ?? true) {

Check failure on line 61 in src/Field/Configurator/CommonPostConfigurator.php

View workflow job for this annotation

GitHub Actions / phpstan

Only booleans are allowed in an if condition, mixed given.
return $formatted;
}

// if the callable returns a string, wrap it in a Twig Markup to render the
// HTML and CSS/JS elements that it might contain
return \is_string($formatted) ? new Markup($formatted, $this->charset) : $formatted;
Expand Down
9 changes: 9 additions & 0 deletions src/Field/FieldTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
trait FieldTrait
{
public const OPTION_STRIP_TAGS = 'stripTags';

private FieldDto $dto;

private function __construct()
Expand Down Expand Up @@ -337,6 +339,13 @@ public function setCustomOptions(array $options): self

return $this;
}

public function stripTags(bool $stripTags = true): self
{
$this->setCustomOption(self::OPTION_STRIP_TAGS, $stripTags);

return $this;
}

public function hideOnDetail(): self
{
Expand Down
8 changes: 0 additions & 8 deletions src/Field/TextField.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ final class TextField implements FieldInterface

public const OPTION_MAX_LENGTH = 'maxLength';
public const OPTION_RENDER_AS_HTML = 'renderAsHtml';
public const OPTION_STRIP_TAGS = 'stripTags';

/**
* @param TranslatableInterface|string|false|null $label
Expand Down Expand Up @@ -55,11 +54,4 @@ public function renderAsHtml(bool $asHtml = true): self

return $this;
}

public function stripTags(bool $stripTags = true): self
{
$this->setCustomOption(self::OPTION_STRIP_TAGS, $stripTags);

return $this;
}
}
9 changes: 0 additions & 9 deletions src/Field/TextareaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ final class TextareaField implements FieldInterface
public const OPTION_MAX_LENGTH = TextField::OPTION_MAX_LENGTH;
public const OPTION_NUM_OF_ROWS = 'numOfRows';
public const OPTION_RENDER_AS_HTML = TextField::OPTION_RENDER_AS_HTML;
public const OPTION_STRIP_TAGS = TextField::OPTION_STRIP_TAGS;

/**
* @param TranslatableInterface|string|false|null $label
*/
Expand Down Expand Up @@ -70,11 +68,4 @@ public function renderAsHtml(bool $asHtml = true): self

return $this;
}

public function stripTags(bool $stripTags = true): self
{
$this->setCustomOption(self::OPTION_STRIP_TAGS, $stripTags);

return $this;
}
}
Loading