Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from __future__ import unicode_literals
import re
import six
from bleach import callbacks as linkify_callbacks
from bleach import html5lib_shim
from bleach.utils import alphabetize_attributes, force_unicode
#: List of default callbacks
DEFAULT_CALLBACKS = [linkify_callbacks.nofollow]
TLDS = """ac ad ae aero af ag ai al am an ao aq ar arpa as asia at au aw ax az
ba bb bd be bf bg bh bi biz bj bm bn bo br bs bt bv bw by bz ca cat
cc cd cf cg ch ci ck cl cm cn co com coop cr cu cv cx cy cz de dj dk
dm do dz ec edu ee eg er es et eu fi fj fk fm fo fr ga gb gd ge gf gg
gh gi gl gm gn gov gp gq gr gs gt gu gw gy hk hm hn hr ht hu id ie il
im in info int io iq ir is it je jm jo jobs jp ke kg kh ki km kn kp
kr kw ky kz la lb lc li lk lr ls lt lu lv ly ma mc md me mg mh mil mk
ml mm mn mo mobi mp mq mr ms mt mu museum mv mw mx my mz na name nc ne
net nf ng ni nl no np nr nu nz om org pa pe pf pg ph pk pl pm pn post
pr pro ps pt pw py qa re ro rs ru rw sa sb sc sd se sg sh si sj sk sl
sm sn so sr ss st su sv sx sy sz tc td tel tf tg th tj tk tl tm tn to
tp tr travel tt tv tw tz ua ug uk us uy uz va vc ve vg vi vn vu wf ws
xn xxx ye yt yu za zm zw""".split()
text = self.parseAllQuotes(text)
text = self.replaceExternalLinks(text)
text = self.formatHeadings(text, True, toc_string)
text = self.unstrip(text)
text = self.fixtags(text)
text = self.doBlockLevels(text, True)
text = self.replaceInternalLinks(text)
text = self.replaceInternalTemplates(text)
text = self.unstripNoWiki(text)
text = text.split('\n')
text = '\n'.join(text)
if taggedNewline and text[-1:] == '\n':
text = text[:-1]
# Pass output through bleach and linkify
if nofollow:
callbacks = [bleach.callbacks.nofollow]
else:
callbacks = []
text = bleach.linkify(text, callbacks=callbacks)
return bleach.clean(text, tags=self.tags, attributes=attributes,
styles=styles, strip_comments=strip_comments)
def clean_localized_string(self):
# All links (text and markup) are normalized.
linkify_filter = partial(
bleach.linkifier.LinkifyFilter,
callbacks=[linkify_bounce_url_callback, bleach.callbacks.nofollow])
# Keep only the allowed tags and attributes, escape the rest.
cleaner = bleach.Cleaner(
tags=self.allowed_tags, attributes=self.allowed_attributes,
filters=[linkify_filter])
return cleaner.clean(str(self.localized_string))
def linkify_and_clean(text):
callbacks = [linkify_only_full_urls, bleach.callbacks.nofollow]
return bleach.linkify(bleach.clean(str(text)), callbacks=callbacks)
def linkify_with_outgoing(text, nofollow=True, only_full=False):
"""Wrapper around bleach.linkify: uses get_outgoing_url."""
callbacks = [linkify_only_full_urls] if only_full else []
callbacks.append(linkify_bounce_url_callback)
if nofollow:
callbacks.append(bleach.callbacks.nofollow)
return bleach.linkify(unicode(text), callbacks=callbacks)
def __call__(self, rule: Result = None, keys_limit: int = None) -> None:
if rule:
template = self.env.get_template("single-rule.html")
resultHTML = template.render(
rule=rule,
pd=pd,
linkfy_callbacks=[callbacks.target_blank],
keys_limit=keys_limit,
)
else:
template = self.env.get_template("full-report.html")
resultHTML = template.render(
rules=sorted(self.results.values(), key=lambda x: x.outcome.value),
pd=pd,
linkfy_callbacks=[callbacks.target_blank],
keys_limit=keys_limit,
)
# this renders the report as an iframe
# the option was added for generating the docs
template = self.env.get_template("iframe.html")
resultHTML = template.render(html_str=resultHTML)
display_html(resultHTML, raw=True)
def linkify_with_outgoing(text):
"""Wrapper around bleach.linkify: uses get_outgoing_url."""
callbacks = [linkify_bounce_url_callback, bleach.callbacks.nofollow]
return bleach.linkify(str(text), callbacks=callbacks)
if styles is None:
styles = ALLOWED_STYLES
# Clean the output using Bleach
cleaner = bleach.sanitizer.Cleaner(
tags=tags,
attributes=attributes,
styles=styles,
filters=[
# Bleach Linkify makes it easy to modify links, however, we will
# not be using it to create additional links.
functools.partial(
bleach.linkifier.LinkifyFilter,
callbacks=[
lambda attrs, new: attrs if not new else None,
bleach.callbacks.nofollow,
],
skip_tags=["pre"],
parse_email=False,
),
],
)
try:
cleaned = cleaner.clean(html)
return cleaned
except ValueError:
return None