HTML стол для очистки очистителя - PullRequest
0 голосов
/ 21 февраля 2020

Я использую HTML Очиститель с Laravel, и он портит таблицы.

Вместо отображения этого:

<table class="table-stripped">
    <thead>
        <tr>
            <th>Test</th>
            <th>Max score</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
        <tr>
            <td>2</td>
            <td>3</td>
        </tr>
        <tr>
            <td>3</td>
            <td>5</td>
        </tr>
    </tbody>
</table>

HTML Очиститель испортит таблицу выше и превратит ее в

<table class="table-stripped">
    <thead></thead>
    <tbody>
        <tr>
            <th>Test</th>
            <th>Max score</th>
        </tr>
    </tbody>
    <tbody></tbody>
    <tbody>
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
        <tr>
            <td>2</td>
            <td>3</td>
        </tr>
        <tr>
            <td>3</td>
            <td>5</td>
        </tr>
    </tbody>
</table>

Я также проверял это, используя PHP Artisan Tinker. См. Рисунок ниже HTML Purifier output using Tinker.

Мой app/config/purifier.php

<?php

return [
    'encoding'      => 'UTF-8',
    'finalize'      => true,
    'cachePath'     => storage_path('app/purifier'),
    'cacheFileMode' => 0755,
    'settings'      => [
        'default' => [
            'HTML.Doctype'             => 'HTML 4.01 Transitional',
            'HTML.Allowed'             => 'h2[style|class],h3[style|class],h4[style|class],h5[style|class],h6[style|class],div[class|style],section,b,strong,i[style|class],em,a[href|title|class],ul[style|class],ol[style|class],li[style|class],p[style|class],br,span[style|class],img[width|height|alt|src|class],table[style|class],tbody[style|class],thead[style|class],tr[style|class],th[style|class],td[style|class],button,p,object[type|width|data|height|name|form|usemap|class],embed[src|type|width|height|class],iframe[src|height|width|class|name]',
            'CSS.AllowedProperties'    => 'text-decoration,padding,color,background-color,text-align,margin,height,width,max-height,max-width',
            'AutoFormat.AutoParagraph' => false,
            'AutoFormat.RemoveEmpty'   => true,
            'Attr.EnableID' => 'true',
        ],
        "youtube" => [
            "HTML.SafeIframe"      => 'true',
            "URI.SafeIframeRegexp" => "%^(http://|https://|//)(www.youtube.com/embed/|player.vimeo.com/video/)%",
        ],
        'custom_definition' => [
            'id'  => 'html5-definitions',
            'rev' => 1,
            'debug' => false,
            'elements' => [
                // http://developers.whatwg.org/sections.html
                ['section', 'Block', 'Flow', 'Common'],
                ['nav',     'Block', 'Flow', 'Common'],
                ['article', 'Block', 'Flow', 'Common'],
                ['aside',   'Block', 'Flow', 'Common'],
                ['header',  'Block', 'Flow', 'Common'],
                ['footer',  'Block', 'Flow', 'Common'],
                ['table',  'Block', 'Flow', 'Common'],

                // Content model actually excludes several tags, not modelled here
                ['address', 'Block', 'Flow', 'Common'],
                ['hgroup', 'Block', 'Required: h2 | h3 | h4 | h5 | h6', 'Common'],

                // http://developers.whatwg.org/grouping-content.html
                ['figure', 'Block', 'Optional: (figcaption, Flow) | (Flow, figcaption) | Flow', 'Common'],
                ['figcaption', 'Inline', 'Flow', 'Common'],

                // http://developers.whatwg.org/the-video-element.html#the-video-element
                ['video', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [
                    'src' => 'URI',
                    'type' => 'Text',
                    'width' => 'Length',
                    'height' => 'Length',
                    'poster' => 'URI',
                    'preload' => 'Enum#auto,metadata,none',
                    'controls' => 'Bool',
                ]],
                ['source', 'Block', 'Flow', 'Common', [
                    'src' => 'URI',
                    'type' => 'Text',
                ]],
                ['object', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [
                    'data' => 'URI',
                    'form' => 'Text',
                    'name' => 'Text',
                    'usemap' => 'Text',
                    'type' => 'Text',
                    'width' => 'Length',
                    'height' => 'Length',
                ]],
                ['iframe', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [
                    'src' => 'URI',
                    'name' => 'Text',
                    'class' => 'Text',
                    'usemap' => 'Text',
                    'width' => 'Length',
                    'height' => 'Length',
                ]],
                ['embed', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [
                    'src' => 'URI',
                    'type' => 'Text',
                    'width' => 'Length',
                    'height' => 'Length',
                ]],

                //
                ['button', 'Inline', 'Flow', 'Common', [
                    'type' => 'Text',
                    'aria-label' => 'Text'
                ]],

                // http://developers.whatwg.org/text-level-semantics.html
                ['s',    'Inline', 'Inline', 'Common'],
                ['var',  'Inline', 'Inline', 'Common'],
                ['sub',  'Inline', 'Inline', 'Common'],
                ['sup',  'Inline', 'Inline', 'Common'],
                ['mark', 'Inline', 'Inline', 'Common'],
                ['wbr',  'Inline', 'Empty', 'Core'],

                // http://developers.whatwg.org/edits.html
                ['ins', 'Block', 'Flow', 'Common', ['cite' => 'URI', 'datetime' => 'CDATA']],
                ['del', 'Block', 'Flow', 'Common', ['cite' => 'URI', 'datetime' => 'CDATA']],
            ],
            'attributes' => [
                ['iframe', 'allowfullscreen', 'Bool'],
                ['table', 'height', 'Text'],
                ['td', 'border', 'Text'],
                ['th', 'border', 'Text'],
                ['tr', 'width', 'Text'],
                ['tr', 'height', 'Text'],
                ['tr', 'border', 'Text'],
            ],
        ],
        'custom_attributes' => [
            ['a', 'target', 'Enum#_blank,_self,_target,_top'],
        ],
        'custom_elements' => [
            ['u', 'Inline', 'Inline', 'Common'],
        ],
    ],

];

Как решить эту проблему?

Я не могу отключить Purifier, потому что некоторые пользователи могут создавать сообщения в блоге в HTML, поэтому мне нужно отобразить HTML, и Purifier, кажется, является лучшим вариантом для запрета скриптов и злонамеренных атак в случае, если некоторые хакеры получат доступ к этим пользователям. учетные записи.

...