Zum Inhalt springen

HTML-Bereinigung

Bereinigen Sie nicht vertrauenswürdiges HTML, um XSS-Angriffe zu verhindern. Basiert auf bluemonday.

Bereinigung funktioniert durch Parsen von HTML und Filtern durch eine Whitelist-Richtlinie. Elemente und Attribute, die nicht explizit erlaubt sind, werden entfernt. Die Ausgabe ist immer wohlgeformtes HTML.

local html = require("html")

Drei eingebaute Richtlinien für häufige Anwendungsfälle:

RichtlinieAnwendungsfallErlaubt
new_policyBenutzerdefinierte BereinigungNichts (von Grund auf aufbauen)
ugc_policyBenutzerkommentare, ForenGängige Formatierung (p, b, i, a, Listen, etc.)
strict_policyReintext-ExtraktionNichts (entfernt alles HTML)

Erstellt eine Richtlinie, die nichts erlaubt. Verwenden Sie dies, um eine benutzerdefinierte Whitelist von Grund auf aufzubauen.

local policy, err = html.sanitize.new_policy()
policy:allow_elements("p", "strong", "em")
policy:allow_attrs("class"):globally()
local clean = policy:sanitize(user_input)

Gibt zurück: Policy, error

Vorkonfiguriert für benutzergenerierten Inhalt. Erlaubt gängige Formatierungselemente.

local policy = html.sanitize.ugc_policy()
local safe = policy:sanitize('<p>Hello <strong>world</strong></p>')
-- '<p>Hello <strong>world</strong></p>'
local xss = policy:sanitize('<p>Hello <script>alert("xss")</script></p>')
-- '<p>Hello </p>'

Gibt zurück: Policy, error

Entfernt alles HTML, gibt nur Reintext zurück.

local policy = html.sanitize.strict_policy()
local text = policy:sanitize('<p>Hello <b>world</b>!</p>')
-- 'Hello world!'

Gibt zurück: Policy, error

Bestimmte HTML-Elemente auf Whitelist setzen.

local policy = html.sanitize.new_policy()
policy:allow_elements("p", "strong", "em", "br")
policy:allow_elements("h1", "h2", "h3")
policy:allow_elements("a", "img")
local result = policy:sanitize('<p>Hello <strong>world</strong></p>')
-- '<p>Hello <strong>world</strong></p>'
ParameterTypBeschreibung
...stringElement-Tag-Namen

Gibt zurück: Policy

Attribut-Berechtigung starten. Mit on_elements() oder globally() verketten.

policy:allow_attrs("href"):on_elements("a")
policy:allow_attrs("src", "alt"):on_elements("img")
policy:allow_attrs("class", "id"):globally()
ParameterTypBeschreibung
...stringAttributnamen

Gibt zurück: AttrBuilder

Attribute nur auf bestimmten Elementen erlauben.

policy:allow_elements("a", "img")
policy:allow_attrs("href", "target"):on_elements("a")
policy:allow_attrs("src", "alt", "width", "height"):on_elements("img")
ParameterTypBeschreibung
...stringElement-Tag-Namen

Gibt zurück: Policy

Attribute global auf jedem erlaubten Element erlauben.

policy:allow_attrs("class"):globally()
policy:allow_attrs("id"):globally()

Gibt zurück: Policy

Attributwerte gegen Regex-Muster validieren.

-- Nur Hex-Farben in style erlauben
local builder, err = policy:allow_attrs("style"):matching("^color:#[0-9a-fA-F]{6}$")
if err then
return nil, err
end
builder:on_elements("span")
policy:sanitize('<span style="color:#ff0000">Red</span>')
-- '<span style="color:#ff0000">Red</span>'
policy:sanitize('<span style="background:red">Bad</span>')
-- '<span>Bad</span>'
ParameterTypBeschreibung
patternstringRegex-Muster

Gibt zurück: AttrBuilder, error

URL-Behandlung mit Sicherheitsstandards aktivieren.

policy:allow_elements("a")
policy:allow_attrs("href"):on_elements("a")
policy:allow_standard_urls()

Gibt zurück: Policy

Einschränken, welche URL-Schemata erlaubt sind.

policy:allow_url_schemes("https", "mailto")
policy:sanitize('<a href="https://example.com">OK</a>')
-- '<a href="https://example.com">OK</a>'
policy:sanitize('<a href="javascript:alert(1)">XSS</a>')
-- '<a>XSS</a>'
ParameterTypBeschreibung
...stringErlaubte Schemata

Gibt zurück: Policy

Relative URLs erlauben oder verbieten.

policy:allow_relative_urls(true)
policy:sanitize('<a href="/page">Link</a>')
-- '<a href="/page">Link</a>'
ParameterTypBeschreibung
allowbooleanRelative URLs erlauben

Gibt zurück: Policy

URLs ablehnen, die nicht sauber geparst werden können. Mit true werden Attribut-URLs, die der HTML-Sanitizer nicht parsen kann, entfernt anstatt durchgereicht.

policy:require_parseable_urls(true)
ParameterTypBeschreibung
requirebooleanURLs müssen parsbar sein

Gibt zurück: Policy

rel="nofollow" zu allen Links hinzufügen. Verhindert SEO-Spam.

policy:allow_attrs("href", "rel"):on_elements("a")
policy:require_nofollow_on_links(true)
policy:sanitize('<a href="https://example.com">Link</a>')
-- '<a href="https://example.com" rel="nofollow">Link</a>'
ParameterTypBeschreibung
requirebooleanNofollow hinzufügen

Gibt zurück: Policy

rel="noreferrer" zu allen Links hinzufügen. Verhindert Referrer-Lecks.

policy:require_noreferrer_on_links(true)
ParameterTypBeschreibung
requirebooleanNoreferrer hinzufügen

Gibt zurück: Policy

target="_blank" zu vollqualifizierten URLs hinzufügen.

policy:allow_attrs("href", "target"):on_elements("a")
policy:add_target_blank_to_fully_qualified_links(true)
policy:sanitize('<a href="https://example.com">Link</a>')
-- '<a href="https://example.com" target="_blank">Link</a>'
ParameterTypBeschreibung
addbooleanTarget blank hinzufügen

Gibt zurück: Policy

<img> mit Standardattributen erlauben.

policy:allow_images()
policy:sanitize('<img src="photo.jpg" alt="Photo">')
-- '<img src="photo.jpg" alt="Photo">'

Gibt zurück: Policy

Base64-eingebettete Bilder erlauben.

policy:allow_elements("img")
policy:allow_attrs("src"):on_elements("img")
policy:allow_data_uri_images()
policy:sanitize('<img src="data:image/png;base64,iVBORw...">')
-- '<img src="data:image/png;base64,iVBORw...">'

Gibt zurück: Policy

Listenelemente erlauben: ul, ol, li, dl, dt, dd.

policy:allow_lists()
policy:sanitize('<ul><li>Item 1</li><li>Item 2</li></ul>')
-- '<ul><li>Item 1</li><li>Item 2</li></ul>'

Gibt zurück: Policy

Tabellenelemente erlauben: table, thead, tbody, tfoot, tr, td, th, caption.

policy:allow_tables()
policy:sanitize('<table><tr><td>Cell</td></tr></table>')
-- '<table><tr><td>Cell</td></tr></table>'

Gibt zurück: Policy

Gängige Attribute erlauben: id, class, title, dir, lang.

policy:allow_elements("p")
policy:allow_standard_attributes()
policy:sanitize('<p id="intro" class="text" title="Introduction">Hello</p>')
-- '<p id="intro" class="text" title="Introduction">Hello</p>'

Gibt zurück: Policy

Richtlinie auf HTML-String anwenden.

local policy = html.sanitize.ugc_policy()
policy:require_nofollow_on_links(true)
local dirty = '<p>Hello</p><script>alert("xss")</script>'
local clean = policy:sanitize(dirty)
-- '<p>Hello</p>'
ParameterTypBeschreibung
htmlstringZu bereinigendes HTML

Gibt zurück: string

BedingungArtWiederholbar
Ungültiges Regex-Mustererrors.INVALIDnein

Siehe Fehlerbehandlung für die Arbeit mit Fehlern.