Skip to main content

Safely add untrusted strings to HTML/XML markup.

Project description

MarkupSafe implements a text object that escapes characters so it is safe to use in HTML and XML. Characters that have special meanings are replaced so that they display as the actual characters. This mitigates injection attacks, meaning untrusted user input can safely be displayed on a page.

Installing

Install and update using pip:

pip install -U MarkupSafe

Examples

>>> from markupsafe import Markup, escape

>>> # escape replaces special characters and wraps in Markup
>>> escape("<script>alert(document.cookie);</script>")
Markup('&lt;script&gt;alert(document.cookie);&lt;/script&gt;')

>>> # wrap in Markup to mark text "safe" and prevent escaping
>>> Markup("<strong>Hello</strong>")
Markup('<strong>hello</strong>')

>>> escape(Markup("<strong>Hello</strong>"))
Markup('<strong>hello</strong>')

>>> # Markup is a str subclass
>>> # methods and operators escape their arguments
>>> template = Markup("Hello <em>{name}</em>")
>>> template.format(name='"World"')
Markup('Hello <em>&#34;World&#34;</em>')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

MarkupSafe-2.1.1.tar.gz (18.7 kB view hashes)

Uploaded source

Built Distributions

MarkupSafe-2.1.1-cp310-cp310-win_amd64.whl (17.1 kB view hashes)

Uploaded cp310

MarkupSafe-2.1.1-cp310-cp310-win32.whl (16.5 kB view hashes)

Uploaded cp310

MarkupSafe-2.1.1-cp39-cp39-win_amd64.whl (17.1 kB view hashes)

Uploaded cp39

MarkupSafe-2.1.1-cp39-cp39-win32.whl (16.5 kB view hashes)

Uploaded cp39

MarkupSafe-2.1.1-cp38-cp38-win_amd64.whl (17.1 kB view hashes)

Uploaded cp38

MarkupSafe-2.1.1-cp38-cp38-win32.whl (16.4 kB view hashes)

Uploaded cp38

MarkupSafe-2.1.1-cp37-cp37m-win_amd64.whl (17.0 kB view hashes)

Uploaded cp37

MarkupSafe-2.1.1-cp37-cp37m-win32.whl (14.2 kB view hashes)

Uploaded cp37

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page