Escape
Prevent xss attacks with html escape.
""" XSS
Prevent cross site scriting attacks
Escape html tags with html library
"""
import html
a = """& < " ' >"""
x = html.escape(a)
b = "<script>alert('hack');</script>"
y = html.escape(b)
print(x) # & < " ' >
print(y) # <script>alert('hack');</script>
XML
The sax library escape should execute faster.
""" XSS
Prevent cross site scriting attacks
The sax library escape should execute faster
"""
from xml.sax.saxutils import escape
from xml.sax.saxutils import quoteattr
a = '< & >'
x = escape(a)
b = "a ' b"
y = quoteattr(b)
assert x == '< & >'
assert y == '"a \' b"'
print('pass')
Last update: 420 days ago