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.


Install and update using pip:

pip install -U MarkupSafe


>>> from markupsafe import Markup, escape

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

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

>>> escape(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.3.tar.gz (19.1 kB view hashes)

Uploaded source

Built Distributions

MarkupSafe-2.1.3-cp312-cp312-win_amd64.whl (17.0 kB view hashes)

Uploaded cp312

MarkupSafe-2.1.3-cp312-cp312-win32.whl (16.4 kB view hashes)

Uploaded cp312

MarkupSafe-2.1.3-cp311-cp311-win_amd64.whl (17.1 kB view hashes)

Uploaded cp311

MarkupSafe-2.1.3-cp311-cp311-win32.whl (16.5 kB view hashes)

Uploaded cp311

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

Uploaded cp310

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

Uploaded cp310

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

Uploaded cp39

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

Uploaded cp39

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

Uploaded cp38

MarkupSafe-2.1.3-cp38-cp38-win32.whl (16.5 kB view hashes)

Uploaded cp38

MarkupSafe-2.1.3-cp37-cp37m-win_amd64.whl (17.1 kB view hashes)

Uploaded cp37

MarkupSafe-2.1.3-cp37-cp37m-win32.whl (16.5 kB view hashes)

Uploaded cp37

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page