Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'table', 'td', 'tr', 'th', 'thead', 'tbody',
'col', 'pre', 'img', 'hr', 'dl', 'dt', 'dd', 'span',
'kbd', 'var', 'del', 'cite',
]
if ignore:
for tag in ignore:
if tag in tags:
tags.remove(tag)
kwargs = {
'tags': tags,
'attributes': attrs
}
# newer bleach allow to customize the protocol supported
bleach_v = bleach.__version__.split('.')
for idx, val in enumerate(bleach_v):
try:
val = int(val)
except ValueError:
pass
bleach_v[idx] = val
if tuple(bleach_v) >= (1, 5, 0):
protocols=bleach.ALLOWED_PROTOCOLS + ['irc', 'ircs']
kwargs['protocols'] = protocols
return bleach.clean(text, **kwargs)
def clean_input(text, ignore=None):
""" For a given html text, escape everything we do not want to support
to avoid potential security breach.
"""
if ignore and not isinstance(ignore, (tuple, set, list)):
ignore = [ignore]
bleach_v = bleach.__version__.split('.')
for idx, val in enumerate(bleach_v):
try:
val = int(val)
except ValueError: # pragma: no cover
pass
bleach_v[idx] = val
attrs = bleach.ALLOWED_ATTRIBUTES.copy()
attrs['table'] = ['class']
attrs['span'] = ['class', 'id']
attrs['div'] = ['class']
attrs['td'] = ['align']
attrs['th'] = ['align']
if not ignore or 'img' not in ignore:
# newer bleach need three args for attribute callable
if tuple(bleach_v) >= (2, 0, 0): # pragma: no cover
def markup(context, text):
"""
Return HTML from a markdown string.
Args:
context (mako.runtime.Context): Unused.
text (basestring): Markdown text to be converted to HTML.
Returns:
basestring: HTML representation of the markdown text.
"""
# determine the major component of the bleach version installed.
# this is similar to the approach that Pagure uses to determine the bleach version
# https://pagure.io/pagure/pull-request/2269#request_diff
bleach_major_v = int(bleach.__version__.split('.')[0])
# the only difference in the bleach API that we use between v1 and v2 is
# the formatting of the attributes parameter. Bleach 1 only allowed you
# to specify attributes to be whitelisted for all whitelisted tags.
# Bleach 2 requires you to specify the list of attributes whitelisted for
# specific tags.
if bleach_major_v >= 2:
markdown_attrs = {
"img": ["src", "alt", "title"],
"a": ["href", "alt", "title"],
"div": ["class"],
}
else:
markdown_attrs = [
"src", "href", "alt", "title", "class"
]
def markup(context, text, bodhi=True):
"""
Return HTML from a markdown string.
Args:
context (mako.runtime.Context): Unused.
text (str): Markdown text to be converted to HTML.
bodhi (bool): Enable or disable Bodhi markup extensions.
Returns:
str: HTML representation of the markdown text.
"""
# determine the major component of the bleach version installed.
# this is similar to the approach that Pagure uses to determine the bleach version
# https://pagure.io/pagure/pull-request/2269#request_diff
bleach_major_v = int(bleach.__version__.split('.')[0])
# the only difference in the bleach API that we use between v1 and v2 is
# the formatting of the attributes parameter. Bleach 1 only allowed you
# to specify attributes to be whitelisted for all whitelisted tags.
# Bleach 2 requires you to specify the list of attributes whitelisted for
# specific tags.
if bleach_major_v >= 2:
markdown_attrs = {
"img": ["src", "alt", "title"],
"a": ["href", "alt", "title"],
"div": ["class"],
}
else:
markdown_attrs = [
"src", "href", "alt", "title", "class"
]