从表单输入清理 HTML 不起作用 (symfony6.3 php 8.2)

Sanitizing HTML from Form Input not working (symfony6.3 php 8.2)

提问人:pok_net 提问时间:9/28/2023 最后编辑:pok_net 更新时间:10/12/2023 访问量:136

问:

尝试实现一个根据 https://symfony.com/doc/current/html_sanitizer.html#sanitizing-html-from-form-input 清理 html 输入的表单,但我无法使其正常工作。 这是我的设置:

html_sanitizer.yaml

framework:
    html_sanitizer:
        sanitizers:
            app.post_sanitizer:
                allow_safe_elements: true
                #allow_static_elements: true
                allow_relative_medias: true
                allowed_link_schemes: ['http', 'https', 'href']
                allow_relative_links: true
                allow_elements:
                    img: '*'
                    div: '*'
                    span: '*'
                    p: '*'
                    a: '*'
                    i: '*'

活动丰富文本形式类型.php

class ActivityRichTextFormType extends AbstractType
{

    public function __construct(
        private readonly HtmlSanitizerInterface $appPostSanitizer,
    ) {
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        //$data1 = $options['data']->getContent();
        //$data1 = $this->appPostSanitizer->sanitize($data1);
        //$options['data']->setContent($data1);

        $builder->add('content', TextareaType::class,
            ['label' => '', 'empty_data' => '']
        );
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => ActRichText::class,
            'sanitize_html' => true,
            'sanitizer' => 'app.post_sanitizer',
            'translation_domain' => false
        ]);
    }
}

实体字段:

    #[ORM\Column(type: Types::TEXT , nullable: true)]
    #[Assert\Length(max: 2255)]
    private ?string $content = null;

然后为了测试功能,我输入类似的东西

<h2>Testing html form</h2>
<script>// <![CDATA[
(function(i,s,o,g,r,a,m){var ql=document.querySelectorAll('A[quiz],DIV[quiz]'); 
// ]]></script>

当我只是在解析器中使用时,html 文本不会被清理。即脚本标签保留在内容中。'sanitize_html' => true, 'sanitizer' => 'app.post_sanitizer',

作为临时解决方法,我添加了一个手动消毒器:

        $data1 = $options['data']->getContent();
        $data1 = $this->appPostSanitizer->sanitize($data1);
        $options['data']->setContent($data1);

当我删除注释并激活此解决方法时,html 确实会被清理并删除脚本标签。

任何提示为什么在不起作用?
谢谢!
sanitize_htmlresolver

更新:
创建了一个全新的项目来测试此问题,并将其上传到 github symfony-html-sanitizer
如果有人想检查一下,我使用模型而不是实体来简化事情。

php 表单 symfony sanitization symfony6

评论

0赞 Jason Olson 10/1/2023
什么是“ActRichText::class”?对于symfony表单集成,它必须是“表单,或扩展此类型的任何表单(例如TextareaType)”。ActRichText 和 TextType 的扩展是另一种类型?这可能就是问题所在。TextType
0赞 pok_net 10/1/2023
ActRichText是具有字段的实体。我没有列出整个实体,而是只发布了表单中使用的 1 个字段。$content
0赞 Jason Olson 10/2/2023
是的,但是当实体类型为 Types:TEXT 时,为什么要在选项中声明类型?如果省略: 会发生什么情况: ?ArtRichTextdata_class' => ActRichText::class,
0赞 pok_net 10/2/2023
@JasonOlson :我检查了它,如果我省略它,则没有任何变化。我按照 symfony.com/doc/current/forms.html#creating-form-classes 的建议使用它。如本页所述,虽然并不总是必要的,但明确指定data_class选项通常是一个好主意。
0赞 pok_net 10/2/2023
在表单类型中添加手动消毒剂也不太有效。我现在最好的选择是在控制器上添加一个手动消毒剂,在表单 isSubmitted isValid 检查中。

答:

-1赞 Ankita Sharma 10/12/2023 #1

$sanitizedInput = htmlspecialchars($_POST['inputField'], ENT_QUOTES, 'UTF-8');

echo htmlspecialchars($sanitizedInput, ENT_QUOTES, 'UTF-8');

var cleanHTML = DOMPurify.sanitize(dirtyHTML);

评论

0赞 Cristik 10/14/2023
正如目前所写的那样,你的答案尚不清楚。请编辑以添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以在帮助中心找到有关如何写出好答案的更多信息。