Skip to content

Commit 82b310b

Browse files
committed
Removed deprecated safe_mode.
1 parent 17213b0 commit 82b310b

30 files changed

+26
-648
lines changed

markdown/core.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ def __init__(self, **kwargs):
6868
Note that it is suggested that the more specific formats ("xhtml1"
6969
and "html4") be used as "xhtml" or "html" may change in the future
7070
if it makes sense at that time.
71-
* safe_mode: Deprecated! Disallow raw html. One of "remove", "replace"
72-
or "escape".
73-
* html_replacement_text: Deprecated! Text used when safe_mode is set
74-
to "replace".
7571
* tab_length: Length of tabs in the source. Default: 4
7672
* enable_attributes: Enable the conversion of attributes. Default: True
7773
* smart_emphasis: Treat `_connected_words_` intelligently Default: True
@@ -83,24 +79,6 @@ def __init__(self, **kwargs):
8379
for option, default in self.option_defaults.items():
8480
setattr(self, option, kwargs.get(option, default))
8581

86-
self.safeMode = kwargs.get('safe_mode', False)
87-
if self.safeMode and 'enable_attributes' not in kwargs:
88-
# Disable attributes in safeMode when not explicitly set
89-
self.enable_attributes = False
90-
91-
if 'safe_mode' in kwargs:
92-
warnings.warn('"safe_mode" is deprecated in Python-Markdown. '
93-
'Use an HTML sanitizer (like '
94-
'Bleach http://bleach.readthedocs.org/) '
95-
'if you are parsing untrusted markdown text. '
96-
'See the 2.6 release notes for more info',
97-
DeprecationWarning)
98-
99-
if 'html_replacement_text' in kwargs:
100-
warnings.warn('The "html_replacement_text" keyword is '
101-
'deprecated along with "safe_mode".',
102-
DeprecationWarning)
103-
10482
self.registeredExtensions = []
10583
self.docType = ""
10684
self.stripTopLevelTags = True

markdown/extensions/codehilite.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,7 @@ def run(self, root):
215215
tab_length=self.markdown.tab_length,
216216
use_pygments=self.config['use_pygments']
217217
)
218-
placeholder = self.markdown.htmlStash.store(code.hilite(),
219-
safe=True)
218+
placeholder = self.markdown.htmlStash.store(code.hilite())
220219
# Clear codeblock in etree instance
221220
block.clear()
222221
# Change to p element which will later

markdown/extensions/extra.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,14 @@ def __init__(self, *args, **kwargs):
5858
def extendMarkdown(self, md, md_globals):
5959
""" Register extension instances. """
6060
md.registerExtensions(extensions, self.config)
61-
if not md.safeMode:
62-
# Turn on processing of markdown text within raw html
63-
md.preprocessors['html_block'].markdown_in_raw = True
64-
md.parser.blockprocessors.add('markdown_block',
65-
MarkdownInHtmlProcessor(md.parser),
66-
'_begin')
67-
md.parser.blockprocessors.tag_counter = -1
68-
md.parser.blockprocessors.contain_span_tags = re.compile(
69-
r'^(p|h[1-6]|li|dd|dt|td|th|legend|address)$', re.IGNORECASE)
61+
# Turn on processing of markdown text within raw html
62+
md.preprocessors['html_block'].markdown_in_raw = True
63+
md.parser.blockprocessors.add('markdown_block',
64+
MarkdownInHtmlProcessor(md.parser),
65+
'_begin')
66+
md.parser.blockprocessors.tag_counter = -1
67+
md.parser.blockprocessors.contain_span_tags = re.compile(
68+
r'^(p|h[1-6]|li|dd|dt|td|th|legend|address)$', re.IGNORECASE)
7069

7170

7271
def makeExtension(*args, **kwargs):

markdown/extensions/fenced_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def run(self, lines):
9191
code = self.CODE_WRAP % (lang,
9292
self._escape(m.group('code')))
9393

94-
placeholder = self.markdown.htmlStash.store(code, safe=True)
94+
placeholder = self.markdown.htmlStash.store(code)
9595
text = '%s\n%s\n%s' % (text[:m.start()],
9696
placeholder,
9797
text[m.end():])

markdown/extensions/smarty.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def handleMatch(self, m):
161161
if isinstance(part, int):
162162
result += m.group(part)
163163
else:
164-
result += self.markdown.htmlStash.store(part, safe=True)
164+
result += self.markdown.htmlStash.store(part)
165165
return result
166166

167167

markdown/extensions/toc.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,9 @@ def stashedHTML2text(text, md):
4949
def _html_sub(m):
5050
""" Substitute raw html with plain text. """
5151
try:
52-
raw, safe = md.htmlStash.rawHtmlBlocks[int(m.group(1))]
52+
raw = md.htmlStash.rawHtmlBlocks[int(m.group(1))]
5353
except (IndexError, TypeError): # pragma: no cover
5454
return m.group(0)
55-
if md.safeMode and not safe: # pragma: no cover
56-
return ''
5755
# Strip out tags and entities - leaveing text
5856
return re.sub(r'(<[^>]+>)|(&[\#a-zA-Z0-9]+;)', '', raw)
5957

markdown/inlinepatterns.py

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ def build_inlinepatterns(md_instance, **kwargs):
7373
inlinePatterns["autolink"] = AutolinkPattern(AUTOLINK_RE, md_instance)
7474
inlinePatterns["automail"] = AutomailPattern(AUTOMAIL_RE, md_instance)
7575
inlinePatterns["linebreak"] = SubstituteTagPattern(LINE_BREAK_RE, 'br')
76-
if md_instance.safeMode != 'escape':
77-
inlinePatterns["html"] = HtmlPattern(HTML_RE, md_instance)
76+
inlinePatterns["html"] = HtmlPattern(HTML_RE, md_instance)
7877
inlinePatterns["entity"] = HtmlPattern(ENTITY_RE, md_instance)
7978
inlinePatterns["not_strong"] = SimpleTextPattern(NOT_STRONG_RE)
8079
inlinePatterns["em_strong"] = DoubleTagPattern(EM_STRONG_RE, 'strong,em')
@@ -201,8 +200,6 @@ def __init__(self, pattern, markdown_instance=None):
201200
self.compiled_re = re.compile("^(.*?)%s(.*?)$" % pattern,
202201
re.DOTALL | re.UNICODE)
203202

204-
# Api for Markdown to pass safe_mode into instance
205-
self.safe_mode = False
206203
if markdown_instance:
207204
self.markdown = markdown_instance
208205

@@ -362,7 +359,7 @@ def handleMatch(self, m):
362359
if href:
363360
if href[0] == "<":
364361
href = href[1:-1]
365-
el.set("href", self.sanitize_url(self.unescape(href.strip())))
362+
el.set("href", self.unescape(href.strip()))
366363
else:
367364
el.set("href", "")
368365

@@ -371,52 +368,6 @@ def handleMatch(self, m):
371368
el.set("title", title)
372369
return el
373370

374-
def sanitize_url(self, url):
375-
"""
376-
Sanitize a url against xss attacks in "safe_mode".
377-
378-
Rather than specifically blacklisting `javascript:alert("XSS")` and all
379-
its aliases (see <http://ha.ckers.org/xss.html>), we whitelist known
380-
safe url formats. Most urls contain a network location, however some
381-
are known not to (i.e.: mailto links). Script urls do not contain a
382-
location. Additionally, for `javascript:...`, the scheme would be
383-
"javascript" but some aliases will appear to `urlparse()` to have no
384-
scheme. On top of that relative links (i.e.: "foo/bar.html") have no
385-
scheme. Therefore we must check "path", "parameters", "query" and
386-
"fragment" for any literal colons. We don't check "scheme" for colons
387-
because it *should* never have any and "netloc" must allow the form:
388-
`username:password@host:port`.
389-
390-
"""
391-
if not self.markdown.safeMode:
392-
# Return immediately bipassing parsing.
393-
return url
394-
395-
try:
396-
scheme, netloc, path, params, query, fragment = url = urlparse(url)
397-
except ValueError: # pragma: no cover
398-
# Bad url - so bad it couldn't be parsed.
399-
return ''
400-
401-
locless_schemes = ['', 'mailto', 'news']
402-
allowed_schemes = locless_schemes + ['http', 'https', 'ftp', 'ftps']
403-
if scheme not in allowed_schemes:
404-
# Not a known (allowed) scheme. Not safe.
405-
return ''
406-
407-
if netloc == '' and scheme not in locless_schemes: # pragma: no cover
408-
# This should not happen. Treat as suspect.
409-
return ''
410-
411-
for part in url[2:]:
412-
if ":" in part:
413-
# A colon in "path", "parameters", "query"
414-
# or "fragment" is suspect.
415-
return ''
416-
417-
# Url passes all tests. Return url as-is.
418-
return urlunparse(url)
419-
420371

421372
class ImagePattern(LinkPattern):
422373
""" Return a img element from the given match. """
@@ -427,7 +378,7 @@ def handleMatch(self, m):
427378
src = src_parts[0]
428379
if src[0] == "<" and src[-1] == ">":
429380
src = src[1:-1]
430-
el.set('src', self.sanitize_url(self.unescape(src)))
381+
el.set('src', self.unescape(src))
431382
else:
432383
el.set('src', "")
433384
if len(src_parts) > 1:
@@ -469,7 +420,7 @@ def handleMatch(self, m):
469420
def makeTag(self, href, title, text):
470421
el = util.etree.Element('a')
471422

472-
el.set('href', self.sanitize_url(href))
423+
el.set('href', href)
473424
if title:
474425
el.set('title', title)
475426

@@ -481,7 +432,7 @@ class ImageReferencePattern(ReferencePattern):
481432
""" Match to a stored reference and return img element. """
482433
def makeTag(self, href, title, text):
483434
el = util.etree.Element("img")
484-
el.set("src", self.sanitize_url(href))
435+
el.set("src", href)
485436
if title:
486437
el.set("title", title)
487438

markdown/postprocessors.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,19 @@ class RawHtmlPostprocessor(Postprocessor):
4949
""" Restore raw html to the document. """
5050

5151
def run(self, text):
52-
""" Iterate over html stash and restore "safe" html. """
52+
""" Iterate over html stash and restore html. """
5353
for i in range(self.markdown.htmlStash.html_counter):
54-
html, safe = self.markdown.htmlStash.rawHtmlBlocks[i]
55-
if self.markdown.safeMode and not safe:
56-
if str(self.markdown.safeMode).lower() == 'escape':
57-
html = self.escape(html)
58-
elif str(self.markdown.safeMode).lower() == 'remove':
59-
html = ''
60-
else:
61-
html = self.markdown.html_replacement_text
62-
if (self.isblocklevel(html) and
63-
(safe or not self.markdown.safeMode)):
54+
html = self.markdown.htmlStash.rawHtmlBlocks[i]
55+
if self.isblocklevel(html):
6456
text = text.replace(
65-
"<p>%s</p>" %
66-
(self.markdown.htmlStash.get_placeholder(i)),
57+
"<p>%s</p>" % (self.markdown.htmlStash.get_placeholder(i)),
6758
html + "\n"
6859
)
6960
text = text.replace(
7061
self.markdown.htmlStash.get_placeholder(i), html
7162
)
7263
return text
7364

74-
def escape(self, html):
75-
""" Basic html escaping """
76-
html = html.replace('&', '&amp;')
77-
html = html.replace('<', '&lt;')
78-
html = html.replace('>', '&gt;')
79-
return html.replace('"', '&quot;')
80-
8165
def isblocklevel(self, html):
8266
m = re.match(r'^\<\/?([^ >]+)', html)
8367
if m:

markdown/preprocessors.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ def build_preprocessors(md_instance, **kwargs):
1717
""" Build the default set of preprocessors used by Markdown. """
1818
preprocessors = odict.OrderedDict()
1919
preprocessors['normalize_whitespace'] = NormalizeWhitespace(md_instance)
20-
if md_instance.safeMode != 'escape':
21-
preprocessors["html_block"] = HtmlBlockPreprocessor(md_instance)
20+
preprocessors["html_block"] = HtmlBlockPreprocessor(md_instance)
2221
preprocessors["reference"] = ReferencePreprocessor(md_instance)
2322
return preprocessors
2423

markdown/util.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def __init__(self):
141141
self.tag_counter = 0
142142
self.tag_data = [] # list of dictionaries in the order tags appear
143143

144-
def store(self, html, safe=False):
144+
def store(self, html):
145145
"""
146146
Saves an HTML segment for later reinsertion. Returns a
147147
placeholder string that needs to be inserted into the
@@ -150,12 +150,11 @@ def store(self, html, safe=False):
150150
Keyword arguments:
151151
152152
* html: an html segment
153-
* safe: label an html segment as safe for safemode
154153
155154
Returns : a placeholder string
156155
157156
"""
158-
self.rawHtmlBlocks.append((html, safe))
157+
self.rawHtmlBlocks.append(html)
159158
placeholder = self.get_placeholder(self.html_counter)
160159
self.html_counter += 1
161160
return placeholder

0 commit comments

Comments
 (0)