silksh / security-bundle
用于 Symfony 的安全相关扩展
v1.0.3
2020-02-17 11:15 UTC
Requires
- ezyang/htmlpurifier: ^4.10
- symfony/framework-bundle: ^3.0 || ^4.0 || ^5.0
- symfony/validator: ^3.0 || ^4.0 || ^5.0
- twig/twig: ^1.28 || ^2.0 || ^3.0
This package is not auto-updated.
Last update: 2024-09-24 09:16:32 UTC
README
此组件需要 Symfony 3 或 Symfony 4。
组件安装
将组件添加到项目依赖项
composer require silksh/security-bundle
Symfony 3. 启用组件
// app/AppKernel.php class AppKernel extends Kernel { public function registerBundles() { return array( // ... new SilkSH\SecurityBundle\SilkshSecurityBundle(), ); } }
Symfony 4. 这将自动完成,但如果 Symfony 没有为您完成,请手动在 bundles.php
中启用组件
// config/bundles.php return [ // ... SilkSH\SecurityBundle\SilkshSecurityBundle::class => ['all' => true], ]
验证器
该组件在命名空间 SilkSH\SecurityBundle\Validator\Constraints
中提供了一些验证器。
-
FileName
验证文件名。可能的属性maxFilenameLength
,默认值:100。maxFilenameLengthMessage
:自定义长度错误消息。您可以在其中使用{{ max_length }}
。allowedExtensions
,默认值:"pdf","txt","doc","docx","ppt","pptx","jpg","jpeg","png"allowedExtensionsMessage
,关于错误扩展的自定义错误消息。您可以在其中使用{{ extension }}
和{{ extensions }}
。
示例
use SilkSH\SecurityBundle\Validator\Constraints as SecurityAssert; ... /** * @Vich\UploadableField(mapping="uploads", fileNameProperty="filename") * @SecurityAssert\FileName( * maxFilenameLength=8, * maxFilenameLengthMessage="Maximal file length is {{ max_length }} characters", * allowedExtensions={"zip","bz2"}, * allowedExtensionsMessage="Extension '{{ extension }}' is not allowed. Allowed extensions: {{ extensions }}" * ) */ private $file;
-
Name
只允许国际字母数字和一些特殊字符(A-z 0-9 - + _ . , @ " ')。可能的属性message
:自定义错误消息。您可以在其中使用{{ allowed_signs }}
。
-
HTMLPurifier
只允许白名单中的 HTML 标签和属性。它使用 HTML Purifier 库。可能的属性message
:自定义错误消息。
-
TagWhitelist
:一个简单且存在问题的 HTML 标签验证器,它使用DOMDocument
。建议使用HTMLPurifier
。可能的属性allowedTags
,默认值:"html","head","meta","title","style","body","table","tr","th","td","h1","h2","h3","h4","h5","h6","p","a","img","br","span","small"。allowedTagsMessage
,关于无效标签的自定义错误消息。您可以在其中使用{{ allowed_tags }}
。allowedAttributes
,默认值:"width","align","cellspacing","cellpadding","class","style","href","http-equiv","name","alt","border","content","bgcolor","type","target","src"。allowedAttributesMessage
,关于无效属性的自定义错误消息。您可以在其中使用{{ allowed_attributes }}
。
Twig 扩展
该组件为 Twig 提供了 purify
过滤器。它使用 HTML Purifier 从 HTML 代码中删除所有不安全的标签(如 <script>
)和属性(如 onclick
)。
假设我们在变量 value
中有一些 HTML 代码,并希望以未转义的方式呈现它,以便用户看到格式化的输出。用法
{{ value|purify|raw }}